-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
298 lines (251 loc) · 12.4 KB
/
server.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
import urllib.parse
from sakura import Server
import cherrypy
import json
import re
# websockets imports
import asyncio
import websockets
import threading
from os.path import abspath, dirname
PATH = dirname(abspath(__file__))
class Disclone(Server):
@cherrypy.expose
def index(self):
return open(PATH + "/ressources/home/home.html")
@cherrypy.expose
def channels(self, uid="me"):
self.checkJwt()
return open(PATH + "/ressources/main.html")
def onStart(self):
self.id = 1 # TODO give a different id to each server to allow them to contact eachother
self.pool = {}
self.wating_clients = {}
self.currentWaiting = 0
websocket_thread = threading.Thread(target=self.startWebSockets)
websocket_thread.start()
def onLogin(self, uid):
if not self.db.getSomething("disclone_account", uid):
self.db.insertDict("disclone_account", {"id": uid, "username": '#' + str(uid)})
if not self.db.getSomething("status", uid):
self.db.insertDict("status", {"id": uid})
def getUserConnection(self, id):
return self.db.getSomething("active_client", id, "userid")
def newConv(self, name, members):
account_id = self.getUser()
conv_id = self.db.insertDict('conversation', {'name': name}, getId=True)
self.db.insertDict('accessconversation', {'account': account_id, 'conversation': conv_id})
for i in range(0, len(members)): # this could be batched !
self.db.insertDict('accessconversation', {'account': members[i], 'conversation': conv_id})
return conv_id
def startWebSockets(self):
asyncio.run(self.runWebsockets())
async def runWebsockets(self):
async with websockets.serve(self.handle_message, self.config.get("server", "IP"),
int(self.config.get("NOTIFICATION", "PORT"))):
await asyncio.Future() # Run the server forever
# --------------------------------WEBSOCKETS--------------------------------
async def handle_message(self, websocket):
async for message in websocket:
data = json.loads(message)
match data["type"]:
case "register":
self.wating_clients[self.currentWaiting] = {"connection": websocket, "uid": data["uid"]}
self.currentWaiting += 1
answer = {"type": "register_request", "servId": self.id, "connectionId": self.currentWaiting - 1}
await websocket.send(json.dumps(answer))
case "typing":
members = self.db.getAll("accessconversation", data["conv"], "conversation")
for user in members:
if user["account"] != data["uid"]:
await self.sendNotificationAsync(user["account"], data)
case _:
print("unknown message received", message)
@cherrypy.expose
def authWS(self, connectionId):
account_id = self.getUser()
if self.wating_clients[int(connectionId)]["uid"] != account_id:
return "forbidden"
connection = self.db.insertDict("active_client", {"userid": account_id, "server": self.id}, True)
self.pool[connection] = self.wating_clients[int(connectionId)]["connection"]
del self.wating_clients[int(connectionId)]
async def sendNotificationAsync(self, account, content):
message = {"type": "notif", "content": content}
clients = self.db.getAll("active_client", account, "userid")
for client in clients:
#TODO handle multi server
if self.pool.get(client["id"]):
websocket = self.pool[client["id"]]
try:
await websocket.send(json.dumps(message))
except Exception as e:
print("exception sending a message on a ws", e)
del self.pool[client["id"]]
self.db.deleteSomething("active_client", client["id"])
else:
self.db.deleteSomething("active_client", client["id"])
def sendNotification(self, account, content):
message = {"type": "notif", "content": content}
async def ws_send(message):
await websocket.send(message)
clients = self.db.getAll("active_client", account, "userid")
for client in clients:
#TODO handle multi server
if self.pool.get(client["id"]):
websocket = self.pool[client["id"]]
try:
asyncio.run(ws_send(json.dumps(message)))
except Exception as e:
print("exception sending a message on a ws", e)
del self.pool[client["id"]]
self.db.deleteSomething("active_client", client["id"])
else:
self.db.deleteSomething("active_client", client["id"])
# -----------------------------------API-------------------------------------
@cherrypy.expose
def createServer(self):
account_id = self.getUser()
server_id = self.db.insertDict('server', {'name': "New Server", "owner": account_id}, getId=True)
self.db.insertDict('accessserver', {'account': account_id, 'server': server_id})
@cherrypy.expose
def getUserServers(self):
uid = self.getUser()
servers = self.db.getSomethingProxied("server", "accessserver", "account", uid)
return json.dumps(servers)
@cherrypy.expose
def createConv(self, name, members):
self.newConv(name, members)
@cherrypy.expose
def getUserConvs(self):
uid = self.getUser()
convs = self.db.getSomethingProxied("conversation", "accessconversation", "account", uid)
for j in range(0, len(convs)):
# TODO refactor, this is slow for no reason
convs[j]["members"] = self.db.getFilters("accessconversation", ["conversation", "=", convs[j]["id"]])
for i in range(0, len(convs[j]["members"])):
convs[j]["members"][i] = convs[j]["members"][i]["account"]
return json.dumps(convs)
@cherrypy.expose
def getUsersInfo(self, users):
uid = self.getUser()
users = self.db.getFilters("disclone_account", ["id", "in", json.loads(users)])
self.getUsersStatus(users)
return json.dumps(users, default=str)
@cherrypy.expose
def getConvContent(self, convId):
uid = self.getUser()
conv = self.db.getFilters("accessconversation", ["conversation", "=", convId, "and", "account", "=", uid])
if conv:
content = {}
content["messages"] = self.db.getFilters("message", ["place", "=", convId])
return json.dumps(content, default=str)
@cherrypy.expose
def getUserInfo(self):
uid = self.getUser()
user = self.db.getSomething("disclone_account", uid)
self.getUsersStatus([user])
return json.dumps(user, default=str)
@cherrypy.expose
def sendMessage(self, conv, content):
uid = self.getUser()
conv = json.loads(conv)
print(conv)
if not conv.get("id"):
conv["id"] = self.newConv("Noname", [uid, conv["dest"]])
message = {"sender": uid, "place": conv["id"], "body": content}
self.db.insertDict("message", message)
members = self.db.getAll("accessconversation", conv["id"], "conversation")
for user in members:
if user["account"] != uid:
self.sendNotification(user["account"], {"type": "message", "content": message})
return
@cherrypy.expose
def registerActivity(self, SDP):
uid = self.getUser()
self.db.insertDict("active_client", {"userid": uid, "SDP": SDP})
@cherrypy.expose
def friends(self, action, arg=""):
uid = self.getUser()
if action == "add":
friend = self.db.getSomething("disclone_account", arg, "username")
if not friend:
return "user not found"
friendship = self.db.getFilters("boatakopin",
["kopinprincipal", "=", uid, "and", "kopinsecondaire", "=", friend['id'],
") or (", "kopinprincipal", "=", friend['id'], "and", "kopinsecondaire",
"=", uid, ')'], "(")
if friendship:
return "You're already friends/invitation already sent"
request = {"kopinprincipal": uid, "kopinsecondaire": friend['id'], "accepted": False}
request["id"] = self.db.insertDict("boatakopin", request, True)
self.sendNotification(friend['id'], {"type": "friend_request", "content": request})
return "ok"
elif action == "accept":
friendship = self.db.getFilters("boatakopin", ["id", "=", arg, "and", "kopinsecondaire", "=", uid])
if friendship:
self.db.edit("boatakopin", arg, "accepted", True)
conv_id = self.db.insertDict('conversation', {'name': ""}, getId=True)
self.db.insertDict('accessconversation', {'account': uid, 'conversation': conv_id})
self.db.insertDict('accessconversation',
{'account': friendship[0]["kopinprincipal"], 'conversation': conv_id})
self.db.edit("boatakopin", arg, "conv", conv_id)
friendship[0]["conv"] = conv_id
self.sendNotification(friendship[0]["kopinprincipal"], {"type": "accepted_request", "content": friendship[0]})
conv = {"id": conv_id, "name": "", "members": [uid, friendship[0]["kopinprincipal"]]}
self.sendNotification(uid, {"type": "added_conv", "content": conv})
self.sendNotification(friendship[0]["kopinprincipal"], {"type": "added_conv", "content": conv})
elif action == "get":
friends = self.db.getFilters("boatakopin",
["accepted", "=", True, "and (", "kopinprincipal", "=", uid, "or",
"kopinsecondaire", "=", uid, ")"])
return json.dumps(friends)
elif action == "invitations":
invitations = self.db.getFilters("boatakopin",
["accepted", "=", False, "and (", "kopinprincipal", "=", uid, "or",
"kopinsecondaire", "=", uid, ")"])
return json.dumps(invitations)
@cherrypy.expose
def change(self, element, value):
uid = self.getUser()
if element == "id":
return "forbidden"
elif element == "username":
if re.fullmatch(REGEX_USERNAME, value):
if not self.db.getSomething("disclone_account", value, element):
self.db.edit("disclone_account", uid, element, value)
return "ok"
else:
return "this " + element + " already exists"
return "invalid username "
else:
self.db.edit("disclone_account", uid, element, value)
def getUsersStatus(self, users):
#these requests could be batched
for user in users:
params = self.db.getSomething('status', user['id'])
if not params:
self.db.insertDict("status", {"id": user['id']})
params = {"mode": 0}
print (params)
if params['mode'] == 0:
clients = self.db.getAll('active_client', user['id'], "userid")
print("user is in 0 mode id:",user['id'],clients)
idle = True
if not len(clients):
user['status'] = {'icon': 'spymode', 'text': 'Offline'}
continue
for client in clients:
if not client['idle']:
idle = False
if idle:
user['status'] = {'icon': 'orange', 'text': 'Inactive'}
else:
user['status'] = {'icon': 'green', 'text': 'Online'}
elif params['mode'] == 3:
user['status'] = {'icon': 'spymode', 'text': 'Offline'}
elif params['mode'] == 2:
user['status'] = {'icon': 'RED', 'text': 'Do not Disturb'}
elif params['mode'] == 1:
user['status'] = {'icon': 'orange', 'text': 'Inactive'}
REGEX_USERNAME = re.compile('^(?=.{3,}$)[a-zA-Z0-9_\-\.]*$')
Disclone(path=PATH, configFile="/server.ini")