1 ;;; hashcash.el --- Add hashcash payments to email
3 ;; Copyright (C) 2003, 2004 Free Software Foundation
4 ;; Copyright (C) 1997--2002 Paul E. Foley
6 ;; Maintainer: Paul Foley <mycroft@actrix.gen.nz>
7 ;; Keywords: mail, hashcash
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; The hashcash binary is at http://www.hashcash.org/.
30 ;; Call mail-add-payment to add a hashcash payment to a mail message
31 ;; in the current buffer.
33 ;; To automatically add payments to all outgoing mail:
34 ;; (add-hook 'message-send-hook 'mail-add-payment)
39 (autoload 'executable-find "executable"))
41 (defcustom hashcash-default-payment 10
42 "*The default number of bits to pay to unknown users.
43 If this is zero, no payment header will be generated.
44 See `hashcash-payment-alist'."
47 (defcustom hashcash-payment-alist '()
48 "*An association list mapping email addresses to payment amounts.
49 Elements may consist of (ADDR AMOUNT) or (ADDR STRING AMOUNT), where
50 ADDR is the email address of the intended recipient and AMOUNT is
51 the value of hashcash payment to be made to that user. STRING, if
52 present, is the string to be hashed; if not present ADDR will be used.")
54 (defcustom hashcash-default-accept-payment 10
55 "*The default minimum number of bits to accept on incoming payments."
58 (defcustom hashcash-accept-resources `((,user-mail-address nil))
59 "*An association list mapping hashcash resources to payment amounts.
60 Resources named here are to be accepted in incoming payments. If the
61 corresponding AMOUNT is NIL, the value of `hashcash-default-accept-payment'
64 (defcustom hashcash-path (executable-find "hashcash")
65 "*The path to the hashcash binary.")
67 (defcustom hashcash-double-spend-database "hashcash.db"
68 "*The path to the double-spending database.")
70 (defcustom hashcash-in-news nil
71 "*Specifies whether or not hashcash payments should be made to newsgroups."
77 (if (fboundp 'point-at-bol)
78 (defalias 'hashcash-point-at-bol 'point-at-bol)
79 (defalias 'hashcash-point-at-bol 'line-beginning-position))
81 (if (fboundp 'point-at-eol)
82 (defalias 'hashcash-point-at-eol 'point-at-eol)
83 (defalias 'hashcash-point-at-eol 'line-end-position)))
85 (defun hashcash-strip-quoted-names (addr)
86 (setq addr (mail-strip-quoted-names addr))
87 (if (and addr (string-match "\\`\\([^+@]+\\)\\+[^@]*\\(@.+\\)" addr))
88 (concat (match-string 1 addr) (match-string 2 addr))
91 (defun hashcash-token-substring ()
96 (concat token (buffer-substring (point) (hashcash-point-at-eol))))
97 (goto-char (hashcash-point-at-eol))
99 (unless (looking-at "[ \t]") (return token))
100 (while (looking-at "[ \t]") (forward-char 1))))))
102 (defun hashcash-payment-required (addr)
103 "Return the hashcash payment value required for the given address."
104 (let ((val (assoc addr hashcash-payment-alist)))
105 (or (nth 2 val) (nth 1 val) hashcash-default-payment)))
107 (defun hashcash-payment-to (addr)
108 "Return the string with which hashcash payments should collide."
109 (let ((val (assoc addr hashcash-payment-alist)))
110 (or (nth 1 val) (nth 0 val) addr)))
112 (defun hashcash-generate-payment (str val)
113 "Generate a hashcash payment by finding a VAL-bit collison on STR."
117 (set-buffer (get-buffer-create " *hashcash*"))
119 (call-process hashcash-path nil t nil
120 "-m" "-q" "-b" (number-to-string val) str)
121 (goto-char (point-min))
122 (hashcash-token-substring))
123 (error "No `hashcash' binary found")))
125 (defun hashcash-check-payment (token str val)
126 "Check the validity of a hashcash payment."
128 (zerop (call-process hashcash-path nil nil nil "-c"
129 "-d" "-f" hashcash-double-spend-database
130 "-b" (number-to-string val)
134 (message "No hashcash binary found")
138 (defun hashcash-version (token)
139 "Find the format version of a hashcash token."
140 ;; Version 1.2 looks like n:yymmdd:rrrrr:xxxxxxxxxxxxxxxx
141 ;; This carries its own version number embedded in the token,
142 ;; so no further format number changes should be necessary
143 ;; in the X-Payment header.
145 ;; Version 1.1 looks like yymmdd:rrrrr:xxxxxxxxxxxxxxxx
146 ;; You need to upgrade your hashcash binary.
148 ;; Version 1.0 looked like nnnnnrrrrrxxxxxxxxxxxxxxxx
149 ;; This is no longer supported.
150 (cond ((equal (aref token 1) ?:) 1.2)
151 ((equal (aref token 6) ?:) 1.1)
152 (t (error "Unknown hashcash format version"))))
155 (defun hashcash-insert-payment (arg)
156 "Insert X-Payment and X-Hashcash headers with a payment for ARG"
157 (interactive "sPay to: ")
158 (let ((pay (hashcash-generate-payment (hashcash-payment-to arg)
159 (hashcash-payment-required arg))))
161 ;; (insert-before-markers "X-Payment: hashcash "
162 ;; (number-to-string (hashcash-version pay)) " "
164 (insert-before-markers "X-Hashcash: " pay "\n"))))
167 (defun hashcash-verify-payment (token &optional resource amount)
168 "Verify a hashcash payment"
169 (let* ((split (split-string token ":"))
170 (key (if (< (hashcash-version token) 1.2)
172 (case (string-to-number (nth 0 split))
174 (1 (nth 3 split))))))
175 (cond ((null resource)
176 (let ((elt (assoc key hashcash-accept-resources)))
177 (and elt (hashcash-check-payment token (car elt)
178 (or (cadr elt) hashcash-default-accept-payment)))))
180 (hashcash-check-payment token resource
181 (or amount hashcash-default-accept-payment)))
185 (defun mail-add-payment (&optional arg)
186 "Add X-Payment: and X-Hashcash: headers with a hashcash payment
187 for each recipient address. Prefix arg sets default payment temporarily."
189 (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
190 hashcash-default-payment))
194 (goto-char (point-min))
195 (search-forward mail-header-separator)
197 (narrow-to-region (point-min) (point))
198 (let ((to (hashcash-strip-quoted-names (mail-fetch-field "To" nil t)))
199 (cc (hashcash-strip-quoted-names (mail-fetch-field "Cc" nil t)))
200 (ng (hashcash-strip-quoted-names (mail-fetch-field "Newsgroups"
203 (setq addrlist (split-string to ",[ \t\n]*")))
205 (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*"))))
206 (when (and hashcash-in-news ng)
207 (setq addrlist (nconc addrlist (split-string ng ",[ \t\n]*")))))
209 (mapcar #'hashcash-insert-payment addrlist))))) ; mapc
213 (defun mail-check-payment (&optional arg)
214 "Look for a valid X-Payment: or X-Hashcash: header.
215 Prefix arg sets default accept amount temporarily."
217 (let ((hashcash-default-accept-payment (if arg (prefix-numeric-value arg)
218 hashcash-default-accept-payment))
219 (version (hashcash-version (hashcash-generate-payment "x" 1))))
221 (goto-char (point-min))
222 (search-forward "\n\n")
226 (goto-char (point-min))
227 (while (and (not ok) (search-forward "X-Payment: hashcash " end t))
228 (let ((value (split-string (hashcash-token-substring) " ")))
229 (when (equal (car value) (number-to-string version))
230 (setq ok (hashcash-verify-payment (cadr value))))))
231 (goto-char (point-min))
232 (while (and (not ok) (search-forward "X-Hashcash: " end t))
233 (setq ok (hashcash-verify-payment (hashcash-token-substring))))
235 (message "Payment valid"))