From xemacs-m  Wed Feb 12 19:50:42 1997
Received: from beavis.bayserve.net (jmiller@port76.bayserve.net [206.148.244.167])
	by xemacs.org (8.8.5/8.8.5) with ESMTP id TAA17028
	for <xemacs-beta@xemacs.org>; Wed, 12 Feb 1997 19:50:35 -0600 (CST)
Received: (from jmiller@localhost) by beavis.bayserve.net (8.7.5/8.7.3) id VAA07045; Wed, 12 Feb 1997 21:07:53 -0500
Date: Wed, 12 Feb 1997 21:07:53 -0500
Message-Id: <199702130207.VAA07045@beavis.bayserve.net>
From: jmiller@bayserve.net
To: xemacs-beta@xemacs.org
Subject: vacation.el
Reply-to: jmiller@bayserve.net
Mime-Version: 1.0 (generated by tm-edit 7.101)
Content-Type: text/plain; charset=US-ASCII


  Any interest?

  This is pretty much the first and only lisp utility I wrote.  When I
  converted from mailtool -> vm, the one main feature I missed was using
  mailtool to turn vacation on & off.  So I put this together.  I am sure
  that it will make most of the experienced lisp programmers out there
  nauseous.  :)

  It would be nice to put the vacation status in a modeline somewhere, but
  I couldn't figure that out.

;;
;;
;;

;; 
;; location of response file
(defvar vacation-msg "~/.vacation.msg"
  "Filename of standard messages reply for vacation"
  )

;; your vacation program
(defvar vacation-prog "/usr/ucb/vacation"
  "Location of vacation program.  Use the full path"
  )

;; vacation program command line options to use
(defvar vacation-options ""
  "Options to pass to vacation when called from the .forward"
  )

;; default reply
(defvar vacation-reply "Subject: I am on vacation
Precedence: junk

Your mail regarding $SUBJECT will be read when I return.\n"
  "The default reply vacation will use if no previous vacation msg is found"
  )

;; startup variables
(defvar vacation-msg-entry-mode nil)
(defvar vacation-active nil)

(defun vacation-start()
  "Enable the vacation function for email"
  (interactive)
  ;; edit the reply
  (cond
   ((not (file-exists-p vacation-msg))
    ;; User does not already have a vacation msg.  Create default one.
    (vacation-startup-message)
    (pop-to-buffer (get-buffer vacation-msg))
    )
   ((file-exists-p vacation-msg)
    ;; User does have a vacation msg.  Read in and edit
    (find-file-other-frame (expand-file-name vacation-msg))
    )
   )
  (message "Edit vacation message.  Type C-c C-c when done.")
  (vacation-msg-mode)
  )

;;
;; Initialization code.  Only done at startup
;;

(if vacation-msg-entry-mode
    nil
  (setq vacation-msg-entry-mode (make-sparse-keymap))
  (set-keymap-name vacation-msg-entry-mode 'vacation-msg-entry-mode)
  (define-key vacation-msg-entry-mode "\C-c\C-c" 'vacation-finish-msg)
  )

(defvar vacation-active nil)
(defvar forward "~/.forward")
(defvar forward_ext ".BACKUP")

(setq forward_back (format "%s.%s" forward forward_ext))

(defun vacation-active-p ()
  "Determines whether vacation is running or not"
  (interactive)

  (cond
   ((not (file-exists-p forward ))
    ;; if no .forward file then vacation is not active
    (setq vacation-active nil)
    )
   ((file-exists-p forward)
    ;; ok, .forward exists, but is the vacation being used?
    (setq forward-buffer (find-file-noselect forward 'nowarn))
    (set-buffer forward-buffer)
    (setq vacation-active (re-search-forward "vacation" nil t nil))
    (kill-buffer (current-buffer))
    )
   )
  )

(defun vacation-menu ()
  (interactive)
  (add-submenu '("Folder")
	       '("Vacation"
		 ["Start" vacation-start :active (not (vacation-menu-p))]
		 ["End"   vacation-end   :active      (vacation-menu-p)]
		 ["Init DB" vacation-clear-database t])
	       )
  )


;(remove-hook 'vm-summary-mode-hook 'vacation-menu)

(defun vacation-menu-p ()
  (save-excursion
    (vacation-active-p)
    (and vacation-active t)
    )
  )

(provide 'vacation-active-p)
(provide 'vacation-menu)

(defun vacation-finish-msg ()
  (interactive)
  (set-visited-file-name vacation-msg)
  (save-buffer)
  (delete-frame)
  (kill-buffer (get-file-buffer vacation-msg))
  
  ;; Initialize the vacation database
  (vacation-clear-database)
  
  ;; Make .forward
  ;; Double check whether a .forward/vacation setup is already present
  (vacation-active-p)
  (if vacation-active
      (progn
	;; .forward already exist & already has vacation in it
      (message "Vacation is active in .forward")
      )
    (progn
      (if (file-exists-p forward)
	  ;; hmmm, .forward already exists, better just append vacation pipe
	  ;; also, make a backup copy
	  (progn
	    (copy-file forward forward_back)
	    (setq forward-buffer (find-file-noselect forward 'nowarn))
	    (set-buffer forward-buffer)
	    (goto-char (point-max))
	    (insert (format ", \|\"%s %s %s\""
			    vacation-prog vacation-options (user-login-name)
			    ))
	    (save-buffer)
	    (kill-buffer (current-buffer))
	    )
	)
      (if (not (file-exists-p forward))
	  (progn
	    ;; no .forward, just insert user name & vacation pipe
	    (set-buffer (get-buffer-create forward))
	    ;; insert default contents
	    (insert (format "\\%s, \|\"%s %s %s\""
			 (user-login-name) vacation-prog vacation-options 
			 (user-login-name)
			 ))
	    (set-visited-file-name forward)
	    (save-buffer)
	    (kill-buffer (current-buffer))
	    ))))
  ;; Updates vacation-active & menu items
  (vacation-active-p)
  )

(defun vacation-clear-database ()
  "Initialize the vacation dbm file"
  (interactive)
  (setq vacation (format "%s -I" vacation-prog))
  (shell-command vacation)
  )

(defun vacation-startup-message ()
  "Insert default reply in current buffer"
  ;; create buffer for msg
  (set-buffer (get-buffer-create vacation-msg))
  ;; insert default contents
  (insert (format vacation-reply ))
  )

(defun vacation-msg-mode ()
  "Minor Mode for editing vacation-msg"
  (interactive)
  (set-syntax-table text-mode-syntax-table)
  (use-local-map vacation-msg-entry-mode)
  (setq local-abbrev-table text-mode-abbrev-table)
  (setq major-mode 'vacation-msg-mode)
  (setq mode-name "Vacation")
  )

(defun vacation-end ()
  "Time to end the vacation :( "
  (interactive)
  ;; double check to make sure vacation is really active
  (vacation-active-p)
  (if vacation-active
      (progn
	(if (file-exists-p forward_back)
	    (progn
	      ;; if a backup exists, restore it, & delete backup
	      (copy-file forward_back forward t)
	      (delete-file forward_back)
	      )
	  ;; otherwise, just remove the .forward
	  (delete-file forward)
	  )))
  ;; should cleard vacation-active & update menu items
  (vacation-active-p)
  )

  (add-hook 'vm-summary-mode-hook 'vacation-menu)

