forked from meower-media/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.py
1059 lines (858 loc) · 41.6 KB
/
commands.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from cloudlink import CloudlinkServer, CloudlinkClient
from supporter import Supporter
from database import db, registration_blocked_ips, get_total_pages
from threading import Thread
import pymongo
import security
import time
import re
import uuid
class MeowerCommands:
def __init__(self, cl: CloudlinkServer, supporter: Supporter):
self.cl = cl
self.supporter = supporter
# Authentication
self.cl.add_command("authpswd", self.authpswd)
self.cl.add_command("gen_account", self.gen_account)
# Accounts
self.cl.add_command("get_profile", self.get_profile)
self.cl.add_command("update_config", self.update_config)
self.cl.add_command("change_pswd", self.change_pswd)
self.cl.add_command("del_tokens", self.del_tokens)
self.cl.add_command("del_account", self.del_account)
# Chats
self.cl.add_command("create_chat", self.create_chat)
self.cl.add_command("leave_chat", self.leave_chat)
self.cl.add_command("get_chat_list", self.get_chat_list)
self.cl.add_command("get_chat_data", self.get_chat_data)
self.cl.add_command("add_to_chat", self.add_to_chat)
self.cl.add_command("remove_from_chat", self.remove_from_chat)
self.cl.add_command("set_chat_state", self.set_chat_state)
# Posts
self.cl.add_command("post_home", self.post_home)
self.cl.add_command("post_chat", self.post_chat)
self.cl.add_command("delete_post", self.delete_post)
# Moderation
self.cl.add_command("report", self.report)
async def authpswd(self, client: CloudlinkClient, val, listener: str = None):
# Make sure the client isn't already authenticated
if client.username:
return await client.send_statuscode("OK", listener)
# Check val datatype
if not isinstance(val, dict):
return await client.send_statuscode("Datatype", listener)
# Check val syntax
if ("username" not in val) or ("pswd" not in val):
return await client.send_statuscode("Syntax", listener)
# Extract username and password
username = val["username"]
password = val["pswd"]
# Check username and password datatypes
if (not isinstance(username, str)) or (not isinstance(password, str)):
return await client.send_statuscode("Datatype", listener)
# Check username and password syntax
if len(username) < 1 or len(username) > 20 or len(password) < 1 or len(password) > 255:
return await client.send_statuscode("Syntax", listener)
# Check ratelimits
for bucket_id in [
f"login:i:{client.ip}",
f"login:u:{username}:s",
f"login:u:{username}:f"
]:
if security.ratelimited(bucket_id):
return await client.send_statuscode("RateLimit", listener)
# Ratelimit IP
security.ratelimit(f"login:i:{client.ip}", 100, 1800)
# Get tokens, password, permissions, ban state, and delete after timestamp
account = db.usersv0.find_one({"_id": username}, projection={
"tokens": 1,
"pswd": 1,
"flags": 1,
"permissions": 1,
"ban": 1,
"delete_after": 1
})
if not account:
return await client.send_statuscode("IDNotFound", listener)
elif (account["flags"] & security.UserFlags.DELETED) or (account["delete_after"] and account["delete_after"] <= time.time()+60):
security.ratelimit(f"login:u:{username}:f", 5, 60)
return await client.send_statuscode("Deleted", listener)
# Check password
if (password not in account["tokens"]) and (not security.check_password_hash(password, account["pswd"])):
security.ratelimit(f"login:u:{username}:f", 5, 60)
return await client.send_statuscode("PasswordInvalid", listener)
# Update netlog
netlog_result = db.netlog.update_one({"_id": {"ip": client.ip, "user": username}}, {"$set": {"last_used": int(time.time())}}, upsert=True)
# Check ban
if (account["ban"]["state"] == "perm_ban") or (account["ban"]["state"] == "temp_ban" and account["ban"]["expires"] > time.time()):
security.ratelimit(f"login:u:{username}:f", 5, 60)
await client.send({
"mode": "banned",
"payload": account["ban"]
}, direct_wrap=True, listener=listener)
return await client.send_statuscode("Banned", listener)
# Ratelimit successful login
security.ratelimit(f"login:u:{username}:s", 25, 300)
# Alert user of new IP address if user has admin permissions
if account["permissions"] and netlog_result.upserted_id:
self.supporter.create_post("inbox", username, f"Your account was logged into on a new IP address ({client.ip})! You are receiving this message because you have admin permissions. Please make sure to keep your account secure.")
# Alert user if account was pending deletion
if account["delete_after"]:
self.supporter.create_post("inbox", username, f"Your account was scheduled for deletion but you logged back in. Your account is no longer scheduled for deletion! If you didn't request for your account to be deleted, please change your password immediately.")
# Generate new token
token = security.generate_token()
# Update user
db.usersv0.update_one({"_id": username}, {
"$addToSet": {"tokens": token},
"$set": {"last_seen": int(time.time()), "delete_after": None}
})
# Authenticate client
client.set_username(username)
# Get relationships
relationships = [{
"username": relationship["_id"]["to"],
"state": relationship["state"],
"updated_at": relationship["updated_at"]
} for relationship in db.relationships.find({"_id.from": username})]
# Return info to sender
await client.send({
"mode": "auth",
"payload": {
"username": username,
"token": token,
"account": security.get_account(username, True),
"relationships": relationships
}}, direct_wrap=True, listener=listener)
# Tell the client it is authenticated
await client.send_statuscode("OK", listener)
async def gen_account(self, client: CloudlinkClient, val, listener: str = None):
# Make sure the client isn't already authenticated
if client.username:
return await client.send_statuscode("OK", listener)
# Make sure registration isn't disabled
if not self.supporter.registration:
return await client.send_statuscode("Disabled", listener)
# Check val datatype
if not isinstance(val, dict):
return await client.send_statuscode("Datatype", listener)
# Check val syntax
if ("username" not in val) or ("pswd" not in val):
return await client.send_statuscode("Syntax", listener)
# Extract username and password
username = val["username"]
password = val["pswd"]
# Check username and password datatypes
if (not isinstance(username, str)) or (not isinstance(password, str)):
return await client.send_statuscode("Datatype", listener)
# Check username and password syntax
if len(username) < 1 or len(username) > 20 or len(password) < 1 or len(password) > 255:
return await client.send_statuscode("Syntax", listener)
# Check username regex
if not re.fullmatch(security.USERNAME_REGEX, username):
return await client.send_statuscode("IllegalChars", listener)
# Check ratelimit
if security.ratelimited(f"registration:{client.ip}:f") or security.ratelimited(f"registration:{client.ip}:s"):
return await client.send_statuscode("RateLimit", listener)
# Check whether IP is blocked from creating new accounts
if registration_blocked_ips.search_best(client.ip):
security.ratelimit(f"registration:{client.ip}:f", 5, 30)
return await client.send_statuscode("Blocked", listener)
# Make sure username doesn't already exist
if security.account_exists(username, ignore_case=True):
security.ratelimit(f"registration:{client.ip}:f", 5, 30)
return await client.send_statuscode("IDExists", listener)
# Generate new token
token = security.generate_token()
# Create account
security.create_account(username, password, token=token)
# Ratelimit
security.ratelimit(f"registration:{client.ip}:s", 5, 900)
# Update netlog
db.netlog.update_one({"_id": {"ip": client.ip, "user": username}}, {"$set": {"last_used": int(time.time())}}, upsert=True)
# Send welcome message
self.supporter.create_post("inbox", username, "Welcome to Meower! We welcome you with open arms! You can get started by making friends in the global chat or home, or by searching for people and adding them to a group chat. We hope you have fun!")
# Authenticate client
client.set_username(username)
# Return info to sender
await client.send({
"mode": "auth",
"payload": {
"username": username,
"token": token,
"account": security.get_account(username, True),
"relationships": []
}}, direct_wrap=True, listener=listener)
# Tell the client it is authenticated
await client.send_statuscode("OK", listener)
# Auto-report if client is on a VPN
if security.get_netinfo(client.ip)["vpn"]:
db.reports.insert_one({
"_id": str(uuid.uuid4()),
"type": "user",
"content_id": username,
"status": "pending",
"escalated": False,
"reports": [{
"user": "Server",
"ip": client.ip,
"reason": "User registered while using a VPN.",
"comment": "",
"time": int(time.time())
}]
})
async def get_profile(self, client: CloudlinkClient, val, listener: str = None):
# Check datatype
if not isinstance(val, str):
return await client.send_statuscode("Datatype", listener)
# Check syntax
if len(val) < 1 or len(val) > 20:
return await client.send_statuscode("Syntax", listener)
# Get profile
account = security.get_account(val, (client.username and val.lower() == client.username.lower()))
if not account:
return await client.send_statuscode("IDNotFound", listener)
# Return profile
await client.send({
"mode": "profile",
"payload": account,
"user_id": account["_id"]
}, direct_wrap=True, listener=listener)
await client.send_statuscode("OK", listener)
async def update_config(self, client: CloudlinkClient, val, listener: str = None):
# Make sure the client is authenticated
if not client.username:
return await client.send_statuscode("Refused", listener)
# Check datatype
if not isinstance(val, dict):
return await client.send_statuscode("Datatype", listener)
# Check ratelimit
if security.ratelimited(f"config:{client.username}"):
return await client.send_statuscode("RateLimit", listener)
# Ratelimit
security.ratelimit(f"config:{client.username}", 10, 5)
# Delete updated profile data if account is restricted
if security.is_restricted(client.username, security.Restrictions.EDITING_PROFILE):
if "pfp_data" in val:
del val["pfp_data"]
if "avatar" in val:
del val["avatar"]
if "avatar_color" in val:
del val["avatar_color"]
if "quote" in val:
del val["quote"]
# Update config
security.update_settings(client.username, val)
# Sync config between sessions
self.cl.broadcast({
"mode": "update_config",
"payload": val
}, direct_wrap=True, usernames=[client.username])
# Send updated pfp and quote to other clients
updated_profile_data = {"_id": client.username}
if "pfp_data" in val:
updated_profile_data["pfp_data"] = val["pfp_data"]
if "avatar" in val:
updated_profile_data["avatar"] = val["avatar"]
if "avatar_color" in val:
updated_profile_data["avatar_color"] = val["avatar_color"]
if "quote" in val:
updated_profile_data["quote"] = val["quote"]
if len(updated_profile_data) > 1:
self.cl.broadcast({
"mode": "update_profile",
"payload": updated_profile_data
}, direct_wrap=True)
# Tell the client the config was updated
await client.send_statuscode("OK", listener)
async def change_pswd(self, client: CloudlinkClient, val, listener: str = None):
# Make sure the client is authenticated
if not client.username:
return await client.send_statuscode("Refused", listener)
# Check datatype
if not isinstance(val, dict):
return await client.send_statuscode("Datatype", listener)
# Check syntax
if ("old" not in val) or ("new" not in val):
return await client.send_statuscode("Syntax", listener)
# Extract old password and new password
old_pswd = val["old"]
new_pswd = val["new"]
# Check old password and new password datatypes
if (not isinstance(old_pswd, str)) or (not isinstance(new_pswd, str)):
return await client.send_statuscode("Datatype", listener)
# Check old password and new password syntax
if len(old_pswd) < 1 or len(old_pswd) > 255 or len(new_pswd) < 8 or len(new_pswd) > 255:
return await client.send_statuscode("Syntax", listener)
# Check ratelimit
if security.ratelimited(f"login:u:{client.username}:f"):
return await client.send_statuscode("RateLimit", listener)
# Ratelimit
security.ratelimit(f"login:u:{client.username}:f", 5, 60)
# Check password
account = db.usersv0.find_one({"_id": client.username}, projection={"pswd": 1})
if not security.check_password_hash(old_pswd, account["pswd"]):
return await client.send_statuscode("PasswordInvalid", listener)
# Update password
db.usersv0.update_one({"_id": client.username}, {"$set": {"pswd": security.hash_password(new_pswd)}})
# Tell the client the password was updated
await client.send_statuscode("OK", listener)
async def del_tokens(self, client: CloudlinkClient, val, listener: str = None):
# Make sure the client is authenticated
if not client.username:
return await client.send_statuscode("Refused", listener)
# Revoke tokens
db.usersv0.update_one({"_id": client.username}, {"$set": {"tokens": []}})
# Tell the client the tokens were revoked
await client.send_statuscode("OK", listener)
# Disconnect the client
for client in self.cl.usernames.get(client.username, []):
client.kick(statuscode="LoggedOut")
async def del_account(self, client: CloudlinkClient, val, listener: str = None):
# Make sure the client is authenticated
if not client.username:
return await client.send_statuscode("Refused", listener)
# Check datatype
if not isinstance(val, str):
return await client.send_statuscode("Datatype", listener)
# Check syntax
if len(val) < 1 or len(val) > 255:
return await client.send_statuscode("Syntax", listener)
# Check ratelimit
if security.ratelimited(f"login:u:{client.username}:f"):
return await client.send_statuscode("RateLimit", listener)
# Ratelimit
security.ratelimit(f"login:u:{client.username}:f", 5, 60)
# Check password
account = db.usersv0.find_one({"_id": client.username}, projection={"pswd": 1})
if not security.check_password_hash(val, account["pswd"]):
return await client.send_statuscode("PasswordInvalid", listener)
# Schedule account for deletion
db.usersv0.update_one({"_id": client.username}, {"$set": {
"tokens": [],
"delete_after": int(time.time())+604800 # 7 days
}})
# Tell the client their account was scheduled for deletion
await client.send_statuscode("OK", listener)
# Disconnect the client
for client in self.cl.usernames.get(client.username, []):
client.kick(statuscode="LoggedOut")
async def create_chat(self, client: CloudlinkClient, val, listener: str = None):
# Make sure the client is authenticated
if not client.username:
return await client.send_statuscode("Refused", listener)
# Check ratelimit
if security.ratelimited(f"create_chat:{client.username}"):
return await client.send_statuscode("RateLimit", listener)
# Ratelimit
security.ratelimit(f"create_chat:{client.username}", 5, 30)
# Check restrictions
if security.is_restricted(client.username, security.Restrictions.NEW_CHATS):
return await client.send_statuscode("Banned", listener)
# Check datatype
if not isinstance(val, str):
return await client.send_statuscode("Datatype", listener)
# Check syntax
if len(val) < 1 or len(val) > 50:
return await client.send_statuscode("Syntax", listener)
# Make sure the client isn't in too many chats
if db.chats.count_documents({"type": 0, "members": client.username}, limit=150) >= 150:
return await client.send_statuscode("Syntax", listener)
# Create chat
chat = {
"_id": str(uuid.uuid4()),
"type": 0,
"nickname": self.supporter.wordfilter(val),
"icon": "",
"icon_color": "000000",
"owner": client.username,
"members": [client.username],
"created": int(time.time()),
"last_active": int(time.time()),
"deleted": False,
"allow_pinning": False
}
db.chats.insert_one(chat)
# Tell the client the chat was created
self.cl.broadcast({
"mode": "create_chat",
"payload": chat
}, direct_wrap=True, usernames=[client.username])
await client.send_statuscode("OK", listener)
async def leave_chat(self, client: CloudlinkClient, val, listener: str = None):
# Make sure the client is authenticated
if not client.username:
return await client.send_statuscode("Refused", listener)
# Check ratelimit
if security.ratelimited(f"update_chat:{client.username}"):
return await client.send_statuscode("RateLimit", listener)
# Ratelimit
security.ratelimit(f"update_chat:{client.username}", 5, 5)
# Check datatype
if not isinstance(val, str):
return await client.send_statuscode("Datatype", listener)
# Check syntax
if len(val) < 1 or len(val) > 50:
return await client.send_statuscode("Syntax", listener)
# Get chat
chat = db.chats.find_one({"_id": val, "members": client.username, "deleted": False})
if not chat:
return await client.send_statuscode("IDNotFound", listener)
if chat["type"] == 0:
# Remove member
chat["members"].remove(client.username)
# Update chat if it's not empty, otherwise delete the chat
if len(chat["members"]) > 0:
# Transfer ownership, if owner
if chat["owner"] == client.username:
chat["owner"] = chat["members"][0]
# Update chat
db.chats.update_one({"_id": val}, {
"$set": {"owner": chat["owner"]},
"$pull": {"members": client.username}
})
# Send update chat event
self.cl.broadcast({
"mode": "update_chat",
"payload": {
"_id": chat["_id"],
"owner": chat["owner"],
"members": chat["members"]
}
}, direct_wrap=True, usernames=chat["members"])
# Send in-chat notification
self.supporter.create_post(chat["_id"], "Server", f"@{client.username} has left the group chat.", chat_members=chat["members"])
else:
db.posts.delete_many({"post_origin": val, "isDeleted": False})
db.chats.delete_one({"_id": val})
elif chat["type"] == 1:
# Remove chat from client's active DMs list
db.user_settings.update_one({"_id": client}, {
"$pull": {"active_dms": val}
})
else:
return await client.send_statuscode("InternalServerError", listener)
# Send delete event to client
self.cl.broadcast({
"mode": "delete",
"id": chat["_id"]
}, direct_wrap=True, usernames=[client.username])
# Tell the client it left the chat
await client.send_statuscode("OK", listener)
async def get_chat_list(self, client: CloudlinkClient, val, listener: str = None):
# Make sure the client is authenticated
if not client.username:
return await client.send_statuscode("Refused", listener)
# Get page
page = 1
if isinstance(val, dict):
try:
page = int(val["page"])
except: pass
# Get chats
query = {"members": client.username, "type": 0}
chats = list(db.chats.find(query, skip=(page-1)*25, limit=25))
# Return chats
await client.send({
"mode": "chats",
"payload": {
"all_chats": chats,
"index": [chat["_id"] for chat in chats],
"page#": page,
"pages": get_total_pages("chats", query)
}
}, direct_wrap=True, listener=listener)
await client.send_statuscode("OK", listener)
async def get_chat_data(self, client: CloudlinkClient, val, listener: str = None):
# Make sure the client is authenticated
if not client.username:
return await client.send_statuscode("Refused", listener)
# Check datatype
if not isinstance(val, str):
return await client.send_statuscode("Datatype", listener)
# Check syntax
if len(val) < 1 or len(val) > 50:
return await client.send_statuscode("Syntax", listener)
# Get chat
chat = db.chats.find_one({"_id": val, "members": client.username, "deleted": False})
if not chat:
return await client.send_statuscode("IDNotFound", listener)
# Return chat
chat["chatid"] = chat["_id"]
await client.send({
"mode": "chat_data",
"payload": chat
}, direct_wrap=True, listener=listener)
await client.send_statuscode("OK", listener)
async def add_to_chat(self, client: CloudlinkClient, val, listener: str = None):
# Make sure the client is authenticated
if not client.username:
return await client.send_statuscode("Refused", listener)
# Check ratelimit
if security.ratelimited(f"update_chat:{client}"):
return await client.send_statuscode("RateLimit", listener)
# Ratelimit
security.ratelimit(f"update_chat:{client}", 5, 5)
# Check restrictions
if security.is_restricted(client, security.Restrictions.NEW_CHATS):
return await client.send_statuscode("Banned", listener)
# Check val datatype
if not isinstance(val, dict):
return await client.send_statuscode("Datatype", listener)
# Check val syntax
if ("chatid" not in val) or ("username" not in val):
return await client.send_statuscode("Syntax", listener)
# Extract chatid and username
chatid = val["chatid"]
username = val["username"]
# Check chatid and username datatypes
if (not isinstance(chatid, str)) or (not isinstance(username, str)):
return await client.send_statuscode("Datatype", listener)
# Check chatid and username syntax
if (len(chatid) > 50) or (len(username) > 20):
return await client.send_statuscode("Syntax", listener)
# Get chat
chat = db.chats.find_one({"_id": chatid, "members": client.username, "deleted": False})
if not chat:
return await client.send_statuscode("IDNotFound", listener)
# Make sure the chat isn't full
if chat["type"] == 1 or len(chat["members"]) >= 256:
return await client.send_statuscode("ChatFull", listener)
# Make sure the user isn't already in the chat
if username in chat["members"]:
return await client.send_statuscode("IDExists", listener)
# Make sure requested user exists and isn't deleted
user = db.usersv0.find_one({"_id": username}, projection={"permissions": 1})
if (not user) or (user["permissions"] is None):
return await client.send_statuscode("IDNotFound", listener)
# Make sure requested user isn't blocked or is blocking client
if db.relationships.count_documents({"$or": [
{
"_id": {"from": client.username, "to": username},
"state": 2
},
{
"_id": {"from": username, "to": client.username},
"state": 2
}
]}, limit=1) > 0:
return await client.send_statuscode("MissingPermissions", listener)
# Update chat
chat["members"].append(username)
db.chats.update_one({"_id": chatid}, {"$addToSet": {"members": username}})
# Send create chat event
self.cl.broadcast({
"mode": "create_chat",
"payload": chat
}, direct_wrap=True, usernames=[username])
# Send update chat event
self.cl.broadcast({
"mode": "update_chat",
"payload": {
"_id": chatid,
"members": chat["members"]
}
}, direct_wrap=True, usernames=chat["members"])
# Send inbox message to user
self.supporter.create_post("inbox", username, f"You have been added to the group chat '{chat['nickname']}' by @{client.username}!")
# Send in-chat notification
self.supporter.create_post(chatid, "Server", f"@{client.username} added @{username} to the group chat.", chat_members=chat["members"])
# Tell the client the user was added
await client.send_statuscode("OK", listener)
async def remove_from_chat(self, client: CloudlinkClient, val: dict, listener: str = None):
# Make sure the client is authenticated
if not client.username:
return await client.send_statuscode("Refused", listener)
# Check ratelimit
if security.ratelimited(f"update_chat:{client}"):
return await client.send_statuscode("RateLimit", listener)
# Ratelimit
security.ratelimit(f"update_chat:{client}", 5, 5)
# Check val datatype
if not isinstance(val, dict):
return await client.send_statuscode("Datatype", listener)
# Check val syntax
if ("chatid" not in val) or ("username" not in val):
return await client.send_statuscode("Syntax", listener)
# Extract chatid and username
chatid = val["chatid"]
username = val["username"]
# Check chatid and username datatypes
if (not isinstance(chatid, str)) or (not isinstance(username, str)):
return await client.send_statuscode("Datatype", listener)
# Check chatid and username syntax
if (len(chatid) > 50) or (len(username) > 20):
return await client.send_statuscode("Syntax", listener)
# Get chat
chat = db.chats.find_one({
"_id": chatid,
"members": {"$all": [client.username, username]},
"deleted": False
})
if not chat:
return await client.send_statuscode("IDNotFound", listener)
# Make sure client is owner of chat
if chat["owner"] != client.username:
return await client.send_statuscode("MissingPermissions", listener)
# Update chat
chat["members"].remove(username)
db.chats.update_one({"_id": chatid}, {"$pull": {"members": username}})
# Send delete chat event
self.cl.broadcast({
"mode": "delete",
"id": chat["_id"]
}, direct_wrap=True, usernames=[username])
# Send update chat event
self.cl.broadcast({
"mode": "update_chat",
"payload": {
"_id": chatid,
"members": chat["members"]
}
}, direct_wrap=True, usernames=[username])
# Send inbox message to user
self.supporter.create_post("inbox", username, f"You have been removed from the group chat '{chat['nickname']}' by @{client.username}!")
# Send in-chat notification
self.supporter.create_post(chatid, "Server", f"@{client.username} removed @{username} from the group chat.", chat_members=chat["members"])
# Tell the client the user was added
await client.send_statuscode("OK", listener)
async def set_chat_state(self, client: CloudlinkClient, val: dict, listener: str = None):
# Make sure the client is authenticated
if not client.username:
return await client.send_statuscode("Refused", listener)
# Check val datatype
if not isinstance(val, dict):
return await client.send_statuscode("Datatype", listener)
# Check val syntax
if ("chatid" not in val) or ("state" not in val):
return await client.send_statuscode("Syntax", listener)
# Extract chatid and state
chatid = val["chatid"]
state = val["state"]
# Check chatid and state datatypes
if (not isinstance(chatid, str)) or (not isinstance(state, int)):
return await client.send_statuscode("Datatype", listener)
# Check chatid syntax
if len(chatid) < 1 or len(chatid) > 50:
return await client.send_statuscode("Syntax", listener)
# Get chat
if chatid != "livechat":
chat = db.chats.find_one({
"_id": chatid,
"members": client.username,
"deleted": False
})
if not chat:
return await client.send_statuscode("IDNotFound", listener)
# Send new state
self.cl.broadcast({
"chatid": chatid,
"u": client.username,
"state": state
}, direct_wrap=True, usernames=(chat["members"] if chatid != "livechat" else None))
# Tell the client the new state was sent
await client.send_statuscode("OK", listener)
async def post_home(self, client: CloudlinkClient, val: str, listener: str = None):
# Make sure the client is authenticated
if not client.username:
return await client.send_statuscode("Refused", listener)
# Check datatype
if not isinstance(val, str):
return await client.send_statuscode("Datatype", listener)
# Check syntax
if len(val) < 1 or len(val) > 4000:
return await client.send_statuscode("Syntax", listener)
# Check ratelimit
if security.ratelimited(f"post:{client.username}"):
return await client.send_statuscode("RateLimit", listener)
# Ratelimit
security.ratelimit(f"post:{client.username}", 6, 5)
# Check restrictions
if security.is_restricted(client.username, security.Restrictions.HOME_POSTS):
return await client.send_statuscode("Banned", listener)
# Create post
self.supporter.create_post("home", client.username, val)
# Tell the client the post was created
await client.send_statuscode("OK", listener)
async def post_chat(self, client: CloudlinkClient, val: dict, listener: str = None):
# Make sure the client is authenticated
if not client.username:
return await client.send_statuscode("Refused", listener)
# Check ratelimit
if security.ratelimited(f"post:{client.username}"):
return await client.send_statuscode("RateLimit", listener)
# Ratelimit
security.ratelimit(f"post:{client.username}", 6, 5)
# Check val datatype
if not isinstance(val, dict):
return await client.send_statuscode("Datatype", listener)
# Check val syntax
if ("chatid" not in val) or ("p" not in val):
return await client.send_statuscode("Syntax", listener)
# Extract chatid and content
chatid = val["chatid"]
content = val["p"]
# Check chatid and content datatypes
if (not isinstance(chatid, str)) or (not isinstance(content, str)):
return await client.send_statuscode("Datatype", listener)
# Check chatid and content syntax
if len(chatid) < 1 or len(chatid) > 50 or len(content) < 1 or len(content) > 4000:
return await client.send_statuscode("Syntax", listener)
# Check restrictions
if security.is_restricted(client.username, security.Restrictions.CHAT_POSTS):
return await client.send_statuscode("Banned", listener)
if chatid != "livechat":
# Get chat
chat = db.chats.find_one({
"_id": chatid,
"members": client.username,
"deleted": False
}, projection={"type": 1, "members": 1})
if not chat:
return await client.send_statuscode("IDNotFound", listener)
# DM stuff
if chat["type"] == 1:
# Check privacy options
if db.relationships.count_documents({"$or": [
{"_id": {"from": chat["members"][0], "to": chat["members"][1]}},
{"_id": {"from": chat["members"][1], "to": chat["members"][0]}}
], "state": 2}, limit=1) > 0:
return await client.send_statuscode("MissingPermissions", listener)
# Update user settings
Thread(target=db.user_settings.bulk_write, args=([
pymongo.UpdateMany({"$or": [
{"_id": chat["members"][0]},
{"_id": chat["members"][1]}
]}, {"$pull": {"active_dms": chatid}}),
pymongo.UpdateMany({"$or": [
{"_id": chat["members"][0]},
{"_id": chat["members"][1]}
]}, {"$push": {"active_dms": {
"$each": [chatid],
"$position": 0,
"$slice": -150
}}})
],)).start()
# Create post
self.supporter.create_post(chatid, client.username, content, chat_members=(chat["members"] if chatid != "livechat" else None))
# Tell the client the post was created
await client.send_statuscode("OK", listener)
async def delete_post(self, client: CloudlinkClient, val: str, listener: str = None):
# Make sure the client is authenticated
if not client.username:
return await client.send_statuscode("Refused", listener)
# Check datatype
if not isinstance(val, str):
return await client.send_statuscode("Datatype", listener)
# Check syntax
if len(val) < 1 or len(val) > 50:
return await client.send_statuscode("Syntax", listener)
# Check ratelimit
if security.ratelimited(f"del_post:{client.username}"):
return await client.send_statuscode("RateLimit", listener)
# Ratelimit
security.ratelimit(f"del_post:{client.username}", 6, 5)
# Get post
post = db.posts.find_one({"_id": val, "isDeleted": False})
if not post:
return await client.send_statuscode("IDNotFound", listener)
# Check access
if post["post_origin"] not in ["home", "inbox"]:
chat = db.chats.find_one({
"_id": post["post_origin"],
"members": client.username,
"deleted": False
}, projection={"owner": 1, "members": 1})
if not chat:
return await client.send_statuscode("MissingPermissions", listener)
if post["post_origin"] == "inbox" or post["u"] != client.username:
if (post["post_origin"] in ["home", "inbox"]) or (chat["owner"] != client.username):
return await client.send_statuscode("MissingPermissions", listener)
# Update post
db.posts.update_one({"_id": post["_id"]}, {"$set": {
"isDeleted": True,
"deleted_at": int(time.time())
}})
# Send delete post event
self.cl.broadcast({
"mode": "delete",
"id": post["_id"]
}, direct_wrap=True, usernames=chat["members"])
await client.send_statuscode("OK", listener)
async def report(self, client: CloudlinkClient, val: str, listener: str = None):
# Make sure the client is authenticated
if not client.username:
return await client.send_statuscode("Refused", listener)
# Check datatype
if not isinstance(val, dict):
return await client.send_statuscode("Datatype", listener)
# Check val syntax
if ("type" not in val) or ("id" not in val):
return await client.send_statuscode("Syntax", listener)
# Extract type, ID, reason, and comment
content_type = val["type"]
content_id = val["id"]
reason = val.get("reason", "No reason specified")
comment = val.get("comment", "")
# Check type, ID, reason, and comment datatypes
if (not isinstance(content_type, int)) or (not isinstance(content_id, str)) or (not isinstance(reason, str)) or (not isinstance(comment, str)):
return await client.send_statuscode("Datatype", listener)
# Make sure the content exists
if content_type == 0:
post = db.posts.find_one({
"_id": content_id,