1 #!/usr/bin/env python 2 3 """ 4 Show a MIME-encoded e-mail message as plain text. 5 6 Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from email import message_from_string 23 from os.path import abspath, split 24 import sys 25 26 # Find the modules. 27 28 try: 29 import imiptools 30 except ImportError: 31 parent = abspath(split(split(__file__)[0])[0]) 32 if split(parent)[1] == "imip-agent": 33 sys.path.append(parent) 34 35 from imiptools.utils import decode_part, message_as_string 36 37 def until_from(f, skip=0): 38 number = 0 39 while number <= skip: 40 l = [] 41 s = f.readline() 42 while s: 43 l.append(s) 44 s = f.readline() 45 if s.startswith("From "): 46 number += 1 47 break 48 else: 49 number += 1 50 break 51 if number > skip: 52 return "".join(l) 53 else: 54 return "" 55 56 # Main program. 57 58 if __name__ == "__main__": 59 if len(sys.argv) > 1 and sys.argv[1] == "--help": 60 print >>sys.stderr, """\ 61 Usage: %s [ <messages to skip> ] 62 63 Read MIME-encoded e-mail messages from standard input, writing the decoded 64 messages to standard output. 65 66 Where <messages to skip> is given, the indicated number of messages will be 67 skipped in the input, with the first subsequent message being written out. 68 """ % split(sys.argv[0])[1] 69 sys.exit(1) 70 71 skip = int((sys.argv[1:] or [0])[0]) 72 message = message_from_string(until_from(sys.stdin, skip)) 73 decode_part(message) 74 print message_as_string(message) 75 76 # vim: tabstop=4 expandtab shiftwidth=4