summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Kahn Gillmor <dkg@fifthhorseman.net>2013-05-20 12:59:19 -0300
committerDaniel Kahn Gillmor <dkg@fifthhorseman.net>2019-09-14 14:58:06 -0400
commitdc1d376ae6881c364142899d20ca0053b65039da (patch)
tree476597edbbbc97ac974a9cfbf69d55ac8e99541a
parent1cb54e1d5000856bfedd98c5cf4274aa411e44c3 (diff)
downloadmailscripts-dc1d376ae6881c364142899d20ca0053b65039da.tar.gz
devel: add printmimestructure script (from notmuch devel scripts)
David Bremner writes: I find this script pretty useful when figuring out who to blame for MIME rendering problems. The notmuch repo will be the new primary home for this script, unless and until a better home turns up. Signed-off-by: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
-rwxr-xr-xprintmimestructure52
1 files changed, 52 insertions, 0 deletions
diff --git a/printmimestructure b/printmimestructure
new file mode 100755
index 0000000..34d1293
--- /dev/null
+++ b/printmimestructure
@@ -0,0 +1,52 @@
+#!/usr/bin/env python
+# -*- 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 test(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()):
+ print prefix + '┬╴' + z.get_content_type() + cset + disposition + fname, z.as_string().__len__().__str__() + ' bytes'
+ 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 prefix + '─╴'+ z.get_content_type() + cset + disposition + fname, z.get_payload().__len__().__str__(), 'bytes'
+
+test(email.message_from_file(sys.stdin), '└')