Skip to content

Commit

Permalink
Decompress RTF data
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Rideout committed Nov 30, 2018
1 parent 86f93b0 commit d24ddc8
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ python:

install:
- pip install -e .
- pip install pytest pytest-cov pytest-console-scripts codecov
- pip install pytest pytest-cov pytest-console-scripts codecov compressed_rtf

script:
- pytest --cov
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
license='LGPL',
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
setup_requires=["pytest-runner"],
tests_require = ['pytest', 'coverage'],
tests_require = ['pytest', 'coverage', 'compressed_rtf'],
include_package_data=True,
zip_safe=True,
entry_points = {
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ def test_cmdline_no_htmlbody(script_runner):

def test_cmdline_rtfbody(script_runner):
ret = script_runner.run('tnefparse', '-rb', 'tests/examples/rtf.tnef')
assert len(ret.stdout) == 987
assert len(ret.stdout) == 593
assert ret.stderr == ''
assert ret.success
3 changes: 3 additions & 0 deletions tests/test_decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def test_decode(tnefspec):
if body:
assert getattr(t, body)

if t.rtfbody:
assert t.rtfbody[0:5] == b'{\\rtf'

if objs:
assert objcodes(t) == objs, "wrong objs: %s" % ["0x%2.2x" % o.name for o in t.objects]

Expand Down
6 changes: 3 additions & 3 deletions tnefparse/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,20 @@ def tnefparse():
if args.body:
body = getattr(t, "body")
if body:
print(body)
sys.stdout.write(body.decode('latin-1'))
else:
sys.exit("No body found")

if args.htmlbody:
body = getattr(t, "htmlbody")
if body:
print(body)
sys.stdout.write(body.decode('latin-1'))
else:
sys.exit("No HTML body found")

if args.rtfbody:
body = getattr(t, "rtfbody")
if body:
print(body)
sys.stdout.write(body.decode('latin-1'))
else:
sys.exit("No RTF body found")
18 changes: 15 additions & 3 deletions tnefparse/tnef.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def __init__(self, data, do_checksum=True):
self.mapiprops = []
self.body = None
self.htmlbody = None
self.rtfbody = None
self._rtfbody = None
offset = 6

if not do_checksum:
Expand Down Expand Up @@ -236,7 +236,7 @@ def __init__(self, data, do_checksum=True):
elif p.name == TNEFMAPI_Attribute.MAPI_BODY_HTML:
self.htmlbody = p.data
elif p.name == TNEFMAPI_Attribute.MAPI_RTF_COMPRESSED:
self.rtfbody = p.data
self._rtfbody = p.data
elif obj.name == TNEF.ATTBODY:
self.body = obj.data
elif obj.name == TNEF.ATTTNEFVERSION:
Expand All @@ -249,7 +249,19 @@ def __init__(self, data, do_checksum=True):
logger.debug("Unknown TNEF Object: %s" % obj)

def has_body(self):
return True if (self.body or self.htmlbody or self.rtfbody) else False
return True if (self.body or self.htmlbody or self._rtfbody) else False

@property
def rtfbody(self):
if self._rtfbody:
try:
from compressed_rtf import decompress
return decompress(self._rtfbody + b'\x00')
except ImportError:
logger.warning("Returning compressed RTF. Install compressed_rtf to decompress")
return self._rtfbody
else:
return None

def __str__(self):
atts = (", %i attachments" % len(self.attachments)) if self.attachments else ''
Expand Down

0 comments on commit d24ddc8

Please sign in to comment.