Remove RCS tag.
[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 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 (> val 0)
115       (save-excursion
116         (set-buffer (get-buffer-create " *hashcash*"))
117         (erase-buffer)
118         (call-process hashcash-path nil t nil
119                       "-m" "-q" "-b" (number-to-string val) str)
120         (goto-char (point-min))
121         (hashcash-token-substring))
122     nil))
123
124 (defun hashcash-check-payment (token str val)
125   "Check the validity of a hashcash payment."
126   (zerop (call-process hashcash-path nil nil nil "-c"
127                        "-d" "-f" hashcash-double-spend-database
128                        "-b" (number-to-string val)
129                        "-r" str
130                        token)))
131
132 (defun hashcash-version (token)
133   "Find the format version of a hashcash token."
134   ;; Version 1.2 looks like n:yymmdd:rrrrr:xxxxxxxxxxxxxxxx
135   ;;   This carries its own version number embedded in the token,
136   ;;   so no further format number changes should be necessary
137   ;;   in the X-Payment header.
138   ;;
139   ;; Version 1.1 looks like yymmdd:rrrrr:xxxxxxxxxxxxxxxx
140   ;;   You need to upgrade your hashcash binary.
141   ;;
142   ;; Version 1.0 looked like nnnnnrrrrrxxxxxxxxxxxxxxxx
143   ;;   This is no longer supported.
144   (cond ((equal (aref token 1) ?:) 1.2)
145         ((equal (aref token 6) ?:) 1.1)
146         (t (error "Unknown hashcash format version"))))
147
148 ;;;###autoload
149 (defun hashcash-insert-payment (arg)
150   "Insert X-Payment and X-Hashcash headers with a payment for ARG"
151   (interactive "sPay to: ")
152   (let ((pay (hashcash-generate-payment (hashcash-payment-to arg)
153                                         (hashcash-payment-required arg))))
154     (when pay
155 ;;      (insert-before-markers "X-Payment: hashcash "
156 ;;                           (number-to-string (hashcash-version pay)) " "
157 ;;                           pay "\n")
158       (insert-before-markers "X-Hashcash: " pay "\n"))))
159
160 ;;;###autoload
161 (defun hashcash-verify-payment (token &optional resource amount)
162   "Verify a hashcash payment"
163   (let* ((split (split-string token ":"))
164          (key (if (< (hashcash-version token) 1.2)
165                   (nth 1 split)
166                   (case (string-to-number (nth 0 split))
167                     (0 (nth 2 split))
168                     (1 (nth 3 split))))))
169     (cond ((null resource)
170            (let ((elt (assoc key hashcash-accept-resources)))
171              (and elt (hashcash-check-payment token (car elt)
172                         (or (cadr elt) hashcash-default-accept-payment)))))
173           ((equal token key)
174            (hashcash-check-payment token resource
175                                 (or amount hashcash-default-accept-payment)))
176           (t nil))))
177
178 ;;;###autoload
179 (defun mail-add-payment (&optional arg)
180   "Add X-Payment: and X-Hashcash: headers with a hashcash payment
181 for each recipient address.  Prefix arg sets default payment temporarily."
182   (interactive "P")
183   (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
184                                     hashcash-default-payment))
185         (addrlist nil))
186     (save-excursion
187       (save-restriction
188         (goto-char (point-min))
189         (search-forward mail-header-separator)
190         (beginning-of-line)
191         (narrow-to-region (point-min) (point))
192         (let ((to (hashcash-strip-quoted-names (mail-fetch-field "To" nil t)))
193               (cc (hashcash-strip-quoted-names (mail-fetch-field "Cc" nil t)))
194               (ng (hashcash-strip-quoted-names (mail-fetch-field "Newsgroups"
195                                                                  nil t))))
196           (when to
197             (setq addrlist (split-string to ",[ \t\n]*")))
198           (when cc
199             (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*"))))
200           (when (and hashcash-in-news ng)
201             (setq addrlist (nconc addrlist (split-string ng ",[ \t\n]*")))))
202         (when addrlist
203           (mapcar #'hashcash-insert-payment addrlist))))) ; mapc
204   t)
205
206 ;;;###autoload
207 (defun mail-check-payment (&optional arg)
208   "Look for a valid X-Payment: or X-Hashcash: header.
209 Prefix arg sets default accept amount temporarily."
210   (interactive "P")
211   (let ((hashcash-default-accept-payment (if arg (prefix-numeric-value arg)
212                                            hashcash-default-accept-payment))
213         (version (hashcash-version (hashcash-generate-payment "x" 1))))
214     (save-excursion
215       (goto-char (point-min))
216       (search-forward "\n\n")
217       (beginning-of-line)
218       (let ((end (point))
219             (ok nil))
220         (goto-char (point-min))
221         (while (and (not ok) (search-forward "X-Payment: hashcash " end t))
222           (let ((value (split-string (hashcash-token-substring) " ")))
223             (when (equal (car value) (number-to-string version))
224               (setq ok (hashcash-verify-payment (cadr value))))))
225         (goto-char (point-min))
226         (while (and (not ok) (search-forward "X-Hashcash: " end t))
227           (setq ok (hashcash-verify-payment (hashcash-token-substring))))
228         (when ok
229           (message "Payment valid"))
230         ok))))
231
232 (provide 'hashcash)
233
234 ;;; arch-tag: 0e7fe983-a124-4392-9788-0dbcbd2c4d62