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