* hashcash.el (hashcash-point-at-bol):
[gnus] / contrib / hashcash.el
1 ;;; hashcash.el --- Add hashcash payments to email
2
3 ;; $Revision: 1.6 $
4 ;; Copyright (C) 1997,2001 Paul E. Foley
5
6 ;; Maintainer: Paul Foley <mycroft@actrix.gen.nz>
7 ;; Keywords: mail, hashcash
8
9 ;; Released under the GNU General Public License
10
11 ;;; Commentary:
12
13 ;; The hashcash binary is at http://www.cypherspace.org/hashcash/
14 ;;
15 ;; Call mail-add-payment to add a hashcash payment to a mail message
16 ;; in the current buffer.
17 ;;
18 ;; To automatically add payments to all outgoing mail:
19 ;;    (add-hook 'message-send-hook 'mail-add-payment)
20
21 ;;; Code:
22
23 (defcustom hashcash-default-payment 0
24   "*The default number of bits to pay to unknown users.
25 If this is zero, no payment header will be generated.
26 See `hashcash-payment-alist'."
27   :type 'integer)
28
29 (defcustom hashcash-payment-alist '()
30   "*An association list mapping email addresses to payment amounts.
31 Elements may consist of (ADDR AMOUNT) or (ADDR STRING AMOUNT), where
32 ADDR is the email address of the intended recipient and AMOUNT is
33 the value of hashcash payment to be made to that user.  STRING, if
34 present, is the string to be hashed; if not present ADDR will be used.")
35
36 (defcustom hashcash-default-accept-payment 10
37   "*The default minimum number of bits to accept on incoming payments."
38   :type 'integer)
39
40 (defcustom hashcash-accept-resources `((,(user-mail-address) nil))
41   "*An association list mapping hashcash resources to payment amounts.
42 Resources named here are to be accepted in incoming payments.  If the
43 corresponding AMOUNT is NIL, the value of `hashcash-default-accept-payment'
44 is used instead.")
45
46 (defcustom hashcash "/usr/local/bin/hashcash"
47   "*The path to the hashcash binary.")
48
49 (defcustom hashcash-double-spend-database "hashcash.db"
50   "*The path to the double-spending database.")
51
52 (defcustom hashcash-in-news nil
53   "*Specifies whether or not hashcash payments should be made to newsgroups."
54   :type 'boolean)
55
56 (require 'mail-utils)
57
58 (defalias 'hashcash-point-at-bol
59   (if (fboundp 'point-at-bol)
60       'point-at-bol
61     'line-beginning-position))
62
63 (defalias 'hashcash-point-at-eol
64   (if (fboundp 'point-at-eol)
65       'point-at-eol
66     'line-end-position))
67
68 (defun hashcash-strip-quoted-names (addr)
69   (setq addr (mail-strip-quoted-names addr))
70   (if (and addr (string-match "^[^+@]+\\(\\+[^@]*\\)@" addr))
71       (concat (subseq addr 0 (match-beginning 1)) (subseq addr (match-end 1)))
72     addr))
73
74 (defun hashcash-payment-required (addr)
75   "Return the hashcash payment value required for the given address."
76   (let ((val (assoc addr hashcash-payment-alist)))
77     (if val
78         (if (cddr val)
79             (caddr val)
80           (cadr val))
81       hashcash-default-payment)))
82
83 (defun hashcash-payment-to (addr)
84   "Return the string with which hashcash payments should collide."
85   (let ((val (assoc addr hashcash-payment-alist)))
86     (if val
87         (if (cddr val)
88             (cadr val)
89           (car val))
90       addr)))
91
92 (defun hashcash-generate-payment (str val)
93   "Generate a hashcash payment by finding a VAL-bit collison on STR."
94   (if (> val 0)
95       (save-excursion
96         (set-buffer (get-buffer-create " *hashcash*"))
97         (erase-buffer)
98         (call-process hashcash nil t nil (concat "-b " (number-to-string val))
99                       str)
100         (goto-char (point-min))
101         (buffer-substring (hashcash-point-at-bol) (hashcash-point-at-eol)))
102     nil))
103
104 (defun hashcash-check-payment (token str val)
105   "Check the validity of a hashcash payment."
106   (zerop (call-process hashcash nil nil nil "-c"
107                        "-d" "-f" hashcash-double-spend-database
108                        "-b" (number-to-string val)
109                        "-r" str
110                        token)))
111
112 ;;;###autoload
113 (defun hashcash-insert-payment (arg)
114   "Insert X-Payment and X-Hashcash headers with a payment for ARG"
115   (interactive "sPay to: ")
116   (let ((pay (hashcash-generate-payment (hashcash-payment-to arg)
117                                         (hashcash-payment-required arg))))
118     (when pay
119       (insert-before-markers "X-Payment: hashcash 1.1 " pay "\n")
120       (insert-before-markers "X-Hashcash: " pay "\n"))))
121
122 ;;;###autoload
123 (defun hashcash-verify-payment (token &optional resource amount)
124   "Verify a hashcash payment"
125   (let ((key (cadr (split-string-by-char token ?:))))
126     (cond ((null resource)
127            (let ((elt (assoc key hashcash-accept-resources)))
128              (and elt (hashcash-check-payment token (car elt)
129                         (or (cadr elt) hashcash-default-accept-payment)))))
130           ((equal token key)
131            (hashcash-check-payment token resource
132                                 (or amount hashcash-default-accept-payment)))
133           (t nil))))
134
135 ;;;###autoload
136 (defun mail-add-payment (&optional arg)
137   "Add X-Payment: and X-Hashcash: headers with a hashcash payment
138 for each recipient address.  Prefix arg sets default payment temporarily."
139   (interactive "P")
140   (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
141                                     hashcash-default-payment))
142         (addrlist nil))
143     (save-excursion
144       (save-restriction
145         (goto-char (point-min))
146         (search-forward mail-header-separator)
147         (beginning-of-line)
148         (narrow-to-region (point-min) (point))
149         (let ((to (hashcash-strip-quoted-names (mail-fetch-field "To" nil t)))
150               (cc (hashcash-strip-quoted-names (mail-fetch-field "Cc" nil t)))
151               (ng (hashcash-strip-quoted-names (mail-fetch-field "Newsgroups"
152                                                                  nil t))))
153           (when to
154             (setq addrlist (split-string to ",[ \t\n]*")))
155           (when cc
156             (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*"))))
157           (when (and hashcash-in-news ng)
158             (setq addrlist (nconc addrlist (split-string ng ",[ \t\n]*")))))
159         (when addrlist
160           (mapc #'hashcash-insert-payment addrlist)))))
161   t)
162
163 ;;;###autoload
164 (defun mail-check-payment (&optional arg)
165   "Look for a valid X-Payment: or X-Hashcash: header.
166 Prefix arg sets default accept amount temporarily."
167   (interactive "P")
168   (let ((hashcash-default-accept-payment (if arg (prefix-numeric-value arg)
169                                            hashcash-default-accept-payment)))
170     (save-excursion
171       (goto-char (point-min))
172       (search-forward mail-header-separator)
173       (beginning-of-line)
174       (let ((end (point))
175             (ok nil))
176         (goto-char (point-min))
177         (while (and (not ok) (search-forward "X-Payment: hashcash 1.1 " end t))
178           (setq ok (hashcash-verify-payment
179                     (buffer-substring (point) (hashcash-point-at-eol)))))
180         (goto-char (point-min))
181         (while (and (not ok) (search-forward "X-Hashcash: " end t))
182           (setq ok (hashcash-verify-payment
183                     (buffer-substring (point) (hashcash-point-at-eol)))))
184         (when ok
185           (message "Payment valid"))
186         ok))))
187
188 (provide 'hashcash)
189
190 ;;; hashcash.el ends here