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