Skip to content

Commit 7c22d15

Browse files
committed
Slightly more resilient emlx parsing.
1 parent e33c66d commit 7c22d15

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

emlx/message.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22

33
import plistlib # 3.4+
4+
import logging
45
from maildir_lite import MaildirMessage
56

67

@@ -12,23 +13,24 @@ class EmlxMessage(object):
1213
def __init__(self, message=None):
1314
if isinstance(message, bytes):
1415
# The size of the message is the first line of the file.
15-
start = end = 0
16-
end = message.find(b'\n')
17-
if end is -1:
16+
first_line = message.split(b"\n")[0]
17+
self.content_size = int(first_line)
18+
if self.content_size is 0:
1819
return
19-
self.content_size = int(message[start:end])
2020

2121
# Read in the message portion.
22-
start = end + 1
22+
start = len(first_line) + 1
2323
end = start + self.content_size
2424
if start > 0 and end > 0:
2525
self.content = message[start:end]
2626

2727
# Read in the plist metadata at the end.
2828
if start > 0 and end > 0:
2929
meta = message[end:]
30-
if meta:
30+
try:
3131
self.plist = plistlib.loads(meta)
32+
except:
33+
logging.error("failed to parse message metadata plist")
3234

3335
def __str__(self):
3436
if not self.content:
@@ -105,6 +107,13 @@ def get_maildir_message(self):
105107
m.set_date(self.date_received)
106108

107109
flags_dict = self.flags
110+
111+
# If the plist failed to parse, this will be empty.
112+
# We do lose state here, but there's little to be done
113+
# at this point.
114+
if len(flags_dict) == 0:
115+
return m
116+
108117
if flags_dict['draft']:
109118
m.add_flag('D')
110119
if flags_dict['flagged']:

0 commit comments

Comments
 (0)