Fix URL in comment.
[gnus] / lisp / hashcash.el
1 ;;; hashcash.el --- Add hashcash payments to email
2
3 ;; Copyright (C) 2003, 2004 Free Software Foundation
4 ;; Copyright (C) 1997--2002 Paul E. Foley
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.hashcash.org/.
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     (progn
134       (message "No hashcash binary found")
135       (sleep-for 1)
136       nil)))
137
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.
144   ;;
145   ;; Version 1.1 looks like yymmdd:rrrrr:xxxxxxxxxxxxxxxx
146   ;;   You need to upgrade your hashcash binary.
147   ;;
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"))))
153
154 ;;;###autoload
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))))
160     (when pay
161 ;;      (insert-before-markers "X-Payment: hashcash "
162 ;;                           (number-to-string (hashcash-version pay)) " "
163 ;;                           pay "\n")
164       (insert-before-markers "X-Hashcash: " pay "\n"))))
165
166 ;;;###autoload
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)
171                   (nth 1 split)
172                   (case (string-to-number (nth 0 split))
173                     (0 (nth 2 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)))))
179           ((equal token key)
180            (hashcash-check-payment token resource
181                                 (or amount hashcash-default-accept-payment)))
182           (t nil))))
183
184 ;;;###autoload
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."
188   (interactive "P")
189   (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
190                                     hashcash-default-payment))
191         (addrlist nil))
192     (save-excursion
193       (save-restriction
194         (goto-char (point-min))
195         (search-forward mail-header-separator)
196         (beginning-of-line)
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"
201                                                                  nil t))))
202           (when to
203             (setq addrlist (split-string to ",[ \t\n]*")))
204           (when cc
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]*")))))
208         (when addrlist
209           (mapcar #'hashcash-insert-payment addrlist))))) ; mapc
210   t)
211
212 ;;;###autoload
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."
216   (interactive "P")
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))))
220     (save-excursion
221       (goto-char (point-min))
222       (search-forward "\n\n")
223       (beginning-of-line)
224       (let ((end (point))
225             (ok nil))
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))))
234         (when ok
235           (message "Payment valid"))
236         ok))))
237
238 (provide 'hashcash)
239
240 ;;; arch-tag: 0e7fe983-a124-4392-9788-0dbcbd2c4d62