From 1de22d43cdbbb964ea51e8d807ea808b174a24a2 Mon Sep 17 00:00:00 2001 From: Daniel Kahn Gillmor Date: Tue, 10 Sep 2019 12:51:38 -0400 Subject: rename printmimestructure to email-print-mime-structure Signed-off-by: Daniel Kahn Gillmor --- email-print-mime-structure | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100755 email-print-mime-structure (limited to 'email-print-mime-structure') 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 +# 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), '└') -- cgit v1.2.3 From d803d5b2ff13b431d1029f57e7b436fbd6028c89 Mon Sep 17 00:00:00 2001 From: Daniel Kahn Gillmor Date: Tue, 10 Sep 2019 16:00:53 -0400 Subject: update email-print-mime-structure headers to include licensing Signed-off-by: Daniel Kahn Gillmor --- debian/control | 4 ++++ email-print-mime-structure | 39 +++++++++++++++++++++++++-------------- 2 files changed, 29 insertions(+), 14 deletions(-) (limited to 'email-print-mime-structure') diff --git a/debian/control b/debian/control index 8667a6c..6d3a54f 100644 --- a/debian/control +++ b/debian/control @@ -55,3 +55,7 @@ Description: collection of scripts for manipulating e-mail on Debian maildir-import-patch -- import a git patch series into a maildir . notmuch-import-patch -- import a git patch series into notmuch + . + email-print-mime-structure -- tree view of a message's MIME structure + . + email-extract-openpgp-certs -- extract OpenPGP certificates from a message diff --git a/email-print-mime-structure b/email-print-mime-structure index 49e7208..7adeb2b 100755 --- a/email-print-mime-structure +++ b/email-print-mime-structure @@ -1,23 +1,34 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -# Author: Daniel Kahn Gillmor -# License: GPLv3+ - -# This script reads a MIME message from stdin and produces a treelike -# representation on it stdout. - -# Example: +# Copyright (C) 2019 Daniel Kahn Gillmor +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or (at +# your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. # -# 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:~$ +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +''' +This script reads a MIME message from stdin and produces a treelike +representation on it stdout. -# If you want to number the parts, i suggest piping the output through -# something like "cat -n" +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 -- cgit v1.2.3