extract-msg 0.48.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. extract_msg/__init__.py +71 -0
  2. extract_msg/__main__.py +115 -0
  3. extract_msg/_rtf/__init__.py +21 -0
  4. extract_msg/_rtf/create_doc.py +29 -0
  5. extract_msg/_rtf/inject_rtf.py +174 -0
  6. extract_msg/_rtf/token.py +35 -0
  7. extract_msg/_rtf/tokenize_rtf.py +253 -0
  8. extract_msg/attachments/__init__.py +144 -0
  9. extract_msg/attachments/attachment.py +188 -0
  10. extract_msg/attachments/attachment_base.py +730 -0
  11. extract_msg/attachments/broken_att.py +42 -0
  12. extract_msg/attachments/custom_att.py +173 -0
  13. extract_msg/attachments/custom_att_handler/__init__.py +80 -0
  14. extract_msg/attachments/custom_att_handler/custom_handler.py +134 -0
  15. extract_msg/attachments/custom_att_handler/lnk_obj_att.py +109 -0
  16. extract_msg/attachments/custom_att_handler/outlook_image_dib.py +141 -0
  17. extract_msg/attachments/emb_msg_att.py +134 -0
  18. extract_msg/attachments/signed_att.py +234 -0
  19. extract_msg/attachments/unsupported_att.py +40 -0
  20. extract_msg/attachments/web_att.py +74 -0
  21. extract_msg/constants/__init__.py +294 -0
  22. extract_msg/constants/ps.py +46 -0
  23. extract_msg/constants/re.py +42 -0
  24. extract_msg/constants/st.py +110 -0
  25. extract_msg/data/logging-config/logging-nt.json +64 -0
  26. extract_msg/data/logging-config/logging-posix.json +64 -0
  27. extract_msg/encoding/__init__.py +277 -0
  28. extract_msg/encoding/_dt/__init__.py +4 -0
  29. extract_msg/encoding/_dt/_mac_ce.py +8 -0
  30. extract_msg/encoding/_dt/_mac_cyrillic.py +8 -0
  31. extract_msg/encoding/_dt/_mac_greek.py +8 -0
  32. extract_msg/encoding/_dt/_mac_iceland.py +8 -0
  33. extract_msg/encoding/_dt/_mac_turkish.py +8 -0
  34. extract_msg/encoding/_dt/_win874_dec.py +8 -0
  35. extract_msg/encoding/_dt/_win950_dec.py +6 -0
  36. extract_msg/encoding/utils.py +329 -0
  37. extract_msg/enums.py +2067 -0
  38. extract_msg/exceptions.py +170 -0
  39. extract_msg/msg_classes/__init__.py +49 -0
  40. extract_msg/msg_classes/appointment.py +195 -0
  41. extract_msg/msg_classes/calendar.py +92 -0
  42. extract_msg/msg_classes/calendar_base.py +496 -0
  43. extract_msg/msg_classes/contact.py +1335 -0
  44. extract_msg/msg_classes/journal.py +131 -0
  45. extract_msg/msg_classes/meeting_cancellation.py +108 -0
  46. extract_msg/msg_classes/meeting_exception.py +55 -0
  47. extract_msg/msg_classes/meeting_forward.py +115 -0
  48. extract_msg/msg_classes/meeting_related.py +65 -0
  49. extract_msg/msg_classes/meeting_request.py +189 -0
  50. extract_msg/msg_classes/meeting_response.py +74 -0
  51. extract_msg/msg_classes/message.py +12 -0
  52. extract_msg/msg_classes/message_base.py +1382 -0
  53. extract_msg/msg_classes/message_signed.py +17 -0
  54. extract_msg/msg_classes/message_signed_base.py +168 -0
  55. extract_msg/msg_classes/msg.py +1172 -0
  56. extract_msg/msg_classes/post.py +53 -0
  57. extract_msg/msg_classes/sticky_note.py +67 -0
  58. extract_msg/msg_classes/task.py +363 -0
  59. extract_msg/msg_classes/task_request.py +86 -0
  60. extract_msg/null_date.py +34 -0
  61. extract_msg/ole_writer.py +1086 -0
  62. extract_msg/open_msg.py +204 -0
  63. extract_msg/properties/__init__.py +23 -0
  64. extract_msg/properties/named.py +481 -0
  65. extract_msg/properties/prop.py +506 -0
  66. extract_msg/properties/properties_store.py +476 -0
  67. extract_msg/py.typed +0 -0
  68. extract_msg/recipient.py +362 -0
  69. extract_msg/structures/__init__.py +34 -0
  70. extract_msg/structures/_helpers.py +345 -0
  71. extract_msg/structures/business_card.py +422 -0
  72. extract_msg/structures/cfoas.py +108 -0
  73. extract_msg/structures/contact_link_entry.py +51 -0
  74. extract_msg/structures/dev_mode_a.py +621 -0
  75. extract_msg/structures/dv_target_device.py +168 -0
  76. extract_msg/structures/entry_id.py +697 -0
  77. extract_msg/structures/misc_id.py +222 -0
  78. extract_msg/structures/mon_stream.py +55 -0
  79. extract_msg/structures/odt.py +74 -0
  80. extract_msg/structures/ole_pres.py +302 -0
  81. extract_msg/structures/ole_stream_struct.py +117 -0
  82. extract_msg/structures/recurrence_pattern.py +199 -0
  83. extract_msg/structures/report_tag.py +127 -0
  84. extract_msg/structures/system_time.py +158 -0
  85. extract_msg/structures/time_zone_definition.py +111 -0
  86. extract_msg/structures/time_zone_struct.py +102 -0
  87. extract_msg/structures/toc_entry.py +137 -0
  88. extract_msg/structures/tz_rule.py +148 -0
  89. extract_msg/utils.py +1270 -0
  90. extract_msg-0.48.2.dist-info/LICENSE.txt +674 -0
  91. extract_msg-0.48.2.dist-info/METADATA +321 -0
  92. extract_msg-0.48.2.dist-info/RECORD +95 -0
  93. extract_msg-0.48.2.dist-info/WHEEL +5 -0
  94. extract_msg-0.48.2.dist-info/entry_points.txt +2 -0
  95. extract_msg-0.48.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1382 @@
1
+ __all__ = [
2
+ 'MessageBase',
3
+ ]
4
+
5
+
6
+ import base64
7
+ import datetime
8
+ import email.message
9
+ import email.utils
10
+ import enum
11
+ import functools
12
+ import html
13
+ import json
14
+ import logging
15
+ import os
16
+ import pathlib
17
+ import re
18
+ import subprocess
19
+ import zipfile
20
+
21
+ import bs4
22
+ import compressed_rtf
23
+ import RTFDE
24
+ import RTFDE.exceptions
25
+
26
+ from email import policy
27
+ from email.charset import Charset, QP
28
+ from email.message import EmailMessage
29
+ from email.mime.multipart import MIMEMultipart
30
+ from email.mime.text import MIMEText
31
+ from email.parser import HeaderParser
32
+ from typing import Any, Callable, cast, Dict, List, Optional, Tuple, Type, Union
33
+
34
+ from .. import constants
35
+ from .._rtf.create_doc import createDocument
36
+ from .._rtf.inject_rtf import injectStartRTF
37
+ from ..enums import (
38
+ BodyTypes, DeencapType, ErrorBehavior, RecipientType, SaveType
39
+ )
40
+ from ..exceptions import (
41
+ ConversionError, DataNotFoundError, DeencapMalformedData,
42
+ DeencapNotEncapsulated, IncompatibleOptionsError, WKError
43
+ )
44
+ from .msg import MSGFile
45
+ from ..structures.report_tag import ReportTag
46
+ from ..recipient import Recipient
47
+ from ..utils import (
48
+ addNumToDir, addNumToZipDir, createZipOpen, decodeRfc2047, findWk,
49
+ htmlSanitize, inputToBytes, inputToString, isEncapsulatedRtf,
50
+ prepareFilename, rtfSanitizeHtml, rtfSanitizePlain, validateHtml
51
+ )
52
+
53
+
54
+ logger = logging.getLogger(__name__)
55
+ logger.addHandler(logging.NullHandler())
56
+
57
+
58
+ class MessageBase(MSGFile):
59
+ """
60
+ Base class for Message-like MSG files.
61
+ """
62
+
63
+ def __init__(self, path, **kwargs):
64
+ """
65
+ Supports all of the options from :meth:`MSGFile.__init__` with some
66
+ additional ones.
67
+
68
+ :param recipientSeparator: Optional, separator string to use between
69
+ recipients.
70
+ :param deencapsulationFunc: Optional, if specified must be a callable
71
+ that will override the way that HTML/text is deencapsulated from the
72
+ RTF body. This function must take exactly 2 arguments, the first
73
+ being the RTF body from the message and the second being an instance
74
+ of the enum ``DeencapType`` that will tell the function what type of
75
+ body is desired. The function should return a string for plain text
76
+ and bytes for HTML. If any problems occur, the function *must*
77
+ either return ``None`` or raise one of the appropriate exceptions
78
+ from :mod:`extract_msg.exceptions`. All other exceptions must be
79
+ handled internally or they will not be caught. The original
80
+ deencapsulation method will not run if this is set.
81
+ """
82
+ super().__init__(path, **kwargs)
83
+ # The rest needs to be in a try-except block to ensure the file closes
84
+ # if an error occurs.
85
+ try:
86
+ self.__headerInit = False
87
+ self.__recipientSeparator: str = kwargs.get('recipientSeparator', ';')
88
+ self.__deencap = kwargs.get('deencapsulationFunc')
89
+ self.header
90
+
91
+ # This variable keeps track of what the new line character should be.
92
+ self._crlf = '\n'
93
+ try:
94
+ self.body
95
+ except Exception as e:
96
+ # Prevent an error in the body from preventing opening.
97
+ logger.exception('Critical error accessing the body. File opened but accessing the body will throw an exception.')
98
+ except:
99
+ try:
100
+ self.close()
101
+ except:
102
+ pass
103
+ raise
104
+
105
+ def _genRecipient(self, recipientStr: str, recipientType: RecipientType) -> Optional[str]:
106
+ """
107
+ Method to generate the specified recipient field.
108
+ """
109
+ value = None
110
+ # Check header first.
111
+ if self.headerInit:
112
+ value = cast(Optional[str], self.header[recipientStr])
113
+ if value:
114
+ value = decodeRfc2047(value)
115
+ value = value.replace(',', self.__recipientSeparator)
116
+
117
+ # If the header had a blank field or didn't have the field, generate
118
+ # it manually.
119
+ if not value:
120
+ # Check if the header has initialized.
121
+ if self.headerInit:
122
+ logger.info(f'Header found, but "{recipientStr}" is not included. Will be generated from other streams.')
123
+
124
+ # Get a list of the recipients of the specified type.
125
+ foundRecipients = tuple(recipient.formatted for recipient in self.recipients if recipient.type is recipientType)
126
+
127
+ # If we found recipients, join them with the recipient separator
128
+ # and a space.
129
+ if len(foundRecipients) > 0:
130
+ value = (self.__recipientSeparator + ' ').join(foundRecipients)
131
+
132
+ # Code to fix the formatting so it's all a single line. This allows
133
+ # the user to format it themself if they want. This should probably
134
+ # be redone to use re or something, but I can do that later. This
135
+ # shouldn't be a huge problem for now.
136
+ if value:
137
+ value = value.replace(' \r\n\t', ' ').replace('\r\n\t ', ' ').replace('\r\n\t', ' ')
138
+ value = value.replace('\r\n', ' ').replace('\r', ' ').replace('\n', ' ')
139
+ while value.find(' ') != -1:
140
+ value = value.replace(' ', ' ')
141
+
142
+ return value
143
+
144
+ def asEmailMessage(self) -> EmailMessage:
145
+ """
146
+ Returns an instance of EmailMessage used to represent the contents of
147
+ this message.
148
+
149
+ :raises ConversionError: The function failed to convert one of the
150
+ attachments into a form that it could attach, and the attachment
151
+ data type was not None.
152
+ """
153
+ ret = EmailMessage()
154
+
155
+ # Copy the headers.
156
+ for key, value in self.header.items():
157
+ if key.lower() != 'content-type':
158
+ ret[key] = value.replace('\r\n', '').replace('\n', '')
159
+
160
+ ret['Content-Type'] = 'multipart/mixed'
161
+
162
+ # Attach the body to the EmailMessage instance.
163
+ msgMain = MIMEMultipart('related')
164
+ ret.attach(msgMain)
165
+ bodyParts = MIMEMultipart('alternative')
166
+ msgMain.attach(bodyParts)
167
+
168
+ c = Charset('utf-8')
169
+ c.body_encoding = QP
170
+
171
+ if self.body:
172
+ bodyParts.attach(MIMEText(self.body, 'plain', c))
173
+ if self.htmlBody:
174
+ bodyParts.attach(MIMEText(self.htmlBody.decode('utf-8'), 'html', c))
175
+
176
+ # Process attachments.
177
+ for att in self.attachments:
178
+ if att.dataType:
179
+ if hasattr(att.dataType, 'asEmailMessage'):
180
+ # Replace the extension with '.eml'.
181
+ filename = att.getFilename()
182
+ if filename.lower().endswith('.msg'):
183
+ filename = filename[:-4] + '.eml'
184
+ msgMain.add_attachment(
185
+ att.data.asEmailMessage(),
186
+ filename = filename,
187
+ cid = att.contentId)
188
+ else:
189
+ if issubclass(att.dataType, bytes):
190
+ data = att.data
191
+ elif issubclass(att.dataType, MSGFile):
192
+ if hasattr(att.dataType, 'asBytes'):
193
+ data = att.asBytes
194
+ else:
195
+ data = att.data.exportBytes()
196
+ else:
197
+ raise ConversionError(f'Could not find a suitable method to attach attachment data type "{att.dataType}".')
198
+ mime = att.mimetype or 'application/octet-stream'
199
+ mainType, subType = mime.split('/')[0], mime.split('/')[-1]
200
+ # Need to do this manually instead of using add_attachment.
201
+ attachment = EmailMessage()
202
+ attachment.set_content(data,
203
+ maintype = mainType,
204
+ subtype = subType,
205
+ cid = att.contentId)
206
+ # This is just a very basic check.
207
+ attachment['Content-Disposition'] = f'{"inline" if att.hidden else "attachment"}; filename="{att.getFilename()}"'
208
+
209
+ # Add the attachment.
210
+ msgMain.attach(attachment)
211
+
212
+ return ret
213
+
214
+ def deencapsulateBody(self, rtfBody: bytes, bodyType: DeencapType) -> Optional[Union[bytes, str]]:
215
+ """
216
+ A method to deencapsulate the specified body from the RTF body.
217
+
218
+ Returns a string for plain text and bytes for HTML. If specified, uses
219
+ the deencapsulation override function. Returns ``None`` if nothing
220
+ could be deencapsulated.
221
+
222
+ If you want to change the deencapsulation behaviour in a base class,
223
+ simply override this function.
224
+ """
225
+ if rtfBody:
226
+ bodyType = DeencapType(bodyType)
227
+ if bodyType == DeencapType.PLAIN:
228
+ if self.__deencap:
229
+ try:
230
+ return self.__deencap(rtfBody, DeencapType.PLAIN)
231
+ except DeencapMalformedData:
232
+ logger.exception('Custom deencapsulation function reported encapsulated data was malformed.')
233
+ except DeencapNotEncapsulated:
234
+ logger.exception('Custom deencapsulation function reported data is not encapsulated.')
235
+ else:
236
+ if self.deencapsulatedRtf and self.deencapsulatedRtf.content_type == 'text':
237
+ return self.deencapsulatedRtf.text
238
+ else:
239
+ if self.__deencap:
240
+ try:
241
+ return self.__deencap(rtfBody, DeencapType.HTML)
242
+ except DeencapMalformedData:
243
+ logger.exception('Custom deencapsulation function reported encapsulated data was malformed.')
244
+ except DeencapNotEncapsulated:
245
+ logger.exception('Custom deencapsulation function reported data is not encapsulated.')
246
+ else:
247
+ if self.deencapsulatedRtf and self.deencapsulatedRtf.content_type == 'html':
248
+ return self.deencapsulatedRtf.html
249
+
250
+ if bodyType == DeencapType.PLAIN:
251
+ logger.info('Could not deencapsulate plain text from RTF body.')
252
+ else:
253
+ logger.info('Could not deencapsulate HTML from RTF body.')
254
+ else:
255
+ logger.info('No RTF body to deencapsulate from.')
256
+ return None
257
+
258
+ def dump(self) -> None:
259
+ """
260
+ Prints out a summary of the message.
261
+ """
262
+ print('Message')
263
+ print('Subject:', self.subject)
264
+ if self.date:
265
+ print('Date:', self.date.__format__(self.datetimeFormat))
266
+ print('Body:')
267
+ print(self.body)
268
+
269
+ def getInjectableHeader(self, prefix: str, joinStr: str, suffix: str, formatter: Callable[[str, str], str]) -> str:
270
+ """
271
+ Using the specified prefix, suffix, formatter, and join string,
272
+ generates the injectable header.
273
+
274
+ Prefix is placed at the beginning, followed by a series of format
275
+ strings joined together with the join string, with the suffix placed
276
+ afterwards. Effectively makes this structure:
277
+ {prefix}{formatter()}{joinStr}{formatter()}{joinStr}...{formatter()}{suffix}
278
+
279
+ Formatter be a function that takes first a name variable then a value
280
+ variable and formats the line.
281
+
282
+ If self.headerFormatProperties is None, immediately returns an empty
283
+ string.
284
+ """
285
+ allProps = self.headerFormatProperties
286
+
287
+ if allProps is None:
288
+ return ''
289
+
290
+ formattedProps = []
291
+
292
+ for entry in allProps:
293
+ isGroup = False
294
+ entryUsed = False
295
+ # This is how we handle the groups.
296
+ if isinstance(allProps[entry], dict):
297
+ props = allProps[entry]
298
+ isGroup = True
299
+ else:
300
+ props = {entry: allProps[entry]}
301
+
302
+ for name in props:
303
+ if props[name]:
304
+ if isinstance(props[name], tuple):
305
+ if props[name][1]:
306
+ value = props[name][0] or ''
307
+ elif props[name][0] is not None:
308
+ value = props[name][0]
309
+ else:
310
+ continue
311
+ else:
312
+ value = props[name]
313
+
314
+ entryUsed = True
315
+ formattedProps.append(formatter(name, value))
316
+
317
+ # Now if we are working with a group, add an empty entry to get a
318
+ # second join string between this section and the last, but *only*
319
+ # if any of the entries were used.
320
+ if isGroup and entryUsed:
321
+ formattedProps.append('')
322
+
323
+ # If the last entry is empty, remove it. We don't want extra spacing at
324
+ # the end.
325
+ if formattedProps[-1] == '':
326
+ formattedProps.pop()
327
+
328
+ return prefix + joinStr.join(formattedProps) + suffix
329
+
330
+ def getJson(self) -> str:
331
+ """
332
+ Returns the JSON representation of the Message.
333
+ """
334
+ return json.dumps({
335
+ 'from': self.sender,
336
+ 'to': self.to,
337
+ 'cc': self.cc,
338
+ 'bcc': self.bcc,
339
+ 'subject': self.subject,
340
+ 'date': self.date.__format__(self.datetimeFormat) if self.date else None,
341
+ 'body': self.body,
342
+ })
343
+
344
+ def getSaveBody(self, **_) -> bytes:
345
+ """
346
+ Returns the plain text body that will be used in saving based on the
347
+ arguments.
348
+
349
+ :param _: Used to allow kwargs expansion in the save function.
350
+ Arguments absorbed by this are simply ignored.
351
+ """
352
+ # Get the type of line endings.
353
+ crlf = inputToString(self.crlf, 'utf-8')
354
+
355
+ prefix = ''
356
+ suffix = crlf + '-----------------' + crlf + crlf
357
+ joinStr = crlf
358
+ formatter = (lambda name, value: f'{name}: {value}')
359
+
360
+ header = self.getInjectableHeader(prefix, joinStr, suffix, formatter).encode('utf-8')
361
+ return header + inputToBytes(self.body, 'utf-8')
362
+
363
+ def getSaveHtmlBody(self, preparedHtml: bool = False, charset: str = 'utf-8', **_) -> bytes:
364
+ """
365
+ Returns the HTML body that will be used in saving based on the
366
+ arguments.
367
+
368
+ :param preparedHtml: Whether or not the HTML should be prepared for
369
+ standalone use (add tags, inject images, etc.).
370
+ :param charset: If the html is being prepared, the charset to use for
371
+ the Content-Type meta tag to insert. This exists to ensure that
372
+ something parsing the html can properly determine the encoding (as
373
+ not having this tag can cause errors in some programs). Set this to
374
+ ``None`` or an empty string to not insert the tag. (Default:
375
+ 'utf-8')
376
+ :param _: Used to allow kwargs expansion in the save function.
377
+ Arguments absorbed by this are simply ignored.
378
+ """
379
+ if self.htmlBody:
380
+ # Inject the header into the data.
381
+ data = self.injectHtmlHeader(prepared = preparedHtml)
382
+
383
+ # If we are preparing the HTML, then we should
384
+ if preparedHtml and charset:
385
+ bs = bs4.BeautifulSoup(data, features = 'html.parser')
386
+ if not bs.find('meta', {'http-equiv': 'Content-Type'}):
387
+ # Setup the attributes for the tag.
388
+ tagAttrs = {
389
+ 'http-equiv': 'Content-Type',
390
+ 'content': f'text/html; charset={charset}',
391
+ }
392
+ # Create the tag.
393
+ tag = bs4.Tag(parser = bs, name = 'meta', attrs = tagAttrs, can_be_empty_element = True)
394
+ # Add the tag to the head section.
395
+ if bs.find('head'):
396
+ bs.find('head').insert(0, tag)
397
+ else:
398
+ # If we are here, the head doesn't exist, so let's add
399
+ # it.
400
+ if bs.find('html'):
401
+ # This should always be true, but I want to be safe.
402
+ head = bs4.Tag(parser = bs, name = 'head')
403
+ head.insert(0, tag)
404
+ bs.find('html').insert(0, head)
405
+
406
+ data = bs.prettify('utf-8')
407
+
408
+ return data
409
+ else:
410
+ return self.htmlBody
411
+
412
+ def getSavePdfBody(self, wkPath = None, wkOptions = None, **kwargs) -> bytes:
413
+ """
414
+ Returns the PDF body that will be used in saving based on the arguments.
415
+
416
+ :param wkPath: Used to manually specify the path of the wkhtmltopdf
417
+ executable. If not specified, the function will try to find it.
418
+ Useful if wkhtmltopdf is not on the path. If :param pdf: is
419
+ ``False``, this argument is ignored.
420
+ :param wkOptions: Used to specify additional options to wkhtmltopdf.
421
+ this must be a list or list-like object composed of strings and
422
+ bytes.
423
+ :param kwargs: Used to allow kwargs expansion in the save function.
424
+ Arguments absorbed by this are simply ignored, except for keyword arguments used by :meth:`getSaveHtmlBody`.
425
+
426
+ :raises ExecutableNotFound: The wkhtmltopdf executable could not be
427
+ found.
428
+ :raises WKError: Something went wrong in creating the PDF body.
429
+ """
430
+ # Immediately try to find the executable.
431
+ wkPath = findWk(wkPath)
432
+
433
+ # First thing is first, we need to parse our wkOptions if they exist.
434
+ if wkOptions:
435
+ try:
436
+ # Try to convert to a list, whatever it is, and fail if it is
437
+ # not possible.
438
+ parsedWkOptions = [*wkOptions]
439
+ except TypeError:
440
+ raise TypeError(f':param wkOptions: must be an iterable, not {type(wkOptions)}.')
441
+ else:
442
+ parsedWkOptions = []
443
+
444
+ # Confirm that all of our options we now have are either strings or
445
+ # bytes.
446
+ if not all(isinstance(option, (str, bytes)) for option in parsedWkOptions):
447
+ raise TypeError(':param wkOptions: must be an iterable of strings and bytes.')
448
+
449
+ processArgs = [wkPath, *parsedWkOptions, '-', '-']
450
+ # Log the arguments.
451
+ logger.info(f'Converting to PDF with the following arguments: {processArgs}')
452
+
453
+ # Get the html body *before* calling Popen.
454
+ htmlBody = self.getSaveHtmlBody(**kwargs)
455
+
456
+ # We call the program to convert the html, but give tell it the data
457
+ # will go in and come out through stdin and stdout, respectively. This
458
+ # way we don't have to write temporary files to the disk. We also ask
459
+ # that it be quiet about it.
460
+ process = subprocess.run(processArgs, input = htmlBody, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
461
+ # Give the program the data and wait for the program to finish.
462
+ #output = process.communicate(htmlBody)
463
+
464
+ # If it errored, throw it as an exception.
465
+ if process.returncode != 0:
466
+ raise WKError(process.stderr.decode('utf-8'))
467
+
468
+ return process.stdout
469
+
470
+ def getSaveRtfBody(self, **_) -> bytes:
471
+ """
472
+ Returns the RTF body that will be used in saving based on the arguments.
473
+
474
+ :param kwargs: Used to allow kwargs expansion in the save function.
475
+ Arguments absorbed by this are simply ignored.
476
+ """
477
+ # Inject the header into the data.
478
+ return self.injectRtfHeader()
479
+
480
+ def injectHtmlHeader(self, prepared: bool = False) -> bytes:
481
+ """
482
+ Returns the HTML body from the MSG file (will check that it has one)
483
+ with the HTML header injected into it.
484
+
485
+ :param prepared: Determines whether to be using the standard HTML
486
+ (``False``) or the prepared HTML (``True``) body. (Default: ``False``)
487
+
488
+ :raises AttributeError: The correct HTML body cannot be acquired.
489
+ """
490
+ if not self.htmlBody:
491
+ raise AttributeError('Cannot inject the HTML header without an HTML body attribute.')
492
+
493
+ body = None
494
+
495
+ # We don't do this all at once because the prepared body is not cached.
496
+ if prepared:
497
+ body = self.htmlBodyPrepared
498
+
499
+ # If the body is not valid or not found, raise an AttributeError.
500
+ if not body:
501
+ raise AttributeError('Cannot find a prepared HTML body to inject into.')
502
+ else:
503
+ body = self.htmlBody
504
+
505
+ # Validate the HTML.
506
+ if not validateHtml(body):
507
+ logger.warning('HTML body failed to validate. Code will attempt to correct it.')
508
+
509
+ # If we are here, then we need to do what we can to fix the HTML
510
+ # body. Unfortunately this gets complicated because of the various
511
+ # ways the body could be wrong. If only the <body> tag is missing,
512
+ # then we just need to insert it at the end and be done. If both
513
+ # the <html> and <body> tag are missing, we determine where to put
514
+ # the body tag (around everything if there is no <head> tag,
515
+ # otherwise at the end) and then wrap it all in the <html> tag.
516
+ parser = bs4.BeautifulSoup(body, features = 'html.parser')
517
+ if not parser.find('html') and not parser.find('body'):
518
+ if parser.find('head') or parser.find('footer'):
519
+ # Create the parser we will be using for the corrections.
520
+ correctedHtml = bs4.BeautifulSoup(b'<html></html>', features = 'html.parser')
521
+ htmlTag = correctedHtml.find('html')
522
+
523
+ # Iterate over each of the direct descendents of the parser and
524
+ # add each to a new tag if they are not the head or footer.
525
+ bodyTag = parser.new_tag('body')
526
+ # What we are going to be doing will be causing some of the tags
527
+ # to be moved out of the parser, and so the iterator will end up
528
+ # pointing to the wrong place after that. To compensate we first
529
+ # create a tuple and iterate over that.
530
+ for tag in tuple(parser.children):
531
+ if tag.name.lower() in ('head', 'footer'):
532
+ correctedHtml.append(tag)
533
+ else:
534
+ bodyTag.append(tag)
535
+
536
+ # All the tags should now be properly in the body, so let's
537
+ # insert it.
538
+ if correctedHtml.find('head'):
539
+ correctedHtml.find('head').insert_after(bodyTag)
540
+ elif correctedHtml.find('footer'):
541
+ correctedHtml.find('footer').insert_before(bodyTag)
542
+ else:
543
+ # Neither a head or a body are present, so just append it to
544
+ # the main tag.
545
+ htmlTag.append(bodyTag)
546
+ else:
547
+ # If there is no <html>, <head>, <footer>, or <body> tag, then
548
+ # we just add the tags to the beginning and end of the data and
549
+ # move on.
550
+ body = b'<html><body>' + body + b'</body></html>'
551
+ elif parser.find('html'):
552
+ # Found <html> but not <body>.
553
+ # Iterate over each of the direct descendents of the parser and
554
+ # add each to a new tag if they are not the head or footer.
555
+ bodyTag = parser.new_tag('body')
556
+ # What we are going to be doing will be causing some of the tags
557
+ # to be moved out of the parser, and so the iterator will end up
558
+ # pointing to the wrong place after that. To compensate we first
559
+ # create a tuple and iterate over that.
560
+ for tag in tuple(parser.find('html').children):
561
+ if tag.name and tag.name.lower() not in ('head', 'footer'):
562
+ bodyTag.append(tag)
563
+
564
+ # All the tags should now be properly in the body, so let's
565
+ # insert it.
566
+ if parser.find('head'):
567
+ parser.find('head').insert_after(bodyTag)
568
+ elif parser.find('footer'):
569
+ parser.find('footer').insert_before(bodyTag)
570
+ else:
571
+ parser.find('html').insert(0, bodyTag)
572
+ else:
573
+ # Found <body> but not <html>. Just wrap everything in the <html>
574
+ # tags.
575
+ body = b'<html>' + body + b'</html>'
576
+
577
+ def replace(bodyMarker):
578
+ """
579
+ Internal function to replace the body tag with itself plus the
580
+ header.
581
+ """
582
+ # I recently had to change this and how it worked. Now we use a new
583
+ # property of `MSGFile` that returns a special tuple of tuples to define
584
+ # how to get all of the properties we are formatting. They are all
585
+ # processed in the same way, making everything neat. By defining them
586
+ # in each class, any class can specify a completely different set to be
587
+ # used.
588
+ return bodyMarker.group() + self.htmlInjectableHeader.encode('utf-8')
589
+
590
+ # Use the previously defined function to inject the HTML header.
591
+ return constants.re.HTML_BODY_START.sub(replace, body, 1)
592
+
593
+ def injectRtfHeader(self) -> bytes:
594
+ """
595
+ Returns the RTF body from this MSG file (will check that it has one)
596
+ with the RTF header injected into it.
597
+
598
+ :raises AttributeError: The RTF body cannot be acquired.
599
+ :raises RuntimeError: All injection attempts failed.
600
+ """
601
+ if not self.rtfBody:
602
+ raise AttributeError('Cannot inject the RTF header without an RTF body attribute.')
603
+
604
+ # Try to determine which header to use. Also determines how to sanitize the
605
+ # rtf.
606
+ if isEncapsulatedRtf(self.rtfBody):
607
+ injectableHeader = self.rtfEncapInjectableHeader
608
+ else:
609
+ injectableHeader = self.rtfPlainInjectableHeader
610
+
611
+ def replace(bodyMarker):
612
+ """
613
+ Internal function to replace the body tag with itself plus the
614
+ header.
615
+ """
616
+ return bodyMarker.group() + injectableHeader
617
+
618
+ # This first method only applies to documents with encapsulated HTML
619
+ # that is formatted in a nice way.
620
+ if isEncapsulatedRtf(self.rtfBody):
621
+ data = constants.re.RTF_ENC_BODY_START.sub(replace, self.rtfBody, 1)
622
+ if data != self.rtfBody:
623
+ logger.debug('Successfully injected RTF header using encapsulation method.')
624
+ return data
625
+ logger.debug('RTF has encapsulated HTML, but injection method failed. It is likely dirty. Will use normal RTF injection method.')
626
+
627
+ # If the normal encapsulated HTML injection fails or it isn't
628
+ # encapsulated, use the internal _rtf module.
629
+ logger.debug('Using _rtf module to inject RTF text header.')
630
+ return createDocument(injectStartRTF(self.rtfBody, injectableHeader))
631
+
632
+ def save(self, **kwargs) -> constants.SAVE_TYPE:
633
+ """
634
+ Saves the message body and attachments found in the message.
635
+
636
+ The body and attachments are stored in a folder in the current running
637
+ directory unless :param customPath: has been specified. The name of the
638
+ folder will be determined by 3 factors.
639
+
640
+ * If :param customFilename: has been set, the value provided for that
641
+ will be used.
642
+ * If :param useMsgFilename: has been set, the name of the file used
643
+ to create the Message instance will be used.
644
+ * If the file name has not been provided or :param useMsgFilename:
645
+ has not been set, the name of the folder will be created using
646
+ :property defaultFolderName:.
647
+ * Setting :param maxNameLength: will force all file names to be
648
+ shortened to fit in the space (with the extension included in the
649
+ length). If a number is added to the directory that will not be
650
+ included in the length, so it is recommended to plan for up to 5
651
+ characters extra to be a part of the name. Default is 256.
652
+
653
+ It should be noted that regardless of the value for
654
+ :param maxNameLength:, the name of the file containing the body will always have the name 'message' followed by the full extension.
655
+
656
+ There are several parameters used to determine how the message will be
657
+ saved. By default, the message will be saved as plain text. Setting one
658
+ of the following parameters to ``True`` will change that:
659
+
660
+ * :param html: will output the message in HTML format.
661
+ * :param json: will output the message in JSON format.
662
+ * :param raw: will output the message in a raw format.
663
+ * :param rtf: will output the message in RTF format.
664
+
665
+ Usage of more than one formatting parameter will raise an exception.
666
+
667
+ Using HTML or RTF will raise an exception if they could not be retrieved
668
+ unless you have :param allowFallback: set to ``True``. Fallback will go
669
+ in this order, starting at the top most format that is set:
670
+
671
+ * HTML
672
+ * RTF
673
+ * Plain text
674
+
675
+ If you want to save the contents into a ``ZipFile`` or similar object,
676
+ either pass a path to where you want to create one or pass an instance
677
+ to :param zip:. If :param zip: is set, :param customPath: will refer to
678
+ a location inside the zip file.
679
+
680
+ :param attachmentsOnly: Turns off saving the body and only saves the
681
+ attachments when set.
682
+ :param saveHeader: Turns on saving the header as a separate file when
683
+ set.
684
+ :param skipAttachments: Turns off saving attachments.
685
+ :param skipHidden: If ``True``, skips attachments marked as hidden.
686
+ (Default: ``False``)
687
+ :param skipBodyNotFound: Suppresses errors if no valid body could be
688
+ found, simply skipping the step of saving the body.
689
+ :param charset: If the HTML is being prepared, the charset to use for
690
+ the Content-Type meta tag to insert. This exists to ensure that
691
+ something parsing the HTML can properly determine the encoding (as
692
+ not having this tag can cause errors in some programs). Set this to
693
+ ``None`` or an empty string to not insert the tag (Default:
694
+ ``'utf-8'``).
695
+ :param kwargs: Used to allow kwargs expansion in the save function.
696
+ :param preparedHtml: When set, prepares the HTML body for standalone
697
+ usage, doing things like adding tags, injecting attachments, etc.
698
+ This is useful for things like trying to convert the HTML body
699
+ directly to PDF.
700
+ :param pdf: Used to enable saving the body as a PDF file.
701
+ :param wkPath: Used to manually specify the path of the wkhtmltopdf
702
+ executable. If not specified, the function will try to find it.
703
+ Useful if wkhtmltopdf is not on the path. If :param pdf: is False,
704
+ this argument is ignored.
705
+ :param wkOptions: Used to specify additional options to wkhtmltopdf.
706
+ this must be a list or list-like object composed of strings and
707
+ bytes.
708
+ """
709
+ # Move keyword arguments into variables.
710
+ _json = kwargs.get('json', False)
711
+ html = kwargs.get('html', False)
712
+ rtf = kwargs.get('rtf', False)
713
+ raw = kwargs.get('raw', False)
714
+ pdf = kwargs.get('pdf', False)
715
+ allowFallback = kwargs.get('allowFallback', False)
716
+ _zip = kwargs.get('zip')
717
+ maxNameLength = kwargs.get('maxNameLength', 256)
718
+
719
+ # Variables involved in the save location.
720
+ customFilename = kwargs.get('customFilename')
721
+ useMsgFilename = kwargs.get('useMsgFilename', False)
722
+ #maxPathLength = kwargs.get('maxPathLength', 255)
723
+
724
+ # Track if we are only saving the attachments.
725
+ attachOnly = kwargs.get('attachmentsOnly', False)
726
+ # Track if we are skipping attachments.
727
+ skipAttachments = kwargs.get('skipAttachments', False)
728
+ skipHidden = kwargs.get('skipHidden', False)
729
+ # Track if we should skip the body if no valid body is found instead of
730
+ # raising an exception.
731
+ skipBodyNotFound = kwargs.get('skipBodyNotFound', False)
732
+
733
+ if pdf:
734
+ kwargs['preparedHtml'] = True
735
+
736
+ # Try to get the body, if needed, before messing with the path.
737
+ if not attachOnly:
738
+ # Check what to save the body with.
739
+ fext = 'json' if _json else 'txt'
740
+
741
+ fallbackToPlain = False
742
+ useHtml = False
743
+ usePdf = False
744
+ useRtf = False
745
+ if html:
746
+ if self.htmlBody:
747
+ useHtml = True
748
+ fext = 'html'
749
+ elif not allowFallback:
750
+ if skipBodyNotFound:
751
+ fext = None
752
+ else:
753
+ raise DataNotFoundError('Could not find the htmlBody.')
754
+
755
+ if pdf:
756
+ if self.htmlBody:
757
+ usePdf = True
758
+ fext = 'pdf'
759
+ elif not allowFallback:
760
+ if skipBodyNotFound:
761
+ fext = None
762
+ else:
763
+ raise DataNotFoundError('Count not find the htmlBody to convert to pdf.')
764
+
765
+ if rtf or (html and not useHtml) or (pdf and not usePdf):
766
+ if self.rtfBody:
767
+ useRtf = True
768
+ fext = 'rtf'
769
+ elif not allowFallback:
770
+ if skipBodyNotFound:
771
+ fext = None
772
+ else:
773
+ raise DataNotFoundError('Could not find the rtfBody.')
774
+ else:
775
+ # This was the last resort before plain text, so fall
776
+ # back to that.
777
+ fallbackToPlain = True
778
+
779
+ # After all other options, try to go with plain text if
780
+ # possible.
781
+ if not (rtf or html or pdf) or fallbackToPlain:
782
+ # We need to check if the plain text body was found. If it
783
+ # was found but was empty that is considered valid, so we
784
+ # specifically check against None.
785
+ if self.body is None:
786
+ if skipBodyNotFound:
787
+ fext = None
788
+ else:
789
+ if allowFallback:
790
+ raise DataNotFoundError('Could not find a valid body using current options.')
791
+ else:
792
+ raise DataNotFoundError('Plain text body could not be found.')
793
+
794
+ createdZip = False
795
+ try:
796
+ # ZipFile handling.
797
+ if _zip:
798
+ # `raw` and `zip` are incompatible.
799
+ if raw:
800
+ raise IncompatibleOptionsError('The options `raw` and `zip` are incompatible.')
801
+ # If we are doing a zip file, first check that we have been
802
+ # given a path.
803
+ if isinstance(_zip, (str, pathlib.Path)):
804
+ # If we have a path then we use the zip file.
805
+ _zip = zipfile.ZipFile(_zip, 'a', zipfile.ZIP_DEFLATED)
806
+ kwargs['zip'] = _zip
807
+ createdZip = True
808
+ # Path needs to be done in a special way if we are in a zip
809
+ # file.
810
+ path = pathlib.Path(kwargs.get('customPath', ''))
811
+ # Set the open command to be that of the zip file.
812
+ _open = createZipOpen(_zip.open)
813
+ # Zip files use w for writing in binary.
814
+ mode = 'w'
815
+ else:
816
+ path = pathlib.Path(kwargs.get('customPath', '.')).absolute()
817
+ mode = 'wb'
818
+ _open = open
819
+
820
+ # Reset this for sub save calls.
821
+ kwargs['customFilename'] = None
822
+
823
+ # Check if incompatible options have been provided in any way.
824
+ if _json + html + rtf + raw + attachOnly + pdf > 1:
825
+ raise IncompatibleOptionsError('Only one of the following options may be used at a time: json, raw, html, rtf, attachmentsOnly, pdf.')
826
+
827
+ # TODO: insert code here that will handle checking all of the msg
828
+ # files to see if the path with overflow.
829
+
830
+ if customFilename:
831
+ # First we need to validate it. If there are invalid characters,
832
+ # this will detect it.
833
+ if constants.re.INVALID_FILENAME_CHARS.search(customFilename):
834
+ raise ValueError('Invalid character found in customFilename. Must not contain any of the following characters: \\/:*?"<>|')
835
+ # Quick fix to remove spaces from the end of the filename, if
836
+ # any are there.
837
+ customFilename = customFilename.strip()
838
+ path /= customFilename[:maxNameLength]
839
+ elif useMsgFilename:
840
+ if not self.filename:
841
+ raise ValueError(':param useMsgFilename: is only available if you are using an MSG file on the disk or have provided a filename.')
842
+ # Get the actual name of the file.
843
+ filename = os.path.split(self.filename)[1]
844
+ # Remove the extensions.
845
+ filename = os.path.splitext(filename)[0]
846
+ # Prepare the filename by removing any special characters.
847
+ filename = prepareFilename(filename)
848
+ # Shorted the filename.
849
+ filename = filename[:maxNameLength]
850
+ # Check to make sure we actually have a filename to use.
851
+ if not filename:
852
+ raise ValueError(f'Invalid filename found in self.filename: "{self.filename}"')
853
+
854
+ # Add the file name to the path.
855
+ path /= filename[:maxNameLength]
856
+ else:
857
+ path /= self.defaultFolderName[:maxNameLength]
858
+
859
+ # Create the folders.
860
+ if not _zip:
861
+ try:
862
+ os.makedirs(path)
863
+ except Exception:
864
+ newDirName = addNumToDir(path)
865
+ if newDirName:
866
+ path = newDirName
867
+ else:
868
+ raise OSError(f'Failed to create directory "{path}". Does it already exist?')
869
+ else:
870
+ # In my testing I ended up with multiple files in a zip at the
871
+ # same location so let's try to handle that.
872
+ pathCompare = str(path).replace('\\', '/').rstrip('/') + '/'
873
+ if any(x.startswith(pathCompare) for x in _zip.namelist()):
874
+ newDirName = addNumToZipDir(path, _zip)
875
+ if newDirName:
876
+ path = newDirName
877
+ else:
878
+ raise Exception(f'Failed to create directory "{path}". Does it already exist?')
879
+
880
+ # Update the kwargs.
881
+ kwargs['customPath'] = path
882
+
883
+ if raw:
884
+ self.saveRaw(path)
885
+ return (SaveType.FOLDER, str(path))
886
+
887
+ # If the user has requested the headers for this file, save it now.
888
+ if kwargs.get('saveHeader', False):
889
+ headerText = self.headerText
890
+ if not headerText:
891
+ headerText = constants.HEADER_FORMAT.format(subject = self.subject, **self.header)
892
+
893
+ with _open(str(path / 'header.txt'), mode) as f:
894
+ f.write(headerText.encode('utf-8'))
895
+
896
+
897
+ if not skipAttachments:
898
+ # Save the attachments.
899
+ attachmentReturns = [attachment.save(**kwargs) for attachment in self.attachments if not (skipHidden and attachment.hidden)]
900
+ # Get the names from each.
901
+ attachmentNames = []
902
+ for x in attachmentReturns:
903
+ if isinstance(x[1], str):
904
+ attachmentNames.append(x[1])
905
+ elif isinstance(x[1], list):
906
+ attachmentNames.extend(x[1])
907
+
908
+ if not attachOnly and fext:
909
+ with _open(str(path / ('message.' + fext)), mode) as f:
910
+ if _json:
911
+ emailObj = json.loads(self.getJson())
912
+ if not skipAttachments:
913
+ emailObj['attachments'] = attachmentNames
914
+
915
+ f.write(json.dumps(emailObj).encode('utf-8'))
916
+ elif useHtml:
917
+ f.write(self.getSaveHtmlBody(**kwargs))
918
+ elif usePdf:
919
+ f.write(self.getSavePdfBody(**kwargs))
920
+ elif useRtf:
921
+ f.write(self.getSaveRtfBody(**kwargs))
922
+ else:
923
+ f.write(self.getSaveBody(**kwargs))
924
+
925
+ return (SaveType.FOLDER, str(path))
926
+ finally:
927
+ # Close the ZipFile if this function created it.
928
+ if _zip and createdZip:
929
+ _zip.close()
930
+
931
+ @functools.cached_property
932
+ def bcc(self) -> Optional[str]:
933
+ """
934
+ The "Bcc" field, if it exists.
935
+ """
936
+ return self._genRecipient('bcc', RecipientType.BCC)
937
+
938
+ @functools.cached_property
939
+ def body(self) -> Optional[str]:
940
+ """
941
+ The message body, if it exists.
942
+ """
943
+ # If the body exists but is empty, that means it should be returned.
944
+ if (body := self.getStringStream('__substg1.0_1000')) is not None:
945
+ pass
946
+ elif self.rtfBody:
947
+ # If the body doesn't exist, see if we can get it from the RTF
948
+ # body.
949
+ body = self.deencapsulateBody(self.rtfBody, DeencapType.PLAIN)
950
+
951
+ if body:
952
+ body = inputToString(body, 'utf-8')
953
+ if re.search('\n', body) is not None:
954
+ if re.search('\r\n', body) is not None:
955
+ self._crlf = '\r\n'
956
+
957
+ return body
958
+
959
+ @functools.cached_property
960
+ def cc(self) -> Optional[str]:
961
+ """
962
+ The "Cc" field, if it exists.
963
+ """
964
+ return self._genRecipient('cc', RecipientType.CC)
965
+
966
+ @functools.cached_property
967
+ def compressedRtf(self) -> Optional[bytes]:
968
+ """
969
+ The compressed RTF stream, if it exists.
970
+ """
971
+ return self.getStream('__substg1.0_10090102')
972
+
973
+ @property
974
+ def crlf(self) -> str:
975
+ """
976
+ The value of ``self.__crlf``, should you need it for whatever reason.
977
+ """
978
+ return self._crlf
979
+
980
+ @functools.cached_property
981
+ def date(self) -> Optional[datetime.datetime]:
982
+ """
983
+ The send date, if it exists.
984
+ """
985
+ return self.props.date if self.isSent else None
986
+
987
+ @functools.cached_property
988
+ def deencapsulatedRtf(self) -> Optional[RTFDE.DeEncapsulator]:
989
+ """
990
+ The instance of the deencapsulated RTF body.
991
+
992
+ If there is no RTF body or the body is not encasulated, returns
993
+ ``None``.
994
+ """
995
+ if self.rtfBody:
996
+ # If there is an RTF body, we try to deencapsulate it.
997
+ body = self.rtfBody
998
+ # Sometimes you get MSG files whose RTF body has stuff
999
+ # *after* the body, and RTFDE can't handle that. Here is
1000
+ # how we compensate.
1001
+ while body and body[-1] != 125:
1002
+ body = body[:-1]
1003
+
1004
+ try:
1005
+ deencapsultor = RTFDE.DeEncapsulator(body)
1006
+ deencapsultor.deencapsulate()
1007
+ return deencapsultor
1008
+ except RTFDE.exceptions.NotEncapsulatedRtf:
1009
+ logger.debug('RTF body is not encapsulated.')
1010
+ except RTFDE.exceptions.MalformedEncapsulatedRtf:
1011
+ if ErrorBehavior.RTFDE_MALFORMED not in self.errorBehavior:
1012
+ raise
1013
+ logger.info('RTF body contains malformed encapsulated content.')
1014
+ except Exception:
1015
+ # If we are just ignoring the errors, log it then set to
1016
+ # None. Otherwise, continue the exception.
1017
+ if ErrorBehavior.RTFDE_UNKNOWN_ERROR not in self.errorBehavior:
1018
+ raise
1019
+ logger.exception('Unhandled error happened while using RTFDE. You have choosen to ignore these errors.')
1020
+ return None
1021
+
1022
+ @property
1023
+ def defaultFolderName(self) -> str:
1024
+ """
1025
+ Generates the default name of the save folder.
1026
+ """
1027
+ try:
1028
+ return self._defaultFolderName
1029
+ except AttributeError:
1030
+ d = self.parsedDate or tuple([0] * 9)
1031
+
1032
+ dirName = '{0:02d}-{1:02d}-{2:02d}_{3:02d}{4:02d}'.format(*d) if d else 'UnknownDate'
1033
+ dirName += ' ' + (prepareFilename(self.subject) if self.subject else '[No subject]')
1034
+ dirName = dirName.strip()
1035
+
1036
+ self._defaultFolderName = dirName
1037
+ return dirName
1038
+
1039
+ @functools.cached_property
1040
+ def detectedBodies(self) -> BodyTypes:
1041
+ """
1042
+ The types of bodies stored in the .msg file.
1043
+ """
1044
+ bodies = BodyTypes.NONE
1045
+ if self.sExists('__substg1.0_1000'):
1046
+ bodies |= BodyTypes.PLAIN
1047
+ if self.exists('__substg1.0_10090102'):
1048
+ bodies |= BodyTypes.RTF
1049
+ if self.exists('__substg1.0_10130102'):
1050
+ bodies |= BodyTypes.HTML
1051
+
1052
+ return bodies
1053
+
1054
+ @functools.cached_property
1055
+ def header(self) -> email.message.Message:
1056
+ """
1057
+ The message header, if it exists.
1058
+
1059
+ If one does not exist as a stream, it will be generated from the other
1060
+ properties.
1061
+ """
1062
+ headerText = self.headerText
1063
+ if headerText:
1064
+ # Fix an issue with prefixed headers not parsing correctly.
1065
+ if headerText.startswith('Microsoft Mail Internet Headers Version 2.0'):
1066
+ headerText = headerText[43:].lstrip()
1067
+ header = HeaderParser(policy = policy.compat32).parsestr(headerText)
1068
+ else:
1069
+ logger.info('Header is empty or was not found. Header will be generated from other streams.')
1070
+ header = HeaderParser(policy = policy.compat32).parsestr('')
1071
+ if self.date:
1072
+ header.add_header('Date', email.utils.format_datetime(self.date))
1073
+ header.add_header('From', self.sender)
1074
+ header.add_header('To', self.to)
1075
+ header.add_header('Cc', self.cc)
1076
+ header.add_header('Bcc', self.bcc)
1077
+ header.add_header('Message-Id', self.messageId)
1078
+ # TODO find authentication results outside of header
1079
+ header.add_header('Authentication-Results', None)
1080
+
1081
+ self.__headerInit = True
1082
+ return header
1083
+
1084
+ @functools.cached_property
1085
+ def headerDict(self) -> Dict[str, Any]:
1086
+ """
1087
+ A dictionary of the entries in the header
1088
+ """
1089
+ headerDict = {x: self.header[x] for x in self.header}
1090
+ try:
1091
+ headerDict.pop('Received')
1092
+ except KeyError:
1093
+ pass
1094
+ return headerDict
1095
+
1096
+ @property
1097
+ def headerFormatProperties(self) -> constants.HEADER_FORMAT_TYPE:
1098
+ """
1099
+ Returns a dictionary of properties, in order, to be formatted into the
1100
+ header.
1101
+
1102
+ Keys are the names to use in the header while the values are one of the
1103
+ following:
1104
+
1105
+ * ``None``: Signifies no data was found for the property and it should
1106
+ be omitted from the header.
1107
+ * ``str``: A string to be formatted into the header using the string
1108
+ encoding.
1109
+ * ``Tuple[Union[str, None], bool]``: A string should be formatted into
1110
+ the header. If the bool is ``True``, then place an empty string if
1111
+ the first value is ``None``, otherwise follow the same behavior as
1112
+ regular ``None``.
1113
+
1114
+ Additional note: If the value is an empty string, it will be dropped as
1115
+ well by default.
1116
+
1117
+ Additionally you can group members of a header together by placing them
1118
+ in an embedded dictionary. Groups will be spaced out using a second
1119
+ instance of the join string. If any member of a group is being printed,
1120
+ it will be spaced apart from the next group/item.
1121
+
1122
+ If you class should not do *any* header injection, return ``None`` from
1123
+ this property.
1124
+ """
1125
+ # Checking outlook printing, default behavior is to completely omit
1126
+ # *any* field that is not present. So while for extensability the
1127
+ # option exists to have it be present even if no data is found, we are
1128
+ # specifically not doing that.
1129
+ return {
1130
+ '-basic info-': {
1131
+ 'From': self.sender,
1132
+ 'Sent': self.date.__format__(self.datetimeFormat) if self.date else None,
1133
+ 'To': self.to,
1134
+ 'Cc': self.cc,
1135
+ 'Bcc': self.bcc,
1136
+ 'Subject': self.subject,
1137
+ },
1138
+ '-importance-': {
1139
+ 'Importance': self.importanceString,
1140
+ },
1141
+ }
1142
+
1143
+ @property
1144
+ def headerInit(self) -> bool:
1145
+ """
1146
+ Checks whether the header has been initialized.
1147
+ """
1148
+ return self.__headerInit
1149
+
1150
+ @functools.cached_property
1151
+ def headerText(self) -> Optional[str]:
1152
+ """
1153
+ The raw text of the header stream, if it exists.
1154
+ """
1155
+ return self.getStringStream('__substg1.0_007D')
1156
+
1157
+ @functools.cached_property
1158
+ def htmlBody(self) -> Optional[bytes]:
1159
+ """
1160
+ The HTML body, if it exists.
1161
+ """
1162
+ if (htmlBody := self.getStream('__substg1.0_10130102')) is not None:
1163
+ pass
1164
+ elif self.rtfBody:
1165
+ logger.info('HTML body was not found, attempting to generate from RTF.')
1166
+ htmlBody = self.deencapsulateBody(self.rtfBody, DeencapType.HTML)
1167
+ # This is it's own if statement so we can ensure it will generate
1168
+ # even if there is an rtfBody, in the event it doesn't have HTML.
1169
+ if not htmlBody and self.body:
1170
+ # Convert the plain text body to html.
1171
+ logger.info('HTML body was not found, attempting to generate from plain text body.')
1172
+ correctedBody = html.escape(self.body).replace('\r', '').replace('\n', '<br />')
1173
+ htmlBody = f'<html><body>{correctedBody}</body></head>'.encode('utf-8')
1174
+
1175
+ if not htmlBody:
1176
+ logger.info('HTML body could not be found nor generated.')
1177
+
1178
+ return htmlBody
1179
+
1180
+ @functools.cached_property
1181
+ def htmlBodyPrepared(self) -> Optional[bytes]:
1182
+ """
1183
+ The HTML body that has (where possible) the embedded attachments
1184
+ inserted into the body.
1185
+ """
1186
+ # If we can't get an HTML body then we have nothing to do.
1187
+ if not self.htmlBody:
1188
+ return self.htmlBody
1189
+
1190
+ # Create the BeautifulSoup instance to use.
1191
+ soup = bs4.BeautifulSoup(self.htmlBody, 'html.parser')
1192
+
1193
+ # Get a list of image tags to see if we can inject into. If the source
1194
+ # of an image starts with "cid:" that means it is one of the attachments
1195
+ # and is using the content id of that attachment.
1196
+ tags = (tag for tag in soup.findAll('img') if tag.get('src') and tag.get('src').startswith('cid:'))
1197
+
1198
+ for tag in tags:
1199
+ # Iterate through the attachments until we get the right one.
1200
+ cid = tag['src'][4:]
1201
+ data = next((attachment.data for attachment in self.attachments if attachment.cid == cid), None)
1202
+ # If we found anything, inject it.
1203
+ if data:
1204
+ tag['src'] = (b'data:image;base64,' + base64.b64encode(data)).decode('utf-8')
1205
+
1206
+ return soup.prettify('utf-8')
1207
+
1208
+ @functools.cached_property
1209
+ def htmlInjectableHeader(self) -> str:
1210
+ """
1211
+ The header that can be formatted and injected into the HTML body.
1212
+ """
1213
+ prefix = '<div id="injectedHeader"><div><p class="MsoNormal">'
1214
+ suffix = '<o:p></o:p></p></div></div>'
1215
+ joinStr = '<br/>'
1216
+ formatter = (lambda name, value: f'<b>{name}:</b>&nbsp;{inputToString(htmlSanitize(value), self.stringEncoding)}')
1217
+
1218
+ return self.getInjectableHeader(prefix, joinStr, suffix, formatter)
1219
+
1220
+ @functools.cached_property
1221
+ def inReplyTo(self) -> Optional[str]:
1222
+ """
1223
+ The message id that this message is in reply to.
1224
+ """
1225
+ return self.getStringStream('__substg1.0_1042')
1226
+
1227
+ @functools.cached_property
1228
+ def isRead(self) -> bool:
1229
+ """
1230
+ Whether this email has been marked as read.
1231
+ """
1232
+ return bool(self.getPropertyVal('0E070003', 0) & 1)
1233
+
1234
+ @functools.cached_property
1235
+ def isSent(self) -> bool:
1236
+ """
1237
+ Whether this email has been marked as sent.
1238
+
1239
+ Assumes ``True`` if no flags are found.
1240
+ """
1241
+ return not bool(self.getPropertyVal('0E070003', 0) & 8)
1242
+
1243
+ @functools.cached_property
1244
+ def messageId(self) -> Optional[str]:
1245
+ headerResult = None
1246
+ if self.headerInit:
1247
+ headerResult = self.header['message-id']
1248
+ if headerResult is not None:
1249
+ return headerResult
1250
+
1251
+ if self.headerInit:
1252
+ logger.info('Header found, but "Message-Id" is not included. Will be generated from other streams.')
1253
+ return self.getStringStream('__substg1.0_1035')
1254
+
1255
+ @functools.cached_property
1256
+ def parsedDate(self) -> Optional[Tuple[int, int, int, int, int, int, int, int, int]]:
1257
+ """
1258
+ A 9 tuple of the parsed date from the header.
1259
+ """
1260
+ return email.utils.parsedate(self.header['Date'])
1261
+
1262
+ @functools.cached_property
1263
+ def receivedTime(self) -> Optional[datetime.datetime]:
1264
+ """
1265
+ The date and time the message was received by the server.
1266
+ """
1267
+ return self.getPropertyVal('0E060040')
1268
+
1269
+ @property
1270
+ def recipientSeparator(self) -> str:
1271
+ return self.__recipientSeparator
1272
+
1273
+ @functools.cached_property
1274
+ def recipients(self) -> List[Recipient]:
1275
+ """
1276
+ A list of all recipients.
1277
+ """
1278
+ recipientDirs = []
1279
+ prefixLen = self.prefixLen
1280
+ for dir_ in self.listDir():
1281
+ if dir_[prefixLen].startswith('__recip') and\
1282
+ dir_[prefixLen] not in recipientDirs:
1283
+ recipientDirs.append(dir_[prefixLen])
1284
+
1285
+ return [Recipient(recipientDir, self, self.recipientTypeClass) for recipientDir in recipientDirs]
1286
+
1287
+ @property
1288
+ def recipientTypeClass(self) -> Type[enum.IntEnum]:
1289
+ """
1290
+ The class to use for a recipient's recipientType property.
1291
+
1292
+ The default is :class:`extract_msg.enums.RecipientType`. If a subclass's
1293
+ attributes have different meanings to the values, you can override this
1294
+ property to return a valid enum.
1295
+ """
1296
+ return RecipientType
1297
+
1298
+ @functools.cached_property
1299
+ def reportTag(self) -> Optional[ReportTag]:
1300
+ """
1301
+ Data that is used to correlate the report and the original message.
1302
+ """
1303
+ return self.getStreamAs('__substg1.0_00310102', ReportTag)
1304
+
1305
+ @functools.cached_property
1306
+ def responseRequested(self) -> bool:
1307
+ """
1308
+ Whether to send Meeting Response objects to the organizer.
1309
+ """
1310
+ return bool(self.getPropertyVal('0063000B'))
1311
+
1312
+ @functools.cached_property
1313
+ def rtfBody(self) -> Optional[bytes]:
1314
+ """
1315
+ The decompressed Rtf body from the message.
1316
+ """
1317
+ return compressed_rtf.decompress(self.compressedRtf) if self.compressedRtf else None
1318
+
1319
+ @functools.cached_property
1320
+ def rtfEncapInjectableHeader(self) -> bytes:
1321
+ """
1322
+ The header that can be formatted and injected into the plain RTF body.
1323
+ """
1324
+ prefix = r'\htmlrtf {\htmlrtf0 {\*\htmltag96 <div>}{\*\htmltag96 <div>}{\*\htmltag64 <p class=MsoNormal>}'
1325
+ suffix = r'{\*\htmltag244 <o:p>}{\*\htmlrag252 </o:p>}\htmlrtf \par\par\htmlrtf0 {\*\htmltag72 </p>}{\*\htmltag104 </div>}{\*\htmltag104 </div>}\htmlrtf }\htmlrtf0 '
1326
+ joinStr = r'{\*\htmltag116 <br />}\htmlrtf \line\htmlrtf0 '
1327
+ formatter = (lambda name, value: fr'\htmlrtf {{\b\htmlrtf0{{\*\htmltag84 <b>}}{name}: {{\*\htmltag92 </b>}}\htmlrtf \b0\htmlrtf0 {inputToString(rtfSanitizeHtml(value), self.stringEncoding)}\htmlrtf }}\htmlrtf0')
1328
+
1329
+ return self.getInjectableHeader(prefix, joinStr, suffix, formatter).encode('utf-8')
1330
+
1331
+ @functools.cached_property
1332
+ def rtfPlainInjectableHeader(self) -> bytes:
1333
+ """
1334
+ The header that can be formatted and injected into the encapsulated RTF
1335
+ body.
1336
+ """
1337
+ prefix = '{'
1338
+ suffix = '\\par\\par}'
1339
+ joinStr = '\\line'
1340
+ formatter = (lambda name, value: fr'{{\b {name}: \b0 {inputToString(rtfSanitizePlain(value), self.stringEncoding)}}}')
1341
+
1342
+ return self.getInjectableHeader(prefix, joinStr, suffix, formatter).encode('utf-8')
1343
+
1344
+ @functools.cached_property
1345
+ def sender(self) -> Optional[str]:
1346
+ """
1347
+ The message sender, if it exists.
1348
+ """
1349
+ # Check header first
1350
+ if self.headerInit:
1351
+ headerResult = self.header['from']
1352
+ if headerResult is not None:
1353
+ return decodeRfc2047(headerResult)
1354
+ logger.info('Header found, but "sender" is not included. Will be generated from other streams.')
1355
+ # Extract from other fields
1356
+ text = self.getStringStream('__substg1.0_0C1A')
1357
+ email = self.getStringStream('__substg1.0_5D01')
1358
+ # Will not give an email address sometimes. Seems to exclude the email
1359
+ # address if YOU are the sender.
1360
+ result = None
1361
+ if text is None:
1362
+ result = email
1363
+ else:
1364
+ result = text
1365
+ if email is not None:
1366
+ result += ' <' + email + '>'
1367
+
1368
+ return result
1369
+
1370
+ @functools.cached_property
1371
+ def subject(self) -> Optional[str]:
1372
+ """
1373
+ The message subject, if it exists.
1374
+ """
1375
+ return self.getStringStream('__substg1.0_0037')
1376
+
1377
+ @functools.cached_property
1378
+ def to(self) -> Optional[str]:
1379
+ """
1380
+ The "To" field, if it exists.
1381
+ """
1382
+ return self._genRecipient('to', RecipientType.TO)