-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
76 lines (50 loc) · 1.73 KB
/
server.py
File metadata and controls
76 lines (50 loc) · 1.73 KB
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
import pickle
DB_FILE='server.db'
class Server():
def __init__(self):
try:
self.refresh()
except FileNotFoundError:
self.key_bundles={}
self.messages={}
self.dump()
def dump(self):
with open(DB_FILE,'wb') as file:
pickle.dump(self,file)
def refresh(self):
with open(DB_FILE,'rb') as file:
pkl=pickle.load(file)
self.key_bundles=pkl.key_bundles
self.messages=pkl.messages
def publish(self,username:str,key_bundle:object):
self.refresh()
self.key_bundles[username]=key_bundle
self.dump()
def get_key_bundle(self,username:str) -> dict | None:
self.refresh()
if username in self.key_bundles:
bundle=self.key_bundles[username].copy()
if len(bundle['OPK_p'])>0 :
bundle['OPK_p']=self.key_bundles[username]['OPK_p'].pop()
self.dump()
else :
bundle['OPK_p']=b'\xff'*32 #No OPK is indicated by this
return bundle #dont pass reference
else:
return None
def send(self,fr:str,to:str,message:bytes):
self.refresh()
if (fr,to) not in self.messages:
self.messages[(fr,to)]=[]
self.messages[(fr,to)].append(message)
self.dump()
def get_message(self,username:str) -> list[tuple[str,list[bytes]]]:
self.refresh()
out=[]
messages=self.messages.copy()
for x,y in messages.items():
if x[1]==username:
out.append((x[0],y.copy()))
self.messages.pop(x)
self.dump()
return out