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