(hashcash-generate-payment)
[gnus] / lisp / hashcash.el
1 ;;; hashcash.el --- Add hashcash payments to email
2
3 ;; Copyright (C) 1997--2002 Paul E. Foley
4 ;; Copyright (C) 2003, 2004 Free Software Foundation
5
6 ;; Maintainer: Paul Foley <mycroft@actrix.gen.nz>
7 ;; Keywords: mail, hashcash
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;; The hashcash binary is at http://www.cypherspace.org/hashcash/
29 ;;
30 ;; Call mail-add-payment to add a hashcash payment to a mail message
31 ;; in the current buffer.
32 ;;
33 ;; To automatically add payments to all outgoing mail:
34 ;;    (add-hook 'message-send-hook 'mail-add-payment)
35
36 ;;; Code:
37
38 (eval-and-compile
39  (autoload 'executable-find "executable"))
40
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'."
45   :type 'integer)
46
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.")
53
54 (defcustom hashcash-default-accept-payment 10
55   "*The default minimum number of bits to accept on incoming payments."
56   :type 'integer)
57
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'
62 is used instead.")
63
64 (defcustom hashcash-path (executable-find "hashcash")
65   "*The path to the hashcash binary.")
66
67 (defcustom hashcash-double-spend-database "hashcash.db"
68   "*The path to the double-spending database.")
69
70 (defcustom hashcash-in-news nil
71   "*Specifies whether or not hashcash payments should be made to newsgroups."
72   :type 'boolean)
73
74 (require 'mail-utils)
75
76 (eval-and-compile
77  (if (fboundp 'point-at-bol)
78      (defalias 'hashcash-point-at-bol 'point-at-bol)
79    (defalias 'hashcash-point-at-bol 'line-beginning-position))
80
81  (if (fboundp 'point-at-eol)
82      (defalias 'hashcash-point-at-eol 'point-at-eol)
83    (defalias 'hashcash-point-at-eol 'line-end-position)))
84
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))
89     addr))
90
91 (defun hashcash-token-substring ()
92   (save-excursion
93     (let ((token ""))
94       (loop
95         (setq token
96           (concat token (buffer-substring (point) (hashcash-point-at-eol))))
97         (goto-char (hashcash-point-at-eol))
98         (forward-char 1)
99         (unless (looking-at "[ \t]") (return token))
100         (while (looking-at "[ \t]") (forward-char 1))))))
101
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)))
106
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)))
111
112 (defun hashcash-generate-payment (str val)
113   "Generate a hashcash payment by finding a VAL-bit collison on STR."
114   (if (and (> val 0)
115            hashcash-path)
116       (save-excursion
117         (set-buffer (get-buffer-create " *hashcash*"))
118         (erase-buffer)
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")))
124
125 (defun hashcash-check-payment (token str val)
126   "Check the validity of a hashcash payment."
127   (if hashcash-path
128       (zerop (call-process hashcash-path nil nil nil "-c"
129                            "-d" "-f" hashcash-double-spend-database
130                            "-b" (number-to-string val)
131                            "-r" str
132                            token))
133     (error "No `hashcash' binary found")))
134
135 (defun hashcash-version (token)
136   "Find the format version of a hashcash token."
137   ;; Version 1.2 looks like n:yymmdd:rrrrr:xxxxxxxxxxxxxxxx
138   ;;   This carries its own version number embedded in the token,
139   ;;   so no further format number changes should be necessary
140   ;;   in the X-Payment header.
141   ;;
142   ;; Version 1.1 looks like yymmdd:rrrrr:xxxxxxxxxxxxxxxx
143   ;;   You need to upgrade your hashcash binary.
144   ;;
145   ;; Version 1.0 looked like nnnnnrrrrrxxxxxxxxxxxxxxxx
146   ;;   This is no longer supported.
147   (cond ((equal (aref token 1) ?:) 1.2)
148         ((equal (aref token 6) ?:) 1.1)
149         (t (error "Unknown hashcash format version"))))
150
151 ;;;###autoload
152 (defun hashcash-insert-payment (arg)
153   "Insert X-Payment and X-Hashcash headers with a payment for ARG"
154   (interactive "sPay to: ")
155   (let ((pay (hashcash-generate-payment (hashcash-payment-to arg)
156                                         (hashcash-payment-required arg))))
157     (when pay
158 ;;      (insert-before-markers "X-Payment: hashcash "
159 ;;                           (number-to-string (hashcash-version pay)) " "
160 ;;                           pay "\n")
161       (insert-before-markers "X-Hashcash: " pay "\n"))))
162
163 ;;;###autoload
164 (defun hashcash-verify-payment (token &optional resource amount)
165   "Verify a hashcash payment"
166   (let* ((split (split-string token ":"))
167          (key (if (< (hashcash-version token) 1.2)
168                   (nth 1 split)
169                   (case (string-to-number (nth 0 split))
170                     (0 (nth 2 split))
171                     (1 (nth 3 split))))))
172     (cond ((null resource)
173            (let ((elt (assoc key hashcash-accept-resources)))
174              (and elt (hashcash-check-payment token (car elt)
175                         (or (cadr elt) hashcash-default-accept-payment)))))
176           ((equal token key)
177            (hashcash-check-payment token resource
178                                 (or amount hashcash-default-accept-payment)))
179           (t nil))))
180
181 ;;;###autoload
182 (defun mail-add-payment (&optional arg)
183   "Add X-Payment: and X-Hashcash: headers with a hashcash payment
184 for each recipient address.  Prefix arg sets default payment temporarily."
185   (interactive "P")
186   (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
187                                     hashcash-default-payment))
188         (addrlist nil))
189     (save-excursion
190       (save-restriction
191         (goto-char (point-min))
192         (search-forward mail-header-separator)
193         (beginning-of-line)
194         (narrow-to-region (point-min) (point))
195         (let ((to (hashcash-strip-quoted-names (mail-fetch-field "To" nil t)))
196               (cc (hashcash-strip-quoted-names (mail-fetch-field "Cc" nil t)))
197               (ng (hashcash-strip-quoted-names (mail-fetch-field "Newsgroups"
198                                                                  nil t))))
199           (when to
200             (setq addrlist (split-string to ",[ \t\n]*")))
201           (when cc
202             (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*"))))
203           (when (and hashcash-in-news ng)
204             (setq addrlist (nconc addrlist (split-string ng ",[ \t\n]*")))))
205         (when addrlist
206           (mapcar #'hashcash-insert-payment addrlist))))) ; mapc
207   t)
208
209 ;;;###autoload
210 (defun mail-check-payment (&optional arg)
211   "Look for a valid X-Payment: or X-Hashcash: header.
212 Prefix arg sets default accept amount temporarily."
213   (interactive "P")
214   (let ((hashcash-default-accept-payment (if arg (prefix-numeric-value arg)
215                                            hashcash-default-accept-payment))
216         (version (hashcash-version (hashcash-generate-payment "x" 1))))
217     (save-excursion
218       (goto-char (point-min))
219       (search-forward "\n\n")
220       (beginning-of-line)
221       (let ((end (point))
222             (ok nil))
223         (goto-char (point-min))
224         (while (and (not ok) (search-forward "X-Payment: hashcash " end t))
225           (let ((value (split-string (hashcash-token-substring) " ")))
226             (when (equal (car value) (number-to-string version))
227               (setq ok (hashcash-verify-payment (cadr value))))))
228         (goto-char (point-min))
229         (while (and (not ok) (search-forward "X-Hashcash: " end t))
230           (setq ok (hashcash-verify-payment (hashcash-token-substring))))
231         (when ok
232           (message "Payment valid"))
233         ok))))
234
235 (provide 'hashcash)
236
237 ;;; arch-tag: 0e7fe983-a124-4392-9788-0dbcbd2c4d62