;; -*-Emacs-Lisp-*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; File:         anon-pw.el
;; RCS:          $Header: $
;; Description:  insert anon-pw field in outgoing mail/news
;; Author:       Christian Limpach <chris@nice.ch>
;; Created:      Tue Apr 18 02:34:24 1995
;; Modified:     Tue Apr 18 02:49:31 1995 (Christian Limpach) chris@nice.ch
;; Language:     Lisp Interaction
;; Package:      N/A
;; Status:       Experimental (Do Not Distribute)
;;
;; (C) Copyright 1995, Christian Limpach, all rights reserved.
;; This is free software, and you are welcome to redistribute it
;; under certain conditions; see ftp://nice.ethz.ch/users/chris/TERMS
;; for details. This software comes with ABSOLUTELY NO WARRANTY.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Installation:
;;
;; (setq anon-password "your password goes here..."
;; (add-hook 'mail-send-hook 'anon-add-password-field)
;; (add-hook 'news-inews-hook 'anon-add-password-field)

(defvar anon-servers-regexp "^[^,]*@anon.penet.fi$"
  "Regexp which has to match the To: field. If this match fails, no
password is inserted.")

(defvar anon-empty-fields '("Cc" "Bcc")
  "List of fields which have to be empty. If one of these fields is
present in the header, no password is inserted.")

(defvar anon-password-field "X-Anon-Password"
  "New field to be inserted unless there is already such a field.")

(defvar anon-password "I don't have one..."
  "The secret (well, kind of...) password to be inserted.")

(defun anon-add-password-field ()
  "Insert a password field in the header. To be used in either
mail-send-hook, news-inews-hook or some other hook which is run when
the message is going to be sent. You may also run this command
interactively."
  (interactive)
  (save-excursion
    (let ((header-end (progn (mail-text) (forward-line -1) (point)))
	  (aef (cons anon-password-field anon-empty-fields))
	  (case-fold-search t))
      (if (save-restriction
	    (narrow-to-region (point-min) header-end)
	    (and (string-match anon-servers-regexp
			       (mail-fetch-field "To" nil t))
		 (progn
		   (while (and aef (not (mail-fetch-field (car aef) nil t)))
		     (setq aef (cdr aef)))
		   (not aef))))
	  (progn
	    ;; etiher put the anon-pw field after Subject: or To:
	    (or (mail-position-on-field "Subject" t)
		(mail-position-on-field "To"))
	    (insert "\n" anon-password-field ": " anon-password))))))

(provide 'anon-pw)
