2 # -*- coding: utf-8 -*-
4 # Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
7 # This script reads a MIME message from stdin and produces a treelike
8 # representation on it stdout.
12 # 0 dkg@alice:~$ printmimestructure < 'Maildir/cur/1269025522.M338697P12023.monkey,S=6459,W=6963:2,Sa'
13 # └┬╴multipart/signed 6546 bytes
14 # ├─╴text/plain inline 895 bytes
15 # └─╴application/pgp-signature inline [signature.asc] 836 bytes
19 # If you want to number the parts, i suggest piping the output through
20 # something like "cat -n"
22 from __future__ import print_function
27 def print_part(z, prefix):
28 fname = '' if z.get_filename() is None else ' [' + z.get_filename() + ']'
29 cset = '' if z.get_charset() is None else ' (' + z.get_charset() + ')'
30 disp = z.get_params(None, header='Content-Disposition')
36 if d[0] in [ 'attachment', 'inline' ]:
37 disposition = ' ' + d[0]
39 nbytes = len(z.as_string())
41 nbytes = len(z.get_payload())
43 print('{}{}{}{}{} {:d} bytes'.format(
52 def test(z, prefix=''):
53 if (z.is_multipart()):
54 print_part(z, prefix+'┬╴')
55 if prefix.endswith('└'):
56 prefix = prefix.rpartition('└')[0] + ' '
57 if prefix.endswith('├'):
58 prefix = prefix.rpartition('├')[0] + '│'
59 parts = z.get_payload()
61 while (i < parts.__len__()-1):
62 test(parts[i], prefix + '├')
64 test(parts[i], prefix + '└')
65 # FIXME: show epilogue?
67 print_part(z, prefix+'─╴')
69 test(email.message_from_file(sys.stdin), '└')