diff options
author | Daniel Kahn Gillmor <dkg@fifthhorseman.net> | 2019-09-10 12:51:38 -0400 |
---|---|---|
committer | Daniel Kahn Gillmor <dkg@fifthhorseman.net> | 2019-09-14 14:58:06 -0400 |
commit | 1de22d43cdbbb964ea51e8d807ea808b174a24a2 (patch) | |
tree | 22edfeba666683ba98c5d646cfb6aec7a16624a0 /email-print-mime-structure | |
parent | 37946242391b530d01da277918e4531f3855c875 (diff) | |
download | mailscripts-1de22d43cdbbb964ea51e8d807ea808b174a24a2.tar.gz |
rename printmimestructure to email-print-mime-structure
Signed-off-by: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
Diffstat (limited to 'email-print-mime-structure')
-rwxr-xr-x | email-print-mime-structure | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/email-print-mime-structure b/email-print-mime-structure new file mode 100755 index 0000000..49e7208 --- /dev/null +++ b/email-print-mime-structure @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net> +# License: GPLv3+ + +# This script reads a MIME message from stdin and produces a treelike +# representation on it stdout. + +# Example: +# +# 0 dkg@alice:~$ printmimestructure < 'Maildir/cur/1269025522.M338697P12023.monkey,S=6459,W=6963:2,Sa' +# └┬╴multipart/signed 6546 bytes +# ├─╴text/plain inline 895 bytes +# └─╴application/pgp-signature inline [signature.asc] 836 bytes +# 0 dkg@alice:~$ + +# If you want to number the parts, i suggest piping the output through +# something like "cat -n" + +import email +import sys + +def print_part(z, prefix): + fname = '' if z.get_filename() is None else ' [' + z.get_filename() + ']' + cset = '' if z.get_charset() is None else ' (' + z.get_charset() + ')' + disp = z.get_params(None, header='Content-Disposition') + if (disp is None): + disposition = '' + else: + disposition = '' + for d in disp: + if d[0] in [ 'attachment', 'inline' ]: + disposition = ' ' + d[0] + if z.is_multipart(): + nbytes = len(z.as_string()) + else: + nbytes = len(z.get_payload()) + + print('{}{}{}{}{} {:d} bytes'.format( + prefix, + z.get_content_type(), + cset, + disposition, + fname, + nbytes, + )) + +def test(z, prefix=''): + if (z.is_multipart()): + print_part(z, prefix+'┬╴') + if prefix.endswith('└'): + prefix = prefix.rpartition('└')[0] + ' ' + if prefix.endswith('├'): + prefix = prefix.rpartition('├')[0] + '│' + parts = z.get_payload() + i = 0 + while (i < parts.__len__()-1): + test(parts[i], prefix + '├') + i += 1 + test(parts[i], prefix + '└') + # FIXME: show epilogue? + else: + print_part(z, prefix+'─╴') + +test(email.message_from_file(sys.stdin), '└') |