-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgat.py
More file actions
executable file
·73 lines (62 loc) · 2.49 KB
/
gat.py
File metadata and controls
executable file
·73 lines (62 loc) · 2.49 KB
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
#!/usr/bin/env python2
from base64 import b64decode
import email
import sys
import argparse
from collections import namedtuple
Mail = namedtuple("Mail", "path content")
def get_mails(files):
mails = list()
for path in files:
with open(path) as f:
mails.append(Mail(path, email.message_from_file(f)))
f.close()
return mails
def print_entry(part, mail, header):
if header:
msg = '-----\nPart from file {}\nID: {}\n-----'
print msg.format(mail.path, mail.content.get('Subject'))
if (part.get('Content-Transfer-Encoding') == 'base64'):
print b64decode(part.get_payload())
else:
print part.get_payload()
def get_entry(mail, subtype, filename, header, base64, nobase64):
printed = 0
for part in mail.content.walk():
partname = part.get_filename().lower() if part.get_filename() else ''
if ((not subtype or subtype in part.get_content_subtype()) and
(not filename or partname == filename.lower())):
isbase64 = (part.get('Content-Transfer-Encoding') == 'base64')
if ((isbase64 and base64) or
(not isbase64 and nobase64) or
(not base64 and not nobase64)):
printed += 1
print_entry(part, mail, header)
return printed
def main():
parser = argparse.ArgumentParser(
description='Gets attachments from mail files.')
parser.add_argument('-t', '--subtype', metavar='TYPE',
help='Only print parts with subtype TYPE')
parser.add_argument('-f', '--filename', metavar='NAME',
help='Only print attachments with filename NAME')
parser.add_argument('-b', '--base64', action='store_true',
help='print only base64-encoded parts')
parser.add_argument('-B', '--nobase64', action='store_true',
help='skip base64-encoded parts')
parser.add_argument('--header', action='store_true',
help='print information about the mail')
parser.add_argument('files', nargs=argparse.REMAINDER)
args = parser.parse_args()
# no filename given
if not len(sys.argv) > 1:
parser.print_help()
exit(2)
printed = 0
mails = get_mails(args.files)
for mail in mails:
printed += get_entry(mail, args.subtype, args.filename,
args.header, args.base64, args.nobase64)
exit(0 if printed > 0 else 1)
if __name__ == "__main__":
main()