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@1279 | 6 | Copyright (C) 2015, 2016, 2017 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@1279 | 23 | from os.path import abspath, split |
paul@564 | 24 | import sys |
paul@564 | 25 | |
paul@1279 | 26 | # Find the modules. |
paul@1279 | 27 | |
paul@1153 | 28 | try: |
paul@1279 | 29 | import imiptools |
paul@1153 | 30 | except ImportError: |
paul@1279 | 31 | parent = abspath(split(split(__file__)[0])[0]) |
paul@1279 | 32 | if split(parent)[1] == "imip-agent": |
paul@1279 | 33 | sys.path.append(parent) |
paul@1279 | 34 | |
paul@1279 | 35 | from imiptools.utils import decode_part, message_as_string |
paul@1153 | 36 | |
paul@1153 | 37 | def until_from(f, skip=0): |
paul@1153 | 38 | number = 0 |
paul@1153 | 39 | while number <= skip: |
paul@1153 | 40 | l = [] |
paul@1051 | 41 | s = f.readline() |
paul@1153 | 42 | while s: |
paul@1153 | 43 | l.append(s) |
paul@1153 | 44 | s = f.readline() |
paul@1153 | 45 | if s.startswith("From "): |
paul@1153 | 46 | number += 1 |
paul@1153 | 47 | break |
paul@1153 | 48 | else: |
paul@1153 | 49 | number += 1 |
paul@1051 | 50 | break |
paul@1153 | 51 | if number > skip: |
paul@1153 | 52 | return "".join(l) |
paul@1153 | 53 | else: |
paul@1153 | 54 | return "" |
paul@1153 | 55 | |
paul@1153 | 56 | # Main program. |
paul@564 | 57 | |
paul@1153 | 58 | if __name__ == "__main__": |
paul@1203 | 59 | if len(sys.argv) > 1 and sys.argv[1] == "--help": |
paul@1203 | 60 | print >>sys.stderr, """\ |
paul@1203 | 61 | Usage: %s [ <messages to skip> ] |
paul@1203 | 62 | |
paul@1203 | 63 | Read MIME-encoded e-mail messages from standard input, writing the decoded |
paul@1203 | 64 | messages to standard output. |
paul@1203 | 65 | |
paul@1203 | 66 | Where <messages to skip> is given, the indicated number of messages will be |
paul@1203 | 67 | skipped in the input, with the first subsequent message being written out. |
paul@1203 | 68 | """ % split(sys.argv[0])[1] |
paul@1203 | 69 | sys.exit(1) |
paul@1203 | 70 | |
paul@1153 | 71 | skip = int((sys.argv[1:] or [0])[0]) |
paul@1153 | 72 | message = message_from_string(until_from(sys.stdin, skip)) |
paul@1279 | 73 | decode_part(message) |
paul@1279 | 74 | print message_as_string(message) |
paul@572 | 75 | |
paul@572 | 76 | # vim: tabstop=4 expandtab shiftwidth=4 |