c06d6f8d671962a381b35e86a9146f166a51ea09
[gnus] / contrib / hashcash.el
1 ;;; hashcash.el --- Add hashcash payments to email
2
3 ;; $Revision: 7.1 $
4 ;; Copyright (C) 2003 Free Software Foundation
5 ;; Copyright (C) 1997--2002 Paul E. Foley
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 (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-payment-required (addr)
79   "Return the hashcash payment value required for the given address."
80   (let ((val (assoc addr hashcash-payment-alist)))
81     (or (nth 2 val) (nth 1 val) hashcash-default-payment)))
82
83 (defun hashcash-payment-to (addr)
84   "Return the string with which hashcash payments should collide."
85   (let ((val (assoc addr hashcash-payment-alist)))
86     (or (nth 1 val) (nth 0 val) addr)))
87
88 (defun hashcash-generate-payment (str val)
89   "Generate a hashcash payment by finding a VAL-bit collison on STR."
90   (if (> val 0)
91       (save-excursion
92         (set-buffer (get-buffer-create " *hashcash*"))
93         (erase-buffer)
94         (call-process hashcash-path nil t nil
95                       "-m" "-q" "-b" (number-to-string val) str)
96         (goto-char (point-min))
97         (buffer-substring (hashcash-point-at-bol) (hashcash-point-at-eol)))
98     nil))
99
100 (defun hashcash-check-payment (token str val)
101   "Check the validity of a hashcash payment."
102   (zerop (call-process hashcash-path nil nil nil "-c"
103                        "-d" "-f" hashcash-double-spend-database
104                        "-b" (number-to-string val)
105                        "-r" str
106                        token)))
107
108 (defun hashcash-version (token)
109   "Find the format version of a hashcash token."
110   ;; Version 1.2 looks like n:yymmdd:rrrrr:xxxxxxxxxxxxxxxx
111   ;;   This carries its own version number embedded in the token,
112   ;;   so no further format number changes should be necessary
113   ;;   in the X-Payment header.
114   ;;
115   ;; Version 1.1 looks like yymmdd:rrrrr:xxxxxxxxxxxxxxxx
116   ;;   You need to upgrade your hashcash binary.
117   ;;
118   ;; Version 1.0 looked like nnnnnrrrrrxxxxxxxxxxxxxxxx
119   ;;   This is no longer supported.
120   (cond ((equal (aref token 1) ?:) 1.2)
121         ((equal (aref token 6) ?:) 1.1)
122         (t (error "Unknown hashcash format version"))))
123
124 ;;;###autoload
125 (defun hashcash-insert-payment (arg)
126   "Insert X-Payment and X-Hashcash headers with a payment for ARG"
127   (interactive "sPay to: ")
128   (let ((pay (hashcash-generate-payment (hashcash-payment-to arg)
129                                         (hashcash-payment-required arg))))
130     (when pay
131 ;      (insert-before-markers "X-Payment: hashcash "
132 ;                            (number-to-string (hashcash-version pay)) " "
133 ;                            pay "\n")
134       (insert-before-markers "X-Hashcash: " pay "\n"))))
135
136 ;;;###autoload
137 (defun hashcash-verify-payment (token &optional resource amount)
138   "Verify a hashcash payment"
139   (let ((key (if (< (hashcash-version token) 1.2)
140                  (nth 1 (split-string token ":"))
141                  (nth 2 (split-string token ":")))))
142     (cond ((null resource)
143            (let ((elt (assoc key hashcash-accept-resources)))
144              (and elt (hashcash-check-payment token (car elt)
145                         (or (cadr elt) hashcash-default-accept-payment)))))
146           ((equal token key)
147            (hashcash-check-payment token resource
148                                 (or amount hashcash-default-accept-payment)))
149           (t nil))))
150
151 ;;;###autoload
152 (defun mail-add-payment (&optional arg)
153   "Add X-Payment: and X-Hashcash: headers with a hashcash payment
154 for each recipient address.  Prefix arg sets default payment temporarily."
155   (interactive "P")
156   (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
157                                     hashcash-default-payment))
158         (addrlist nil))
159     (save-excursion
160       (save-restriction
161         (goto-char (point-min))
162         (search-forward mail-header-separator)
163         (beginning-of-line)
164         (narrow-to-region (point-min) (point))
165         (let ((to (hashcash-strip-quoted-names (mail-fetch-field "To" nil t)))
166               (cc (hashcash-strip-quoted-names (mail-fetch-field "Cc" nil t)))
167               (ng (hashcash-strip-quoted-names (mail-fetch-field "Newsgroups"
168                                                                  nil t))))
169           (when to
170             (setq addrlist (split-string to ",[ \t\n]*")))
171           (when cc
172             (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*"))))
173           (when (and hashcash-in-news ng)
174             (setq addrlist (nconc addrlist (split-string ng ",[ \t\n]*")))))
175         (when addrlist
176           (mapcar #'hashcash-insert-payment addrlist))))) ; mapc
177   t)
178
179 ;;;###autoload
180 (defun mail-check-payment (&optional arg)
181   "Look for a valid X-Payment: or X-Hashcash: header.
182 Prefix arg sets default accept amount temporarily."
183   (interactive "P")
184   (let ((hashcash-default-accept-payment (if arg (prefix-numeric-value arg)
185                                            hashcash-default-accept-payment))
186         (version (hashcash-version (hashcash-generate-payment "x" 1))))
187     (save-excursion
188       (goto-char (point-min))
189       (search-forward "\n\n")
190       (beginning-of-line)
191       (let ((end (point))
192             (ok nil))
193         (goto-char (point-min))
194         (while (and (not ok) (search-forward "X-Payment: hashcash " end t))
195           (let ((value (split-string
196                           (buffer-substring (point) (hashcash-point-at-eol))
197                           " ")))
198             (when (equal (car value) (number-to-string version))
199               (setq ok (hashcash-verify-payment (cadr value))))))
200         (goto-char (point-min))
201         (while (and (not ok) (search-forward "X-Hashcash: " end t))
202           (setq ok (hashcash-verify-payment
203                     (buffer-substring (point) (hashcash-point-at-eol)))))
204         (when ok
205           (message "Payment valid"))
206         ok))))
207
208 (provide 'hashcash)
209
210 ;;; arch-tag: 0e7fe983-a124-4392-9788-0dbcbd2c4d62