# HG changeset patch # User Paul Boddie # Date 1384194569 -3600 # Node ID 8d27c74f274e9a9cf9a252bb37a268c75835cdbc # Parent 920b5fd270f9821d59422eaf78ba89118a6c9e7e Attempt to decode key blocks and return a representation of key properties. diff -r 920b5fd270f9 -r 8d27c74f274e MoinMessage.py --- a/MoinMessage.py Sun Nov 10 23:06:21 2013 +0100 +++ b/MoinMessage.py Mon Nov 11 19:29:29 2013 +0100 @@ -358,6 +358,48 @@ text = self.run(["--armor", "--export", keyid]) return MIMEApplication(text, "pgp-keys", encode_noop) + def getKeysFromMessagePart(self, part): + + """ + Process an application/pgp-keys message 'part', returning a list of + key details. + """ + + return self.getKeysFromString(part.get_payload()) + + def getKeysFromString(self, s): + + "Return a list of key details extracted from the given string 's'." + + text = self.run(["--with-colons", "--with-fingerprint"], s) + keys = [] + + for line in text.split("\n"): + try: + recordtype, trust, keylength, algorithm, keyid, cdate, expdate, serial, ownertrust, _rest = line.split(":", 9) + except ValueError: + break + + if recordtype == "pub": + userid, _rest = _rest.split(":", 1) + keys.append({ + "type" : recordtype, "trust" : trust, "keylength" : keylength, + "algorithm" : algorithm, "keyid" : keyid, "cdate" : cdate, + "expdate" : expdate, "userid" : userid, "ownertrust" : ownertrust, + "fingerprint" : None, "subkeys" : [] + }) + elif recordtype == "sub" and keys: + keys[-1]["subkeys"].append({ + "trust" : trust, "keylength" : keylength, "algorithm" : algorithm, + "keyid" : keyid, "cdate" : cdate, "expdate" : expdate, + "ownertrust" : ownertrust + }) + elif recordtype == "fpr" and keys: + fingerprint, _rest = _rest.split(":", 1) + keys[-1]["fingerprint"] = fingerprint + + return keys + # Message decoding functions. # Detect PGP/GPG-encoded payloads.