#!/usr/local/bin/python

import string
import getpass
import os
import popen2
import time

keyList = (
	( 0, "3D78 B385", "9694 BAB7 9780 C9E2 3724  3FB6 387A 3EAB 3D78 B385"), 
	( 1, "30AD BB9E", "4709 D9E9 D368 86FF 7C69  D3EE D9F3 7794 30AD BB9E"),
	( 2, "AE4B 5D92", "CF67 D67A 398C D0B8 7234  31C4 C974 25FB AE4B 5D92"),
	( 3, "SKIP", ""),
	( 4, "A17E D584", "DA1A E0F0 7E07 27C3 7539  F2F4 5AF1 2C82 A17E D584"),
	( 5, "32B2 C17B", "DDD4 7509 0BC8 D042 6A81  567B 02A5 4B0F 32B2 C17B"),
	( 6, "17B5 6C41", "C6 D9 AE 3A 39 3B B0 D6 B5 4B 07 03 40 AB 48 D0"),
	( 7, "E0F0 0E77", "014A 0256 D049 3553 0E22  67B4 D26A DE17 E0F0 0E77"),
	( 8, "DA62 4043", "6B64 B624 4CA0 CD37 3451  A026 7E5C CA9A DA62 4043"),
	( 9, "D6C6 BB91", "0B1E 7F7F 429E 4435 CF74  A082 ACDF 8C1C D6C6 BB91"),
	(10, "7541 9E91", "F6 7B 89 B2 95 F4 FE DA 38 80 91 3A 5B 45 1F 14"),
	(11, "8ABA 36F5", "9EC7 BC6D E688 A184 9F58  FD4C 2C12 CC14 8ABA 36F5"),
	(12, "60DD 9550", "F87B 9407 29F4 A0B8 99EA  7C3F 7AF6 D6F4 60DD 9550"),
	(13, "3E23 B74B", "6158 7DF8 B6B0 9FB0 D1A3  029B 0702 CC58 3E23 B74B"),
	(14, "A45A 8AB3", "7811 E9DD 9EC6 6CDB E090  D421 7F75 AB41 A45A 8AB3"),
	(15, "DDFF 0260", "4A44 8227 EF17 EB41 82D6  2C16 B144 43E4 DDFF 0260"),
	(16, "7D9A 22B7", "A15D DADC A5F4 F90B F27E  7E8C AC65 B58B 7D9A 22B7"),
	(17, "0406 2854", "7570 8D99 1630 C5A2 569D  1D1D 05E4 7A11 0406 2854"),
	(18, "3B90 DFE4", "41A9 2BDE 8E11 F1C5 87A6  03EE 34B3 E075 3B90 DFE4"),
	(19, "6845 7839", "7B38 BE14 DBFC 4F6F 597F  4B70 FE45 8B30 6845 7839"),
	(20, "5A06 A150", "1422 F557 87C4 EA92 4C0E  EEBD 99EC 4624 5A06 A150"),
	(21, "8E9F F4FC", "C572 556E 7FF0 1E07 A8ED  82B7 E4B6 9E85 8E9F F4FC"),
	(22, "ECAB 4C20", "AB9A CFCC 462D 25F2 BA49  966B 2C6D B27C ECAB 4C20"),
	(23, "D430 6C52", "4A3F CF8A 54A7 DA74 D1E2  561D 78F6 8831 D430 6C52"),
	(24, "SKIP", ""),
	(25, "4A5D 4D10", "19CD 20F2 CC28 AD6A CEF0  7E3F D71C E62C 4A5D 4D10"),
)

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)
		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."


