2004-09-27 Simon Josefsson <jas@extundo.com>
[gnus] / lisp / hashcash.el
1 ;;; hashcash.el --- Add hashcash payments to email
2
3 ;; $Revision: 1.13 $
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 10
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 (eval-and-compile
64  (if (fboundp 'point-at-bol)
65      (defalias 'hashcash-point-at-bol 'point-at-bol)
66    (defalias 'hashcash-point-at-bol 'line-beginning-position))
67
68  (if (fboundp 'point-at-eol)
69      (defalias 'hashcash-point-at-eol 'point-at-eol)
70    (defalias 'hashcash-point-at-eol 'line-end-position)))
71
72 (defun hashcash-strip-quoted-names (addr)
73   (setq addr (mail-strip-quoted-names addr))
74   (if (and addr (string-match "\\`\\([^+@]+\\)\\+[^@]*\\(@.+\\)" addr))
75       (concat (match-string 1 addr) (match-string 2 addr))
76     addr))
77
78 (defun hashcash-token-substring ()
79   (save-excursion
80     (let ((token ""))
81       (loop
82         (setq token
83           (concat token (buffer-substring (point) (hashcash-point-at-eol))))
84         (goto-char (hashcash-point-at-eol))
85         (forward-char 1)
86         (unless (looking-at "[ \t]") (return token))
87         (while (looking-at "[ \t]") (forward-char 1))))))
88
89 (defun hashcash-payment-required (addr)
90   "Return the hashcash payment value required for the given address."
91   (let ((val (assoc addr hashcash-payment-alist)))
92     (or (nth 2 val) (nth 1 val) hashcash-default-payment)))
93
94 (defun hashcash-payment-to (addr)
95   "Return the string with which hashcash payments should collide."
96   (let ((val (assoc addr hashcash-payment-alist)))
97     (or (nth 1 val) (nth 0 val) 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                       "-m" "-q" "-b" (number-to-string val) str)
107         (goto-char (point-min))
108         (hashcash-token-substring))
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* ((split (split-string token ":"))
151          (key (if (< (hashcash-version token) 1.2)
152                   (nth 1 split)
153                   (case (string-to-number (nth 0 split))
154                     (0 (nth 2 split))
155                     (1 (nth 3 split))))))
156     (cond ((null resource)
157            (let ((elt (assoc key hashcash-accept-resources)))
158              (and elt (hashcash-check-payment token (car elt)
159                         (or (cadr elt) hashcash-default-accept-payment)))))
160           ((equal token key)
161            (hashcash-check-payment token resource
162                                 (or amount hashcash-default-accept-payment)))
163           (t nil))))
164
165 ;;;###autoload
166 (defun mail-add-payment (&optional arg)
167   "Add X-Payment: and X-Hashcash: headers with a hashcash payment
168 for each recipient address.  Prefix arg sets default payment temporarily."
169   (interactive "P")
170   (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
171                                     hashcash-default-payment))
172         (addrlist nil))
173     (save-excursion
174       (save-restriction
175         (goto-char (point-min))
176         (search-forward mail-header-separator)
177         (beginning-of-line)
178         (narrow-to-region (point-min) (point))
179         (let ((to (hashcash-strip-quoted-names (mail-fetch-field "To" nil t)))
180               (cc (hashcash-strip-quoted-names (mail-fetch-field "Cc" nil t)))
181               (ng (hashcash-strip-quoted-names (mail-fetch-field "Newsgroups"
182                                                                  nil t))))
183           (when to
184             (setq addrlist (split-string to ",[ \t\n]*")))
185           (when cc
186             (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*"))))
187           (when (and hashcash-in-news ng)
188             (setq addrlist (nconc addrlist (split-string ng ",[ \t\n]*")))))
189         (when addrlist
190           (mapcar #'hashcash-insert-payment addrlist))))) ; mapc
191   t)
192
193 ;;;###autoload
194 (defun mail-check-payment (&optional arg)
195   "Look for a valid X-Payment: or X-Hashcash: header.
196 Prefix arg sets default accept amount temporarily."
197   (interactive "P")
198   (let ((hashcash-default-accept-payment (if arg (prefix-numeric-value arg)
199                                            hashcash-default-accept-payment))
200         (version (hashcash-version (hashcash-generate-payment "x" 1))))
201     (save-excursion
202       (goto-char (point-min))
203       (search-forward "\n\n")
204       (beginning-of-line)
205       (let ((end (point))
206             (ok nil))
207         (goto-char (point-min))
208         (while (and (not ok) (search-forward "X-Payment: hashcash " end t))
209           (let ((value (split-string (hashcash-token-substring) " ")))
210             (when (equal (car value) (number-to-string version))
211               (setq ok (hashcash-verify-payment (cadr value))))))
212         (goto-char (point-min))
213         (while (and (not ok) (search-forward "X-Hashcash: " end t))
214           (setq ok (hashcash-verify-payment (hashcash-token-substring))))
215         (when ok
216           (message "Payment valid"))
217         ok))))
218
219 (provide 'hashcash)
220
221 ;;; arch-tag: 0e7fe983-a124-4392-9788-0dbcbd2c4d62