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