;;; signature.el --- small hack to insert a custom signature

;; Copyright (C) 1999 by Association April

;; Author: Benjamin Drieu <drieu@alpha10.bocal.cs.univ-paris8.fr>
;; Keywords: mail

;; This file is NOT part of GNU Emacs

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation; either version 2, or (at your
;; option) any later version.

;; This file is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this file; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; signature.el inserts random signatures at the end of a mail.
;; Signatures are picked randomly from a file in the directory
;; $HOME/`signature-directory'/

;; Matching is performed against all the keys of the variable
;; `signature-email-addresses-alist' (from which the name of the file
;; is taken).  As soon as a match is correct, 

;; $Id$

;;; Code:


(provide 'signature)

(defvar signature-email-addresses-alist nil)
(defvar signature-my-nickname "Moi")
(defvar signature-directory ".emacs-signatures")


(defun mail-signature (atpoint)
  "Sign letter with contents of the file `mail-signature-file'.
Prefix arg means put contents at point."
  (interactive "P")
  (let ((here (point)))
    (goto-char (point-max))
    (eval mail-signature)
    (goto-char here)))



(defun signature-insert-signature ()
  ""
  (interactive)
  (insert "\n\n-- \n")
  (let ((file (signature-match signature-email-addresses-alist)))
    (if file 
	(let ((sig (signature-insert-random-quote file)))
	  (if sig
	      (insert sig)
	    (insert-file-contents mail-signature-file)))
      (insert-file-contents mail-signature-file))))



(defun signature-match (l)
  (cond
   ((null l) nil)
   ((string-match (concat "^[A-Za-z][A-Za-z]: " (caar l)) (buffer-string))
    (cdar l))
   (t (signature-match (cdr l)))))



(defun signature-insert-random-quote (&optional file)
  ""
  (interactive)
  (let ((cbuffer (current-buffer))
	(buffer (find-file-noselect (concat (getenv "HOME") "/" signature-directory "/" file))))
    (if buffer
	(progn
	  (save-excursion
	    (set-buffer buffer)
	    (goto-line (random* (count-lines (point-min) (point-max))))
	    (let ((s (thing-at-point 'line)))
	      (set-buffer cbuffer)
	      (kill-buffer buffer)
	      (concat signature-my-nickname "\n" s))))
      nil)))



;;;###autoload
(defun signature-init ()
  ""
  (interactive)
;  (make-local-variable 'mail-signature)
  (set-variable 'mail-signature '(signature-insert-signature)))

;;; signature.el ends here
