;; [eichin:19880309.2005EST]
;; zsend.el
;; Send zephyrgrams from emacs...
;;

;; Revision:
;; marc     03/30/91  Added check for nil message, to prompt
;; gamadrid 02/04/91  Added username/instance completion.

(defvar zsend-*who* (cons 'who (user-login-name)))

;; Create two association lists for people and instances
(defvar zsend-*who-list* (list (list (cdr zsend-*who*))))
(defvar zsend-*instance-list* (list (list )))

(defun zsend-mkprint (targ)
  (cond ((eq (car targ) 'instance) (concat "[" (cdr targ) "]"))
	((eq (car targ) 'who)      (concat "<" (cdr targ) ">"))
	(t                         (cdr targ))))

(defun zsend-mkdef (prompt old)
  (concat prompt " " (zsend-mkprint old) ":"))

(defun zsend-last-deal (new old)
  (if (equal (cdr new) "") old new))

(defun zsend (&optional who message)
  "Send zephyrgrams from emacs.

Usernames/instance names can be typed in or selected from a list of
previously used names.  

If a null messages is entered, a buffer will pop up in which to enter
the text of the message.  C-c C-c to close the buffer and send the message."
  (interactive 
   (let* (
	  (target (if current-prefix-arg
		      (cons 'instance 
			    (completing-read (zsend-mkdef "Instance"
							  zsend-*who*)
					     zsend-*instance-list*
					     nil
					     nil))    
		    (cons 'who 
			  (completing-read (zsend-mkdef "Who"
							zsend-*who*) 
					   zsend-*who-list*
					   nil
					   nil))))    
	  (real-target (zsend-last-deal target zsend-*who*))
	  (msg (read-input (concat "To " (zsend-mkprint real-target) ":"))))
     (list real-target msg)))
  (if (not message)			; added by Marc Horowitz <marc>
      (setq message (read-input
		     (concat "To " (zsend-mkprint who) ":"))))
  (let ((tempbuf (generate-new-buffer "*zephyr*send*")))
    (switch-to-buffer tempbuf)
    (local-set-key "" 'zsend-buffer)
    (local-set-key "j" 'zsend-buffer)
    (make-local-variable 'zsend-who)
    (erase-buffer)
    (setq zsend-*who*  who)		; or, use the old value
    (setq zsend-who who)
    
    (if (eq 'who (car who))
	(zsend-add-to-list zsend-*who-list* (cdr who))
      (zsend-add-to-list zsend-*instance-list* (cdr who)))
    
    (if (not (equal message ""))
	(progn
	  (insert message)
	  (zwrite who)))
  (if (equal message "")
	(switch-to-buffer tempbuf))))

(defun zsend-buffer ()
  (interactive)
  (zwrite zsend-who)
  (kill-buffer (current-buffer)))

(defun zwrite (who)
  (if (eq 'who (car who))
      (call-process-region (point-min) (point-max) ;range
			   "/usr/athena/bin/zwrite" ;process
			   t		;delete-p
			   t		;output-p
			   nil		;redisplay-p
			   "-a"		; jtkohl is being a twit
;			   "-q"		;args
			   "-l"		;ignore "."
			   "-n"		;no ping
			   (cdr who))
    (call-process-region (point-min) (point-max) ;range
			   "/usr/athena/bin/zwrite" ;process
			   t		;delete-p
			   t		;output-p
			   nil		;redisplay-p
			   "-a"		; jtkohl is being a twit
;			   "-q"		;args
			   "-l"		;ignore "."
			   "-n"		;no ping
			   "-i"		;[eichin:19880312.0015EST]
			   (cdr who)))
  (if (not (equal (point-max) 1))
      (message (buffer-substring 1 (1- (point-max))))))

(defun zsend-add-to-list (which-list name)
  (if (not (assoc name which-list))
      (let ((new-entry (cons name nil))	; Create new list element for name
	    (new-cell (cons (car which-list) (cdr which-list)))
	    )
	(setcar which-list new-entry)
	(setcdr which-list new-cell))))

;; [eichin:19880309.2223EST]
