paul@565 | 1 | #!/usr/bin/env python |
paul@565 | 2 | |
paul@1203 | 3 | """ |
paul@1203 | 4 | Show a MIME-encoded e-mail message as plain text. |
paul@1203 | 5 | |
paul@1203 | 6 | Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@1203 | 7 | |
paul@1203 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@1203 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@1203 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@1203 | 11 | version. |
paul@1203 | 12 | |
paul@1203 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@1203 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@1203 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@1203 | 16 | details. |
paul@1203 | 17 | |
paul@1203 | 18 | You should have received a copy of the GNU General Public License along with |
paul@1203 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@1203 | 20 | """ |
paul@1203 | 21 | |
paul@1051 | 22 | from email import message_from_string |
paul@1153 | 23 | from email.generator import Generator |
paul@1203 | 24 | from os.path import split |
paul@564 | 25 | import sys |
paul@564 | 26 | |
paul@1153 | 27 | try: |
paul@1153 | 28 | from cStringIO import StringIO |
paul@1153 | 29 | except ImportError: |
paul@1153 | 30 | from StringIO import StringIO |
paul@1153 | 31 | |
paul@1153 | 32 | def until_from(f, skip=0): |
paul@1153 | 33 | number = 0 |
paul@1153 | 34 | while number <= skip: |
paul@1153 | 35 | l = [] |
paul@1051 | 36 | s = f.readline() |
paul@1153 | 37 | while s: |
paul@1153 | 38 | l.append(s) |
paul@1153 | 39 | s = f.readline() |
paul@1153 | 40 | if s.startswith("From "): |
paul@1153 | 41 | number += 1 |
paul@1153 | 42 | break |
paul@1153 | 43 | else: |
paul@1153 | 44 | number += 1 |
paul@1051 | 45 | break |
paul@1153 | 46 | if number > skip: |
paul@1153 | 47 | return "".join(l) |
paul@1153 | 48 | else: |
paul@1153 | 49 | return "" |
paul@1153 | 50 | |
paul@1153 | 51 | def as_string(message): |
paul@1153 | 52 | |
paul@1153 | 53 | """ |
paul@1153 | 54 | Return the string representation of 'message', attempting to preserve the |
paul@1153 | 55 | precise original formatting. |
paul@1153 | 56 | """ |
paul@1153 | 57 | |
paul@1153 | 58 | out = StringIO() |
paul@1153 | 59 | generator = Generator(out, False, 0) # disable reformatting measures |
paul@1153 | 60 | generator.flatten(message) |
paul@1153 | 61 | return out.getvalue() |
paul@1051 | 62 | |
paul@564 | 63 | def decode(part): |
paul@1153 | 64 | |
paul@1153 | 65 | """ |
paul@1153 | 66 | Change the transfer encoding on 'part' and its subparts so that a plain text |
paul@1153 | 67 | representation may be displayed. |
paul@1153 | 68 | """ |
paul@1153 | 69 | |
paul@1153 | 70 | payload = part.get_payload(decode=True) |
paul@1153 | 71 | if payload: |
paul@1153 | 72 | encoding = part.get("Content-Transfer-Encoding") |
paul@1153 | 73 | if encoding: |
paul@1153 | 74 | del part["Content-Transfer-Encoding"] |
paul@1153 | 75 | part["Content-Transfer-Encoding"] = "8bit" |
paul@1153 | 76 | part.set_payload(payload) |
paul@564 | 77 | else: |
paul@1153 | 78 | for p in part.get_payload(): |
paul@1153 | 79 | decode(p) |
paul@1153 | 80 | |
paul@1153 | 81 | # Main program. |
paul@564 | 82 | |
paul@1153 | 83 | if __name__ == "__main__": |
paul@1203 | 84 | if len(sys.argv) > 1 and sys.argv[1] == "--help": |
paul@1203 | 85 | print >>sys.stderr, """\ |
paul@1203 | 86 | Usage: %s [ <messages to skip> ] |
paul@1203 | 87 | |
paul@1203 | 88 | Read MIME-encoded e-mail messages from standard input, writing the decoded |
paul@1203 | 89 | messages to standard output. |
paul@1203 | 90 | |
paul@1203 | 91 | Where <messages to skip> is given, the indicated number of messages will be |
paul@1203 | 92 | skipped in the input, with the first subsequent message being written out. |
paul@1203 | 93 | """ % split(sys.argv[0])[1] |
paul@1203 | 94 | sys.exit(1) |
paul@1203 | 95 | |
paul@1153 | 96 | skip = int((sys.argv[1:] or [0])[0]) |
paul@1153 | 97 | message = message_from_string(until_from(sys.stdin, skip)) |
paul@1153 | 98 | decode(message) |
paul@1153 | 99 | print as_string(message) |
paul@572 | 100 | |
paul@572 | 101 | # vim: tabstop=4 expandtab shiftwidth=4 |