-
Notifications
You must be signed in to change notification settings - Fork 0
/
messagestore.py
252 lines (209 loc) · 8.18 KB
/
messagestore.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
# Message store is a collection of all of our
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import uuid
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship, backref
from email.parser import Parser
from sqlalchemy.types import Boolean
import re
from dateutil import parser
#An email message
import uuid
import time
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, DateTime
import datetime
Base = declarative_base()
#message object
class Message(Base):
__tablename__ = 'messages'
id = Column(String, primary_key=True)
body = Column(String)
subject = Column(String)
fromaddress = Column(String)
datetime = Column(DateTime)
senderishistorygraphenabled = Column(Boolean)
messageisencrypted = Column(Boolean)
addresses = relationship("Address", order_by="Address.id", backref="messages")
tags = relationship("Tag", order_by="Tag.id", backref="messages")
def __init__(self):
self.id = str(uuid.uuid1())
#self.direction = "None"
#self.to = list()
self.fromaddress = ""
#self.replyto = list()
#self.cc = list()
#self.bcc = list()
self.body = ""
self.subject = ""
self.datetime = datetime.datetime.now()
self.senderishistorygraphenabled = False
@staticmethod
def fromrawbody(rawbody):
emailmsg = Parser().parsestr(rawbody)
ret = Message()
ret.subject = emailmsg["subject"]
ret.fromaddress = emailmsg["From"]
toaddress = Address()
toaddress.email_address = emailmsg["to"]
toaddress.message_id = ret.id
toaddress.addresstype = "To"
ret.addresses.append(toaddress)
if "Date" in emailmsg:
ret.datetime = parser.parse(emailmsg["Date"])
else:
ret.datetime = datetime.datetime.now()
if emailmsg.is_multipart() == False:
ret.body = emailmsg.get_payload()
else:
for part in emailmsg.walk():
if part.get_content_type() == "text/plain":
ret.body = part.get_payload()
lines = ret.body.split("\n")
foundfirstsigline = False
foundsecondsigline = False
foundthirdsigline = False
for line in lines:
if line[:2] != "--":
if foundfirstsigline == False and foundsecondsigline == False \
and foundthirdsigline == False:
if re.match("^=+$", line) is not None:
foundfirstsigline = True
elif foundfirstsigline == True and foundsecondsigline == False \
and foundthirdsigline == False:
if re.match("^HistoryGraph enabled emailer http://wwww.historygraph.io \([a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\)$", line):
foundsecondsigline = True
else:
foundfirstsigline = False
elif foundfirstsigline == True and foundsecondsigline == True \
and foundthirdsigline == False:
if re.match("^=+$", line) is not None:
ret.senderishistorygraphenabled = True
foundfirstsigline = False
foundsecondsigline = False
return ret
class Address(Base):
__tablename__ = 'addresses'
id = Column(String, primary_key=True)
email_address = Column(String, nullable=False)
message_id = Column(String, ForeignKey('messages.id'))
addresstype = Column(String, nullable=False) #Can be To, CC or BCC
#message = relationship("Message", backref=backref('addresses', order_by=id))
def __init__(self):
self.id = str(uuid.uuid1())
def __repr__(self):
return "<Address(email_address='%s')>" % self.email_address
class Contact(Base):
__tablename__ = 'contacts'
id = Column(String, primary_key=True)
name = Column(String)
emailaddress = Column(String)
ishistorygraph = Column(Boolean)
publickey = Column(String)
def __init__(self):
self.id = str(uuid.uuid1())
self.name = ""
self.emailaddress = ""
self.ishistorygraph = False
self.publickey = ""
def CleanedEmailAddress(emailaddress):
i = emailaddress.find('<')
if i == -1:
return emailaddress
j = emailaddress.find('>')
if j <= i:
return emailaddress
return emailaddress[i+1:j]
class Tag(Base):
__tablename__ = 'tags'
id = Column(String, primary_key=True)
tagname = Column(String)
message_id = Column(String, ForeignKey('messages.id'))
def __init__(self):
self.id = str(uuid.uuid1())
self.tagname = ""
class ContactStore:
def GetContactsByFilter(self, filter):
return filter.applyFilter(self.demux.session).all()
def AddContact(self, contact):
self.demux.session.add(contact)
self.demux.session.commit()
def GetContacts(self):
#Return all contacts unfiltered
return self.demux.session.query(Contact)
def GetContactsByEmailAddress(self, emailaddress):
return self.demux.session.query(Contact).filter(Contact.emailaddress.like(emailaddress))
def __init__(self, demux):
self.demux = demux
class MessageStore:
def GetMessagesByFilter(self, filter):
return sorted(filter.applyFilter(self.demux.session), key=lambda message: message.datetime)
def AddMessage(self, message, makehistorygraphhandler):
self.demux.session.add(message)
self.demux.session.commit()
l = self.demux.contactstore.GetContactsByEmailAddress(message.fromaddress)
if l.count() == 0:
contact = Contact()
contact.name = message.fromaddress
contact.emailaddress = message.fromaddress
contact.ishistorygraph = message.senderishistorygraphenabled
self.demux.contactstore.AddContact(contact)
if makehistorygraphhandler:
makehistorygraphhandler(contact)
elif l.count() == 1:
if message.senderishistorygraphenabled == True:
contact = l.first()
contact.ishistorygraph = True
self.demux.contactstore.AddContact(contact)
if makehistorygraphhandler:
makehistorygraphhandler(contact)
else:
assert False
def GetMessages(self):
#Return all messages unfiltered
return self.demux.session.query(Message)
def __init__(self, demux):
self.demux = demux
#message object
class Setting(Base):
__tablename__ = 'settings'
name = Column(String, primary_key=True)
value = Column(String)
def __init__(self):
self.name = ""
self.value = ""
class SettingsStore:
def AddSetting(self, setting):
self.demux.session.add(setting)
self.demux.session.commit()
def GetSetting(self, searchname):
l = self.demux.session.query(Setting).filter(Setting.name == searchname)
if l.count > 0:
return l.first()
else:
return None
def SaveSetting(self, name, value):
setting = self.GetSetting(name)
if setting == None:
setting = Setting()
setting.name = name
setting.value = value
self.AddSetting(setting)
def LoadSetting(self, name):
setting = self.GetSetting(name)
if setting == None:
return ""
else:
return setting.value
def LoadSettingInt(self, name):
setting = self.GetSetting(name)
if setting == None:
return 0
else:
return int(setting.value)
def __init__(self, demux):
self.demux = demux