]> git.phdru.name Git - mimedecode.git/blobdiff - mimedecode.py
Version 2.2.0. BZipped archive.
[mimedecode.git] / mimedecode.py
index bfee2fe65c0d0c9f001c40c6245b3d9a80c31c17..41bb76ba06f996ebde3dd93e95d41ba688e9e124 100755 (executable)
@@ -1,19 +1,14 @@
-#! /usr/local/bin/python -O
-"""Decode MIME message.
+#! /usr/bin/env python
+"""Decode MIME message"""
 
-Author: Oleg Broytmann <phd@phd.pp.ru>
-Copyright: (C) 2001-2006 PhiloSoft Design
-License: GPL
-"""
 
-_version = "2.1.0"
+_version = "2.2.0"
 __version__ = "$Revision$"[11:-2]
-__date__ = "$Date$"[7:-2]
 __revision__ = "$Id$"[5:-2]
-__author__ = "Oleg Broytmann <phd@phd.pp.ru>"
-__copyright__ = "Copyright (C) 2001-2006 PhiloSoft Design"
+__date__ = "$Date$"[7:-2]
+__author__ = "Oleg Broytman <phd@phdru.name>"
+__copyright__ = "Copyright (C) 2001-2010 PhiloSoft Design"
 __license__ = "GNU GPL"
-__docformat__ = "epytext en"
 
 
 import sys, os
@@ -33,7 +28,7 @@ me = os.path.basename(sys.argv[0])
 
 def version(exit=1):
    sys.stdout.write("""\
-BroytMann mimedecode.py version %s, %s
+Broytman mimedecode.py version %s, %s
 """ % (_version, __copyright__))
    if exit: sys.exit(0)
 
@@ -59,11 +54,11 @@ def output_headers(msg, outfile = sys.stdout):
 
 
 def recode(s, charset):
-   return unicode(s, charset, "replace").encode(GlobalOptions.default_charset, "replace")
+   return unicode(s, charset, "replace").encode(GlobalOptions.default_encoding, "replace")
 
 
 def recode2(s, charset):
-   if charset and charset <> GlobalOptions.default_charset:
+   if charset and charset.lower() <> GlobalOptions.default_encoding:
       s = recode(s, charset)
    return s
 
@@ -197,11 +192,11 @@ def recode_charset(msg, s):
    "Recode charset of the message to the default charset"
 
    save_charset = charset = msg.get_content_charset()
-   if charset and charset <> GlobalOptions.default_charset:
+   if charset and charset.lower() <> GlobalOptions.default_encoding:
       s = recode2(s, charset)
       content_type = msg.get_content_type()
-      set_content_type(msg, content_type, GlobalOptions.default_charset)
-      msg["X-MIME-Autoconverted"] = "from %s to %s by %s id %s" % (save_charset, GlobalOptions.default_charset, host_name, me)
+      set_content_type(msg, content_type, GlobalOptions.default_encoding)
+      msg["X-MIME-Autoconverted"] = "from %s to %s by %s id %s" % (save_charset, GlobalOptions.default_encoding, host_name, me)
    return s
 
 
@@ -296,7 +291,7 @@ def decode_file(infile):
 
 
 class GlobalOptions:
-   default_charset = sys.getdefaultencoding()
+   from m_lib.defenc import default_encoding
    recode_charset = 1 # recode charset of message body
 
    decode_headers = ["Subject", "From"] # A list of headers to decode
@@ -333,7 +328,7 @@ def init():
       elif option == '-C':
          GlobalOptions.recode_charset = 0
       elif option == '-f':
-         GlobalOptions.default_charset = value
+         GlobalOptions.default_encoding = value
       elif option == '-d':
          GlobalOptions.decode_headers.append(value)
       elif option == '-D':
@@ -359,9 +354,10 @@ def init():
 if __name__ == "__main__":
    arguments = init()
 
-   if len(arguments) == 0:
+   la = len(arguments)
+   if la == 0:
       infile = sys.stdin
-   elif len(arguments) <> 1:
+   elif la <> 1:
       usage(1)
    elif arguments[0] == '-':
       infile = sys.stdin