[hashcash] Python hashcash minting script for Mutt

  • From: Aaron Toponce <aaron.toponce@xxxxxxxxx>
  • To: hashcash@xxxxxxxxxxxxx
  • Date: Thu, 24 Mar 2011 17:27:58 -0600

I have finalized my Python minting script for Mutt. I haven't written a
verification script yet, although that should be trivial with
$display_filter. That's next.

Anyway, the script is attached. If you wish to give it a go, here is what
you'll need to do to set it up:

Set the following varibles in your ~/.muttrc:

set editor="/path/to/mint_hashcash.py"  # required
set edit_headers="yes"                  # required
set askcc="yes"                         # recommended

The Python script calls Vim on line 10 of the script. Feel free to replace
it with your favorite editor.

A couple notes about the logic:

* Python 2.5 or later is required.
* It uses formatting of version 1 of the protocol only.
* It only mints new tokens after exiting your editor. If you add
additional emails, you will need to re-edit the mail, then quit to
mint the new tokens.
* Already existing tokens are left in place without duplicates minted.
* Both "X-Hashcash" and "Hashcash" headers are supported.
* 20 bits is minted, and compression level 2 is used.

If you use it, and find any bugs, please let me know. I've been using it
all day, and I've been trying to break it, but haven't found anything yet.

--
. o .   o . o   . . o   o . .   . o .
. . o   . o o   o . o   . o o   . . o
o o o   . o .   . o o   o o .   o o o
#!/usr/bin/env python

import csv
import fileinput
import re
import rfc822
import subprocess
import sys

subprocess.call("vim %s" % sys.argv[1], shell=True)

file = open(sys.argv[1], 'r')
headers = rfc822.Message(file)

from_email = headers.getaddrlist("From")
to_emails = headers.getaddrlist("To")
cc_emails = headers.getaddrlist("Cc")
bcc_emails = headers.getaddrlist("Bcc")

email_addrs = []
tokens = []

# Harvest all email addresses from the header
for email in to_emails:
    email_addrs.append(email[1])

for email in cc_emails:
    email_addrs.append(email[1])

# Remove duplicate emails from the list, requires Python 2.5 and later
email_addrs = list(set(email_addrs))

# Check if an appropriate token is already generated for the mail
if headers.has_key("X-Hashcash"):
    for list in headers.getheaders("X-Hashcash"):
        email_addrs.remove(list.split(":")[3])
if headers.has_key("Hashcash"):
    for list in headers.getheaders("Hashcash"):
        email_addrs.remove(list.split(":")[3])

# Call the hashcash function from the operating system to mint tokens
for email in email_addrs:
    t = subprocess.Popen("hashcash -m %s -X -Z 2" % email, shell=True, 
stdout=subprocess.PIPE)
    tokens.append(t.stdout.read())

f = fileinput.FileInput(sys.argv[1], inplace=1)
for line in f:
    line = line.strip()
    if f.lineno() == 7:
        for token in tokens:
            print token,
        print line
        continue
    else:
        print line

file.close()

Other related posts: