#!/usr/local/bin/python

import string
import getpass
import os
import popen2
import time

# make sure that both Python and GPG are in your system path before
# running this script. If you are using PGP instead of GPG, you will
# not be able to use this script, since PGP doesn't allow this
# kind of automation

# be sure to verify the accuracy of each key and fingerprint below. Feel
# free to delete any line you dont feel is valid. 
keyList = (
( 0, "5A06 A150", "1422 F557 87C4 EA92 4C0E  EEBD 99EC 4624 5A06 A150"),
( 1, "A45A 8AB3", "7811 E9DD 9EC6 6CDB E090  D421 7F75 AB41 A45A 8AB3"),
( 2, "DDFF 0260", "4A44 8227 EF17 EB41 82D6  2C16 B144 43E4 DDFF 0260"),
( 3, "7D9A 22B7", "A15D DADC A5F4 F90B F27E  7E8C AC65 B58B 7D9A 22B7"),
( 4, "8E9F F4FC", "C572 556E 7FF0 1E07 A8ED  82B7 E4B6 9E85 8E9F F4FC"),
( 5, "33A0 0AF6", "437F AD8B 6D73 FED9 4D8F  C7AD 28C3 2CB2 33A0 0AF6"),
( 6, "skip"),
( 7, "F3F6 CB92", "4255 FB43 17C8 2B80 8074  7DB6 7DD7 939B F3F6 CB92"),
( 8, "81D0 F250", "D83D 9CC8 E240 AFB0 95EC  D1AC B7F3 ED32 81D0 F250")
)

pgp = "gpg"
keyserver = "pgp.mit.edu"
emptyTrans = string.maketrans("", "")

passphrase = getpass.getpass("Enter your passphrase (or type skip): ")

responses = {
	"[GNUPG:] GET_BOOL keyedit.sign_all.okay" : "Y",
	"[GNUPG:] GET_LINE sign_uid.expire" : "Y",
	"[GNUPG:] GET_LINE sign_uid.class" : "3",
	"[GNUPG:] GET_BOOL sign_uid.okay" : "Y",
	"[GNUPG:] GET_HIDDEN passphrase.enter" : passphrase,
	"[GNUPG:] GET_LINE keyedit.prompt" : "quit",
}
for prompt in responses.keys():
	responses[prompt.lower()] = responses[prompt]

for key in keyList:
	if cmp(key[1].lower(), "skip") == 0:
		continue
	keyId = key[1].translate(emptyTrans, (" "))
	fingerprint = key[2].translate(emptyTrans, (" "))
	os.system(pgp + " --keyserver " + keyserver + " --recv-keys " + keyId)
	output = os.popen(pgp + " " + "--fingerprint " + keyId, "r")
	lines = output.readlines()
	pgpFingerprint = None
	for line in lines:
		splitLine = line.split("=")
		for i in (0, len(splitLine)-1):
			splitLine[i] = splitLine[i].strip()
		if splitLine[0] == "Key fingerprint":
			pgpFingerprint = splitLine[1].translate(emptyTrans, (" "))
			break
	if pgpFingerprint == None:
		print "Key: " + key[1] + ": Unable to find fingerprint from PGP output. Skipping.\n"
		continue
	if cmp(fingerprint, pgpFingerprint) != 0:
		print "key " + key[1] + ": fingerprints don't match.  Skipping.\n"
		continue
	print "key " + key[1] + ": fingerprints match.  Signing."
	if cmp(passphrase.lower(), "skip") == 0 or len(passphrase) == 0:
		os.system(pgp + " --sign-key " + keyId)
	else:
		#o, inp = popen2.popen4(pgp + " --command-fd 0 --status-fd 1 --sign-key " + keyId, 1024)
		o, inp = os.popen4(pgp + " --command-fd 0 --status-fd 1 --sign-key " + keyId)
		counter = 100
		while counter > 0:
			counter -= 1;
			line = o.readline().strip()
			if len(line) == 0:
				continue;
			if line.find("[GNUPG:]") != 0:
				print line
			for prompt in responses.keys():
				if line.lower().find(prompt) >= 0:
					response = responses[prompt]
					inp.write(response + "\n")
					inp.flush()
					break;
		inp.close()
		o.close()
	os.system(pgp + " --keyserver " + keyserver + " --send-key " + keyId)
	print "Key " + key[1] + ": Done signing."


