paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | from email.mime.multipart import MIMEMultipart |
paul@1 | 4 | from email.mime.application import MIMEApplication |
paul@1 | 5 | from email.mime.base import MIMEBase |
paul@1 | 6 | from email.encoders import encode_noop |
paul@3 | 7 | from email import message_from_string |
paul@0 | 8 | import httplib |
paul@0 | 9 | import sys |
paul@0 | 10 | |
paul@0 | 11 | if __name__ == "__main__": |
paul@0 | 12 | host = sys.argv[1] |
paul@0 | 13 | path = sys.argv[2] + "?action=PostMessage" |
paul@0 | 14 | |
paul@3 | 15 | try: |
paul@3 | 16 | message = sys.argv[3] |
paul@3 | 17 | text = open(message).read() |
paul@3 | 18 | signature = sys.stdin.read() |
paul@3 | 19 | protocol = "application/pgp-signature" |
paul@3 | 20 | subtype = "signed" |
paul@3 | 21 | except IndexError: |
paul@3 | 22 | text = sys.stdin.read() |
paul@3 | 23 | signature = None |
paul@3 | 24 | protocol = "application/pgp-encrypted" |
paul@3 | 25 | subtype = "encrypted" |
paul@3 | 26 | |
paul@3 | 27 | # Make the container for the message. |
paul@3 | 28 | |
paul@3 | 29 | message = MIMEMultipart(subtype, protocol=protocol) |
paul@3 | 30 | |
paul@3 | 31 | # For encrypted content, add the declaration and content. |
paul@0 | 32 | |
paul@3 | 33 | if not signature: |
paul@3 | 34 | declaration = MIMEBase("application", "pgp-encrypted") |
paul@3 | 35 | declaration.set_payload("Version: 1") |
paul@3 | 36 | message.attach(declaration) |
paul@3 | 37 | |
paul@3 | 38 | content = MIMEApplication(text, "octet-stream", encode_noop) |
paul@3 | 39 | message.attach(content) |
paul@3 | 40 | |
paul@3 | 41 | # For signed content, |
paul@0 | 42 | |
paul@3 | 43 | else: |
paul@3 | 44 | submessage = message_from_string(text) |
paul@3 | 45 | message.attach(submessage) |
paul@3 | 46 | |
paul@3 | 47 | signature_part = MIMEBase("application", "pgp-signature") |
paul@3 | 48 | signature_part.set_payload(signature) |
paul@3 | 49 | message.attach(signature_part) |
paul@3 | 50 | |
paul@3 | 51 | # Show the resulting message text. |
paul@0 | 52 | |
paul@0 | 53 | text = message.as_string() |
paul@1 | 54 | |
paul@0 | 55 | print text |
paul@0 | 56 | print |
paul@0 | 57 | |
paul@0 | 58 | req = httplib.HTTPConnection(host) |
paul@0 | 59 | req.request("PUT", path, text) # {"Content-Length" : len(text)} |
paul@0 | 60 | resp = req.getresponse() |
paul@0 | 61 | print resp.read() |
paul@0 | 62 | |
paul@0 | 63 | # vim: tabstop=4 expandtab shiftwidth=4 |