hashcash.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. ;;; hashcash.el --- Add hashcash payments to email
  2. ;; Copyright (C) 2003-2005, 2007-2012 Free Software Foundation, Inc.
  3. ;; Written by: Paul Foley <mycroft@actrix.gen.nz> (1997-2002)
  4. ;; Maintainer: Paul Foley <mycroft@actrix.gen.nz>
  5. ;; Keywords: mail, hashcash
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; The hashcash binary is at http://www.hashcash.org/.
  19. ;;
  20. ;; Call mail-add-payment to add a hashcash payment to a mail message
  21. ;; in the current buffer.
  22. ;;
  23. ;; Call mail-add-payment-async after writing the addresses but before
  24. ;; writing the mail to start calculating the hashcash payment
  25. ;; asynchronously.
  26. ;;
  27. ;; The easiest way to do this automatically for all outgoing mail
  28. ;; is to set `message-generate-hashcash' to t. If you want more
  29. ;; control, try the following hooks.
  30. ;;
  31. ;; To automatically add payments to all outgoing mail when sending:
  32. ;; (add-hook 'message-send-hook 'mail-add-payment)
  33. ;;
  34. ;; To start calculations automatically when addresses are prefilled:
  35. ;; (add-hook 'message-setup-hook 'mail-add-payment-async)
  36. ;;
  37. ;; To check whether calculations are done before sending:
  38. ;; (add-hook 'message-send-hook 'hashcash-wait-or-cancel)
  39. ;;; Code:
  40. ;; For Emacs <22.2 and XEmacs.
  41. (eval-and-compile
  42. (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
  43. (eval-when-compile (require 'cl)) ; for case
  44. (defgroup hashcash nil
  45. "Hashcash configuration."
  46. :group 'mail)
  47. (defcustom hashcash-default-payment 20
  48. "*The default number of bits to pay to unknown users.
  49. If this is zero, no payment header will be generated.
  50. See `hashcash-payment-alist'."
  51. :type 'integer
  52. :group 'hashcash)
  53. (defcustom hashcash-payment-alist '()
  54. "*An association list mapping email addresses to payment amounts.
  55. Elements may consist of (ADDR AMOUNT) or (ADDR STRING AMOUNT), where
  56. ADDR is the email address of the intended recipient and AMOUNT is
  57. the value of hashcash payment to be made to that user. STRING, if
  58. present, is the string to be hashed; if not present ADDR will be used."
  59. :type '(repeat (choice (list :tag "Normal"
  60. (string :name "Address")
  61. (integer :name "Amount"))
  62. (list :tag "Replace hash input"
  63. (string :name "Address")
  64. (string :name "Hash input")
  65. (integer :name "Amount"))))
  66. :group 'hashcash)
  67. (defcustom hashcash-default-accept-payment 20
  68. "*The default minimum number of bits to accept on incoming payments."
  69. :type 'integer
  70. :group 'hashcash)
  71. (defcustom hashcash-accept-resources `((,user-mail-address nil))
  72. "*An association list mapping hashcash resources to payment amounts.
  73. Resources named here are to be accepted in incoming payments. If the
  74. corresponding AMOUNT is NIL, the value of `hashcash-default-accept-payment'
  75. is used instead."
  76. :group 'hashcash)
  77. (defcustom hashcash-path (executable-find "hashcash")
  78. "*The path to the hashcash binary."
  79. :group 'hashcash)
  80. (defcustom hashcash-extra-generate-parameters nil
  81. "*A list of parameter strings passed to `hashcash-path' when minting.
  82. For example, you may want to set this to '(\"-Z2\") to reduce header length."
  83. :type '(repeat string)
  84. :group 'hashcash)
  85. (defcustom hashcash-double-spend-database "hashcash.db"
  86. "*The path to the double-spending database."
  87. :group 'hashcash)
  88. (defcustom hashcash-in-news nil
  89. "*Specifies whether or not hashcash payments should be made to newsgroups."
  90. :type 'boolean
  91. :group 'hashcash)
  92. (defvar hashcash-process-alist nil
  93. "Alist of asynchronous hashcash processes and buffers.")
  94. (require 'mail-utils)
  95. (eval-and-compile
  96. (if (fboundp 'point-at-bol)
  97. (defalias 'hashcash-point-at-bol 'point-at-bol)
  98. (defalias 'hashcash-point-at-bol 'line-beginning-position))
  99. (if (fboundp 'point-at-eol)
  100. (defalias 'hashcash-point-at-eol 'point-at-eol)
  101. (defalias 'hashcash-point-at-eol 'line-end-position)))
  102. (defun hashcash-strip-quoted-names (addr)
  103. (setq addr (mail-strip-quoted-names addr))
  104. (if (and addr (string-match "\\`\\([^+@]+\\)\\+[^@]*\\(@.+\\)" addr))
  105. (concat (match-string 1 addr) (match-string 2 addr))
  106. addr))
  107. (declare-function message-narrow-to-headers-or-head "message" ())
  108. (declare-function message-fetch-field "message" (header &optional not-all))
  109. (declare-function message-goto-eoh "message" ())
  110. (declare-function message-narrow-to-headers "message" ())
  111. (defun hashcash-token-substring ()
  112. (save-excursion
  113. (let ((token ""))
  114. (loop
  115. (setq token
  116. (concat token (buffer-substring (point) (hashcash-point-at-eol))))
  117. (goto-char (hashcash-point-at-eol))
  118. (forward-char 1)
  119. (unless (looking-at "[ \t]") (return token))
  120. (while (looking-at "[ \t]") (forward-char 1))))))
  121. (defun hashcash-payment-required (addr)
  122. "Return the hashcash payment value required for the given address."
  123. (let ((val (assoc addr hashcash-payment-alist)))
  124. (or (nth 2 val) (nth 1 val) hashcash-default-payment)))
  125. (defun hashcash-payment-to (addr)
  126. "Return the string with which hashcash payments should collide."
  127. (let ((val (assoc addr hashcash-payment-alist)))
  128. (or (nth 1 val) (nth 0 val) addr)))
  129. (defun hashcash-generate-payment (str val)
  130. "Generate a hashcash payment by finding a VAL-bit collison on STR."
  131. (if (and (> val 0)
  132. hashcash-path)
  133. (with-current-buffer (get-buffer-create " *hashcash*")
  134. (erase-buffer)
  135. (apply 'call-process hashcash-path nil t nil
  136. "-m" "-q" "-b" (number-to-string val) str
  137. hashcash-extra-generate-parameters)
  138. (goto-char (point-min))
  139. (hashcash-token-substring))
  140. (error "No `hashcash' binary found")))
  141. (defun hashcash-generate-payment-async (str val callback)
  142. "Generate a hashcash payment by finding a VAL-bit collison on STR.
  143. Return immediately. Call CALLBACK with process and result when ready."
  144. (if (and (> val 0)
  145. hashcash-path)
  146. (let ((process (apply 'start-process "hashcash" nil
  147. hashcash-path "-m" "-q"
  148. "-b" (number-to-string val) str
  149. hashcash-extra-generate-parameters)))
  150. (setq hashcash-process-alist (cons
  151. (cons process (current-buffer))
  152. hashcash-process-alist))
  153. (set-process-filter process `(lambda (process output)
  154. (funcall ,callback process output))))
  155. (funcall callback nil nil)))
  156. (defun hashcash-check-payment (token str val)
  157. "Check the validity of a hashcash payment."
  158. (if hashcash-path
  159. (zerop (call-process hashcash-path nil nil nil "-c"
  160. "-d" "-f" hashcash-double-spend-database
  161. "-b" (number-to-string val)
  162. "-r" str
  163. token))
  164. (progn
  165. (message "No hashcash binary found")
  166. (sleep-for 1)
  167. nil)))
  168. (defun hashcash-version (token)
  169. "Find the format version of a hashcash token."
  170. ;; Version 1.2 looks like n:yymmdd:rrrrr:xxxxxxxxxxxxxxxx
  171. ;; This carries its own version number embedded in the token,
  172. ;; so no further format number changes should be necessary
  173. ;; in the X-Payment header.
  174. ;;
  175. ;; Version 1.1 looks like yymmdd:rrrrr:xxxxxxxxxxxxxxxx
  176. ;; You need to upgrade your hashcash binary.
  177. ;;
  178. ;; Version 1.0 looked like nnnnnrrrrrxxxxxxxxxxxxxxxx
  179. ;; This is no longer supported.
  180. (cond ((equal (aref token 1) ?:) 1.2)
  181. ((equal (aref token 6) ?:) 1.1)
  182. (t (error "Unknown hashcash format version"))))
  183. (defun hashcash-already-paid-p (recipient)
  184. "Check for hashcash token to RECIPIENT in current buffer."
  185. (save-excursion
  186. (save-restriction
  187. (message-narrow-to-headers-or-head)
  188. (let ((token (message-fetch-field "x-hashcash"))
  189. (case-fold-search t))
  190. (and (stringp token)
  191. (string-match (regexp-quote recipient) token))))))
  192. ;;;###autoload
  193. (defun hashcash-insert-payment (arg)
  194. "Insert X-Payment and X-Hashcash headers with a payment for ARG"
  195. (interactive "sPay to: ")
  196. (unless (hashcash-already-paid-p arg)
  197. (let ((pay (hashcash-generate-payment (hashcash-payment-to arg)
  198. (hashcash-payment-required arg))))
  199. (when pay
  200. (insert-before-markers "X-Hashcash: " pay "\n")))))
  201. ;;;###autoload
  202. (defun hashcash-insert-payment-async (arg)
  203. "Insert X-Payment and X-Hashcash headers with a payment for ARG
  204. Only start calculation. Results are inserted when ready."
  205. (interactive "sPay to: ")
  206. (unless (hashcash-already-paid-p arg)
  207. (hashcash-generate-payment-async
  208. (hashcash-payment-to arg)
  209. (hashcash-payment-required arg)
  210. `(lambda (process payment)
  211. (hashcash-insert-payment-async-2 ,(current-buffer) process payment)))))
  212. (defun hashcash-insert-payment-async-2 (buffer process pay)
  213. (when (buffer-live-p buffer)
  214. (with-current-buffer buffer
  215. (save-excursion
  216. (save-restriction
  217. (setq hashcash-process-alist (delq
  218. (assq process hashcash-process-alist)
  219. hashcash-process-alist))
  220. (message-goto-eoh)
  221. (when pay
  222. (insert-before-markers "X-Hashcash: " pay)))))))
  223. (defun hashcash-cancel-async (&optional buffer)
  224. "Delete any hashcash processes associated with BUFFER.
  225. BUFFER defaults to the current buffer."
  226. (interactive)
  227. (unless buffer (setq buffer (current-buffer)))
  228. (let (entry)
  229. (while (setq entry (rassq buffer hashcash-process-alist))
  230. (delete-process (car entry))
  231. (setq hashcash-process-alist
  232. (delq entry hashcash-process-alist)))))
  233. (defun hashcash-wait-async (&optional buffer)
  234. "Wait for asynchronous hashcash processes in BUFFER to finish.
  235. BUFFER defaults to the current buffer."
  236. (interactive)
  237. (unless buffer (setq buffer (current-buffer)))
  238. (let (entry)
  239. (while (setq entry (rassq buffer hashcash-process-alist))
  240. (accept-process-output (car entry) 1))))
  241. (defun hashcash-processes-running-p (buffer)
  242. "Return non-nil if hashcash processes in BUFFER are still running."
  243. (rassq buffer hashcash-process-alist))
  244. (defun hashcash-wait-or-cancel ()
  245. "Ask user whether to wait for hashcash processes to finish."
  246. (interactive)
  247. (when (hashcash-processes-running-p (current-buffer))
  248. (if (y-or-n-p
  249. "Hashcash process(es) still running; wait for them to finish? ")
  250. (hashcash-wait-async)
  251. (hashcash-cancel-async))))
  252. ;;;###autoload
  253. (defun hashcash-verify-payment (token &optional resource amount)
  254. "Verify a hashcash payment"
  255. (let* ((split (split-string token ":"))
  256. (key (if (< (hashcash-version token) 1.2)
  257. (nth 1 split)
  258. (case (string-to-number (nth 0 split))
  259. (0 (nth 2 split))
  260. (1 (nth 3 split))))))
  261. (cond ((null resource)
  262. (let ((elt (assoc key hashcash-accept-resources)))
  263. (and elt (hashcash-check-payment token (car elt)
  264. (or (cadr elt) hashcash-default-accept-payment)))))
  265. ((equal token key)
  266. (hashcash-check-payment token resource
  267. (or amount hashcash-default-accept-payment)))
  268. (t nil))))
  269. ;;;###autoload
  270. (defun mail-add-payment (&optional arg async)
  271. "Add X-Payment: and X-Hashcash: headers with a hashcash payment
  272. for each recipient address. Prefix arg sets default payment temporarily.
  273. Set ASYNC to t to start asynchronous calculation. (See
  274. `mail-add-payment-async')."
  275. (interactive "P")
  276. (let ((hashcash-default-payment (if arg (prefix-numeric-value arg)
  277. hashcash-default-payment))
  278. (addrlist nil))
  279. (save-excursion
  280. (save-restriction
  281. (message-narrow-to-headers)
  282. (let ((to (hashcash-strip-quoted-names (mail-fetch-field "To" nil t)))
  283. (cc (hashcash-strip-quoted-names (mail-fetch-field "Cc" nil t)))
  284. (ng (hashcash-strip-quoted-names (mail-fetch-field "Newsgroups"
  285. nil t))))
  286. (when to
  287. (setq addrlist (split-string to ",[ \t\n]*")))
  288. (when cc
  289. (setq addrlist (nconc addrlist (split-string cc ",[ \t\n]*"))))
  290. (when (and hashcash-in-news ng)
  291. (setq addrlist (nconc addrlist (split-string ng ",[ \t\n]*")))))
  292. (when addrlist
  293. (mapc (if async
  294. #'hashcash-insert-payment-async
  295. #'hashcash-insert-payment)
  296. addrlist)))))
  297. t)
  298. ;;;###autoload
  299. (defun mail-add-payment-async (&optional arg)
  300. "Add X-Payment: and X-Hashcash: headers with a hashcash payment
  301. for each recipient address. Prefix arg sets default payment temporarily.
  302. Calculation is asynchronous."
  303. (interactive "P")
  304. (mail-add-payment arg t))
  305. ;;;###autoload
  306. (defun mail-check-payment (&optional arg)
  307. "Look for a valid X-Payment: or X-Hashcash: header.
  308. Prefix arg sets default accept amount temporarily."
  309. (interactive "P")
  310. (let ((hashcash-default-accept-payment (if arg (prefix-numeric-value arg)
  311. hashcash-default-accept-payment))
  312. (version (hashcash-version (hashcash-generate-payment "x" 1))))
  313. (save-excursion
  314. (goto-char (point-min))
  315. (search-forward "\n\n")
  316. (beginning-of-line)
  317. (let ((end (point))
  318. (ok nil))
  319. (goto-char (point-min))
  320. (while (and (not ok) (search-forward "X-Payment: hashcash " end t))
  321. (let ((value (split-string (hashcash-token-substring) " ")))
  322. (when (equal (car value) (number-to-string version))
  323. (setq ok (hashcash-verify-payment (cadr value))))))
  324. (goto-char (point-min))
  325. (while (and (not ok) (search-forward "X-Hashcash: " end t))
  326. (setq ok (hashcash-verify-payment (hashcash-token-substring))))
  327. (when ok
  328. (message "Payment valid"))
  329. ok))))
  330. (provide 'hashcash)
  331. ;;; hashcash.el ends here