Fixed email.Parser typo in send_pgp_mime.py imports for Python 2.4.
[pgp-mime.git] / interfaces / email / interactive / send_pgp_mime.py
index 3a60013f4742849769db3adaa7a0a97c4d9d0caf..c19483ef157f609ae1178708f749cd8436b57c1e 100644 (file)
@@ -51,7 +51,7 @@ except ImportError:
     from email.MIMENonMultipart import MIMENonMultipart
     from email.Encoders import encode_7or8bit
     from email.Generator import Generator
-    from email.parser import Parser
+    from email.Parser import Parser
     from email.Utils import getaddresses
 
     getaddress = getaddresses
@@ -153,6 +153,35 @@ def header_from_text(text, encoding="us-ascii"):
     p = Parser()
     return p.parsestr(text, headersonly=True)
 
+def guess_encoding(text):
+    if type(text) == types.StringType:
+        encoding = "us-ascii"
+    elif type(text) == types.UnicodeType:
+        for encoding in ["us-ascii", "iso-8859-1", "utf-8"]:
+            try:
+                text.encode(encoding)
+            except UnicodeError:
+                pass
+            else:
+                break
+        assert encoding != None
+    return encoding
+
+def encodedMIMEText(body, encoding=None):
+    if encoding == None:
+        encoding = guess_encoding(body)
+    if encoding == "us-ascii":
+        return MIMEText(body)
+    else:
+        # Create the message ('plain' stands for Content-Type: text/plain)
+        return MIMEText(body.encode(encoding), 'plain', encoding)
+
+def append_text(text_part, new_text):
+    original_payload = text_part.get_payload(decode=True)
+    new_payload = u"%s%s" % (original_payload, new_text)
+    new_encoding = guess_encoding(new_payload)
+    text_part.set_payload(new_payload.encode(new_encoding), new_encoding)
+
 def attach_root(header, root_part):
     """
     Attach the email.Message root_part to the email.Message header
@@ -212,7 +241,7 @@ def flatten(msg, to_unicode=False):
     g.flatten(msg)
     text = fp.getvalue()
     if to_unicode == True:
-        encoding = msg.get_content_charset()
+        encoding = msg.get_content_charset() or "utf-8"
         text = unicode(text, encoding=encoding)
     return text
 
@@ -253,7 +282,7 @@ class PGPMimeMessageFactory (object):
     True
     >>> target_emails(header) == [to_addr]
     True
-    >>> m = EncryptedMessageFactory('check 1 2\\ncheck 1 2\\n')
+    >>> m = PGPMimeMessageFactory('check 1 2\\ncheck 1 2\\n')
     >>> print flatten(m.clearBodyPart())
     Content-Type: text/plain; charset="us-ascii"
     MIME-Version: 1.0
@@ -355,26 +384,8 @@ class PGPMimeMessageFactory (object):
     """
     def __init__(self, body):
         self.body = body
-    def encodedMIMEText(self, body, encoding=None):
-        if encoding == None:
-            if type(body) == types.StringType:
-                encoding = "us-ascii"
-            elif type(body) == types.UnicodeType:
-                for encoding in ["us-ascii", "iso-8859-1", "utf-8"]:
-                    try:
-                        body.encode(encoding)
-                    except UnicodeError:
-                        pass
-                    else:
-                        break
-                assert encoding != None
-        # Create the message ('plain' stands for Content-Type: text/plain)
-        if encoding == "us-ascii":
-            return MIMEText(body)
-        else:
-            return MIMEText(body.encode(encoding), 'plain', encoding)
     def clearBodyPart(self):
-        body = self.encodedMIMEText(self.body)
+        body = encodedMIMEText(self.body)
         body.add_header('Content-Disposition', 'inline')
         return body
     def passphrase_arg(self, passphrase=None):
@@ -387,7 +398,7 @@ class PGPMimeMessageFactory (object):
         """
         text/plain
         """
-        return self.encodedMIMEText(self.body)
+        return encodedMIMEText(self.body)
     def sign(self, header, passphrase=None):
         """
         multipart/signed
@@ -580,7 +591,7 @@ if __name__ == '__main__':
     if body == None:
         raise Exception, "missing body"
 
-    m = EncryptedMessageFactory(body)
+    m = PGPMimeMessageFactory(body)
     if options.mode == "sign":
         bodymsg = m.sign(header)
     elif options.mode == "encrypt":