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"
25 def test(z, prefix=''):
26 fname = '' if z.get_filename() is None else ' [' + z.get_filename() + ']'
27 cset = '' if z.get_charset() is None else ' (' + z.get_charset() + ')'
28 disp = z.get_params(None, header='Content-Disposition')
34 if d[0] in [ 'attachment', 'inline' ]:
35 disposition = ' ' + d[0]
36 if (z.is_multipart()):
37 print prefix + '┬╴' + z.get_content_type() + cset + disposition + fname, z.as_string().__len__().__str__() + ' bytes'
38 if prefix.endswith('└'):
39 prefix = prefix.rpartition('└')[0] + ' '
40 if prefix.endswith('├'):
41 prefix = prefix.rpartition('├')[0] + '│'
42 parts = z.get_payload()
44 while (i < parts.__len__()-1):
45 test(parts[i], prefix + '├')
47 test(parts[i], prefix + '└')
48 # FIXME: show epilogue?
50 print prefix + '─╴'+ z.get_content_type() + cset + disposition + fname, z.get_payload().__len__().__str__(), 'bytes'
52 test(email.message_from_file(sys.stdin), '└')