-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demux.py
422 lines (360 loc) · 17.8 KB
/
Demux.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
import mysmtplib as smtplib
import mypoplib as poplib
import datetime
from messagestore import ContactStore, MessageStore, SettingsStore, Base, Message, Contact, CleanedEmailAddress
from Crypto.PublicKey import RSA
from Crypto import Random
from json import JSONEncoder, JSONDecoder
from Crypto.Hash import SHA256
import base64
from historygraph import ImmutableObject
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship, backref
from sqlalchemy.types import Boolean
import uuid
import json
import utils
#A demux is a class that deals with receiving data from the email and routing it to the correct place
class Demux(object):
database_file_name = 'historygraph.db'
def __init__(self, fromfile, **kwargs):
self.database_file_name = fromfile
filename = self.get_database_filename()
engine = create_engine('sqlite:///' + filename, echo=False)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
self.session = Session()
self.contactstore = ContactStore(self)
self.messagestore = MessageStore(self)
self.settingsstore = SettingsStore(self)
self.myemail = self.settingsstore.LoadSetting('myemail')
self.smtpserver = self.settingsstore.LoadSetting('smtpserver')
self.smtpport = self.settingsstore.LoadSettingInt('smtpport')
self.smtpuser = self.settingsstore.LoadSetting('smtpuser')
self.smtppass = self.settingsstore.LoadSetting('smtppass')
self.popserver = self.settingsstore.LoadSetting('popserver')
self.popuser = self.settingsstore.LoadSetting('popuser')
self.poppass = self.settingsstore.LoadSetting('poppass')
self.popport = self.settingsstore.LoadSettingInt('popport')
if self.settingsstore.LoadSetting('key') != "":
self.key = RSA.importKey(self.settingsstore.LoadSetting('key'))
self.Init_Setting('myemail', kwargs)
self.Init_Setting('smtpserver', kwargs)
self.Init_Setting('smtpport', kwargs)
self.Init_Setting('smtpuser', kwargs)
self.Init_Setting('smtppass', kwargs)
self.Init_Setting('popserver', kwargs)
self.Init_Setting('popuser', kwargs)
self.Init_Setting('poppass', kwargs)
self.Init_Setting('popport', kwargs)
self.Init_Setting('popport', kwargs)
if 'key' not in kwargs and hasattr(self, 'key') == False:
random_generator = Random.new().read
self.key = RSA.generate(1024, random_generator)
elif hasattr(self, 'key') == False:
self.key = kwargs['key']
self.settingsstore.SaveSetting('key', self.key.exportKey("PEM"))
self.appdir = kwargs.get("appdir", None)
self.registeredapps = dict()
def Init_Setting(self, settingname, init_kwargs):
if settingname in init_kwargs:
setattr(self, settingname, init_kwargs[settingname])
self.settingsstore.SaveSetting(settingname, init_kwargs[settingname])
def get_database_filename(self):
return self.database_file_name
def ProcessMessage(self, s):
pass
def RegisterApp(self, app):
self.registeredapps[app.__class__.__name__] = app
if self.appdir is not None:
#print "Attempting to load from " + self.appdir
app.LoadDocumentCollectionFromDisk(self.appdir)
def SendPlainEmail(self, receivers, subject, message):
if subject == "HistoryGraph encoded message":
pass
#print "message = ",message
#assert False
#print "SendPlainEmail subject = ",subject," message = ",message
smtpObj = smtplib.SMTP_SSL(self.smtpserver, self.smtpport)
prcoessedmessage = "From: <" + self.myemail + ">\n"
receivers = ",".join(["<" + r + ">" for r in receivers])
prcoessedmessage += "To: " + receivers + " \n"
prcoessedmessage += "Date: " + datetime.datetime.now().strftime("%c") + """
Subject: """ + subject + """
Content-Type: text/plain
""" + message + """
================
HistoryGraph enabled emailer http://wwww.historygraph.io (""" + self.myemail + """)
================
"""
smtpObj.sendmail(self.myemail, receivers, prcoessedmessage)
def GetNumMessages(self):
M = poplib.POP3_SSL(self.popserver, self.popport)
M.user(self.popuser)
M.pass_("")
numMessages = len(M.list()[1])
totalMessages = 0
for i in range(numMessages):
messages = M.retr(i+1)[1]
totalMessages += len(messages)
M.quit()
return totalMessages
def CheckEmail(self):
M = poplib.POP3_SSL(self.popserver, self.popport)
M.user(self.popuser)
M.pass_(self.poppass)
numMessages = len(M.list()[1])
#utils.log_output('Demux.myemail = ', self.myemail, ' CheckEmail numMessages = ',numMessages)
public_key = None
#print "numMessages = ",numMessages
for i in range(numMessages):
#utils.log_output("Reading message = ",i)
lines = M.retr(i+1)[1]
headers = list()
body = list()
inheader = True
#print "Starting message lines =",lines
for line in lines:
#print "line=",line
if line == "":
inheader = False
elif inheader:
#print "headers.append "
headers.append(line)
else:
#print "body.append "
body.append(line)
isencodedmessage = False
for line in headers:
if line[:8] == "Subject:":
isencodedmessage = line == "Subject: HistoryGraph encoded message"
if line[:6] == "From: ":
k = line.find("<")
if k > 0:
fromemail = line[k + 1:]
k = fromemail.find(">")
assert k > 0 # "> not found")
fromemail = fromemail[:k]
else:
fromemail = line[6:]
if isencodedmessage:
inencodedarea = False
wasinencodedarea = False
wasinendencodedarea = False
message = []
for line in body:
if line == self.begin_enc:
inencodedarea = True
wasinencodedarea = True
elif line == self.end_enc:
inencodedarea = False
wasinendencodedarea = True
elif inencodedarea:
message.append(line)
message = "".join(message)
line = base64.b64decode(message)
line = JSONDecoder().decode(line)
assert len(line) == 2 #"Message is not an iterable of length two")
l = line[0]
sig = long(line[1])
l2 = JSONDecoder().decode(l)
assert len(l2) == 2 # "Message is not an iterable of length two"
assert l2[0] == "XR1"# "Protocol version incorrect"
d = l2[1]
assert isinstance(d, dict) # "d must be a dict"
#utils.log_output('CheckEmail received message of class - ', d['class'], ' from ', fromemail)
#utils.log_output('Full email', lines)
if d["class"] == "identity": #An identity message identifies the other sender: Ie gives us their public key
assert len(d) == 4 #"d must contain 4 elements")
assert d["email"] == fromemail # "Source email must match the message")
public_key = RSA.importKey(d["key"])
hash = SHA256.new(l).digest()
verified = public_key.verify(hash, (sig, ))
assert verified # "Signature not verified"
l3 = [c for c in self.contactstore.GetContacts() if CleanedEmailAddress(c.emailaddress) == CleanedEmailAddress(fromemail)]
if len(l3) == 0:
contact = Contact()
contact.name = d["email"]
assert fromemail[0] != '<'
assert fromemail[-1] != '>'
contact.emailaddress = CleanedEmailAddress(fromemail)
contact.publickey = d["key"]
contact.ishistorygraph = True
self.contactstore.AddContact(contact)
self.SendConfirmationEmail(contact)
else:
contact = l3[0]
if not (contact.publickey != '' and not self.is_verified(fromemail, l, sig)):
#Silently ignore unverified attempts to change keys
contact.publickey = d["key"]
contact.ishistorygraph = True
self.contactstore.AddContact(contact)
elif d["class"] == "createdocumentcollection": #An identity message identifies the other sender: Ie gives us their public key
if self.is_verified(fromemail, l, sig):
#Silently ignore unverified messages
app = self.registeredapps[d["appname"]]
if app.HasDocumentCollection(d["dcid"]):
dc = app.GetDocumentCollectionByID(d["dcid"])
else:
dc = app.CreateNewDocumentCollection(d["dcid"])
dc.LoadFromJSON(d["dcjson"])
elif d["class"] == "edges": #Some edges we should apply
if self.is_verified(fromemail, l, sig):
if d["dcid"] in self.registeredapps[d["appname"]].dcdict:
#Silently ignore unverified edges
dc = self.registeredapps[d["appname"]].GetDocumentCollectionByID(d["dcid"])
#utils.log_output("received edges = " + str(d["edges"]))
#print "Calling LoadfromJSON from Demux = ",self.myemail
dc.LoadFromJSON(d["edges"])
elif d["class"] == "immutableobject": #immutableobject to create
if not self.is_verified(fromemail, l, sig):
#Silently ignore unverified messages
if d["dcid"] in self.registeredapps[d["appname"]].dcdict:
dc = self.registeredapps[d["appname"]].GetDocumentCollectionByID(d["dcid"])
d2 = d["immutableobject"]
classname = d2["classname"]
theclass = dc.classes[classname]
assert issubclass(theclass, ImmutableObject)
thehash = d2["hash"]
del d2["classname"]
del d2["hash"]
io = theclass(**d2)
if io.GetHash() not in dc.objects[classname]:
dc.objects[classname][io.GetHash()] = io
elif d["class"] == "encryptedemail": #Some edges we should apply
message2 = Message()
message2.body = d["message"]
message2.subject = d["subject"]
message2.fromaddress = d["sender"]
message2.senderishistorygraphenabled = True
message2.messageisencrypted = True
message2.datetime = datetime.datetime.now()
self.messagestore.AddMessage(message2, None)
else:
assert False #Message type not implemented yet
else:
lines = [line for line in lines if line != "Content-Type: text/plain"]
rawmessage = '\n'.join(lines)
message2 = Message.fromrawbody(rawmessage)
assert message2.fromaddress != ""
self.messagestore.AddMessage(message2, None)
self.ProcessBodyHistoryGraphMessages(message2)
l = [c for c in self.contactstore.GetContacts() if c.emailaddress == message2.fromaddress]
send_confirmation_email = False
if len(l) == 0:
contact = Contact()
contact.name = message2.fromaddress
assert message2.fromaddress[0] != '<'
assert message2.fromaddress[-1] != '>'
contact.emailaddress = CleanedEmailAddress(message2.fromaddress)
contact.ishistorygraph = message2.senderishistorygraphenabled
self.contactstore.AddContact(contact)
send_confirmation_email = contact.ishistorygraph
elif len(l) == 1:
if message2.senderishistorygraphenabled == True:
contact = l[0]
contact.ishistorygraph = True
self.contactstore.AddContact(contact)
send_confirmation_email = True
else:
assert False
if send_confirmation_email:
#utils.log_output('sending confirmation from ', self.myemail, ' for message ', lines)
self.SendConfirmationEmail(contact)
for i in range(numMessages):
M.dele(i + 1)
M.quit()
self.SaveAllDCs()
def is_verified(self, fromemail, l, sig):
if self.myemail == fromemail:
#print 'Messages from ourselves are never verified'
return False
contacts = [c for c in self.contactstore.GetContacts() if CleanedEmailAddress(c.emailaddress) == CleanedEmailAddress(fromemail)]
if len(contacts) != 1:
#print "is_verified contacts = ",contacts
print "is_verified failed for " + self.myemail + " fromemail = " + str(fromemail) + " sig = " + str(sig)
return False
contact = contacts[0]
public_key = RSA.importKey(contact.publickey)
hash = SHA256.new(l).digest()
return public_key.verify(hash, (sig, ))
begin_enc = "-----BEGIN-HISTORYGRAPH-ENCODED-MESSAGE--------"
end_enc = "-----END-HISTORYGRAPH-ENCODED-MESSAGE----------"
def SendConfirmationEmail(self, contact):
sender = self.myemail
emailaddress = CleanedEmailAddress(contact.emailaddress)
assert emailaddress[0] != '<'
assert emailaddress[-1] != '>'
assert emailaddress != ''
receivers = [emailaddress]
public_key = self.key.publickey().exportKey("PEM")
for r in receivers:
assert self.myemail != r
message = self.GetEncodedMessage({"id":str(uuid.uuid4()),"class":"identity","email": sender,"key":public_key})
self.SendPlainEmail(receivers, "HistoryGraph encoded message", message)
def SendEncryptedEmail(self, contact, subject, message):
sender = self.myemail
emailaddress = CleanedEmailAddress(contact.emailaddress)
assert emailaddress[0] != '<'
assert emailaddress[-1] != '>'
assert emailaddress != ''
receivers = [emailaddress]
key = RSA.importKey(contact.publickey)
d = {"id":str(uuid.uuid4()),"class":"encryptedemail","sender": sender,"message":message,"subject":subject}
message = self.GetEncodedMessage(d)
self.SendPlainEmail(receivers, "HistoryGraph encoded message", message)
def GetEncodedMessage(self, d):
message = "\n\n" + self.begin_enc + "\n"
l = ["XR1", d]
l = JSONEncoder().encode(l)
hash = SHA256.new(l).digest()
signature = self.key.sign(hash, '')
l2 = [l, str(signature[0])]
l2 = JSONEncoder().encode(l2)
line = base64.b64encode(l2)
n = 30
lines = [line[i:i+n] for i in range(0, len(line), n)]
message += '\n'.join(lines) + "\n"
message += self.end_enc + "\n\n"
#print "SendConformEmail message = ",message
#message += "=================================================\n"
#message += "Livewire enabled emailer http://wwww.livewirecommunicator.org (" + self.myemail + ")\n"
#message += "=================================================\n"
return message
def ProcessBodyHistoryGraphMessages(self, message):
#print "ProcessBodyHistoryGraphMessages called"
body = message.body
#print 'ProcessBodyHistoryGraphMessages body = ' + body
k = body.find(self.begin_enc)
#print "body = ",body
if k < 0:
return
#print "ProcessBodyHistoryGraphMessages 1"
body = body[k + len(self.begin_enc):]
#print 'ProcessBodyHistoryGraphMessages after removing begin_enc body = ' + body
k = body.find(self.end_enc)
assert k >= 0, 'end_enc not found body is ' + body + ', self.end_enc = ' + self.end_enc
body = body[:k]
json = base64.b64decode(body)
#print "ProcessBodyHistoryGraphMessages json",json
l2 = JSONDecoder().decode(json)
#print 'ProcessBodyHistoryGraphMessages l2=',l2
assert l2[0] == "XR1"
d = l2[1]
assert d["class"] == "identity"
messageid = d["id"]
sender = d["email"]
public_key = d["key"]
l = [c for c in self.contactstore if c.email == sender]
assert len(l) == 1
for c in l:
#print "public_key = ",public_key
c.publickey = public_key
s.save()
def SaveAllDCs(self):
for app in self.registeredapps.values():
app.SaveAllDCs()