-
-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathmessage_base.py
1423 lines (1238 loc) · 59.4 KB
/
message_base.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
__all__ = [
'MessageBase',
]
import base64
import datetime
import email.message
import email.utils
import enum
import functools
import html
import json
import logging
import os
import pathlib
import re
import subprocess
import zipfile
import bs4
import compressed_rtf
import RTFDE
import RTFDE.exceptions
from email import policy
from email.charset import Charset, QP
from email.message import EmailMessage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.parser import HeaderParser
from typing import Any, Callable, cast, Dict, List, Optional, Tuple, Type, Union
from .. import constants
from .._rtf.create_doc import createDocument
from .._rtf.inject_rtf import injectStartRTF
from ..enums import (
BodyTypes, DeencapType, ErrorBehavior, RecipientType, SaveType
)
from ..exceptions import (
ConversionError, DataNotFoundError, DeencapMalformedData,
DeencapNotEncapsulated, IncompatibleOptionsError, MimetypeFailureError,
WKError
)
from .msg import MSGFile
from ..structures.report_tag import ReportTag
from ..recipient import Recipient
from ..utils import (
addNumToDir, addNumToZipDir, createZipOpen, decodeRfc2047, findWk,
htmlSanitize, inputToBytes, inputToString, isEncapsulatedRtf,
prepareFilename, rtfSanitizeHtml, rtfSanitizePlain, stripRtf,
validateHtml
)
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())
class MessageBase(MSGFile):
"""
Base class for Message-like MSG files.
"""
def __init__(self, path, **kwargs):
"""
Supports all of the options from :meth:`MSGFile.__init__` with some
additional ones.
:param recipientSeparator: Optional, separator string to use between
recipients.
:param deencapsulationFunc: Optional, if specified must be a callable
that will override the way that HTML/text is deencapsulated from the
RTF body. This function must take exactly 2 arguments, the first
being the RTF body from the message and the second being an instance
of the enum ``DeencapType`` that will tell the function what type of
body is desired. The function should return a string for plain text
and bytes for HTML. If any problems occur, the function *must*
either return ``None`` or raise one of the appropriate exceptions
from :mod:`extract_msg.exceptions`. All other exceptions must be
handled internally or they will not be caught. The original
deencapsulation method will not run if this is set.
"""
super().__init__(path, **kwargs)
# The rest needs to be in a try-except block to ensure the file closes
# if an error occurs.
try:
self.__headerInit = False
self.__recipientSeparator: str = kwargs.get('recipientSeparator', ';')
self.__deencap = kwargs.get('deencapsulationFunc')
self.header
# This variable keeps track of what the new line character should be.
self._crlf = '\n'
try:
self.body
except Exception as e:
# Prevent an error in the body from preventing opening.
logger.exception('Critical error accessing the body. File opened but accessing the body will throw an exception.')
self._htmlEncoding = None
except:
try:
self.close()
except:
pass
raise
def _genRecipient(self, recipientStr: str, recipientType: RecipientType) -> Optional[str]:
"""
Method to generate the specified recipient field.
"""
value = None
# Check header first.
if self.headerInit:
value = cast(Optional[str], self.header[recipientStr])
if value:
value = decodeRfc2047(value)
value = value.replace(',', self.__recipientSeparator)
# If the header had a blank field or didn't have the field, generate
# it manually.
if not value:
# Check if the header has initialized.
if self.headerInit:
logger.info(f'Header found, but "{recipientStr}" is not included. Will be generated from other streams.')
# Get a list of the recipients of the specified type.
foundRecipients = tuple(recipient.formatted for recipient in self.recipients if recipient.type is recipientType)
# If we found recipients, join them with the recipient separator
# and a space.
if len(foundRecipients) > 0:
value = (self.__recipientSeparator + ' ').join(foundRecipients)
# Code to fix the formatting so it's all a single line. This allows
# the user to format it themself if they want. This should probably
# be redone to use re or something, but I can do that later. This
# shouldn't be a huge problem for now.
if value:
value = value.replace(' \r\n\t', ' ').replace('\r\n\t ', ' ').replace('\r\n\t', ' ')
value = value.replace('\r\n', ' ').replace('\r', ' ').replace('\n', ' ')
while value.find(' ') != -1:
value = value.replace(' ', ' ')
return value
def _getHtmlEncoding(self, soup: bs4.BeautifulSoup) -> None:
"""
Helper function to set the html encoding.
"""
if not self._htmlEncoding:
try:
self._htmlEncoding = cast(Optional[str], soup.original_encoding or soup.declared_html_encoding)
except AttributeError:
pass
def asEmailMessage(self) -> EmailMessage:
"""
Returns an instance of EmailMessage used to represent the contents of
this message.
:raises ConversionError: The function failed to convert one of the
attachments into a form that it could attach, and the attachment
data type was not None.
"""
ret = EmailMessage()
# Copy the headers.
for key, value in self.header.items():
if key.lower() != 'content-type':
ret[key] = value.replace('\r\n', '').replace('\n', '')
ret['Content-Type'] = 'multipart/mixed'
# Attach the body to the EmailMessage instance.
msgMain = MIMEMultipart('related')
ret.attach(msgMain)
bodyParts = MIMEMultipart('alternative')
msgMain.attach(bodyParts)
c = Charset('utf-8')
c.body_encoding = QP
if self.body:
bodyParts.attach(MIMEText(self.body, 'plain', c))
if self.htmlBody:
bodyParts.attach(MIMEText(self.htmlBody.decode('utf-8'), 'html', c))
# Process attachments.
for att in self.attachments:
if att.dataType:
if hasattr(att.dataType, 'asEmailMessage'):
# Replace the extension with '.eml'.
filename = att.name or ''
if filename.lower().endswith('.msg'):
filename = filename[:-4] + '.eml'
msgMain.attach(att.data.asEmailMessage())
else:
if issubclass(att.dataType, bytes):
data = att.data
elif issubclass(att.dataType, MSGFile):
if hasattr(att.dataType, 'asBytes'):
data = att.asBytes
else:
data = att.data.exportBytes()
else:
raise ConversionError(f'Could not find a suitable method to attach attachment data type "{att.dataType}".')
mime = att.mimetype or 'application/octet-stream'
mainType, subType = mime.split('/')[0], mime.split('/')[-1]
# Need to do this manually instead of using add_attachment.
attachment = EmailMessage()
attachment.set_content(data,
maintype = mainType,
subtype = subType,
cid = att.contentId)
# This is just a very basic check.
attachment['Content-Disposition'] = f'{"inline" if att.hidden else "attachment"}; filename="{att.getFilename()}"'
# Add the attachment.
msgMain.attach(attachment)
return ret
def deencapsulateBody(self, rtfBody: bytes, bodyType: DeencapType) -> Optional[Union[bytes, str]]:
"""
A method to deencapsulate the specified body from the RTF body.
Returns a string for plain text and bytes for HTML. If specified, uses
the deencapsulation override function. Returns ``None`` if nothing
could be deencapsulated.
If you want to change the deencapsulation behaviour in a base class,
simply override this function.
"""
if rtfBody:
bodyType = DeencapType(bodyType)
if bodyType == DeencapType.PLAIN:
if self.__deencap:
try:
return self.__deencap(rtfBody, DeencapType.PLAIN)
except DeencapMalformedData:
logger.exception('Custom deencapsulation function reported encapsulated data was malformed.')
except DeencapNotEncapsulated:
logger.exception('Custom deencapsulation function reported data is not encapsulated.')
else:
if self.deencapsulatedRtf and self.deencapsulatedRtf.content_type == 'text':
return self.deencapsulatedRtf.text
else:
if self.__deencap:
try:
return self.__deencap(rtfBody, DeencapType.HTML)
except DeencapMalformedData:
logger.exception('Custom deencapsulation function reported encapsulated data was malformed.')
except DeencapNotEncapsulated:
logger.exception('Custom deencapsulation function reported data is not encapsulated.')
else:
if self.deencapsulatedRtf and self.deencapsulatedRtf.content_type == 'html':
return self.deencapsulatedRtf.html
if bodyType == DeencapType.PLAIN:
logger.info('Could not deencapsulate plain text from RTF body.')
else:
logger.info('Could not deencapsulate HTML from RTF body.')
else:
logger.info('No RTF body to deencapsulate from.')
return None
def dump(self) -> None:
"""
Prints out a summary of the message.
"""
print('Message')
print('Subject:', self.subject)
if self.date:
print('Date:', self.date.__format__(self.datetimeFormat))
print('Body:')
print(self.body)
def getInjectableHeader(self, prefix: str, joinStr: str, suffix: str, formatter: Callable[[str, str], str]) -> str:
"""
Using the specified prefix, suffix, formatter, and join string,
generates the injectable header.
Prefix is placed at the beginning, followed by a series of format
strings joined together with the join string, with the suffix placed
afterwards. Effectively makes this structure:
{prefix}{formatter()}{joinStr}{formatter()}{joinStr}...{formatter()}{suffix}
Formatter be a function that takes first a name variable then a value
variable and formats the line.
If self.headerFormatProperties is None, immediately returns an empty
string.
"""
allProps = self.headerFormatProperties
if allProps is None:
return ''
formattedProps = []
for entry in allProps:
isGroup = False
entryUsed = False
# This is how we handle the groups.
if isinstance(allProps[entry], dict):
props = allProps[entry]
isGroup = True
else:
props = {entry: allProps[entry]}
for name in props:
if props[name]:
if isinstance(props[name], tuple):
if props[name][1]:
value = props[name][0] or ''
elif props[name][0] is not None:
value = props[name][0]
else:
continue
else:
value = props[name]
entryUsed = True
formattedProps.append(formatter(name, value))
# Now if we are working with a group, add an empty entry to get a
# second join string between this section and the last, but *only*
# if any of the entries were used.
if isGroup and entryUsed:
formattedProps.append('')
# If the last entry is empty, remove it. We don't want extra spacing at
# the end.
if formattedProps[-1] == '':
formattedProps.pop()
return prefix + joinStr.join(formattedProps) + suffix
def getJson(self) -> str:
"""
Returns the JSON representation of the Message.
"""
return json.dumps({
'from': self.sender,
'to': self.to,
'cc': self.cc,
'bcc': self.bcc,
'subject': self.subject,
'date': self.date.__format__(self.datetimeFormat) if self.date else None,
'body': self.body,
})
def getSaveBody(self, **_) -> bytes:
"""
Returns the plain text body that will be used in saving based on the
arguments.
:param _: Used to allow kwargs expansion in the save function.
Arguments absorbed by this are simply ignored.
"""
# Get the type of line endings.
crlf = inputToString(self.crlf, 'utf-8')
prefix = ''
suffix = crlf + '-----------------' + crlf + crlf
joinStr = crlf
formatter = (lambda name, value: f'{name}: {value}')
header = self.getInjectableHeader(prefix, joinStr, suffix, formatter).encode('utf-8')
return header + inputToBytes(self.body, 'utf-8')
def getSaveHtmlBody(self, preparedHtml: bool = False, charset: str = 'utf-8', **_) -> bytes:
"""
Returns the HTML body that will be used in saving based on the
arguments.
:param preparedHtml: Whether or not the HTML should be prepared for
standalone use (add tags, inject images, etc.).
:param charset: If the html is being prepared, the charset to use for
the Content-Type meta tag to insert. This exists to ensure that
something parsing the html can properly determine the encoding (as
not having this tag can cause errors in some programs). Set this to
``None`` or an empty string to not insert the tag. (Default:
'utf-8')
:param _: Used to allow kwargs expansion in the save function.
Arguments absorbed by this are simply ignored.
"""
if self.htmlBody:
# Inject the header into the data.
data = self.injectHtmlHeader(prepared = preparedHtml)
# If we are preparing the HTML, then we should
if preparedHtml and charset:
bs = bs4.BeautifulSoup(data, features = 'html.parser', from_encoding = self._htmlEncoding)
self._getHtmlEncoding(bs)
if not bs.find('meta', {'http-equiv': 'Content-Type'}):
# Setup the attributes for the tag.
tagAttrs = {
'http-equiv': 'Content-Type',
'content': f'text/html; charset={charset}',
}
# Create the tag.
tag = bs4.Tag(parser = bs, name = 'meta', attrs = tagAttrs, can_be_empty_element = True)
# Add the tag to the head section.
if bs.find('head'):
bs.find('head').insert(0, tag)
else:
# If we are here, the head doesn't exist, so let's add
# it.
if bs.find('html'):
# This should always be true, but I want to be safe.
head = bs4.Tag(parser = bs, name = 'head')
head.insert(0, tag)
bs.find('html').insert(0, head)
data = bs.encode('utf-8')
return data
else:
return self.htmlBody or b''
def getSavePdfBody(self, wkPath = None, wkOptions = None, **kwargs) -> bytes:
"""
Returns the PDF body that will be used in saving based on the arguments.
:param wkPath: Used to manually specify the path of the wkhtmltopdf
executable. If not specified, the function will try to find it.
Useful if wkhtmltopdf is not on the path. If :param pdf: is
``False``, this argument is ignored.
:param wkOptions: Used to specify additional options to wkhtmltopdf.
this must be a list or list-like object composed of strings and
bytes.
:param kwargs: Used to allow kwargs expansion in the save function.
Arguments absorbed by this are simply ignored, except for keyword arguments used by :meth:`getSaveHtmlBody`.
:raises ExecutableNotFound: The wkhtmltopdf executable could not be
found.
:raises WKError: Something went wrong in creating the PDF body.
"""
# Immediately try to find the executable.
wkPath = findWk(wkPath)
# First thing is first, we need to parse our wkOptions if they exist.
if wkOptions:
try:
# Try to convert to a list, whatever it is, and fail if it is
# not possible.
parsedWkOptions = [*wkOptions]
except TypeError:
raise TypeError(f':param wkOptions: must be an iterable, not {type(wkOptions)}.')
else:
parsedWkOptions = []
# Confirm that all of our options we now have are either strings or
# bytes.
if not all(isinstance(option, (str, bytes)) for option in parsedWkOptions):
raise TypeError(':param wkOptions: must be an iterable of strings and bytes.')
processArgs = [wkPath, *parsedWkOptions, '-', '-']
# Log the arguments.
logger.info(f'Converting to PDF with the following arguments: {processArgs}')
# Get the html body *before* calling Popen.
htmlBody = self.getSaveHtmlBody(**kwargs)
# We call the program to convert the html, but give tell it the data
# will go in and come out through stdin and stdout, respectively. This
# way we don't have to write temporary files to the disk. We also ask
# that it be quiet about it.
process = subprocess.run(processArgs, input = htmlBody, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
# Give the program the data and wait for the program to finish.
#output = process.communicate(htmlBody)
# If it errored, throw it as an exception.
if process.returncode != 0:
raise WKError(process.stderr.decode('utf-8'))
return process.stdout
def getSaveRtfBody(self, **_) -> bytes:
"""
Returns the RTF body that will be used in saving based on the arguments.
:param kwargs: Used to allow kwargs expansion in the save function.
Arguments absorbed by this are simply ignored.
"""
# Inject the header into the data.
return self.injectRtfHeader()
def injectHtmlHeader(self, prepared: bool = False) -> bytes:
"""
Returns the HTML body from the MSG file (will check that it has one)
with the HTML header injected into it.
:param prepared: Determines whether to be using the standard HTML
(``False``) or the prepared HTML (``True``) body. (Default: ``False``)
:raises AttributeError: The correct HTML body cannot be acquired.
"""
if not self.htmlBody:
raise AttributeError('Cannot inject the HTML header without an HTML body attribute.')
body = None
# We don't do this all at once because the prepared body is not cached.
if prepared:
body = self.htmlBodyPrepared
# If the body is not valid or not found, raise an AttributeError.
if not body:
raise AttributeError('Cannot find a prepared HTML body to inject into.')
else:
body = self.htmlBody
# Validate the HTML.
if not validateHtml(body, self._htmlEncoding):
logger.warning('HTML body failed to validate. Code will attempt to correct it.')
# If we are here, then we need to do what we can to fix the HTML
# body. Unfortunately this gets complicated because of the various
# ways the body could be wrong. If only the <body> tag is missing,
# then we just need to insert it at the end and be done. If both
# the <html> and <body> tag are missing, we determine where to put
# the body tag (around everything if there is no <head> tag,
# otherwise at the end) and then wrap it all in the <html> tag.
parser = bs4.BeautifulSoup(body, features = 'html.parser', from_encoding = self._htmlEncoding)
self._getHtmlEncoding(parser)
if not parser.find('html') and not parser.find('body'):
if parser.find('head') or parser.find('footer'):
# Create the parser we will be using for the corrections.
correctedHtml = bs4.BeautifulSoup(b'<html></html>', features = 'html.parser')
htmlTag = correctedHtml.find('html')
# Iterate over each of the direct descendents of the parser and
# add each to a new tag if they are not the head or footer.
bodyTag = parser.new_tag('body')
# What we are going to be doing will be causing some of the tags
# to be moved out of the parser, and so the iterator will end up
# pointing to the wrong place after that. To compensate we first
# create a tuple and iterate over that.
for tag in tuple(parser.children):
if tag.name.lower() in ('head', 'footer'):
correctedHtml.append(tag)
else:
bodyTag.append(tag)
# All the tags should now be properly in the body, so let's
# insert it.
if correctedHtml.find('head'):
correctedHtml.find('head').insert_after(bodyTag)
elif correctedHtml.find('footer'):
correctedHtml.find('footer').insert_before(bodyTag)
else:
# Neither a head or a body are present, so just append it to
# the main tag.
htmlTag.append(bodyTag)
else:
# If there is no <html>, <head>, <footer>, or <body> tag, then
# we just add the tags to the beginning and end of the data and
# move on.
body = b'<html><body>' + body + b'</body></html>'
elif parser.find('html'):
# Found <html> but not <body>.
# Iterate over each of the direct descendents of the parser and
# add each to a new tag if they are not the head or footer.
bodyTag = parser.new_tag('body')
# What we are going to be doing will be causing some of the tags
# to be moved out of the parser, and so the iterator will end up
# pointing to the wrong place after that. To compensate we first
# create a tuple and iterate over that.
for tag in tuple(parser.find('html').children):
if tag.name and tag.name.lower() not in ('head', 'footer'):
bodyTag.append(tag)
# All the tags should now be properly in the body, so let's
# insert it.
if parser.find('head'):
parser.find('head').insert_after(bodyTag)
elif parser.find('footer'):
parser.find('footer').insert_before(bodyTag)
else:
parser.find('html').insert(0, bodyTag)
else:
# Found <body> but not <html>. Just wrap everything in the <html>
# tags.
body = b'<html>' + body + b'</html>'
def replace(bodyMarker):
"""
Internal function to replace the body tag with itself plus the
header.
"""
# I recently had to change this and how it worked. Now we use a new
# property of `MSGFile` that returns a special tuple of tuples to define
# how to get all of the properties we are formatting. They are all
# processed in the same way, making everything neat. By defining them
# in each class, any class can specify a completely different set to be
# used.
return bodyMarker.group() + self.htmlInjectableHeader.encode('utf-8')
# Use the previously defined function to inject the HTML header.
return constants.re.HTML_BODY_START.sub(replace, body, 1)
def injectRtfHeader(self) -> bytes:
"""
Returns the RTF body from this MSG file (will check that it has one)
with the RTF header injected into it.
:raises AttributeError: The RTF body cannot be acquired.
:raises RuntimeError: All injection attempts failed.
"""
if not self.rtfBody:
raise AttributeError('Cannot inject the RTF header without an RTF body attribute.')
# Try to determine which header to use. Also determines how to sanitize the
# rtf.
if isEncapsulatedRtf(self.rtfBody):
injectableHeader = self.rtfEncapInjectableHeader
else:
injectableHeader = self.rtfPlainInjectableHeader
def replace(bodyMarker):
"""
Internal function to replace the body tag with itself plus the
header.
"""
return bodyMarker.group() + injectableHeader
# This first method only applies to documents with encapsulated HTML
# that is formatted in a nice way.
if isEncapsulatedRtf(self.rtfBody):
data = constants.re.RTF_ENC_BODY_START.sub(replace, self.rtfBody, 1)
if data != self.rtfBody:
logger.debug('Successfully injected RTF header using encapsulation method.')
return data
logger.debug('RTF has encapsulated HTML, but injection method failed. It is likely dirty. Will use normal RTF injection method.')
# If the normal encapsulated HTML injection fails or it isn't
# encapsulated, use the internal _rtf module.
logger.debug('Using _rtf module to inject RTF text header.')
return createDocument(injectStartRTF(self.rtfBody, injectableHeader))
def save(self, **kwargs) -> constants.SAVE_TYPE:
"""
Saves the message body and attachments found in the message.
The body and attachments are stored in a folder in the current running
directory unless :param customPath: has been specified. The name of the
folder will be determined by 3 factors.
* If :param customFilename: has been set, the value provided for that
will be used.
* If :param useMsgFilename: has been set, the name of the file used
to create the Message instance will be used.
* If the file name has not been provided or :param useMsgFilename:
has not been set, the name of the folder will be created using
:property defaultFolderName:.
* Setting :param maxNameLength: will force all file names to be
shortened to fit in the space (with the extension included in the
length). If a number is added to the directory that will not be
included in the length, so it is recommended to plan for up to 5
characters extra to be a part of the name. Default is 256.
It should be noted that regardless of the value for
:param maxNameLength:, the name of the file containing the body will always have the name 'message' followed by the full extension.
There are several parameters used to determine how the message will be
saved. By default, the message will be saved as plain text. Setting one
of the following parameters to ``True`` will change that:
* :param html: will output the message in HTML format.
* :param json: will output the message in JSON format.
* :param raw: will output the message in a raw format.
* :param rtf: will output the message in RTF format.
Usage of more than one formatting parameter will raise an exception.
Using HTML or RTF will raise an exception if they could not be retrieved
unless you have :param allowFallback: set to ``True``. Fallback will go
in this order, starting at the top most format that is set:
* HTML
* RTF
* Plain text
If you want to save the contents into a ``ZipFile`` or similar object,
either pass a path to where you want to create one or pass an instance
to :param zip:. If :param zip: is set, :param customPath: will refer to
a location inside the zip file.
:param attachmentsOnly: Turns off saving the body and only saves the
attachments when set.
:param saveHeader: Turns on saving the header as a separate file when
set.
:param skipAttachments: Turns off saving attachments.
:param skipHidden: If ``True``, skips attachments marked as hidden.
(Default: ``False``)
:param skipBodyNotFound: Suppresses errors if no valid body could be
found, simply skipping the step of saving the body.
:param charset: If the HTML is being prepared, the charset to use for
the Content-Type meta tag to insert. This exists to ensure that
something parsing the HTML can properly determine the encoding (as
not having this tag can cause errors in some programs). Set this to
``None`` or an empty string to not insert the tag (Default:
``'utf-8'``).
:param kwargs: Used to allow kwargs expansion in the save function.
:param preparedHtml: When set, prepares the HTML body for standalone
usage, doing things like adding tags, injecting attachments, etc.
This is useful for things like trying to convert the HTML body
directly to PDF.
:param pdf: Used to enable saving the body as a PDF file.
:param wkPath: Used to manually specify the path of the wkhtmltopdf
executable. If not specified, the function will try to find it.
Useful if wkhtmltopdf is not on the path. If :param pdf: is False,
this argument is ignored.
:param wkOptions: Used to specify additional options to wkhtmltopdf.
this must be a list or list-like object composed of strings and
bytes.
"""
# Move keyword arguments into variables.
_json = kwargs.get('json', False)
html = kwargs.get('html', False)
rtf = kwargs.get('rtf', False)
raw = kwargs.get('raw', False)
pdf = kwargs.get('pdf', False)
allowFallback = kwargs.get('allowFallback', False)
_zip = kwargs.get('zip')
maxNameLength = kwargs.get('maxNameLength', 256)
# Variables involved in the save location.
customFilename = kwargs.get('customFilename')
useMsgFilename = kwargs.get('useMsgFilename', False)
#maxPathLength = kwargs.get('maxPathLength', 255)
# Track if we are only saving the attachments.
attachOnly = kwargs.get('attachmentsOnly', False)
# Track if we are skipping attachments.
skipAttachments = kwargs.get('skipAttachments', False)
skipHidden = kwargs.get('skipHidden', False)
# Track if we should skip the body if no valid body is found instead of
# raising an exception.
skipBodyNotFound = kwargs.get('skipBodyNotFound', False)
if pdf:
kwargs['preparedHtml'] = True
# Try to get the body, if needed, before messing with the path.
if not attachOnly:
# Check what to save the body with.
fext = 'json' if _json else 'txt'
fallbackToPlain = False
useHtml = False
usePdf = False
useRtf = False
if html:
if self.htmlBody:
useHtml = True
fext = 'html'
elif not allowFallback:
if skipBodyNotFound:
fext = None
else:
raise DataNotFoundError('Could not find the htmlBody.')
if pdf:
if self.htmlBody:
usePdf = True
fext = 'pdf'
elif not allowFallback:
if skipBodyNotFound:
fext = None
else:
raise DataNotFoundError('Count not find the htmlBody to convert to pdf.')
if rtf or (html and not useHtml) or (pdf and not usePdf):
if self.rtfBody:
useRtf = True
fext = 'rtf'
elif not allowFallback:
if skipBodyNotFound:
fext = None
else:
raise DataNotFoundError('Could not find the rtfBody.')
else:
# This was the last resort before plain text, so fall
# back to that.
fallbackToPlain = True
# After all other options, try to go with plain text if
# possible.
if not (rtf or html or pdf) or fallbackToPlain:
# We need to check if the plain text body was found. If it
# was found but was empty that is considered valid, so we
# specifically check against None.
if self.body is None:
if skipBodyNotFound:
fext = None
else:
if allowFallback:
raise DataNotFoundError('Could not find a valid body using current options.')
else:
raise DataNotFoundError('Plain text body could not be found.')
createdZip = False
try:
# ZipFile handling.
if _zip:
# `raw` and `zip` are incompatible.
if raw:
raise IncompatibleOptionsError('The options `raw` and `zip` are incompatible.')
# If we are doing a zip file, first check that we have been
# given a path.
if isinstance(_zip, (str, pathlib.Path)):
# If we have a path then we use the zip file.
_zip = zipfile.ZipFile(_zip, 'a', zipfile.ZIP_DEFLATED)
kwargs['zip'] = _zip
createdZip = True
# Path needs to be done in a special way if we are in a zip
# file.
path = pathlib.Path(kwargs.get('customPath', ''))
# Set the open command to be that of the zip file.
_open = createZipOpen(_zip.open)
# Zip files use w for writing in binary.
mode = 'w'
else:
path = pathlib.Path(kwargs.get('customPath', '.')).absolute()
mode = 'wb'
_open = open
# Reset this for sub save calls.
kwargs['customFilename'] = None
# Check if incompatible options have been provided in any way.
if _json + html + rtf + raw + attachOnly + pdf > 1:
raise IncompatibleOptionsError('Only one of the following options may be used at a time: json, raw, html, rtf, attachmentsOnly, pdf.')
# TODO: insert code here that will handle checking all of the msg
# files to see if the path with overflow.
if customFilename:
# First we need to validate it. If there are invalid characters,
# this will detect it.
if constants.re.INVALID_FILENAME_CHARS.search(customFilename):
raise ValueError('Invalid character found in customFilename. Must not contain any of the following characters: \\/:*?"<>|')
# Quick fix to remove spaces from the end of the filename, if
# any are there.
customFilename = customFilename.strip()
path /= customFilename[:maxNameLength]
elif useMsgFilename:
if not self.filename:
raise ValueError(':param useMsgFilename: is only available if you are using an MSG file on the disk or have provided a filename.')
# Get the actual name of the file.
filename = os.path.split(self.filename)[1]
# Remove the extensions.
filename = os.path.splitext(filename)[0]
# Prepare the filename by removing any special characters.
filename = prepareFilename(filename)
# Shorted the filename.
filename = filename[:maxNameLength]
# Check to make sure we actually have a filename to use.
if not filename:
raise ValueError(f'Invalid filename found in self.filename: "{self.filename}"')
# Add the file name to the path.
path /= filename[:maxNameLength]
else:
path /= self.defaultFolderName[:maxNameLength]
# Create the folders.
if not _zip:
try:
os.makedirs(path)
except Exception:
newDirName = addNumToDir(path)
if newDirName:
path = newDirName
else:
raise OSError(f'Failed to create directory "{path}". Does it already exist?')
else:
# In my testing I ended up with multiple files in a zip at the
# same location so let's try to handle that.
pathCompare = str(path).replace('\\', '/').rstrip('/') + '/'
if any(x.startswith(pathCompare) for x in _zip.namelist()):
newDirName = addNumToZipDir(path, _zip)
if newDirName:
path = newDirName
else:
raise Exception(f'Failed to create directory "{path}". Does it already exist?')
# Update the kwargs.
kwargs['customPath'] = path
if raw:
self.saveRaw(path)
return (SaveType.FOLDER, str(path))
# If the user has requested the headers for this file, save it now.
if kwargs.get('saveHeader', False):
headerText = self.headerText
if not headerText:
headerText = constants.HEADER_FORMAT.format(subject = self.subject, **self.header)
with _open(str(path / 'header.txt'), mode) as f:
f.write(headerText.encode('utf-8'))
if not skipAttachments:
# Save the attachments.
attachmentReturns = [attachment.save(**kwargs) for attachment in self.attachments if not (skipHidden and attachment.hidden)]
# Get the names from each.
attachmentNames = []
for x in attachmentReturns:
if isinstance(x[1], str):
attachmentNames.append(x[1])
elif isinstance(x[1], list):
attachmentNames.extend(x[1])
if not attachOnly and fext:
with _open(str(path / ('message.' + fext)), mode) as f:
if _json:
emailObj = json.loads(self.getJson())
if not skipAttachments:
emailObj['attachments'] = attachmentNames
f.write(json.dumps(emailObj).encode('utf-8'))
elif useHtml:
f.write(self.getSaveHtmlBody(**kwargs))
elif usePdf:
f.write(self.getSavePdfBody(**kwargs))
elif useRtf:
f.write(self.getSaveRtfBody(**kwargs))
else:
f.write(self.getSaveBody(**kwargs))
return (SaveType.FOLDER, str(path))
finally:
# Close the ZipFile if this function created it.
if _zip and createdZip:
_zip.close()
@functools.cached_property
def bcc(self) -> Optional[str]:
"""
The "Bcc" field, if it exists.
"""
return self._genRecipient('bcc', RecipientType.BCC)
@functools.cached_property
def body(self) -> Optional[str]:
"""
The message body, if it exists.
"""
# If the body exists but is empty, that means it should be returned.
if (body := self.getStringStream('__substg1.0_1000')) is not None:
pass
elif self.rtfBody:
# If the body doesn't exist, see if we can get it from the RTF
# body.
body = self.deencapsulateBody(self.rtfBody, DeencapType.PLAIN)
if body:
body = inputToString(body, 'utf-8')
if re.search('\n', body) is not None:
if re.search('\r\n', body) is not None:
self._crlf = '\r\n'
return body
@functools.cached_property
def cc(self) -> Optional[str]:
"""
The "Cc" field, if it exists.
"""
return self._genRecipient('cc', RecipientType.CC)
@functools.cached_property
def compressedRtf(self) -> Optional[bytes]:
"""
The compressed RTF stream, if it exists.
"""
return self.getStream('__substg1.0_10090102')
@property
def crlf(self) -> str:
"""
The value of ``self.__crlf``, should you need it for whatever reason.
"""
return self._crlf
@functools.cached_property
def date(self) -> Optional[datetime.datetime]:
"""
The send date, if it exists.
"""
return self.props.date if self.isSent else None
@functools.cached_property
def deencapsulatedRtf(self) -> Optional[RTFDE.DeEncapsulator]: