;; -*- emacs-lisp -*-

;; upn-mail-vm-settings.el
;; RCS Id : $Id: upn-mail-vm-settings.el,v 1.5 2005/02/10 18:55:31 unair Exp unair $
;; Umesh P Nair, 1998-2007
;; Settings for e-mail using VM

;; This file (normally the compiled form) is *not* loaded by .emacs on
;; startup.  It is loaded from ~/.vm when VM is started

;; I have taken care to make this compile on GNU emacs and
;; xemacs. However, this has been used only on GNU Emacs. No serious
;; testing has been done on xemacs.

;; ==========================================================
;; BBDB (Big Brother Database - The address book I use) setup. 
;; ==========================================================

;; Need to do before VM because .vm has reference to BBDB
(require 'bbdb)
(require 'bbdb-vm)

;; My addresses
(setq bbdb-user-mail-names "^\\(umesh_nair\\|unair\\)@")
(setq bbdb-default-domain "mentor.com")

;; I will have Indian as well as internal phone numbers as well
(setq bbdb-north-american-phone-numbers-p nil)
(setq bbdb-default-area-code 8213)

(setq bbdb-expand-mail-aliases nil)
(setq bbdb-message-caching-enabled t)

;; Don't add every address. I get junk e-mails too
(setq bbdb/mail-auto-create-p nil)

;; Want the BBDB window pop up every time? (t = Yes, nil = No)
(setq bbdb-use-pop-up nil)

(bbdb-initialize)

;; =============
;; Mail using VM
;; =============
(require 'vm)
(require 'vm-autoload)
(autoload 'vm                       "vm"  "Start VM on your primary inbox."        t)
(autoload 'vm-other-frame           "vm"  "Like `vm' but starts in another frame." t)
(autoload 'vm-visit-folder          "vm"  "Start VM on an arbitrary folder."       t)
(autoload 'vm-visit-virtual-folder  "vm"  "Visit a VM virtual folder."             t)
(autoload 'vm-mode                  "vm"  "Run VM major mode on a buffer"          t)
(autoload 'vm-mail                  "vm"  "Send a mail message using VM."          t)
(autoload 'vm-submit-bug-report     "vm"  "Send a bug report about VM."            t)

;; My addresses
(setq user-mail-address my-email-address)
(defvar mail-domain-name "mentor.com")
(setq mail-default-reply-to my-email-address)

;; Show addresses as "Name" <E-mail ID>
(setq mail-from-style 'angles)

;; Send me a BCC
(setq mail-self-blind t)
;;(add-hook 'mail-setup-hook 'mail-abbrevs-setup)


(setq vm-primary-inbox "~/vm-mail/folders/INBOX")
(setq vm-crash-box "~/vm-mail/folders/INBOX.CRASH")

;; Life was very smooth before MS exchange server took over Mentor Graphics.
;; Now, these are the values
(defvar upn-incoming-mail-host "<mail host name>")
(defvar upn-my-nt-domain-name "<NT domain name>")
(defvar upn-exchange-server-username (concat upn-my-nt-domain-name "/" my-login-id)) ; Damn Exchange server!!
(setq upn-exchange-server-username my-login-id)

;; Mail username is what my exchange server username
(defvar upn-mail-username upn-exchange-server-username)

;; I don't want to hardcode my password here.  Let it ask every time.
(defvar upn-mail-password "*")  

;; Example of handling INBOX and another folder called "Aksharaslokam"
(defvar upn-imap-string-for-inbox 
  (concat "imap:" 
          upn-incoming-mail-host 
          ":143:inbox:login:" 
          upn-mail-username 
          ":"
          upn-mail-password))
(defvar upn-imap-string-for-Aksharaslokam 
  (concat "imap:" 
          upn-incoming-mail-host 
          ":143:Aksharaslokam:login:" 
          upn-mail-username 
          ":"
          upn-mail-password))

(setq vm-spool-files
      (list
        (list "~/vm-mail/folders/INBOX"
         upn-imap-string-for-inbox
         "INBOX.CRASH")
        (list "~/vm-mail/folders/Aksharaslokam"
         upn-imap-string-for-Aksharaslokam
         "Aksharaslokam.CRASH")
       )
      )

;; Keep the messages in the server
(setq vm-imap-expunge-after-retrieving nil)

;; Following configurations sets whether a new frame should
;; be opened for certain operations.  Since, when working
;; remotely, it is slower to open many X windows, and a
;; single frame is used for all purposes.  This is set by
;; the environment variable UPN_REMOTE_WORKING

;; Open(t)/Do not open (nil) a new frame upon composing
;; mail:
(setq vm-frame-per-composition (and upn-not-remote t))

;; Open(t)/Do not open (nil) a new frame upon executing the
;; vm-edit-message function:
(setq vm-frame-per-edit (and upn-not-remote nil))

;; Open(t)/Do not open (nil) a new frame when visiting a new
;; folder:
(setq vm-frame-per-folder (and upn-not-remote t))

;; Display(t)/Don't display(nil) folder summary in a new frame:
(setq vm-frame-per-summary (and upn-not-remote nil))

;; vm-move-message-forward and vm-move-message-backward should physically move
;; messages in VM folder files, not just in the presentation of the summary of
;; the file:
(setq vm-move-messages-physically t)

;; Display(toolbar vector)/Do not display (nil) that left-hand toolbar 
;; (if (not upn-not-remote)
;;     (setq vm-use-toolbar (and upn-not-remote nil))
;;   (setq vm-use-toolbar (list 'getmail 'compose 'next 'previous 'print 'quit 'reply 'visit)))

;; Display(toolbar vector)/Do not display (nil) that left-hand toolbar 
(setq vm-use-toolbar (and upn-not-remote nil))

;; No preview, show me the message
(setq vm-preview-lines (and upn-not-remote nil))

;; Move to the next message after deleting one
(setq vm-move-after-deleting t)

;; Pressing SPACE at the end of a meesage SHOULD NOT go the next
;; message
(setq vm-auto-next-message nil)

;; Don't keep sent mail buffers
(setq vm-keep-sent-messages nil)

;; Highlight Sender and subject
(setq vm-highlighted-header-regexp "From:\\|Subject:")

;; Prepend reply messages with "Re: "
(setq vm-reply-subject-prefix "Re: ")

;; Format of the forwarding header "[Fw:<From>]: <subject>"
(setq vm-forwarding-subject-format "[Fw:%F]: %s")

;; Don't want the HTML part appearing on my e-mails
(setq vm-auto-displayed-mime-content-type-exceptions '("text/html"))

;;; Set my x-attribution to "UPN", so that people who reply can use
;;; it as the default citation lead string
(setq mail-default-headers "X-attribution: UPN")

;; Message Summary format
(setq vm-summary-format "%n %*%a %-17.17F %3.3w %-3.3m %2d %5H %5z %6c %I\"%s\"\n")

;; Folders
(setq vm-folder-directory "~/vm-mail/folders")
(setq vm-confirm-new-folders t)
(setq vm-visit-when-saving 0)
(setq vm-delete-after-saving t)

;; Handle some special care for messages sent from MS Outlook.  Damn
;; Microsoft!
(if upn-is-it-xemacs
    (load "upn-handle-ms-outlook.el" nil nil t)
  (load "upn-handle-ms-outlook"))

;; Show simileys in messages
(require 'u-vm-color)
(add-hook 'vm-select-message-hook
			 '(lambda ()
             (u-vm-color-copy-gnus-faces)
				 (smiley-region (point-min) (point-max))))


;; Settings for Supercite.  It is in a separate file because
;; I use them with Gnus also
(load "upn-supercite-settings")

;; To recover inbox - Bound to C-c m r
(defun upn-recover-inbox ()
  (interactive)
  (recover-file vm-primary-inbox))


(defun upn-vm-print-message-with-faces (&optional filename)
  "Print the current message to a PostScript printer (or file) with font information"
  (interactive)
  (vm-select-folder-buffer)
  (let* ((msg (vm-real-message-of (car vm-message-pointer)))
         (buffer (set-buffer (if (vm-mime-plain-message-p msg)
                                 (vm-buffer-of msg)
                               vm-presentation-buffer)))
         (subject-line (or (vm-get-header-contents msg "Subject:") "<No Subject>">))
         (from-line (concat "From: " (or (vm-get-header-contents msg "From:") "<No Sender>")))
         (ps-left-header (list 'subject-line 'from-line))
         (ps-right-header (list "/pagenumberstring load" 'time-stamp-dd-mon-yy 'time-stamp-hh:mm:ss))
         (ps-header-lines 2)
         (ps-print-header-frame t))
    (ps-print-buffer-with-faces)))


(defun upn-print-vm-message ()
  "Pretty-print (PostScript) current mail message from within VM using 'mp'. 2.0"
  (interactive)
  (when (y-or-n-p "Do you want to print this mail message? ")
    (message "Pretty-printing mail message ...")
    (save-window-excursion    
      (vm-pipe-message-to-command "mp -l -s 'E-mail for Umesh Nair' -F | lp"  '(64))
      (message "Pretty-printing mail message ... done"))))

(define-key vm-mode-map "o" 'upn-print-vm-message)
(define-key vm-mode-map [C-f2] 'upn-print-vm-message)



(require 'u-vm-color)
(add-hook 'vm-summary-mode-hook 'u-vm-color-summary-mode)
(add-hook 'vm-select-message-hook 'u-vm-color-fontify-buffer)
(defadvice vm-decode-mime-message (after u-vm-color activate)
  (u-vm-color-fontify-buffer-even-more))

;; If you like to use gnus' face definitions, set this to t:
(setq u-vm-color-use-gnus-faces nil)

;; ----------------------------------
;; Routines to handle signature files
;; ----------------------------------

(defconst upn-work-sig-file "~/.work-signature")  ; Work signature file
(defconst upn-pers-sig-file "~/.pers-signature")  ; Personal signature file
(defconst upn-home-sig-file "~/.homework-signature")  ; Signature file for working from home
(defconst upn-chess-sig-file "~/.chess-signature") ; Another test signature file, not really used


;; Default signature file is the work one
(setq mail-signature-file upn-work-sig-file)

;; Last signature file, if we want to revert back
(defvar upn-old-signature-file upn-work-sig-file)

;; Toggles between work and personal signatures.  Bound to [C-c m s]
(defun upn-toggle-mail-signature-file (sig-file-id)
  "Toggles the signature file between different ones."
  (interactive "c")
  (let ((old-signature-file mail-signature-file))
    (cond 
     ((equal sig-file-id ?w) 
      (progn
        (message (concat "Mail signature changed from WORK."))
        (setq mail-signature-file upn-work-sig-file)))
     ((equal sig-file-id ?h) 
      (progn
        (message (concat "Mail signature changed from HOME."))
        (setq mail-signature-file upn-home-sig-file)))
     ((equal sig-file-id ?p) 
      (progn
        (message (concat "Mail signature changed from PERS."))
        (setq mail-signature-file upn-pers-sig-file)))
     ((equal sig-file-id ?c) 
      (progn
        (message (concat "Mail signature changed from CHESS."))
        (setq mail-signature-file upn-chess-sig-file)))
     (t 
      (message (concat "No valid value.  Should be w, p, h or c.")))
     (unless (equal old-signature-file mail-signature-file)
       (setq upn-old-signature-file old-signature-file)))))

;; Reverts to old signature file
(defun upn-revert-to-old-signature ()
  (interactive)
  (let ((temp-signature-file mail-signature-file))
    (unless (equal mail-signature-file upn-old-signature-file)
      (setq mail-signature-file upn-old-signature-file)
      (setq upn-old-signature-file temp-signature-file))))

;; Replace supercite citing to boxquote citing
(defun upn-fix-sc-to-boxquote ()
  (interactive)
  (if (eq mark-active nil)
       (message "Region is not active")
    
    ;; else 
    (save-excursion  ;; We are changing mrk/point locations, restore them
      (when (< (mark) (point))
        (exchange-point-and-mark))

      (save-excursion
        (setq title "E-mail")
        ;; (if (looking-at "^ \\([^>]+\\)>" )
        (if (re-search-forward "^ \\([^>]+\\)>" nil t)
            (setq title (match-string 1))
          (setq title "E-mail")))
      (exchange-point-and-mark)
      (sc-uncite-region (mark) (point))
      (boxquote-region (mark) (point))
      ;; (goto-char start)
      (boxquote-title title))))
  
;; Put Timestamp in my signature.  The used functions are in
;; upn-own-functions.el and upn-time-functions.el
(defadvice mail-signature (after upn-put-ts-in-sig)
  (goto-char (point-max))
  (upn-insert-dashed-line 60)
  (insert "\n")
  (upn-insert-timestamp-lines 60)
  (upn-insert-dashed-line 60)
  (insert "\n"))

(ad-activate 'mail-signature)

(bbdb-insinuate-vm)