Add hashcash.el
[gnus] / contrib / hashcash.el
1 ;;; hashcash.el --- Add hashcash payments to email
2
3 ;; $Revision: 2.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 (defvar 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 (defvar hashcash-payment-alist nil
28   "*An association list mapping email addresses to payment amounts.
29  Elements may consist of (ADDR AMOUNT) or (ADDR STRING AMOUNT), where
30  ADDR is the email address of the intended recipient and AMOUNT is
31  the value of hashcash payment to be made to that user.  STRING, if
32  present, is the string to be hashed; if not present ADDR will be used.")
33 (defvar hashcash "/usr/local/bin/hashcash"
34   "*The path to the hashcash binary.")
35
36 (require 'mail-utils)
37
38 (defun hashcash-strip-quoted-names (addr)
39   (setq addr (mail-strip-quoted-names addr))
40   (if (and addr (string-match "^[^+@]+\\(\\+[^@]*\\)@" addr))
41       (concat (subseq addr 0 (match-beginning 1)) (subseq addr (match-end 1)))
42     addr))
43
44 (defun hashcash-payment-required (addr)
45   "Return the hashcash payment value required for the given address."
46   (let ((val (assoc addr hashcash-payment-alist)))
47     (if val
48         (if (cddr val)
49             (caddr val)
50           (cadr val))
51       hashcash-default-payment)))
52
53 (defun hashcash-payment-to (addr)
54   "Return the string with which hashcash payments should collide."
55   (let ((val (assoc addr hashcash-payment-alist)))
56     (if val
57         (if (cddr val)
58             (cadr val)
59           (car val))
60       addr)))
61
62 (defun hashcash-generate-payment (str val)
63   "Generate a hashcash payment by finding a VAL-bit collison on STR."
64   (if (> val 0)
65       (save-excursion
66         (set-buffer (get-buffer-create " *hashcash*"))
67         (erase-buffer)
68         (call-process hashcash nil t nil (concat "-" (number-to-string val))
69                       str)
70         (search-backward "collision: ")
71         (forward-char 11)
72         (let ((pos (point-marker)))
73           (end-of-line)
74           (buffer-substring pos (point))))
75     nil))
76
77 (defun hashcash-insert-payment (arg)
78   "Insert an X-Payment 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-Payment: " pay "\n"))))
84
85 (defun mail-add-payment (&optional arg)
86   "Add an X-Payment: header with a hashcash payment for each recipient address
87 Prefix arg sets default payment temporarily."
88   (interactive "P")
89   (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
90                                     hashcash-default-payment))
91         (addrlist nil))
92     (save-excursion
93       (save-restriction
94         (goto-char (point-min))
95         (search-forward mail-header-separator)
96         (beginning-of-line)
97         (narrow-to-region (point-min) (point))
98         (let ((to (hashcash-strip-quoted-names (mail-fetch-field "To" nil t)))
99               (cc (hashcash-strip-quoted-names (mail-fetch-field "Cc" nil t))))
100           (when to
101             (setq addrlist (split-string to ",[ \t\n]*")))
102           (when cc
103             (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*")))))
104         (when addrlist
105           (mapc #'hashcash-insert-payment addrlist)))))
106   t)