Synch to Oort Gnus 200303112120.
[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   (cond ((equal (aref token 1) ?:) 1.2)
132         ((equal (aref token 6) ?:) 1.1)
133         (t (error "Unknown hashcash format version"))))
134
135 ;;;###autoload
136 (defun hashcash-insert-payment (arg)
137   "Insert X-Payment and X-Hashcash headers with a payment for ARG"
138   (interactive "sPay to: ")
139   (let ((pay (hashcash-generate-payment (hashcash-payment-to arg)
140                                         (hashcash-payment-required arg))))
141     (when pay
142       (insert-before-markers "X-Payment: hashcash "
143                              (number-to-string (hashcash-version pay)) " "
144                              pay "\n")
145       (insert-before-markers "X-Hashcash: " pay "\n"))))
146
147 ;;;###autoload
148 (defun hashcash-verify-payment (token &optional resource amount)
149   "Verify a hashcash payment"
150   (let ((key (if (< (hashcash-version token) 1.2)
151                  (cadr (split-string token ":"))
152                  (caddr (split-string token ":")))))
153     (cond ((null resource)
154            (let ((elt (assoc key hashcash-accept-resources)))
155              (and elt (hashcash-check-payment token (car elt)
156                         (or (cadr elt) hashcash-default-accept-payment)))))
157           ((equal token key)
158            (hashcash-check-payment token resource
159                                 (or amount hashcash-default-accept-payment)))
160           (t nil))))
161
162 ;;;###autoload
163 (defun mail-add-payment (&optional arg)
164   "Add X-Payment: and X-Hashcash: headers with a hashcash payment
165 for each recipient address.  Prefix arg sets default payment temporarily."
166   (interactive "P")
167   (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
168                                     hashcash-default-payment))
169         (addrlist nil))
170     (save-excursion
171       (save-restriction
172         (goto-char (point-min))
173         (re-search-forward (concat "^\\("
174                                    (regexp-quote mail-header-separator)
175                                    "\\)?$"))
176         (beginning-of-line)
177         (narrow-to-region (point-min) (point))
178         (let ((to (hashcash-strip-quoted-names (mail-fetch-field "To" nil t)))
179               (cc (hashcash-strip-quoted-names (mail-fetch-field "Cc" nil t)))
180               (ng (hashcash-strip-quoted-names (mail-fetch-field "Newsgroups"
181                                                                  nil t))))
182           (when to
183             (setq addrlist (split-string to ",[ \t\n]*")))
184           (when cc
185             (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*"))))
186           (when (and hashcash-in-news ng)
187             (setq addrlist (nconc addrlist (split-string ng ",[ \t\n]*")))))
188         (while addrlist
189           (hashcash-insert-payment (pop addrlist))))))
190   t)
191
192 ;;;###autoload
193 (defun mail-check-payment (&optional arg)
194   "Look for a valid X-Payment: or X-Hashcash: header.
195 Prefix arg sets default accept amount temporarily."
196   (interactive "P")
197   (let ((hashcash-default-accept-payment (if arg (prefix-numeric-value arg)
198                                            hashcash-default-accept-payment))
199         (version (hashcash-version (hashcash-generate-payment "x" 1))))
200     (save-excursion
201       (goto-char (point-min))
202       (search-forward "\n\n")
203       (beginning-of-line)
204       (let ((end (point))
205             (ok nil))
206         (goto-char (point-min))
207         (while (and (not ok) (search-forward "X-Payment: hashcash " end t))
208           (let ((value (split-string
209                           (buffer-substring (point) (hashcash-point-at-eol))
210                           " ")))
211             (when (equal (car value) (number-to-string version))
212               (setq ok (hashcash-verify-payment (cadr value))))))
213         (goto-char (point-min))
214         (while (and (not ok) (search-forward "X-Hashcash: " end t))
215           (setq ok (hashcash-verify-payment
216                     (buffer-substring (point) (hashcash-point-at-eol)))))
217         (when ok
218           (message "Payment valid"))
219         ok))))
220
221 (provide 'hashcash)