2009/10/23

Mail Goggles in Emacs

I wrote an elisp which implements Mail Goggles in Emacs.
Mail Goggles is an extension of gmail and it provides a confirmation before sending emails by some simple calculations in order to prevent sending inappropriate emails with little care.

Here is the elisp.
--------------------

(defvar mail-goggles-default-number-of-times 3
"The number of times that you need before do actions.")

(defvar mail-goggles-calculate-function-list '(+ - * /)
"The list of calculation functions for mail-goggles.")

(defun mail-goggles ()
"Gmail-like mail goggles.
You have to do calculation several times that is specified by
`mail-goggles-default-number-of-times'.
The calculation types are defined in `mail-goggles-calculate-function-list'."
(block "calc"
(let ((i 0))
(while (< i mail-goggles-default-number-of-times)
(if (not
(let* ((calc-func (nth (random 4) mail-goggles-calculate-function-list))
(x (random 100))
(y
(cond ((equal calc-func '/)
(let ((tmp (random 10)))
(while (= 0 tmp)
(setq tmp (random 10)))
tmp)
(random 10))
((equal calc-func '*)
(random 10))
(t (random 100))))
(answer (funcall calc-func x y)))
(= (read-number (format "Calculate %d%s%d: " x calc-func y))
answer)))
(progn
(message "You are tired, aren't you?")
(return-from "calc" nil)))
(setq i (1+ i)))
t)))

--------------------
I'm using gnus as my mail client, so I added these few lines.
--------------------

(defadvice message-send-and-exit (around mail-goggles)
"Do mail-goggles before sending a mail."
(if (mail-goggles)
ad-do-it))

(ad-activate 'message-send-and-exit)

--------------------
This advice enables you to do mail-goggles before sending an email.