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