1 ;;; hashcash.el --- Add hashcash payments to email
4 ;; Copyright (C) 1997,2001 Paul E. Foley
6 ;; Maintainer: Paul Foley <mycroft@actrix.gen.nz>
7 ;; Keywords: mail, hashcash
9 ;; Released under the GNU General Public License
13 ;; The hashcash binary is at http://www.cypherspace.org/hashcash/
15 ;; Call mail-add-payment to add a hashcash payment to a mail message
16 ;; in the current buffer.
18 ;; To automatically add payments to all outgoing mail:
19 ;; (add-hook 'message-send-hook 'mail-add-payment)
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'."
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.")
36 (defcustom hashcash-default-accept-payment 10
37 "*The default minimum number of bits to accept on incoming payments."
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'
46 (defcustom hashcash "/usr/local/bin/hashcash"
47 "*The path to the hashcash binary.")
49 (defcustom hashcash-double-spend-database "hashcash.db"
50 "*The path to the double-spending database.")
52 (defcustom hashcash-in-news nil
53 "*Specifies whether or not hashcash payments should be made to newsgroups."
58 (defalias 'hashcash-point-at-bol
59 (if (fboundp 'point-at-bol)
61 'line-beginning-position))
63 (defalias 'hashcash-point-at-eol
64 (if (fboundp 'point-at-eol)
68 (defun hashcash-strip-quoted-names (addr)
69 (setq addr (mail-strip-quoted-names addr))
70 (if (and addr (string-match "^[^+@]+\\(\\+[^@]*\\)@" addr))
71 (concat (subseq addr 0 (match-beginning 1)) (subseq addr (match-end 1)))
74 (defun hashcash-payment-required (addr)
75 "Return the hashcash payment value required for the given address."
76 (let ((val (assoc addr hashcash-payment-alist)))
81 hashcash-default-payment)))
83 (defun hashcash-payment-to (addr)
84 "Return the string with which hashcash payments should collide."
85 (let ((val (assoc addr hashcash-payment-alist)))
92 (defun hashcash-generate-payment (str val)
93 "Generate a hashcash payment by finding a VAL-bit collison on STR."
96 (set-buffer (get-buffer-create " *hashcash*"))
98 (call-process hashcash nil t nil (concat "-b " (number-to-string val))
100 (goto-char (point-min))
101 (buffer-substring (hashcash-point-at-bol) (hashcash-point-at-eol)))
104 (defun hashcash-check-payment (token str val)
105 "Check the validity of a hashcash payment."
106 (zerop (call-process hashcash nil nil nil "-c"
107 "-d" "-f" hashcash-double-spend-database
108 "-b" (number-to-string val)
113 (defun hashcash-insert-payment (arg)
114 "Insert X-Payment and X-Hashcash headers with a payment for ARG"
115 (interactive "sPay to: ")
116 (let ((pay (hashcash-generate-payment (hashcash-payment-to arg)
117 (hashcash-payment-required arg))))
119 (insert-before-markers "X-Payment: hashcash 1.1 " pay "\n")
120 (insert-before-markers "X-Hashcash: " pay "\n"))))
123 (defun hashcash-verify-payment (token &optional resource amount)
124 "Verify a hashcash payment"
125 (let ((key (cadr (split-string-by-char token ?:))))
126 (cond ((null resource)
127 (let ((elt (assoc key hashcash-accept-resources)))
128 (and elt (hashcash-check-payment token (car elt)
129 (or (cadr elt) hashcash-default-accept-payment)))))
131 (hashcash-check-payment token resource
132 (or amount hashcash-default-accept-payment)))
136 (defun mail-add-payment (&optional arg)
137 "Add X-Payment: and X-Hashcash: headers with a hashcash payment
138 for each recipient address. Prefix arg sets default payment temporarily."
140 (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
141 hashcash-default-payment))
145 (goto-char (point-min))
146 (search-forward mail-header-separator)
148 (narrow-to-region (point-min) (point))
149 (let ((to (hashcash-strip-quoted-names (mail-fetch-field "To" nil t)))
150 (cc (hashcash-strip-quoted-names (mail-fetch-field "Cc" nil t)))
151 (ng (hashcash-strip-quoted-names (mail-fetch-field "Newsgroups"
154 (setq addrlist (split-string to ",[ \t\n]*")))
156 (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*"))))
157 (when (and hashcash-in-news ng)
158 (setq addrlist (nconc addrlist (split-string ng ",[ \t\n]*")))))
160 (mapc #'hashcash-insert-payment addrlist)))))
164 (defun mail-check-payment (&optional arg)
165 "Look for a valid X-Payment: or X-Hashcash: header.
166 Prefix arg sets default accept amount temporarily."
168 (let ((hashcash-default-accept-payment (if arg (prefix-numeric-value arg)
169 hashcash-default-accept-payment)))
171 (goto-char (point-min))
172 (search-forward mail-header-separator)
176 (goto-char (point-min))
177 (while (and (not ok) (search-forward "X-Payment: hashcash 1.1 " end t))
178 (setq ok (hashcash-verify-payment
179 (buffer-substring (point) (hashcash-point-at-eol)))))
180 (goto-char (point-min))
181 (while (and (not ok) (search-forward "X-Hashcash: " end t))
182 (setq ok (hashcash-verify-payment
183 (buffer-substring (point) (hashcash-point-at-eol)))))
185 (message "Payment valid"))
190 ;;; hashcash.el ends here