2007-11-22 20:50:16 +00:00
|
|
|
;;; hashcash.el --- Add hashcash payments to email
|
|
|
|
|
2011-01-26 08:36:39 +00:00
|
|
|
;; Copyright (C) 2003-2005, 2007-2011 Free Software Foundation, Inc.
|
2007-11-22 20:50:16 +00:00
|
|
|
|
|
|
|
;; Written by: Paul Foley <mycroft@actrix.gen.nz> (1997-2002)
|
|
|
|
;; Maintainer: Paul Foley <mycroft@actrix.gen.nz>
|
|
|
|
;; Keywords: mail, hashcash
|
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
2008-05-06 07:25:26 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2007-11-22 20:50:16 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 07:25:26 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
2007-11-22 20:50:16 +00:00
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-05-06 07:25:26 +00:00
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2007-11-22 20:50:16 +00:00
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2008-05-06 07:25:26 +00:00
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
2007-11-22 20:50:16 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; The hashcash binary is at http://www.hashcash.org/.
|
|
|
|
;;
|
|
|
|
;; Call mail-add-payment to add a hashcash payment to a mail message
|
|
|
|
;; in the current buffer.
|
|
|
|
;;
|
|
|
|
;; Call mail-add-payment-async after writing the addresses but before
|
|
|
|
;; writing the mail to start calculating the hashcash payment
|
|
|
|
;; asynchronously.
|
|
|
|
;;
|
|
|
|
;; The easiest way to do this automatically for all outgoing mail
|
|
|
|
;; is to set `message-generate-hashcash' to t. If you want more
|
|
|
|
;; control, try the following hooks.
|
|
|
|
;;
|
|
|
|
;; To automatically add payments to all outgoing mail when sending:
|
|
|
|
;; (add-hook 'message-send-hook 'mail-add-payment)
|
|
|
|
;;
|
|
|
|
;; To start calculations automatically when addresses are prefilled:
|
|
|
|
;; (add-hook 'message-setup-hook 'mail-add-payment-async)
|
|
|
|
;;
|
|
|
|
;; To check whether calculations are done before sending:
|
|
|
|
;; (add-hook 'message-send-hook 'hashcash-wait-or-cancel)
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2010-10-11 23:29:33 +00:00
|
|
|
;; For Emacs <22.2 and XEmacs.
|
2007-12-08 22:56:04 +00:00
|
|
|
(eval-and-compile
|
|
|
|
(unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
|
|
|
|
|
2008-06-21 20:34:50 +00:00
|
|
|
(eval-when-compile (require 'cl)) ; for case
|
|
|
|
|
2007-11-22 20:50:16 +00:00
|
|
|
(defgroup hashcash nil
|
|
|
|
"Hashcash configuration."
|
|
|
|
:group 'mail)
|
|
|
|
|
|
|
|
(defcustom hashcash-default-payment 20
|
|
|
|
"*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."
|
|
|
|
:type '(repeat (choice (list :tag "Normal"
|
|
|
|
(string :name "Address")
|
|
|
|
(integer :name "Amount"))
|
|
|
|
(list :tag "Replace hash input"
|
|
|
|
(string :name "Address")
|
|
|
|
(string :name "Hash input")
|
|
|
|
(integer :name "Amount"))))
|
|
|
|
:group 'hashcash)
|
|
|
|
|
|
|
|
(defcustom hashcash-default-accept-payment 20
|
|
|
|
"*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-extra-generate-parameters nil
|
|
|
|
"*A list of parameter strings passed to `hashcash-path' when minting.
|
|
|
|
For example, you may want to set this to '(\"-Z2\") to reduce header length."
|
|
|
|
:type '(repeat string)
|
|
|
|
: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)
|
|
|
|
|
|
|
|
(defvar hashcash-process-alist nil
|
|
|
|
"Alist of asynchronous hashcash processes and buffers.")
|
|
|
|
|
|
|
|
(require 'mail-utils)
|
|
|
|
|
|
|
|
(eval-and-compile
|
2007-12-09 22:14:32 +00:00
|
|
|
(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)))
|
2007-11-22 20:50:16 +00:00
|
|
|
|
|
|
|
(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))
|
|
|
|
addr))
|
|
|
|
|
2010-03-18 06:21:21 +00:00
|
|
|
(declare-function message-narrow-to-headers-or-head "message" ())
|
|
|
|
(declare-function message-fetch-field "message" (header &optional not-all))
|
|
|
|
(declare-function message-goto-eoh "message" ())
|
|
|
|
(declare-function message-narrow-to-headers "message" ())
|
* progmodes/cperl-mode.el (compilation-error-regexp-alist): Pacify
byte compiler.
(cperl-mode): Use with-no-warnings for setting vc-header-alist.
* progmodes/idlwave.el (idlwave-shell-get-path-info)
(idlwave-shell-temp-file, idlwave-shell-is-running)
(widget-value, comint-dynamic-complete-filename, Info-goto-node):
* progmodes/idlw-help.el (idlwave-prepare-structure-tag-completion)
(idlwave-all-method-classes, idlwave-all-method-keyword-classes)
(idlwave-beginning-of-statement, idlwave-best-rinfo-assoc)
(idlwave-class-found-in, idlwave-class-or-superclass-with-tag)
(idlwave-completing-read, idlwave-current-routine)
(idlwave-downcase-safe, idlwave-entry-find-keyword)
(idlwave-expand-keyword, idlwave-find-class-definition)
(idlwave-find-inherited-class, idlwave-find-struct-tag)
(idlwave-get-buffer-visiting, idlwave-in-quote)
(idlwave-make-full-name, idlwave-members-only)
(idlwave-popup-select, idlwave-routine-source-file)
(idlwave-routines, idlwave-sintern-class)
(idlwave-sintern-keyword, idlwave-sintern-method)
(idlwave-sintern-routine-or-method)
(idlwave-substitute-link-target, idlwave-sys-dir)
(idlwave-this-word, idlwave-what-module-find-class)
(idlwave-where):
* progmodes/idlw-complete-structtag.el (idlwave-shell-buffer):
* mail/uce.el (rmail-msg-is-pruned)
(rmail-maybe-set-message-counters, rmail-msgbeg, rmail-msgend)
(rmail-toggle-header):
* mail/sendmail.el (dired-view-file, dired-get-filename):
* mail/rmailkwd.el (rmail-maybe-set-message-counters)
(rmail-display-labels, rmail-msgbeg)
(rmail-set-message-deleted-p, rmail-message-labels-p)
(rmail-show-message, mail-comma-list-regexp)
(mail-parse-comma-list):
* mail/rmail.el (rmail-spam-filter, rmail-summary-goto-msg)
(rmail-summary-mark-undeleted, rmail-summary-mark-deleted)
(rfc822-addresses, mail-abbrev-make-syntax-table)
(mail-sendmail-delimit-header, mail-header-end):
* mail/hashcash.el (message-narrow-to-headers-or-head)
(message-fetch-field, message-goto-eoh)
(message-narrow-to-headers):
* vc.el (view-mode-exit): Declare as functions.
* mail/vms-pmail.el:
* vmsproc.el:
* vms-patch.el: Don't byte compile these files, they don't work.
* Makefile.in (SOME_MACHINE_LISP): Remove VMS files, they are not
compiled anymore.
2007-11-27 06:57:07 +00:00
|
|
|
|
2007-11-22 20:50:16 +00:00
|
|
|
(defun hashcash-token-substring ()
|
|
|
|
(save-excursion
|
|
|
|
(let ((token ""))
|
|
|
|
(loop
|
|
|
|
(setq token
|
|
|
|
(concat token (buffer-substring (point) (hashcash-point-at-eol))))
|
|
|
|
(goto-char (hashcash-point-at-eol))
|
|
|
|
(forward-char 1)
|
|
|
|
(unless (looking-at "[ \t]") (return token))
|
|
|
|
(while (looking-at "[ \t]") (forward-char 1))))))
|
|
|
|
|
|
|
|
(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 (and (> val 0)
|
|
|
|
hashcash-path)
|
* url-util.el (url-insert-entities-in-string):
* url-nfs.el (url-nfs-unescape):
* url-ldap.el (url-ldap):
* url-imap.el (url-imap):
* url-cid.el (url-cid-gnus, url-cid): Use with-current-buffer.
* erc.el (erc-display-line-1, erc-process-away):
* erc-truncate.el (erc-truncate-buffer-to-size):
Use with-current-buffer.
* term/ns-win.el (ns-scroll-bar-move, ns-face-at-pos):
* play/mpuz.el (mpuz-create-buffer):
* play/landmark.el (lm-prompt-for-move, lm-print-wts, lm-print-smell)
(lm-print-y,s,noise, lm-print-w0, lm-init):
* play/gomoku.el (gomoku-prompt-for-move):
* play/fortune.el (fortune-in-buffer):
* play/dissociate.el (dissociated-press):
* play/decipher.el (decipher-adjacency-list, decipher-display-regexp)
(decipher-analyze-buffer, decipher-stats-buffer,decipher-stats-buffer):
* mail/supercite.el (sc-eref-show):
* mail/smtpmail.el (smtpmail-send-it):
* mail/rmailsum.el (rmail-summary-next-labeled-message)
(rmail-summary-previous-labeled-message, rmail-summary-wipe)
(rmail-summary-undelete-many, rmail-summary-rmail-update)
(rmail-summary-goto-msg, rmail-summary-expunge)
(rmail-summary-get-new-mail, rmail-summary-search-backward)
(rmail-summary-add-label, rmail-summary-output-menu)
(rmail-summary-output-body):
* mail/rfc822.el (rfc822-addresses):
* mail/reporter.el (reporter-dump-variable, reporter-dump-state):
* mail/mailpost.el (post-mail-send-it):
* mail/hashcash.el (hashcash-generate-payment):
* mail/feedmail.el (feedmail-run-the-queue)
(feedmail-queue-send-edit-prompt-help-first)
(feedmail-send-it-immediately, feedmail-give-it-to-buffer-eater)
(feedmail-deduce-address-list):
* eshell/esh-ext.el (eshell-remote-command):
* eshell/em-unix.el (eshell-occur-mode-mouse-goto):
* emulation/viper-util.el (viper-glob-unix-files, viper-save-setting)
(viper-wildcard-to-regexp, viper-glob-mswindows-files)
(viper-save-string-in-file, viper-valid-marker):
* emulation/viper-keym.el (viper-toggle-key):
* emulation/viper-ex.el (ex-expand-filsyms, viper-get-ex-file)
(ex-edit, ex-global, ex-mark, ex-next-related-buffer, ex-quit)
(ex-get-inline-cmd-args, ex-tag, ex-command, ex-compile):
* emulation/viper-cmd.el (viper-exec-form-in-vi)
(viper-exec-form-in-emacs, viper-brac-function):
* emulation/viper.el (viper-delocalize-var):
* emulation/vip.el (vip-mode, vip-get-ex-token, vip-ex, vip-get-ex-pat)
(vip-get-ex-command, vip-get-ex-opt-gc, vip-get-ex-buffer)
(vip-get-ex-count, vip-get-ex-file, ex-edit, ex-global, ex-mark)
(ex-map, ex-unmap, ex-quit, ex-read, ex-tag, ex-command):
* emulation/vi.el (vi-switch-mode, vi-ex-cmd):
* emulation/edt.el (edt-electric-helpify):
* emulation/cua-rect.el (cua--rectangle-aux-replace):
* emulation/cua-gmrk.el (cua--insert-at-global-mark)
(cua--delete-at-global-mark, cua--copy-rectangle-to-global-mark)
(cua-indent-to-global-mark-column):
* calendar/diary-lib.el (calendar-mark-1):
* calendar/cal-hebrew.el (calendar-hebrew-mark-date-pattern):
Use with-current-buffer.
* emulation/viper.el (viper-delocalize-var): Use dolist.
2009-11-03 02:04:29 +00:00
|
|
|
(with-current-buffer (get-buffer-create " *hashcash*")
|
2007-11-22 20:50:16 +00:00
|
|
|
(erase-buffer)
|
|
|
|
(apply 'call-process hashcash-path nil t nil
|
|
|
|
"-m" "-q" "-b" (number-to-string val) str
|
|
|
|
hashcash-extra-generate-parameters)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(hashcash-token-substring))
|
|
|
|
(error "No `hashcash' binary found")))
|
|
|
|
|
|
|
|
(defun hashcash-generate-payment-async (str val callback)
|
|
|
|
"Generate a hashcash payment by finding a VAL-bit collison on STR.
|
|
|
|
Return immediately. Call CALLBACK with process and result when ready."
|
|
|
|
(if (and (> val 0)
|
|
|
|
hashcash-path)
|
|
|
|
(let ((process (apply 'start-process "hashcash" nil
|
|
|
|
hashcash-path "-m" "-q"
|
|
|
|
"-b" (number-to-string val) str
|
|
|
|
hashcash-extra-generate-parameters)))
|
|
|
|
(setq hashcash-process-alist (cons
|
|
|
|
(cons process (current-buffer))
|
|
|
|
hashcash-process-alist))
|
|
|
|
(set-process-filter process `(lambda (process output)
|
|
|
|
(funcall ,callback process output))))
|
|
|
|
(funcall callback nil nil)))
|
|
|
|
|
|
|
|
(defun hashcash-check-payment (token str val)
|
|
|
|
"Check the validity of a hashcash payment."
|
|
|
|
(if hashcash-path
|
|
|
|
(zerop (call-process hashcash-path nil nil nil "-c"
|
|
|
|
"-d" "-f" hashcash-double-spend-database
|
|
|
|
"-b" (number-to-string val)
|
|
|
|
"-r" str
|
|
|
|
token))
|
|
|
|
(progn
|
|
|
|
(message "No hashcash binary found")
|
|
|
|
(sleep-for 1)
|
|
|
|
nil)))
|
|
|
|
|
|
|
|
(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-already-paid-p (recipient)
|
|
|
|
"Check for hashcash token to RECIPIENT in current buffer."
|
|
|
|
(save-excursion
|
|
|
|
(save-restriction
|
|
|
|
(message-narrow-to-headers-or-head)
|
|
|
|
(let ((token (message-fetch-field "x-hashcash"))
|
|
|
|
(case-fold-search t))
|
|
|
|
(and (stringp token)
|
|
|
|
(string-match (regexp-quote recipient) token))))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun hashcash-insert-payment (arg)
|
|
|
|
"Insert X-Payment and X-Hashcash headers with a payment for ARG"
|
|
|
|
(interactive "sPay to: ")
|
|
|
|
(unless (hashcash-already-paid-p arg)
|
|
|
|
(let ((pay (hashcash-generate-payment (hashcash-payment-to arg)
|
|
|
|
(hashcash-payment-required arg))))
|
|
|
|
(when pay
|
|
|
|
(insert-before-markers "X-Hashcash: " pay "\n")))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun hashcash-insert-payment-async (arg)
|
|
|
|
"Insert X-Payment and X-Hashcash headers with a payment for ARG
|
|
|
|
Only start calculation. Results are inserted when ready."
|
|
|
|
(interactive "sPay to: ")
|
|
|
|
(unless (hashcash-already-paid-p arg)
|
|
|
|
(hashcash-generate-payment-async
|
|
|
|
(hashcash-payment-to arg)
|
|
|
|
(hashcash-payment-required arg)
|
|
|
|
`(lambda (process payment)
|
|
|
|
(hashcash-insert-payment-async-2 ,(current-buffer) process payment)))))
|
|
|
|
|
|
|
|
(defun hashcash-insert-payment-async-2 (buffer process pay)
|
|
|
|
(when (buffer-live-p buffer)
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(save-excursion
|
|
|
|
(save-restriction
|
|
|
|
(setq hashcash-process-alist (delq
|
|
|
|
(assq process hashcash-process-alist)
|
|
|
|
hashcash-process-alist))
|
|
|
|
(message-goto-eoh)
|
|
|
|
(when pay
|
|
|
|
(insert-before-markers "X-Hashcash: " pay)))))))
|
|
|
|
|
|
|
|
(defun hashcash-cancel-async (&optional buffer)
|
|
|
|
"Delete any hashcash processes associated with BUFFER.
|
|
|
|
BUFFER defaults to the current buffer."
|
|
|
|
(interactive)
|
|
|
|
(unless buffer (setq buffer (current-buffer)))
|
|
|
|
(let (entry)
|
|
|
|
(while (setq entry (rassq buffer hashcash-process-alist))
|
|
|
|
(delete-process (car entry))
|
|
|
|
(setq hashcash-process-alist
|
|
|
|
(delq entry hashcash-process-alist)))))
|
|
|
|
|
|
|
|
(defun hashcash-wait-async (&optional buffer)
|
|
|
|
"Wait for asynchronous hashcash processes in BUFFER to finish.
|
|
|
|
BUFFER defaults to the current buffer."
|
|
|
|
(interactive)
|
|
|
|
(unless buffer (setq buffer (current-buffer)))
|
|
|
|
(let (entry)
|
|
|
|
(while (setq entry (rassq buffer hashcash-process-alist))
|
2010-09-07 00:04:11 +00:00
|
|
|
(accept-process-output (car entry) 1))))
|
2007-11-22 20:50:16 +00:00
|
|
|
|
|
|
|
(defun hashcash-processes-running-p (buffer)
|
|
|
|
"Return non-nil if hashcash processes in BUFFER are still running."
|
|
|
|
(rassq buffer hashcash-process-alist))
|
|
|
|
|
|
|
|
(defun hashcash-wait-or-cancel ()
|
|
|
|
"Ask user whether to wait for hashcash processes to finish."
|
|
|
|
(interactive)
|
|
|
|
(when (hashcash-processes-running-p (current-buffer))
|
2010-03-18 06:21:21 +00:00
|
|
|
(if (y-or-n-p
|
2007-11-22 20:50:16 +00:00
|
|
|
"Hashcash process(es) still running; wait for them to finish? ")
|
|
|
|
(hashcash-wait-async)
|
|
|
|
(hashcash-cancel-async))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun hashcash-verify-payment (token &optional resource amount)
|
|
|
|
"Verify a hashcash payment"
|
|
|
|
(let* ((split (split-string token ":"))
|
|
|
|
(key (if (< (hashcash-version token) 1.2)
|
|
|
|
(nth 1 split)
|
|
|
|
(case (string-to-number (nth 0 split))
|
|
|
|
(0 (nth 2 split))
|
|
|
|
(1 (nth 3 split))))))
|
|
|
|
(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 async)
|
|
|
|
"Add X-Payment: and X-Hashcash: headers with a hashcash payment
|
|
|
|
for each recipient address. Prefix arg sets default payment temporarily.
|
|
|
|
Set ASYNC to t to start asynchronous calculation. (See
|
|
|
|
`mail-add-payment-async')."
|
|
|
|
(interactive "P")
|
|
|
|
(let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
|
|
|
|
hashcash-default-payment))
|
|
|
|
(addrlist nil))
|
|
|
|
(save-excursion
|
|
|
|
(save-restriction
|
|
|
|
(message-narrow-to-headers)
|
|
|
|
(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
|
|
|
|
(mapc (if async
|
|
|
|
#'hashcash-insert-payment-async
|
|
|
|
#'hashcash-insert-payment)
|
|
|
|
addrlist)))))
|
|
|
|
t)
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun mail-add-payment-async (&optional arg)
|
|
|
|
"Add X-Payment: and X-Hashcash: headers with a hashcash payment
|
|
|
|
for each recipient address. Prefix arg sets default payment temporarily.
|
|
|
|
Calculation is asynchronous."
|
|
|
|
(interactive "P")
|
|
|
|
(mail-add-payment arg 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))
|
|
|
|
(version (hashcash-version (hashcash-generate-payment "x" 1))))
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (point-min))
|
|
|
|
(search-forward "\n\n")
|
|
|
|
(beginning-of-line)
|
|
|
|
(let ((end (point))
|
|
|
|
(ok nil))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (and (not ok) (search-forward "X-Payment: hashcash " end t))
|
|
|
|
(let ((value (split-string (hashcash-token-substring) " ")))
|
|
|
|
(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 (hashcash-token-substring))))
|
|
|
|
(when ok
|
|
|
|
(message "Payment valid"))
|
|
|
|
ok))))
|
|
|
|
|
|
|
|
(provide 'hashcash)
|
|
|
|
|
2010-09-02 00:55:51 +00:00
|
|
|
;;; hashcash.el ends here
|