ccdd3146878c636badaf6b84a51abc87a31dfa00
[elisp/gnus.git-] / contrib / hashcash.el
1 ;;; hashcash.el --- Add hashcash payments to email
2
3 ;; $Revision: 1.1.4.1 $
4 ;; Copyright (C) 1997,2001 Paul E. Foley
5
6 ;; Maintainer: Paul Foley <mycroft@actrix.gen.nz>
7 ;; Keywords: mail, hashcash
8
9 ;; Released under the GNU General Public License
10
11 ;;; Commentary:
12
13 ;; The hashcash binary is at http://www.cypherspace.org/hashcash/
14 ;;
15 ;; Call mail-add-payment to add a hashcash payment to a mail message
16 ;; in the current buffer.
17 ;;
18 ;; To automatically add payments to all outgoing mail:
19 ;;    (add-hook 'message-send-hook 'mail-add-payment)
20
21 ;;; Code:
22
23 (eval-when-compile (require 'cl))
24
25 (defcustom hashcash-default-payment 0
26   "*The default number of bits to pay to unknown users.
27 If this is zero, no payment header will be generated.
28 See `hashcash-payment-alist'."
29   :type 'integer)
30
31 (defcustom hashcash-payment-alist nil
32   "*An association list mapping email addresses to payment amounts.
33 Elements may consist of (ADDR AMOUNT) or (ADDR STRING AMOUNT), where
34 ADDR is the email address of the intended recipient and AMOUNT is
35 the value of hashcash payment to be made to that user.  STRING, if
36 present, is the string to be hashed; if not present ADDR will be used.")
37
38 (defcustom hashcash "hashcash"
39   "*The path to the hashcash binary.")
40
41 (require 'mail-utils)
42
43 (defun hashcash-strip-quoted-names (addr)
44   (setq addr (mail-strip-quoted-names addr))
45   (if (and addr (string-match "^[^+@]+\\(\\+[^@]*\\)@" addr))
46       (concat (substring addr 0 (match-beginning 1))
47               (substring addr (match-end 1)))
48     addr))
49
50 (defun hashcash-payment-required (addr)
51   "Return the hashcash payment value required for the given address."
52   (let ((val (assoc addr hashcash-payment-alist)))
53     (if val
54         (if (cddr val)
55             (caddr val)
56           (cadr val))
57       hashcash-default-payment)))
58
59 (defun hashcash-payment-to (addr)
60   "Return the string with which hashcash payments should collide."
61   (let ((val (assoc addr hashcash-payment-alist)))
62     (if val
63         (if (cddr val)
64             (cadr val)
65           (car val))
66       addr)))
67
68 (defun hashcash-generate-payment (str val)
69   "Generate a hashcash payment by finding a VAL-bit collison on STR."
70   (if (> val 0)
71       (save-excursion
72         (set-buffer (get-buffer-create " *hashcash*"))
73         (erase-buffer)
74         (call-process hashcash nil t nil (concat "-b " (number-to-string val))
75                       str)
76         (goto-char (point-min))
77         (buffer-substring (point-at-bol) (point-at-eol)))
78     nil))
79
80 (defun hashcash-insert-payment (arg)
81   "Insert an X-Hashcash header with a payment for ARG"
82   (interactive "sPay to: ")
83   (let ((pay (hashcash-generate-payment (hashcash-payment-to arg)
84                                         (hashcash-payment-required arg))))
85     (when pay
86       (insert-before-markers "X-Hashcash: " pay "\n"))))
87
88 ;;;###autoload
89 (defun mail-add-payment (&optional arg)
90   "Add an X-Hashcash: header with a hashcash payment for each recipient address
91 Prefix arg sets default payment temporarily."
92   (interactive "P")
93   (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
94                                     hashcash-default-payment))
95         (addrlist nil))
96     (save-excursion
97       (save-restriction
98         (goto-char (point-min))
99         (re-search-forward (concat "^\\("
100                                    (regexp-quote mail-header-separator)
101                                    "\\)?$"))
102         (beginning-of-line)
103         (narrow-to-region (point-min) (point))
104         (let ((to (hashcash-strip-quoted-names (mail-fetch-field "To" nil t)))
105               (cc (hashcash-strip-quoted-names (mail-fetch-field "Cc" nil t)))
106               (ng (hashcash-strip-quoted-names
107                    (mail-fetch-field "Newsgroups" nil t))))
108           (when to
109             (setq addrlist (split-string to ",[ \t\n]*")))
110           (when cc
111             (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*"))))
112           (when ng
113             (setq addrlist (nconc addrlist (split-string ng ",[ \t\n]*")))))
114         (while addrlist
115           (hashcash-insert-payment (pop addrlist))))))
116   t)
117
118 (provide 'hashcash)
119
120 ;;; hashcash.el ends here