X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=mimedecode.py;h=e0562967b472db703018925ad9392101e36053b3;hb=ada671b5f9a53d6d2c0b5082354d53de5a5fe999;hp=c0a9ef3ac76371defeff55e2f53d4c3ab047c3c7;hpb=27550cf12d5e7e50f882e53e5e5a632e3758c667;p=mimedecode.git diff --git a/mimedecode.py b/mimedecode.py index c0a9ef3..e056296 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -1,13 +1,11 @@ -#! /usr/local/bin/python -O -"""Decode MIME message. +#! /usr/bin/env python +"""Decode MIME message""" -Author: Oleg Broytmann -Copyright: (C) 2001-2002 PhiloSoft Design -License: GPL -""" -__version__ = "2.0.0" -__copyright__ = "Copyright (C) 2001-2002 PhiloSoft Design" +__version__ = "2.1.2" +__author__ = "Oleg Broytman " +__copyright__ = "Copyright (C) 2001-2013 PhiloSoft Design" +__license__ = "GNU GPL" import sys, os @@ -27,8 +25,8 @@ me = os.path.basename(sys.argv[0]) def version(exit=1): sys.stdout.write("""\ -BroytMann mimedecode.py version %s, %s -""" % (__version__, __copyright__)) +Broytman mimedecode.py version %s, %s +""" % (_version, __copyright__)) if exit: sys.exit(0) @@ -53,11 +51,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 @@ -191,11 +189,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 @@ -290,7 +288,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 @@ -327,7 +325,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': @@ -353,9 +351,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