T-gnus 6.15.10 revision 00.
[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 (defun hashcash-strip-quoted-names (addr)
60   (setq addr (mail-strip-quoted-names addr))
61   (if (and addr (string-match "^[^+@]+\\(\\+[^@]*\\)@" addr))
62       (concat (substring addr 0 (match-beginning 1))
63               (substring addr (match-end 1)))
64     addr))
65
66 (defun hashcash-payment-required (addr)
67   "Return the hashcash payment value required for the given address."
68   (let ((val (assoc addr hashcash-payment-alist)))
69     (if val
70         (if (cddr val)
71             (caddr val)
72           (cadr val))
73       hashcash-default-payment)))
74
75 (defun hashcash-payment-to (addr)
76   "Return the string with which hashcash payments should collide."
77   (let ((val (assoc addr hashcash-payment-alist)))
78     (if val
79         (if (cddr val)
80             (cadr val)
81           (car val))
82       addr)))
83
84 (defun hashcash-generate-payment (str val)
85   "Generate a hashcash payment by finding a VAL-bit collison on STR."
86   (if (> val 0)
87       (save-excursion
88         (set-buffer (get-buffer-create " *hashcash*"))
89         (erase-buffer)
90         (call-process hashcash nil t nil (concat "-b " (number-to-string val))
91                       str)
92         (goto-char (point-min))
93         (buffer-substring (point-at-bol) (point-at-eol)))
94     nil))
95
96 (defun hashcash-check-payment (token str val)
97   "Check the validity of a hashcash payment."
98   (zerop (call-process hashcash nil nil nil "-c"
99                        "-d" "-f" hashcash-double-spend-database
100                        "-b" (number-to-string val)
101                        "-r" str
102                        token)))
103
104 ;;;###autoload
105 (defun hashcash-insert-payment (arg)
106   "Insert X-Payment and X-Hashcash headers with a payment for ARG"
107   (interactive "sPay to: ")
108   (let ((pay (hashcash-generate-payment (hashcash-payment-to arg)
109                                         (hashcash-payment-required arg))))
110     (when pay
111       (insert-before-markers "X-Payment: hashcash 1.1 " pay "\n")
112       (insert-before-markers "X-Hashcash: " pay "\n"))))
113
114 ;;;###autoload
115 (defun hashcash-verify-payment (token &optional resource amount)
116   "Verify a hashcash payment"
117   (let ((key (cadr (split-string-by-char token ?:))))
118     (cond ((null resource)
119            (let ((elt (assoc key hashcash-accept-resources)))
120              (and elt (hashcash-check-payment token (car elt)
121                         (or (cadr elt) hashcash-default-accept-payment)))))
122           ((equal token key)
123            (hashcash-check-payment token resource
124                                 (or amount hashcash-default-accept-payment)))
125           (t nil))))
126
127 ;;;###autoload
128 (defun mail-add-payment (&optional arg)
129   "Add X-Payment: and X-Hashcash: headers with a hashcash payment
130 for each recipient address.  Prefix arg sets default payment temporarily."
131   (interactive "P")
132   (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
133                                     hashcash-default-payment))
134         (addrlist nil))
135     (save-excursion
136       (save-restriction
137         (goto-char (point-min))
138         (re-search-forward (concat "^\\("
139                                    (regexp-quote mail-header-separator)
140                                    "\\)?$"))
141         (beginning-of-line)
142         (narrow-to-region (point-min) (point))
143         (let ((to (hashcash-strip-quoted-names (mail-fetch-field "To" nil t)))
144               (cc (hashcash-strip-quoted-names (mail-fetch-field "Cc" nil t)))
145               (ng (hashcash-strip-quoted-names (mail-fetch-field "Newsgroups"
146                                                                  nil t))))
147           (when to
148             (setq addrlist (split-string to ",[ \t\n]*")))
149           (when cc
150             (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*"))))
151           (when (and hashcash-in-news ng)
152             (setq addrlist (nconc addrlist (split-string ng ",[ \t\n]*")))))
153         (while addrlist
154           (hashcash-insert-payment (pop addrlist))))))
155   t)
156
157 ;;;###autoload
158 (defun mail-check-payment (&optional arg)
159   "Look for a valid X-Payment: or X-Hashcash: header.
160 Prefix arg sets default accept amount temporarily."
161   (interactive "P")
162   (let ((hashcash-default-accept-payment (if arg (prefix-numeric-value arg)
163                                            hashcash-default-accept-payment)))
164     (save-excursion
165       (goto-char (point-min))
166       (search-forward mail-header-separator)
167       (beginning-of-line)
168       (let ((end (point))
169             (ok nil))
170         (goto-char (point-min))
171         (while (and (not ok) (search-forward "X-Payment: hashcash 1.1 " end t))
172           (setq ok (hashcash-verify-payment
173                     (buffer-substring (point) (point-at-eol)))))
174         (goto-char (point-min))
175         (while (and (not ok) (search-forward "X-Hashcash: " end t))
176           (setq ok (hashcash-verify-payment
177                     (buffer-substring (point) (point-at-eol)))))
178         (when ok
179           (message "Payment valid"))
180         ok))))
181
182 (provide 'hashcash)
183
184 ;;; hashcash.el ends here