X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=mimedecode.py;h=41bb76ba06f996ebde3dd93e95d41ba688e9e124;hb=4f1fba5115cee2ffc2ab769511414a5613d507d5;hp=e596f1d2e681ee4385bd1253293b2ecadd57114b;hpb=1bb47d64c37679695d6a7e43b2cc9e162127a765;p=mimedecode.git diff --git a/mimedecode.py b/mimedecode.py index e596f1d..41bb76b 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -1,18 +1,18 @@ -#! /usr/local/bin/python -O +#! /usr/bin/env python """Decode MIME message""" -_version = "2.1.0" + +_version = "2.2.0" __version__ = "$Revision$"[11:-2] -__date__ = "$Date$"[7:-2] __revision__ = "$Id$"[5:-2] -__author__ = "Oleg Broytmann " -__copyright__ = "Copyright (C) 2001-2006 PhiloSoft Design" +__date__ = "$Date$"[7:-2] +__author__ = "Oleg Broytman " +__copyright__ = "Copyright (C) 2001-2010 PhiloSoft Design" __license__ = "GNU GPL" import sys, os import email -import locale try: from cStringIO import StringIO @@ -28,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) @@ -54,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 @@ -192,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 @@ -291,22 +291,7 @@ def decode_file(infile): class GlobalOptions: - # Get a 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() - + 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 @@ -343,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': @@ -369,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