summaryrefslogtreecommitdiff
path: root/email-print-mime-structure
blob: 4f165b198db3916c6999d7c14fb469aab93474d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK
# -*- coding: utf-8 -*-

# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
'''
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 os
import sys
import email
import logging
import subprocess

from argparse import ArgumentParser, Namespace
from typing import Optional, Union, List, Tuple, Any
from email.charset import Charset
from email.message import Message

try:
    import pgpy #type: ignore
except ImportError:
    pgpy = None

try:
    import argcomplete #type: ignore
except ImportError:
    argcomplete = None

class MimePrinter(object):
    def __init__(self, args:Namespace):
        self.args = args

    def print_part(self, z:Message, prefix:str, parent:Optional[Message], num:int) -> None:
        ofname:Optional[str] = z.get_filename()
        fname:str = '' if ofname is None else f' [{ofname}]'
        ocharset:Union[Charset, str, None] = z.get_charset()
        cset:str = '' if ocharset is None else f' ({ocharset})'
        disp:Union[List[Tuple[str,str]], List[str], None] = z.get_params(None, header='Content-Disposition')
        disposition:str = ''
        if (disp is not None):
            for d in disp:
                if d[0] in [ 'attachment', 'inline' ]:
                    disposition = ' ' + d[0]
        nbytes:int
        if z.is_multipart():
            # FIXME: it looks like we are counting chars here, not bytes:
            nbytes = len(z.as_string())
        else:
            payload:Union[List[Message], str, bytes, None] = z.get_payload()
            if not isinstance(payload, (str,bytes)):
                raise TypeError(f'expected payload to be either str or bytes, got {type(payload)}')
            # FIXME: it looks like we are counting chars here, not bytes:
            nbytes = len(payload)

        print(f'{prefix}{z.get_content_type()}{cset}{disposition}{fname} {nbytes:d} bytes')
        try_decrypt:bool = self.args.pgpkey or self.args.use_gpg_agent

        if try_decrypt and \
           (parent is not None) and \
           (parent.get_content_type().lower() == 'multipart/encrypted') and \
           (str(parent.get_param('protocol')).lower() == 'application/pgp-encrypted') and \
           (num == 2):
            cryptopayload:Optional[Message] = None
            ciphertext:Union[List[Message],str,bytes,None] = z.get_payload()
            if not isinstance(ciphertext, str):
                logging.warning('encrypted part was not a leaf mime part somehow')
                return
            if self.args.pgpkey:
                cryptopayload = self.pgpy_decrypt(self.args.pgpkey, ciphertext)
            if cryptopayload is None and self.args.use_gpg_agent:
                cryptopayload = self.gpg_decrypt(ciphertext)
            if cryptopayload is None:
                logging.warning(f'Unable to decrypt')
                return
            newprefix = prefix[:-3] + ' '
            print(f'{newprefix}↧ (decrypts to)')
            self.print_tree(cryptopayload, newprefix + '└', z, 0)

    def pgpy_decrypt(self, keys:List[str], ciphertext:str) -> Optional[Message]:
        if pgpy is None:
            logging.warning(f'Python module pgpy is not available, not decrypting (try "apt install python3-pgpy")')
            return None
        keyname:str
        ret:Optional[Message] = None
        for keyname in keys:
            try:
                key:pgpy.PGPKey
                key, _ = pgpy.PGPKey.from_file(keyname)
                msg:pgpy.PGPMessage = pgpy.PGPMessage.from_blob(ciphertext)
                msg = key.decrypt(msg)
                return email.message_from_bytes(msg.message)
            except:
                pass
        return None

    def gpg_decrypt(self, ciphertext:str) -> Optional[Message]:
        inp:int
        outp:int
        inp, outp = os.pipe()
        with open(outp, 'w') as outf:
            outf.write(ciphertext)
        out:subprocess.CompletedProcess[bytes] = subprocess.run(['gpg', '--batch', '--decrypt'],
                                                                stdin=inp,
                                                                capture_output=True)
        if out.returncode == 0:
            return email.message_from_bytes(out.stdout)
        return None

    def print_tree(self, z:Message, prefix:str, parent:Optional[Message], num:int) -> None:
        if (z.is_multipart()):
            self.print_part(z, prefix+'┬╴', parent, num)
            if prefix.endswith('└'):
                prefix = prefix.rpartition('└')[0] + ' '
            if prefix.endswith('├'):
                prefix = prefix.rpartition('├')[0] + '│'
            parts:Union[List[Message], str, bytes, None] = z.get_payload()
            if not isinstance(parts, list):
                raise TypeError(f'parts was {type(parts)}, expected List[Message]')
            i = 0
            while (i < len(parts)-1):
                self.print_tree(parts[i], prefix + '├', z, i+1)
                i += 1
            self.print_tree(parts[i], prefix + '└', z, i+1)
            # FIXME: show epilogue?
        else:
            self.print_part(z, prefix+'─╴', parent, num)

def main() -> None:
    parser:ArgumentParser = ArgumentParser(description='Read RFC2822 MIME message from stdin and emit a tree diagram to stdout.',
                                           epilog="Example: email-print-mime-structure <message.eml")
    parser.add_argument('--pgpkey', metavar='KEYFILE', action='append',
                        help='OpenPGP Transferable Secret Key for decrypting')
    parser.add_argument('--use-gpg-agent', action='store_true',
                        help='Ask local GnuPG installation for decryption')
    parser.add_argument('--no-use-gpg-agent', action='store_false',
                        help='Don\'t ask local GnuPG installation for decryption')
    parser.set_defaults(use_gpg_agent=False)

    if argcomplete:
        argcomplete.autocomplete(parser)
    elif '_ARGCOMPLETE' in os.environ:
        logging.error('Argument completion requested but the "argcomplete" '
                      'module is not installed. '
                      'Maybe you want to "apt install python3-argcomplete"')
        sys.exit(1)

    args:Namespace = parser.parse_args()
    msg:Union[Message, str, int, Any] = email.message_from_file(sys.stdin)

    if isinstance(msg, Message):
        printer:MimePrinter = MimePrinter(args)
        printer.print_tree(msg, '└', None, 0)
    else:
        logging.error('Input was not an e-mail message')

if __name__ == '__main__':
    main()