Synch with Oort Gnus.
[elisp/gnus.git-] / contrib / hashcash.el
1 ;;; hashcash.el --- Add hashcash payments to email
2
3 ;; Copyright (C) 1997,2001 Paul E. Foley
4
5 ;; Maintainer: Paul Foley <mycroft@actrix.gen.nz>
6 ;; Keywords: mail, hashcash
7
8 ;; Released under the GNU General Public License
9
10 ;;; Commentary:
11
12 ;; The hashcash binary is at http://www.cypherspace.org/hashcash/
13 ;;
14 ;; Call mail-add-payment to add a hashcash payment to a mail message
15 ;; in the current buffer.
16 ;;
17 ;; To automatically add payments to all outgoing mail:
18 ;;    (add-hook 'message-send-hook 'mail-add-payment)
19
20 ;;; Code:
21
22 (eval-when-compile (require 'cl))
23
24 (defcustom hashcash-default-payment 0
25   "*The default number of bits to pay to unknown users.
26 If this is zero, no payment header will be generated.
27 See `hashcash-payment-alist'."
28   :type 'integer)
29
30 (defcustom hashcash-payment-alist nil
31   "*An association list mapping email addresses to payment amounts.
32 Elements may consist of (ADDR AMOUNT) or (ADDR STRING AMOUNT), where
33 ADDR is the email address of the intended recipient and AMOUNT is
34 the value of hashcash payment to be made to that user.  STRING, if
35 present, is the string to be hashed; if not present ADDR will be used.")
36
37 (defcustom hashcash "/usr/local/bin/hashcash"
38   "*The path to the hashcash binary.")
39
40 (defcustom hashcash-in-news nil
41   "*Specifies whether or not hashcash payments should be made to newsgroups."
42   :type 'boolean)
43
44 (require 'mail-utils)
45
46 (defun hashcash-strip-quoted-names (addr)
47   (setq addr (mail-strip-quoted-names addr))
48   (if (and addr (string-match "^[^+@]+\\(\\+[^@]*\\)@" addr))
49       (concat (substring addr 0 (match-beginning 1))
50               (substring addr (match-end 1)))
51     addr))
52
53 (defun hashcash-payment-required (addr)
54   "Return the hashcash payment value required for the given address."
55   (let ((val (assoc addr hashcash-payment-alist)))
56     (if val
57         (if (cddr val)
58             (caddr val)
59           (cadr val))
60       hashcash-default-payment)))
61
62 (defun hashcash-payment-to (addr)
63   "Return the string with which hashcash payments should collide."
64   (let ((val (assoc addr hashcash-payment-alist)))
65     (if val
66         (if (cddr val)
67             (cadr val)
68           (car val))
69       addr)))
70
71 (defun hashcash-generate-payment (str val)
72   "Generate a hashcash payment by finding a VAL-bit collison on STR."
73   (if (> val 0)
74       (save-excursion
75         (set-buffer (get-buffer-create " *hashcash*"))
76         (erase-buffer)
77         (call-process hashcash nil t nil (concat "-b " (number-to-string val))
78                       str)
79         (goto-char (point-min))
80         (buffer-substring (point-at-bol) (point-at-eol)))
81     nil))
82
83 (defun hashcash-insert-payment (arg)
84   "Insert an X-Hashcash header with a payment for ARG"
85   (interactive "sPay to: ")
86   (let ((pay (hashcash-generate-payment (hashcash-payment-to arg)
87                                         (hashcash-payment-required arg))))
88     (when pay
89       (insert-before-markers "X-Hashcash: " pay "\n"))))
90
91 ;;;###autoload
92 (defun mail-add-payment (&optional arg)
93   "Add an X-Hashcash: header with a hashcash payment for each recipient address
94 Prefix arg sets default payment temporarily."
95   (interactive "P")
96   (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
97                                     hashcash-default-payment))
98         (addrlist nil))
99     (save-excursion
100       (save-restriction
101         (goto-char (point-min))
102         (re-search-forward (concat "^\\("
103                                    (regexp-quote mail-header-separator)
104                                    "\\)?$"))
105         (beginning-of-line)
106         (narrow-to-region (point-min) (point))
107         (let ((to (hashcash-strip-quoted-names (mail-fetch-field "To" nil t)))
108               (cc (hashcash-strip-quoted-names (mail-fetch-field "Cc" nil t)))
109               (ng (hashcash-strip-quoted-names (mail-fetch-field "Newsgroups"
110                                                                  nil t))))
111           (when to
112             (setq addrlist (split-string to ",[ \t\n]*")))
113           (when cc
114             (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*"))))
115           (when (and hashcash-in-news ng)
116             (setq addrlist (nconc addrlist (split-string ng ",[ \t\n]*")))))
117         (while addrlist
118           (hashcash-insert-payment (pop addrlist))))))
119   t)
120
121 (provide 'hashcash)
122
123 ;;; hashcash.el ends here