Import Oort Gnus v0.09.
[elisp/gnus.git-] / contrib / hashcash.el
index 3c50fd7..86b5d84 100644 (file)
@@ -1,6 +1,6 @@
 ;;; hashcash.el --- Add hashcash payments to email
 
-;; $Revision: 1.1.1.1 $
+;; $Revision: 1.1.1.2 $
 ;; Copyright (C) 1997,2001 Paul E. Foley
 
 ;; Maintainer: Paul Foley <mycroft@actrix.gen.nz>
@@ -26,16 +26,33 @@ If this is zero, no payment header will be generated.
 See `hashcash-payment-alist'."
   :type 'integer)
 
-(defcustom hashcash-payment-alist nil
+(defcustom hashcash-payment-alist '()
   "*An association list mapping email addresses to payment amounts.
 Elements may consist of (ADDR AMOUNT) or (ADDR STRING AMOUNT), where
 ADDR is the email address of the intended recipient and AMOUNT is
 the value of hashcash payment to be made to that user.  STRING, if
 present, is the string to be hashed; if not present ADDR will be used.")
 
-(defcustom hashcash "hashcash"
+(defcustom hashcash-default-accept-payment 10
+  "*The default minimum number of bits to accept on incoming payments."
+  :type 'integer)
+
+(defcustom hashcash-accept-resources `((,(user-mail-address) nil))
+  "*An association list mapping hashcash resources to payment amounts.
+Resources named here are to be accepted in incoming payments.  If the
+corresponding AMOUNT is NIL, the value of `hashcash-default-accept-payment'
+is used instead.")
+
+(defcustom hashcash "/usr/local/bin/hashcash"
   "*The path to the hashcash binary.")
 
+(defcustom hashcash-double-spend-database "hashcash.db"
+  "*The path to the double-spending database.")
+
+(defcustom hashcash-in-news nil
+  "*Specifies whether or not hashcash payments should be made to newsgroups."
+  :type 'boolean)
+
 (require 'mail-utils)
 
 (defun hashcash-strip-quoted-names (addr)
@@ -74,18 +91,41 @@ present, is the string to be hashed; if not present ADDR will be used.")
        (buffer-substring (point-at-bol) (point-at-eol)))
     nil))
 
+(defun hashcash-check-payment (token str val)
+  "Check the validity of a hashcash payment."
+  (zerop (call-process hashcash nil nil nil "-c"
+                      "-d" "-f" hashcash-double-spend-database
+                      "-b" (number-to-string val)
+                      "-r" str
+                      token)))
+
+;;;###autoload
 (defun hashcash-insert-payment (arg)
-  "Insert an X-Hashcash header with a payment for ARG"
+  "Insert X-Payment and X-Hashcash headers with a payment for ARG"
   (interactive "sPay to: ")
   (let ((pay (hashcash-generate-payment (hashcash-payment-to arg)
                                        (hashcash-payment-required arg))))
     (when pay
+      (insert-before-markers "X-Payment: hashcash 1.1 " pay "\n")
       (insert-before-markers "X-Hashcash: " pay "\n"))))
 
 ;;;###autoload
+(defun hashcash-verify-payment (token &optional resource amount)
+  "Verify a hashcash payment"
+  (let ((key (cadr (split-string-by-char token ?:))))
+    (cond ((null resource)
+          (let ((elt (assoc key hashcash-accept-resources)))
+            (and elt (hashcash-check-payment token (car elt)
+                       (or (cadr elt) hashcash-default-accept-payment)))))
+         ((equal token key)
+          (hashcash-check-payment token resource
+                               (or amount hashcash-default-accept-payment)))
+         (t nil))))
+
+;;;###autoload
 (defun mail-add-payment (&optional arg)
-  "Add an X-Hashcash: header with a hashcash payment for each recipient address
-Prefix arg sets default payment temporarily."
+  "Add X-Payment: and X-Hashcash: headers with a hashcash payment
+for each recipient address.  Prefix arg sets default payment temporarily."
   (interactive "P")
   (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
                                    hashcash-default-payment))
@@ -98,18 +138,43 @@ Prefix arg sets default payment temporarily."
        (narrow-to-region (point-min) (point))
        (let ((to (hashcash-strip-quoted-names (mail-fetch-field "To" nil t)))
              (cc (hashcash-strip-quoted-names (mail-fetch-field "Cc" nil t)))
-             (ng (hashcash-strip-quoted-names 
-                  (mail-fetch-field "Newsgroups" nil t))))
+             (ng (hashcash-strip-quoted-names (mail-fetch-field "Newsgroups"
+                                                                nil t))))
          (when to
            (setq addrlist (split-string to ",[ \t\n]*")))
          (when cc
            (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*"))))
-         (when ng
+         (when (and hashcash-in-news ng)
            (setq addrlist (nconc addrlist (split-string ng ",[ \t\n]*")))))
        (when addrlist
          (mapc #'hashcash-insert-payment addrlist)))))
   t)
 
+;;;###autoload
+(defun mail-check-payment (&optional arg)
+  "Look for a valid X-Payment: or X-Hashcash: header.
+Prefix arg sets default accept amount temporarily."
+  (interactive "P")
+  (let ((hashcash-default-accept-payment (if arg (prefix-numeric-value arg)
+                                          hashcash-default-accept-payment)))
+    (save-excursion
+      (goto-char (point-min))
+      (search-forward mail-header-separator)
+      (beginning-of-line)
+      (let ((end (point))
+           (ok nil))
+       (goto-char (point-min))
+       (while (and (not ok) (search-forward "X-Payment: hashcash 1.1 " end t))
+         (setq ok (hashcash-verify-payment
+                   (buffer-substring (point) (point-at-eol)))))
+       (goto-char (point-min))
+       (while (and (not ok) (search-forward "X-Hashcash: " end t))
+         (setq ok (hashcash-verify-payment
+                   (buffer-substring (point) (point-at-eol)))))
+       (when ok
+         (message "Payment valid"))
+       ok))))
+
 (provide 'hashcash)
 
 ;;; hashcash.el ends here