(hashcash-insert-payment): s/Hashcode/Hashcash/
[gnus] / contrib / hashcash.el
1 ;;; hashcash.el --- Add hashcash payments to email
2
3 ;; $Revision: 1.3 $
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 nil
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 "hashcash"
37   "*The path to the hashcash binary.")
38
39 (require 'mail-utils)
40
41 (defun hashcash-strip-quoted-names (addr)
42   (setq addr (mail-strip-quoted-names addr))
43   (if (and addr (string-match "^[^+@]+\\(\\+[^@]*\\)@" addr))
44       (concat (subseq addr 0 (match-beginning 1)) (subseq addr (match-end 1)))
45     addr))
46
47 (defun hashcash-payment-required (addr)
48   "Return the hashcash payment value required for the given address."
49   (let ((val (assoc addr hashcash-payment-alist)))
50     (if val
51         (if (cddr val)
52             (caddr val)
53           (cadr val))
54       hashcash-default-payment)))
55
56 (defun hashcash-payment-to (addr)
57   "Return the string with which hashcash payments should collide."
58   (let ((val (assoc addr hashcash-payment-alist)))
59     (if val
60         (if (cddr val)
61             (cadr val)
62           (car val))
63       addr)))
64
65 (defun hashcash-generate-payment (str val)
66   "Generate a hashcash payment by finding a VAL-bit collison on STR."
67   (if (> val 0)
68       (save-excursion
69         (set-buffer (get-buffer-create " *hashcash*"))
70         (erase-buffer)
71         (call-process hashcash nil t nil (concat "-b " (number-to-string val))
72                       str)
73         (goto-char (point-min))
74         (buffer-substring (point-at-bol) (point-at-eol)))
75     nil))
76
77 (defun hashcash-insert-payment (arg)
78   "Insert an X-Hashcash header with a payment for ARG"
79   (interactive "sPay to: ")
80   (let ((pay (hashcash-generate-payment (hashcash-payment-to arg)
81                                         (hashcash-payment-required arg))))
82     (when pay
83       (insert-before-markers "X-Hashcash: " pay "\n"))))
84
85 ;;;###autoload
86 (defun mail-add-payment (&optional arg)
87   "Add an X-Hashcash: header with a hashcash payment for each recipient address
88 Prefix arg sets default payment temporarily."
89   (interactive "P")
90   (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
91                                     hashcash-default-payment))
92         (addrlist nil))
93     (save-excursion
94       (save-restriction
95         (goto-char (point-min))
96         (search-forward mail-header-separator)
97         (beginning-of-line)
98         (narrow-to-region (point-min) (point))
99         (let ((to (hashcash-strip-quoted-names (mail-fetch-field "To" nil t)))
100               (cc (hashcash-strip-quoted-names (mail-fetch-field "Cc" nil t)))
101               (ng (hashcash-strip-quoted-names 
102                    (mail-fetch-field "Newsgroups" nil t))))
103           (when to
104             (setq addrlist (split-string to ",[ \t\n]*")))
105           (when cc
106             (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*"))))
107           (when ng
108             (setq addrlist (nconc addrlist (split-string ng ",[ \t\n]*")))))
109         (when addrlist
110           (mapc #'hashcash-insert-payment addrlist)))))
111   t)
112
113 (provide 'hashcash)
114
115 ;;; hashcash.el ends here