blob: 30a53641c20a6c5c3841d1d719199df39cf80272 (
plain)
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
|
from datetime import datetime
import random
import calendar
class Mail:
NOW = calendar.timegm(
datetime.strptime(
datetime.now().isoformat(),
"%Y-%m-%dT%H:%M:%S.%f").timetuple())
@staticmethod
def from_json(mail_json):
mail = Mail()
mail.header = mail_json['header']
mail.header['date'] = datetime.now().isoformat()
mail.ident = mail_json.get('ident', 0)
mail.body = mail_json['body']
mail.tags = mail_json['tags']
mail.security_casing = {}
mail.status = []
mail.draft_reply_for = mail_json.get('draft_reply_for', 0)
return mail
def __init__(self, mbox_mail=None, ident=None):
if mbox_mail:
self.header = self._get_headers(mbox_mail)
self.ident = ident
self.body = self._get_body(mbox_mail)
self.tags = self._get_tags(mbox_mail)
self.security_casing = {}
self.status = self._get_status()
self.draft_reply_for = -1
def _get_body(self, message):
if message.is_multipart():
boundary = '--{boundary}'.format(
boundary=message.get_boundary().strip())
body_parts = [x.as_string() for x in message.get_payload()]
body = boundary + '\n'
body += '{boundary}\n'.format(boundary=boundary).join(body_parts)
body += '{boundary}--\n'.format(boundary=boundary)
return body
else:
return message.get_payload()
def _get_status(self):
status = []
if 'sent' in self.tags:
status.append('read')
return status
def _get_headers(self, mbox_mail):
headers = {}
headers['from'] = mbox_mail.get_from()
headers['to'] = [mbox_mail.get('To')]
headers['subject'] = mbox_mail.get('Subject')
headers['date'] = datetime.fromtimestamp(
random.randrange(
1222222222,
self.NOW)).isoformat()
headers['content_type'] = mbox_mail.get('Content-Type')
return headers
def _get_tags(self, mbox_mail):
return mbox_mail.get('X-TW-Pixelated-Tags').split(', ')
@property
def subject(self):
return self.header['subject']
@property
def date(self):
return self.header['date']
|