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