Synch to Oort Gnus.
[elisp/gnus.git-] / contrib / hashcash.el
1 ;;; hashcash.el --- Add hashcash payments to email
2
3 ;; Copyright (C) 1997--2002 Paul E. Foley
4 ;; Copyright (C) 2003 Free Software Foundation
5
6 ;; Maintainer: Paul Foley <mycroft@actrix.gen.nz>
7 ;; Keywords: mail, hashcash
8
9 ;; Released under the GNU General Public License
10 ;;   (http://www.gnu.org/licenses/gpl.html)
11
12 ;;; Commentary:
13
14 ;; The hashcash binary is at http://www.cypherspace.org/hashcash/
15 ;;
16 ;; Call mail-add-payment to add a hashcash payment to a mail message
17 ;; in the current buffer.
18 ;;
19 ;; To automatically add payments to all outgoing mail:
20 ;;    (add-hook 'message-send-hook 'mail-add-payment)
21
22 ;;; Code:
23
24 (eval-when-compile (require 'cl))
25
26 (eval-and-compile
27  (autoload 'executable-find "executable"))
28
29 (defcustom hashcash-default-payment 0
30   "*The default number of bits to pay to unknown users.
31 If this is zero, no payment header will be generated.
32 See `hashcash-payment-alist'."
33   :type 'integer)
34
35 (defcustom hashcash-payment-alist '()
36   "*An association list mapping email addresses to payment amounts.
37 Elements may consist of (ADDR AMOUNT) or (ADDR STRING AMOUNT), where
38 ADDR is the email address of the intended recipient and AMOUNT is
39 the value of hashcash payment to be made to that user.  STRING, if
40 present, is the string to be hashed; if not present ADDR will be used.")
41
42 (defcustom hashcash-default-accept-payment 10
43   "*The default minimum number of bits to accept on incoming payments."
44   :type 'integer)
45
46 (defcustom hashcash-accept-resources `((,user-mail-address nil))
47   "*An association list mapping hashcash resources to payment amounts.
48 Resources named here are to be accepted in incoming payments.  If the
49 corresponding AMOUNT is NIL, the value of `hashcash-default-accept-payment'
50 is used instead.")
51
52 (defcustom hashcash-path (executable-find "hashcash")
53   "*The path to the hashcash binary.")
54
55 (defcustom hashcash-double-spend-database "hashcash.db"
56   "*The path to the double-spending database.")
57
58 (defcustom hashcash-in-news nil
59   "*Specifies whether or not hashcash payments should be made to newsgroups."
60   :type 'boolean)
61
62 (require 'mail-utils)
63
64 (defalias 'hashcash-point-at-bol
65     (if (fboundp 'point-at-bol)
66         'point-at-bol
67         'line-beginning-position))
68
69 (defalias 'hashcash-point-at-eol
70     (if (fboundp 'point-at-eol)
71         'point-at-eol
72         'line-end-position))
73
74 (defun hashcash-strip-quoted-names (addr)
75   (setq addr (mail-strip-quoted-names addr))
76   (if (and addr (string-match "^[^+@]+\\(\\+[^@]*\\)@" addr))
77       (concat (substring addr 0 (match-beginning 1))
78               (substring addr (match-end 1)))
79     addr))
80
81 (defun hashcash-payment-required (addr)
82   "Return the hashcash payment value required for the given address."
83   (let ((val (assoc addr hashcash-payment-alist)))
84     (if val
85         (if (cddr val)
86             (caddr val)
87           (cadr val))
88       hashcash-default-payment)))
89
90 (defun hashcash-payment-to (addr)
91   "Return the string with which hashcash payments should collide."
92   (let ((val (assoc addr hashcash-payment-alist)))
93     (if val
94         (if (cddr val)
95             (cadr val)
96           (car val))
97       addr)))
98
99 (defun hashcash-generate-payment (str val)
100   "Generate a hashcash payment by finding a VAL-bit collison on STR."
101   (if (> val 0)
102       (save-excursion
103         (set-buffer (get-buffer-create " *hashcash*"))
104         (erase-buffer)
105         (call-process hashcash-path nil t nil
106                       (concat "-b " (number-to-string val)) str)
107         (goto-char (point-min))
108         (buffer-substring (hashcash-point-at-bol) (hashcash-point-at-eol)))
109     nil))
110
111 (defun hashcash-check-payment (token str val)
112   "Check the validity of a hashcash payment."
113   (zerop (call-process hashcash-path nil nil nil "-c"
114                        "-d" "-f" hashcash-double-spend-database
115                        "-b" (number-to-string val)
116                        "-r" str
117                        token)))
118
119 (defun hashcash-version (token)
120   "Find the format version of a hashcash token."
121   ;; Version 1.2 looks like n:yymmdd:rrrrr:xxxxxxxxxxxxxxxx
122   ;;   This carries its own version number embedded in the token,
123   ;;   so no further format number changes should be necessary
124   ;;   in the X-Payment header.
125   ;;
126   ;; Version 1.1 looks like yymmdd:rrrrr:xxxxxxxxxxxxxxxx
127   ;;   You need to upgrade your hashcash binary.
128   ;;
129   ;; Version 1.0 looked like nnnnnrrrrrxxxxxxxxxxxxxxxx
130   ;;   This is no longer supported.
131   (if token
132       (cond ((equal (aref token 1) ?:) 1.2)
133             ((equal (aref token 6) ?:) 1.1)
134             (t (error "Unknown hashcash format version")))
135     nil))
136
137 ;;;###autoload
138 (defun hashcash-insert-payment (arg)
139   "Insert X-Payment and X-Hashcash headers with a payment for ARG"
140   (interactive "sPay to: ")
141   (let* ((pay (hashcash-generate-payment (hashcash-payment-to arg)
142                                          (hashcash-payment-required arg)))
143          (version (hashcash-version pay)))
144     (when pay
145       (insert-before-markers "X-Payment: hashcash "
146                              (number-to-string version) " " pay "\n")
147       (insert-before-markers "X-Hashcash: " pay "\n"))))
148
149 ;;;###autoload
150 (defun hashcash-verify-payment (token &optional resource amount)
151   "Verify a hashcash payment"
152   (let ((key (if (< (hashcash-version token) 1.2)
153                  (cadr (split-string token ":"))
154                  (caddr (split-string token ":")))))
155     (cond ((null resource)
156            (let ((elt (assoc key hashcash-accept-resources)))
157              (and elt (hashcash-check-payment token (car elt)
158                         (or (cadr elt) hashcash-default-accept-payment)))))
159           ((equal token key)
160            (hashcash-check-payment token resource
161                                 (or amount hashcash-default-accept-payment)))
162           (t nil))))
163
164 ;;;###autoload
165 (defun mail-add-payment (&optional arg)
166   "Add X-Payment: and X-Hashcash: headers with a hashcash payment
167 for each recipient address.  Prefix arg sets default payment temporarily."
168   (interactive "P")
169   (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
170                                     hashcash-default-payment))
171         (addrlist nil))
172     (save-excursion
173       (save-restriction
174         (goto-char (point-min))
175         (re-search-forward (concat "^\\("
176                                    (regexp-quote mail-header-separator)
177                                    "\\)?$"))
178         (beginning-of-line)
179         (narrow-to-region (point-min) (point))
180         (let ((to (hashcash-strip-quoted-names (mail-fetch-field "To" nil t)))
181               (cc (hashcash-strip-quoted-names (mail-fetch-field "Cc" nil t)))
182               (ng (hashcash-strip-quoted-names (mail-fetch-field "Newsgroups"
183                                                                  nil t))))
184           (when to
185             (setq addrlist (split-string to ",[ \t\n]*")))
186           (when cc
187             (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*"))))
188           (when (and hashcash-in-news ng)
189             (setq addrlist (nconc addrlist (split-string ng ",[ \t\n]*")))))
190         (while addrlist
191           (hashcash-insert-payment (pop addrlist))))))
192   t)
193
194 ;;;###autoload
195 (defun mail-check-payment (&optional arg)
196   "Look for a valid X-Payment: or X-Hashcash: header.
197 Prefix arg sets default accept amount temporarily."
198   (interactive "P")
199   (let ((hashcash-default-accept-payment (if arg (prefix-numeric-value arg)
200                                            hashcash-default-accept-payment))
201         (version (hashcash-version (hashcash-generate-payment "x" 1))))
202     (save-excursion
203       (goto-char (point-min))
204       (search-forward "\n\n")
205       (beginning-of-line)
206       (let ((end (point))
207             (ok nil))
208         (goto-char (point-min))
209         (while (and (not ok) (search-forward "X-Payment: hashcash " end t))
210           (let ((value (split-string
211                           (buffer-substring (point) (hashcash-point-at-eol))
212                           " ")))
213             (when (equal (car value) (number-to-string version))
214               (setq ok (hashcash-verify-payment (cadr value))))))
215         (goto-char (point-min))
216         (while (and (not ok) (search-forward "X-Hashcash: " end t))
217           (setq ok (hashcash-verify-payment
218                     (buffer-substring (point) (hashcash-point-at-eol)))))
219         (when ok
220           (message "Payment valid"))
221         ok))))
222
223 (provide 'hashcash)