forked from koodaamo/tnefparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdline.py
127 lines (98 loc) · 4.08 KB
/
cmdline.py
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
import argparse
import json
import logging
import os
import sys
from . import properties, tnef
from .tnef import TNEF
#from tnefparse import tnef
logging.basicConfig()
logging.root.setLevel(logging.ERROR)
descr = 'Extract TNEF file contents. Show this help message if no arguments are given.'
parser = argparse.ArgumentParser(description=descr)
argument = parser.add_argument
argument('file', type=argparse.FileType('rb'), nargs='+', action="append",
help='space-separated list of paths to the TNEF files')
argument('-o', '--overview', action='store_true',
help='show (possibly long) overview of TNEF file contents')
argument('-a', '--attachments', action='store_true',
help='extract attachments, by default to current dir')
argument('-z', '--zip', action='store_true',
help='extract attachments to zip file, by default to current dir')
argument('-p', '--path',
help='optional explicit path to extract attachments to')
argument('-b', '--body', action='store_true',
help='extract the body to stdout')
argument('-hb', '--htmlbody', action='store_true',
help='extract the HTML body to stdout')
argument('-rb', '--rtfbody', action='store_true',
help='extract the RTF body to stdout')
argument('-l', '--logging', choices=["DEBUG", "INFO", "WARN", "ERROR"],
help="enable logging by setting a log level")
argument('-c', '--checksum', action="store_true", default=False,
help="calculate checksums (off by default)")
argument('-d', '--dump', action="store_true", default=False,
help="extract a json dump of the tnef contents")
def tnefparse():
"command-line script"
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
if args.logging:
level = eval("logging." + args.logging)
logging.root.setLevel(level)
for tfp in args.file[0]:
try:
t = TNEF(tfp.read(), do_checksum=args.checksum)
except ValueError as exc:
sys.exit(exc.message)
if args.overview:
print("\nOverview of %s: \n" % tfp.name)
# list TNEF attachments
print(" Attachments:\n")
for a in t.attachments:
print(" " + a.long_filename())
# list TNEF objects
print("\n Objects:\n")
print(" " + "\n ".join([TNEF.codes[o.name] for o in t.objects]))
# list TNEF MAPI properties
print("\n Properties:\n")
for p in t.mapiprops:
try:
print(" " + properties.CODE_TO_NAME[p.name])
except KeyError:
logging.root.warning("Unknown MAPI Property: %s" % hex(p.name))
print("")
elif args.dump:
print(json.dumps(t.dump(force_strings=True), sort_keys=True, indent=4))
elif args.attachments:
pth = args.path.rstrip(os.sep) + os.sep if args.path else ''
for a in t.attachments:
with open(pth + a.long_filename(), "wb") as afp:
afp.write(a.data)
sys.stderr.write("Successfully wrote %i files\n" % len(t.attachments))
sys.exit()
elif args.zip:
tfp.seek(0)
pth = args.path.rstrip(os.sep) + os.sep if args.path else ''
test = tfp.read()
a = tnef.to_zip(test)
with open(pth + 'attachments.zip', "wb") as afp:
afp.write(a)
sys.stderr.write("Successfully wrote attachments.zip\n")
sys.exit()
def print_body(attr, description):
body = getattr(t, attr)
if body is None:
sys.exit("No %s found" % description)
elif isinstance(body, bytes):
sys.stdout.write(body.decode('latin-1'))
else:
sys.stdout.write(body)
if args.body:
print_body("body", "body")
if args.htmlbody:
print_body("htmlbody", "HTML body")
if args.rtfbody:
print_body("rtfbody", "RTF body")