]> git.phdru.name Git - mimedecode.git/blobdiff - mimedecode.py
Replace phd2.chat.ru mirror with phd.webhost.ru.
[mimedecode.git] / mimedecode.py
index c0a9ef3ac76371defeff55e2f53d4c3ab047c3c7..f819b84c62e3decd62374b021e7b3b41f80946be 100755 (executable)
@@ -1,18 +1,25 @@
 #! /usr/local/bin/python -O
-"""Decode MIME message.
+"""Decode MIME message"""
 
-Author: Oleg Broytmann <phd@phd.pp.ru>
-Copyright: (C) 2001-2002 PhiloSoft Design
-License: GPL
-"""
 
-__version__ = "2.0.0"
-__copyright__ = "Copyright (C) 2001-2002 PhiloSoft Design"
+_version = "2.1.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"
+__license__ = "GNU GPL"
 
 
 import sys, os
 import email
 
+try:
+   import locale
+   use_locale = True
+except ImportError:
+   use_locale = False
+
 try:
    from cStringIO import StringIO
 except ImportError:
@@ -28,7 +35,7 @@ me = os.path.basename(sys.argv[0])
 def version(exit=1):
    sys.stdout.write("""\
 BroytMann mimedecode.py version %s, %s
-""" % (__version__, __copyright__))
+""" % (_version, __copyright__))
    if exit: sys.exit(0)
 
 
@@ -57,7 +64,7 @@ def recode(s, charset):
 
 
 def recode2(s, charset):
-   if charset and charset <> GlobalOptions.default_charset:
+   if charset and charset.lower() <> GlobalOptions.default_charset:
       s = recode(s, charset)
    return s
 
@@ -191,7 +198,7 @@ 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_charset:
       s = recode2(s, charset)
       content_type = msg.get_content_type()
       set_content_type(msg, content_type, GlobalOptions.default_charset)
@@ -290,7 +297,27 @@ def decode_file(infile):
 
 
 class GlobalOptions:
-   default_charset = sys.getdefaultencoding()
+   if use_locale:
+      # Get the default charset.
+      try:
+         lcAll = locale.setlocale(locale.LC_ALL, '').split('.')
+      except locale.Error, err:
+         print >> sys.stderr, "WARNING:", err
+         lcAll = []
+
+      if len(lcAll) == 2:
+         default_charset = lcAll[1]
+      else:
+         try:
+            default_charset = locale.getpreferredencoding()
+         except locale.Error, err:
+            print >> sys.stderr, "WARNING:", err
+            default_charset = sys.getdefaultencoding()
+   else:
+      default_charset = sys.getdefaultencoding()
+
+   default_charset = default_charset.lower()
+
    recode_charset = 1 # recode charset of message body
 
    decode_headers = ["Subject", "From"] # A list of headers to decode
@@ -353,9 +380,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