Fred Drake | fcc31b4 | 2002-10-01 14:17:10 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3 | """Unpack a MIME message into a directory of files.""" |
Fred Drake | fcc31b4 | 2002-10-01 14:17:10 +0000 | [diff] [blame] | 4 | |
Fred Drake | fcc31b4 | 2002-10-01 14:17:10 +0000 | [diff] [blame] | 5 | import os |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6 | import sys |
| 7 | import email |
Fred Drake | fcc31b4 | 2002-10-01 14:17:10 +0000 | [diff] [blame] | 8 | import errno |
| 9 | import mimetypes |
Fred Drake | fcc31b4 | 2002-10-01 14:17:10 +0000 | [diff] [blame] | 10 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11 | from optparse import OptionParser |
Fred Drake | fcc31b4 | 2002-10-01 14:17:10 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | def main(): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15 | parser = OptionParser(usage="""\ |
| 16 | Unpack a MIME message into a directory of files. |
Fred Drake | fcc31b4 | 2002-10-01 14:17:10 +0000 | [diff] [blame] | 17 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 18 | Usage: %prog [options] msgfile |
| 19 | """) |
| 20 | parser.add_option('-d', '--directory', |
| 21 | type='string', action='store', |
| 22 | help="""Unpack the MIME message into the named |
| 23 | directory, which will be created if it doesn't already |
| 24 | exist.""") |
| 25 | opts, args = parser.parse_args() |
| 26 | if not opts.directory: |
| 27 | parser.print_help() |
| 28 | sys.exit(1) |
Fred Drake | fcc31b4 | 2002-10-01 14:17:10 +0000 | [diff] [blame] | 29 | |
| 30 | try: |
| 31 | msgfile = args[0] |
| 32 | except IndexError: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 33 | parser.print_help() |
| 34 | sys.exit(1) |
Fred Drake | fcc31b4 | 2002-10-01 14:17:10 +0000 | [diff] [blame] | 35 | |
| 36 | try: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 37 | os.mkdir(opts.directory) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 38 | except OSError as e: |
Fred Drake | fcc31b4 | 2002-10-01 14:17:10 +0000 | [diff] [blame] | 39 | # Ignore directory exists error |
Neal Norwitz | 3bd844e | 2006-08-29 04:39:12 +0000 | [diff] [blame] | 40 | if e.errno != errno.EEXIST: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 41 | raise |
Fred Drake | fcc31b4 | 2002-10-01 14:17:10 +0000 | [diff] [blame] | 42 | |
| 43 | fp = open(msgfile) |
| 44 | msg = email.message_from_file(fp) |
| 45 | fp.close() |
| 46 | |
| 47 | counter = 1 |
| 48 | for part in msg.walk(): |
| 49 | # multipart/* are just containers |
| 50 | if part.get_content_maintype() == 'multipart': |
| 51 | continue |
| 52 | # Applications should really sanitize the given filename so that an |
| 53 | # email message can't be used to overwrite important files |
| 54 | filename = part.get_filename() |
| 55 | if not filename: |
| 56 | ext = mimetypes.guess_extension(part.get_type()) |
| 57 | if not ext: |
| 58 | # Use a generic bag-of-bits extension |
| 59 | ext = '.bin' |
| 60 | filename = 'part-%03d%s' % (counter, ext) |
| 61 | counter += 1 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 62 | fp = open(os.path.join(opts.directory, filename), 'wb') |
| 63 | fp.write(part.get_payload(decode=True)) |
Fred Drake | fcc31b4 | 2002-10-01 14:17:10 +0000 | [diff] [blame] | 64 | fp.close() |
| 65 | |
| 66 | |
| 67 | if __name__ == '__main__': |
| 68 | main() |