1 ;;; hashcash.el --- Add hashcash payments to email
3 ;; Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation
6 ;; Written by: Paul Foley <mycroft@actrix.gen.nz> (1997-2002)
7 ;; Maintainer: Paul Foley <mycroft@actrix.gen.nz>
8 ;; Keywords: mail, hashcash
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; The hashcash binary is at http://www.hashcash.org/.
29 ;; Call mail-add-payment to add a hashcash payment to a mail message
30 ;; in the current buffer.
32 ;; Call mail-add-payment-async after writing the addresses but before
33 ;; writing the mail to start calculating the hashcash payment
36 ;; The easiest way to do this automatically for all outgoing mail
37 ;; is to set `message-generate-hashcash' to t. If you want more
38 ;; control, try the following hooks.
40 ;; To automatically add payments to all outgoing mail when sending:
41 ;; (add-hook 'message-send-hook 'mail-add-payment)
43 ;; To start calculations automatically when addresses are prefilled:
44 ;; (add-hook 'message-setup-hook 'mail-add-payment-async)
46 ;; To check whether calculations are done before sending:
47 ;; (add-hook 'message-send-hook 'hashcash-wait-or-cancel)
51 ;; For Emacs <22.2 and XEmacs.
53 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
55 (eval-when-compile (require 'cl)) ; for case
57 (defgroup hashcash nil
58 "Hashcash configuration."
61 (defcustom hashcash-default-payment 20
62 "*The default number of bits to pay to unknown users.
63 If this is zero, no payment header will be generated.
64 See `hashcash-payment-alist'."
68 (defcustom hashcash-payment-alist '()
69 "*An association list mapping email addresses to payment amounts.
70 Elements may consist of (ADDR AMOUNT) or (ADDR STRING AMOUNT), where
71 ADDR is the email address of the intended recipient and AMOUNT is
72 the value of hashcash payment to be made to that user. STRING, if
73 present, is the string to be hashed; if not present ADDR will be used."
74 :type '(repeat (choice (list :tag "Normal"
75 (string :name "Address")
76 (integer :name "Amount"))
77 (list :tag "Replace hash input"
78 (string :name "Address")
79 (string :name "Hash input")
80 (integer :name "Amount"))))
83 (defcustom hashcash-default-accept-payment 20
84 "*The default minimum number of bits to accept on incoming payments."
88 (defcustom hashcash-accept-resources `((,user-mail-address nil))
89 "*An association list mapping hashcash resources to payment amounts.
90 Resources named here are to be accepted in incoming payments. If the
91 corresponding AMOUNT is NIL, the value of `hashcash-default-accept-payment'
95 (defcustom hashcash-path (executable-find "hashcash")
96 "*The path to the hashcash binary."
99 (defcustom hashcash-extra-generate-parameters nil
100 "*A list of parameter strings passed to `hashcash-path' when minting.
101 For example, you may want to set this to '(\"-Z2\") to reduce header length."
102 :type '(repeat string)
105 (defcustom hashcash-double-spend-database "hashcash.db"
106 "*The path to the double-spending database."
109 (defcustom hashcash-in-news nil
110 "*Specifies whether or not hashcash payments should be made to newsgroups."
114 (defvar hashcash-process-alist nil
115 "Alist of asynchronous hashcash processes and buffers.")
117 (require 'mail-utils)
120 (if (fboundp 'point-at-bol)
121 (defalias 'hashcash-point-at-bol 'point-at-bol)
122 (defalias 'hashcash-point-at-bol 'line-beginning-position))
124 (if (fboundp 'point-at-eol)
125 (defalias 'hashcash-point-at-eol 'point-at-eol)
126 (defalias 'hashcash-point-at-eol 'line-end-position)))
128 (defun hashcash-strip-quoted-names (addr)
129 (setq addr (mail-strip-quoted-names addr))
130 (if (and addr (string-match "\\`\\([^+@]+\\)\\+[^@]*\\(@.+\\)" addr))
131 (concat (match-string 1 addr) (match-string 2 addr))
134 (declare-function message-narrow-to-headers-or-head "message" ())
135 (declare-function message-fetch-field "message" (header &optional not-all))
136 (declare-function message-goto-eoh "message" ())
137 (declare-function message-narrow-to-headers "message" ())
139 (defun hashcash-token-substring ()
144 (concat token (buffer-substring (point) (hashcash-point-at-eol))))
145 (goto-char (hashcash-point-at-eol))
147 (unless (looking-at "[ \t]") (return token))
148 (while (looking-at "[ \t]") (forward-char 1))))))
150 (defun hashcash-payment-required (addr)
151 "Return the hashcash payment value required for the given address."
152 (let ((val (assoc addr hashcash-payment-alist)))
153 (or (nth 2 val) (nth 1 val) hashcash-default-payment)))
155 (defun hashcash-payment-to (addr)
156 "Return the string with which hashcash payments should collide."
157 (let ((val (assoc addr hashcash-payment-alist)))
158 (or (nth 1 val) (nth 0 val) addr)))
160 (defun hashcash-generate-payment (str val)
161 "Generate a hashcash payment by finding a VAL-bit collison on STR."
164 (with-current-buffer (get-buffer-create " *hashcash*")
166 (apply 'call-process hashcash-path nil t nil
167 "-m" "-q" "-b" (number-to-string val) str
168 hashcash-extra-generate-parameters)
169 (goto-char (point-min))
170 (hashcash-token-substring))
171 (error "No `hashcash' binary found")))
173 (defun hashcash-generate-payment-async (str val callback)
174 "Generate a hashcash payment by finding a VAL-bit collison on STR.
175 Return immediately. Call CALLBACK with process and result when ready."
178 (let ((process (apply 'start-process "hashcash" nil
179 hashcash-path "-m" "-q"
180 "-b" (number-to-string val) str
181 hashcash-extra-generate-parameters)))
182 (setq hashcash-process-alist (cons
183 (cons process (current-buffer))
184 hashcash-process-alist))
185 (set-process-filter process `(lambda (process output)
186 (funcall ,callback process output))))
187 (funcall callback nil nil)))
189 (defun hashcash-check-payment (token str val)
190 "Check the validity of a hashcash payment."
192 (zerop (call-process hashcash-path nil nil nil "-c"
193 "-d" "-f" hashcash-double-spend-database
194 "-b" (number-to-string val)
198 (message "No hashcash binary found")
202 (defun hashcash-version (token)
203 "Find the format version of a hashcash token."
204 ;; Version 1.2 looks like n:yymmdd:rrrrr:xxxxxxxxxxxxxxxx
205 ;; This carries its own version number embedded in the token,
206 ;; so no further format number changes should be necessary
207 ;; in the X-Payment header.
209 ;; Version 1.1 looks like yymmdd:rrrrr:xxxxxxxxxxxxxxxx
210 ;; You need to upgrade your hashcash binary.
212 ;; Version 1.0 looked like nnnnnrrrrrxxxxxxxxxxxxxxxx
213 ;; This is no longer supported.
214 (cond ((equal (aref token 1) ?:) 1.2)
215 ((equal (aref token 6) ?:) 1.1)
216 (t (error "Unknown hashcash format version"))))
218 (defun hashcash-already-paid-p (recipient)
219 "Check for hashcash token to RECIPIENT in current buffer."
222 (message-narrow-to-headers-or-head)
223 (let ((token (message-fetch-field "x-hashcash"))
224 (case-fold-search t))
226 (string-match (regexp-quote recipient) token))))))
229 (defun hashcash-insert-payment (arg)
230 "Insert X-Payment and X-Hashcash headers with a payment for ARG"
231 (interactive "sPay to: ")
232 (unless (hashcash-already-paid-p arg)
233 (let ((pay (hashcash-generate-payment (hashcash-payment-to arg)
234 (hashcash-payment-required arg))))