-
Notifications
You must be signed in to change notification settings - Fork 0
/
cl_storage.py
396 lines (356 loc) · 14.2 KB
/
cl_storage.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
from cloudlink import server, client
from cloudlink.server.protocols import clpv4, cl4_protocol, scratch_protocol
from typing import Any
import sqlite3
import ujson
import re
class CLStorage:
def __init__(
self,
parent: server,
clpv4: clpv4,
db_file: str = "cl_storage.db",
disable_gmsg: bool = False,
disable_pmsg: bool = False,
disable_gvar: bool = False,
disable_pvar: bool = False,
disable_scratch: bool = False,
enabled_rooms: list[str] = [],
disabled_rooms: list[str] = [],
):
self.enabled_rooms = enabled_rooms
self.disabled_rooms = disabled_rooms
# Connect to database and get cursor
self.db_con = sqlite3.connect(db_file)
self.db_cur = self.db_con.cursor()
# Create tables
self.db_cur.execute(
"""CREATE TABLE IF NOT EXISTS cl_msgs (
room TEXT NOT NULL,
val TEXT NOT NULL,
origin TEXT NOT NULL,
id TEXT NOT NULL,
PRIMARY KEY (room, id)
)"""
)
self.db_cur.execute(
"""CREATE TABLE IF NOT EXISTS cl_vars (
room TEXT NOT NULL,
name TEXT NOT NULL,
val TEXT NOT NULL,
origin TEXT NOT NULL,
id TEXT NOT NULL,
PRIMARY KEY (room, name, id)
)"""
)
self.db_cur.execute(
"""CREATE TABLE IF NOT EXISTS scratch_vars (
project_id TEXT NOT NULL,
name TEXT NOT NULL,
val TEXT NOT NULL,
PRIMARY KEY (project_id, name)
)"""
)
self.db_con.commit()
parent.logger.info("CL Storage database initialized!")
@parent.on_protocol_identified(schema=cl4_protocol)
async def protocol_identified(client):
if not self._is_room_enabled("default"):
return
# Send saved msgs
for val, origin, id in self.fetch_cl_msgs("default"):
val = ujson.loads(val)
origin = ujson.loads(origin)
id = ujson.loads(id)
if id:
if disable_pmsg:
continue
if id != client.username:
continue
parent.send_packet(
client,
{
"cmd": "pmsg",
"val": val,
"origin": origin,
"rooms": "default",
},
)
else:
if disable_gmsg:
continue
parent.send_packet(
client, {"cmd": "gmsg", "val": val, "rooms": "default"}
)
# Send saved vars
for name, val, origin, id in self.fetch_cl_vars("default"):
name = ujson.loads(name)
val = ujson.loads(val)
origin = ujson.loads(origin)
id = ujson.loads(id)
if id:
if disable_pvar:
continue
if id != client.username:
continue
parent.send_packet(
client,
{
"cmd": "pvar",
"name": name,
"val": val,
"origin": origin,
"rooms": "default",
},
)
else:
if disable_gvar:
continue
parent.send_packet(
client,
{"cmd": "gvar", "name": name, "val": val, "rooms": "default"},
)
@parent.on_command(cmd="link", schema=cl4_protocol)
async def on_link(client: client, message):
if isinstance(message["val"], str):
message["val"] = [message["val"]]
for room in message["val"]:
if not self._is_room_enabled(room):
continue
# Send saved msgs
for val, origin, id in self.fetch_cl_msgs(room):
val = ujson.loads(val)
origin = ujson.loads(origin)
id = ujson.loads(id)
if id:
if disable_pmsg:
continue
if id != client.username:
continue
parent.send_packet(
client,
{
"cmd": "pmsg",
"val": val,
"origin": origin,
"rooms": room,
},
)
else:
if disable_gmsg:
continue
parent.send_packet(
client, {"cmd": "gmsg", "val": val, "rooms": room}
)
# Send saved vars
for name, val, origin, id in self.fetch_cl_vars(room):
name = ujson.loads(name)
val = ujson.loads(val)
origin = ujson.loads(origin)
id = ujson.loads(id)
if id:
if disable_pvar:
continue
if id != client.username:
continue
parent.send_packet(
client,
{
"cmd": "pvar",
"name": name,
"val": val,
"origin": origin,
"rooms": room,
},
)
else:
if disable_gvar:
continue
parent.send_packet(
client,
{"cmd": "gvar", "name": name, "val": val, "rooms": room},
)
if not disable_gmsg:
@parent.on_command(cmd="gmsg", schema=cl4_protocol)
async def on_gmsg(client: client, message):
for room in clpv4.gather_rooms(client, message):
if not self._is_room_enabled(room):
continue
self.save_cl_msg(room, message["val"])
if not disable_pmsg:
@parent.on_command(cmd="pmsg", schema=cl4_protocol)
async def on_pmsg(client: client, message):
origin = clpv4.generate_user_object(client)
for room in clpv4.gather_rooms(client, message):
if not self._is_room_enabled(room):
continue
for client in await parent.rooms_manager.get_specific_in_room(
room, cl4_protocol, message["id"]
):
self.save_cl_msg(
room, message["val"], origin=origin, id=client.username
)
if not disable_gvar:
@parent.on_command(cmd="gvar", schema=cl4_protocol)
async def on_gvar(client: client, message):
for room in clpv4.gather_rooms(client, message):
if not self._is_room_enabled(room):
continue
self.save_cl_var(room, message["name"], message["val"])
if not disable_pvar:
@parent.on_command(cmd="pvar", schema=cl4_protocol)
async def on_pvar(client: client, message):
origin = clpv4.generate_user_object(client)
for room in clpv4.gather_rooms(client, message):
if not self._is_room_enabled(room):
continue
for client in await parent.rooms_manager.get_specific_in_room(
room, cl4_protocol, message["id"]
):
self.save_cl_var(
room,
message["name"],
message["val"],
origin=origin,
id=client.username,
)
if not disable_scratch:
@parent.on_command(cmd="handshake", schema=scratch_protocol)
async def handshake(client: client, message):
if not self._is_room_enabled(message["project_id"]):
return
# Get room and whether room exists in room manager
room = parent.rooms_manager.get(message["project_id"])
room_exists = parent.rooms_manager.exists(message["project_id"])
# Send saved variables
for name, val in self.fetch_scratch_vars(message["project_id"]):
if name not in room["global_vars"]:
if room_exists:
room["global_vars"][name] = val
parent.send_packet(
client, {"method": "set", "name": name, "value": val}
)
@parent.on_command(cmd="create", schema=scratch_protocol)
async def create_variable(_, message):
if not self._is_room_enabled(message["project_id"]):
return
self.save_scratch_var(
message["project_id"], message["name"], message["value"]
)
@parent.on_command(cmd="set", schema=scratch_protocol)
async def set_variable(_, message):
if not self._is_room_enabled(message["project_id"]):
return
self.save_scratch_var(
message["project_id"], message["name"], message["value"]
)
@parent.on_command(cmd="rename", schema=scratch_protocol)
async def rename_variable(_, message):
if not self._is_room_enabled(message["project_id"]):
return
self.rename_scratch_var(
message["project_id"], message["name"], message["new_name"]
)
@parent.on_command(cmd="delete", schema=scratch_protocol)
async def delete_variable(_, message):
if not self._is_room_enabled(message["project_id"]):
return
self.delete_scratch_var(message["project_id"], message["name"])
def _is_room_enabled(self, room: str) -> bool:
# Check disabled rooms
for pattern in self.disabled_rooms:
if re.fullmatch(pattern, room):
return False
# Check enabled rooms
if not len(self.enabled_rooms):
return True
for pattern in self.enabled_rooms:
if re.fullmatch(pattern, room):
return True
# Default
return False
def fetch_cl_msgs(
self, room: str
) -> list[tuple[str, None | dict[str, str], None | str]]:
return self.db_cur.execute(
"SELECT val, origin, id FROM cl_msgs WHERE room=?", (room,)
).fetchall()
def save_cl_msg(
self, room: str, val: Any, origin: dict[str, str] = None, id: str = None
):
val = ujson.dumps(val)
origin = ujson.dumps(origin)
id = ujson.dumps(id)
self.db_cur.execute(
"INSERT OR REPLACE INTO cl_msgs (room, val, origin, id) VALUES (?, ?, ?, ?)",
(
room,
val,
origin,
id,
),
)
self.db_con.commit()
def fetch_cl_vars(
self, room: str
) -> list[tuple[str, str, None | dict[str, str], None | str]]:
return self.db_cur.execute(
"SELECT name, val, origin, id FROM cl_vars WHERE room=?", (room,)
).fetchall()
def save_cl_var(
self,
room: str,
name: str,
val: Any,
origin: dict[str, str] = None,
id: str = None,
):
name = ujson.dumps(name)
val = ujson.dumps(val)
origin = ujson.dumps(origin)
id = ujson.dumps(id)
self.db_cur.execute(
"INSERT OR REPLACE INTO cl_vars (room, name, val, origin, id) VALUES (?, ?, ?, ?, ?)",
(
room,
name,
val,
origin,
id,
),
)
self.db_con.commit()
def fetch_scratch_vars(self, project_id: str) -> list[tuple[str, str]]:
return self.db_cur.execute(
"SELECT name, val FROM scratch_vars WHERE project_id=?", (project_id,)
).fetchall()
def save_scratch_var(self, project_id: str, name: str, val: str):
self.db_cur.execute(
"INSERT OR REPLACE INTO scratch_vars (project_id, name, val) VALUES (?, ?, ?)",
(
project_id,
name,
val,
),
)
self.db_con.commit()
def rename_scratch_var(self, project_id: str, name: str, new_name: str):
val = self.db_cur.execute(
"SELECT val FROM scratch_vars WHERE project_id=? name=?",
(
project_id,
name,
),
)
if val:
self.save_scratch_var(project_id, name, val[0])
self.delete_scratch_var(project_id, name)
def delete_scratch_var(self, project_id: str, name: str):
self.db_cur.execute(
"DELETE FROM scratch_vars WHERE project_id=? name=?",
(
project_id,
name,
),
)
self.db_con.commit()