+ (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))
+ (accept-process-output (car entry) 1))))
+
+(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))
+ (if (y-or-n-p
+ "Hashcash process(es) still running; wait for them to finish? ")
+ (hashcash-wait-async)
+ (hashcash-cancel-async))))