gnus / lisp / hashcash.el

;;; hashcash.el --- Add hashcash payments to email

;; Copyright (C) 2002, 2003, 2005 Free Software Foundation
;; Copyright (C) 1997--2002 Paul E. Foley

;; Maintainer: Paul Foley <>
;; Keywords: mail, hashcash

;; Released under the GNU General Public License
;;   (

;;; Commentary:

;; The hashcash binary is at
;; Call mail-add-payment to add a hashcash payment to a mail message
;; in the current buffer.
;; To automatically add payments to all outgoing mail:
;;    (add-hook 'message-send-hook 'mail-add-payment)

;;; Code:

 (autoload 'executable-find "executable"))

(defgroup hashcash nil
  "Hashcash configuration."
  :group 'mail)

(defcustom hashcash-default-payment 0
  "*The default number of bits to pay to unknown users.
If this is zero, no payment header will be generated.
See `hashcash-payment-alist'."
  :type 'integer
  :group 'hashcash)

(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."
  :group 'hashcash)

(defcustom hashcash-default-accept-payment 10
  "*The default minimum number of bits to accept on incoming payments."
  :type 'integer
  :group 'hashcash)

(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."
  :group 'hashcash)

(defcustom hashcash-path (executable-find "hashcash")
  "*The path to the hashcash binary."
  :group 'hashcash)

(defcustom hashcash-double-spend-database "hashcash.db"
  "*The path to the double-spending database."
  :group 'hashcash)

(defcustom hashcash-in-news nil
  "*Specifies whether or not hashcash payments should be made to newsgroups."
  :type 'boolean
  :group 'hashcash)

(require 'mail-utils)

 (if (fboundp 'point-at-bol)
     (defalias 'hashcash-point-at-bol 'point-at-bol)
   (defalias 'hashcash-point-at-bol 'line-beginning-position))

 (if (fboundp 'point-at-eol)
     (defalias 'hashcash-point-at-eol 'point-at-eol)
   (defalias 'hashcash-point-at-eol 'line-end-position)))

(defun hashcash-strip-quoted-names (addr)
  (setq addr (mail-strip-quoted-names addr))
  (if (and addr (string-match "\\`\\([^+@]+\\)\\+[^@]*\\(@.+\\)" addr))
      (concat (match-string 1 addr) (match-string 2 addr))

(defun hashcash-payment-required (addr)
  "Return the hashcash payment value required for the given address."
  (let ((val (assoc addr hashcash-payment-alist)))
    (or (nth 2 val) (nth 1 val) hashcash-default-payment)))

(defun hashcash-payment-to (addr)
  "Return the string with which hashcash payments should collide."
  (let ((val (assoc addr hashcash-payment-alist)))
    (or (nth 1 val) (nth 0 val) addr)))

(defun hashcash-generate-payment (str val)
  "Generate a hashcash payment by finding a VAL-bit collison on STR."
  (if (> val 0)
	(set-buffer (get-buffer-create " *hashcash*"))
	(call-process hashcash-path nil t nil
		      "-m" "-q" "-b" (number-to-string val) str)
	(goto-char (point-min))
	(buffer-substring (hashcash-point-at-bol) (hashcash-point-at-eol)))

(defun hashcash-check-payment (token str val)
  "Check the validity of a hashcash payment."
  (zerop (call-process hashcash-path nil nil nil "-c"
		       "-d" "-f" hashcash-double-spend-database
		       "-b" (number-to-string val)
		       "-r" str

(defun hashcash-version (token)
  "Find the format version of a hashcash token."
  ;; Version 1.2 looks like n:yymmdd:rrrrr:xxxxxxxxxxxxxxxx
  ;;   This carries its own version number embedded in the token,
  ;;   so no further format number changes should be necessary
  ;;   in the X-Payment header.
  ;; Version 1.1 looks like yymmdd:rrrrr:xxxxxxxxxxxxxxxx
  ;;   You need to upgrade your hashcash binary.
  ;; Version 1.0 looked like nnnnnrrrrrxxxxxxxxxxxxxxxx
  ;;   This is no longer supported.
  (cond ((equal (aref token 1) ?:) 1.2)
	((equal (aref token 6) ?:) 1.1)
	(t (error "Unknown hashcash format version"))))

(defun hashcash-insert-payment (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 "
;			     (number-to-string (hashcash-version pay)) " "
;			     pay "\n")
      (insert-before-markers "X-Hashcash: " pay "\n"))))

(defun hashcash-verify-payment (token &optional resource amount)
  "Verify a hashcash payment"
  (let ((key (if (< (hashcash-version token) 1.2)
		 (nth 1 (split-string token ":"))
		 (nth 2 (split-string 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))))

(defun mail-add-payment (&optional arg)
  "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)
	(addrlist nil))
	(goto-char (point-min))
	(search-forward mail-header-separator)
	(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))))
	  (when to
	    (setq addrlist (split-string to ",[ \t\n]*")))
	  (when cc
	    (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*"))))
	  (when (and hashcash-in-news ng)
	    (setq addrlist (nconc addrlist (split-string ng ",[ \t\n]*")))))
	(when addrlist
	  (mapcar #'hashcash-insert-payment addrlist))))) ; mapc

(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)
	(version (hashcash-version (hashcash-generate-payment "x" 1))))
      (goto-char (point-min))
      (search-forward "\n\n")
      (let ((end (point))
	    (ok nil))
	(goto-char (point-min))
	(while (and (not ok) (search-forward "X-Payment: hashcash " end t))
	  (let ((value (split-string
			  (buffer-substring (point) (hashcash-point-at-eol))
			  " ")))
	    (when (equal (car value) (number-to-string version))
	      (setq ok (hashcash-verify-payment (cadr value))))))
	(goto-char (point-min))
	(while (and (not ok) (search-forward "X-Hashcash: " end t))
	  (setq ok (hashcash-verify-payment
		    (buffer-substring (point) (hashcash-point-at-eol)))))
	(when ok
	  (message "Payment valid"))

(provide 'hashcash)

;;; arch-tag: 0e7fe983-a124-4392-9788-0dbcbd2c4d62
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.