# HG changeset patch # User Paul Boddie # Date 1506863169 -7200 # Node ID 962ff52d7755a6d6d1140ec460989e22abd0cb64 # Parent d2d07acdcfda6a7bb90bd795b1033aacea178535 Moved various utility functions into a separate module. diff -r d2d07acdcfda -r 962ff52d7755 imiptools/utils.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/imiptools/utils.py Sun Oct 01 15:06:09 2017 +0200 @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +""" +Mail-related utilities. + +Copyright (C) 2015, 2016, 2017 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +from email.generator import Generator + +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO + +def message_as_string(message): + + """ + Return the string representation of 'message', attempting to preserve the + precise original formatting. + """ + + out = StringIO() + generator = Generator(out, False, 0) # disable reformatting measures + generator.flatten(message) + return out.getvalue() + +def decode_part(part): + + """ + Change the transfer encoding on 'part' and its subparts so that a plain text + representation may be displayed. + """ + + payload = part.get_payload(decode=True) + if payload: + encoding = part.get("Content-Transfer-Encoding") + if encoding: + del part["Content-Transfer-Encoding"] + part["Content-Transfer-Encoding"] = "8bit" + part.set_payload(payload) + else: + for p in part.get_payload(): + decode_part(p) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r d2d07acdcfda -r 962ff52d7755 tools/showmail.py --- a/tools/showmail.py Sat Sep 30 01:27:19 2017 +0200 +++ b/tools/showmail.py Sun Oct 01 15:06:09 2017 +0200 @@ -3,7 +3,7 @@ """ Show a MIME-encoded e-mail message as plain text. -Copyright (C) 2015, 2016 Paul Boddie +Copyright (C) 2015, 2016, 2017 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -20,14 +20,19 @@ """ from email import message_from_string -from email.generator import Generator -from os.path import split +from os.path import abspath, split import sys +# Find the modules. + try: - from cStringIO import StringIO + import imiptools except ImportError: - from StringIO import StringIO + parent = abspath(split(split(__file__)[0])[0]) + if split(parent)[1] == "imip-agent": + sys.path.append(parent) + +from imiptools.utils import decode_part, message_as_string def until_from(f, skip=0): number = 0 @@ -48,36 +53,6 @@ else: return "" -def as_string(message): - - """ - Return the string representation of 'message', attempting to preserve the - precise original formatting. - """ - - out = StringIO() - generator = Generator(out, False, 0) # disable reformatting measures - generator.flatten(message) - return out.getvalue() - -def decode(part): - - """ - Change the transfer encoding on 'part' and its subparts so that a plain text - representation may be displayed. - """ - - payload = part.get_payload(decode=True) - if payload: - encoding = part.get("Content-Transfer-Encoding") - if encoding: - del part["Content-Transfer-Encoding"] - part["Content-Transfer-Encoding"] = "8bit" - part.set_payload(payload) - else: - for p in part.get_payload(): - decode(p) - # Main program. if __name__ == "__main__": @@ -95,7 +70,7 @@ skip = int((sys.argv[1:] or [0])[0]) message = message_from_string(until_from(sys.stdin, skip)) - decode(message) - print as_string(message) + decode_part(message) + print message_as_string(message) # vim: tabstop=4 expandtab shiftwidth=4