1.1 --- a/tools/showmail.py Sat Sep 30 01:27:19 2017 +0200
1.2 +++ b/tools/showmail.py Sun Oct 01 15:06:09 2017 +0200
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 Show a MIME-encoded e-mail message as plain text.
1.6
1.7 -Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This program is free software; you can redistribute it and/or modify it under
1.11 the terms of the GNU General Public License as published by the Free Software
1.12 @@ -20,14 +20,19 @@
1.13 """
1.14
1.15 from email import message_from_string
1.16 -from email.generator import Generator
1.17 -from os.path import split
1.18 +from os.path import abspath, split
1.19 import sys
1.20
1.21 +# Find the modules.
1.22 +
1.23 try:
1.24 - from cStringIO import StringIO
1.25 + import imiptools
1.26 except ImportError:
1.27 - from StringIO import StringIO
1.28 + parent = abspath(split(split(__file__)[0])[0])
1.29 + if split(parent)[1] == "imip-agent":
1.30 + sys.path.append(parent)
1.31 +
1.32 +from imiptools.utils import decode_part, message_as_string
1.33
1.34 def until_from(f, skip=0):
1.35 number = 0
1.36 @@ -48,36 +53,6 @@
1.37 else:
1.38 return ""
1.39
1.40 -def as_string(message):
1.41 -
1.42 - """
1.43 - Return the string representation of 'message', attempting to preserve the
1.44 - precise original formatting.
1.45 - """
1.46 -
1.47 - out = StringIO()
1.48 - generator = Generator(out, False, 0) # disable reformatting measures
1.49 - generator.flatten(message)
1.50 - return out.getvalue()
1.51 -
1.52 -def decode(part):
1.53 -
1.54 - """
1.55 - Change the transfer encoding on 'part' and its subparts so that a plain text
1.56 - representation may be displayed.
1.57 - """
1.58 -
1.59 - payload = part.get_payload(decode=True)
1.60 - if payload:
1.61 - encoding = part.get("Content-Transfer-Encoding")
1.62 - if encoding:
1.63 - del part["Content-Transfer-Encoding"]
1.64 - part["Content-Transfer-Encoding"] = "8bit"
1.65 - part.set_payload(payload)
1.66 - else:
1.67 - for p in part.get_payload():
1.68 - decode(p)
1.69 -
1.70 # Main program.
1.71
1.72 if __name__ == "__main__":
1.73 @@ -95,7 +70,7 @@
1.74
1.75 skip = int((sys.argv[1:] or [0])[0])
1.76 message = message_from_string(until_from(sys.stdin, skip))
1.77 - decode(message)
1.78 - print as_string(message)
1.79 + decode_part(message)
1.80 + print message_as_string(message)
1.81
1.82 # vim: tabstop=4 expandtab shiftwidth=4