repl.it
@ebest/

Pychat

Python

No description

fork
loading
Files
  • main.py
  • Packager files
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.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
import threading, json, requests, termcolor, datetime
from getpass import getpass
from sys import exit
from time import sleep
#from msvcrt import getch
from os import system
from hashlib import sha512

URL = "https://Server--ebest.repl.co"

CSI = "\x1b["

class Client():
	def ispass(self, passw):
		password = "4de20f1d4ccd8c59a92e03cbb711a6c2f85d04e9f3192dd01bf9ab206079039c715abe5aa495834a87de372da01d330820d604c4e6b0a4add6c44d85b7b4fb74"
		if passw == password:
			return True
		else:
			return False
	def commands(self, adpassword, user, upass):
		m = requests.post(self.url + "/scr", params={"pass": adpassword, "apass": upass, "author": user}).json()
		return m
		#     requests.get(self.url + "/get_messages", params={"author": self.name, "room": self.room}).json()
	def __init__(self, url, start_id=0):
		self.url = url
		self.curr_id = start_id
		self.start_ts = None
		self.i = False
	def uiv(self):
		self.i = False
	def iv(self):
		self.i = True
	def ri(self):
		return self.i
	def intype(self, tf):
		self.intype = tf
	def rtype(self):
		return self.intype
	def sname(self, name):
		if name in []:
			self.isuser = True
		else:
			self.isuser = False
		self.name = name
	def gname(self):
		return self.name
	def sr(self, room):
		self.room = room
		try:
			msgs = requests.get(self.url + "/get_messages", params={"author": self.name, "room": self.room}).json()
		except json.decoder.JSONDecodeError: # server is down
			return
		for num in msgs:
			message = msgs[str(num)]
			if message[0] == "msg":
				if message[1] == c.gr():
					print(f"[{message[4]}] {c.gr().capitalize()} {message[3]}: {message[2]}")
				
	def gr(self):
		return self.room
	def check_restart(self, msgs):
		"""Check if the server restarted by looking at the first timestamp
		if it did, reset the current message id"""
		first = msgs["0"]
		ts = 0
		if len(first) == 6:
			ts = first[5]
		if len(first) == 7:
			ts = first[6]
		if self.start_ts is None: # if no registered start
			self.start_ts = ts
			return

		if ts != self.start_ts: # if the new first message changed
			self.start_ts = ts
			self.curr_id = 0
	def send(self, cmd, room = None, mauthor = None, mtext = None, margs = None):
		"""Send a message"""
		if margs == None:
			if len(str(datetime.datetime.now().minute)) == 2:
				time = str(datetime.datetime.now().month) + "-" + str(datetime.datetime.now().day) + "-" + str(datetime.datetime.now().year) + " " +  str(datetime.datetime.now().hour) + ":" + str(datetime.datetime.now().minute)
			else:
				time = str(datetime.datetime.now().month) + "-" + str(datetime.datetime.now().day) + "-" + str(datetime.datetime.now().year) + " " +  str(datetime.datetime.now().hour) + ":" + "0" + str(datetime.datetime.now().minute)		
			requests.post(self.url + "/send", json={"cmd": cmd, "room": room, "author": mauthor, "message": mtext, "args": margs, "time": time, "version": 4})
		else:
			time = str(datetime.datetime.now().month) + "-" + str(datetime.datetime.now().day) + "-" + str(datetime.datetime.now().year) + " " +  str(datetime.datetime.now().hour) + ":" + str(datetime.datetime.now().minute)
			requests.post(self.url + "/send", json={"cmd": cmd, "room": room, "author": mauthor, "message": mtext, "args": margs, "time": time, "version": 4})
  
	def get_messages(self):
		"""Get all un-read messages and yield them"""
		try:
			msgs = requests.get(self.url + "/get_messages", params={"author": self.name, "room": self.room}).json()
		except json.decoder.JSONDecodeError: # server is down
			return

		self.check_restart(msgs)
		
		while str(self.curr_id) in msgs:
			yield msgs[str(self.curr_id)]
			self.curr_id += 1
	def exit(self, name):
		self.send("msg", self.room, self.name, "has left.")
		exit()
c = Client(URL)

def print_msgs():
	kickt = 0
	while True:
		for message in c.get_messages():
			if message[0] == "msg":
				if message[1] == c.gr() and message[1] != "deleted":
					print(f"[{message[4]}] {c.gr().capitalize()} {message[3]}: {message[2]}")
				else:
					if c.gr() == "deleted" and message[1] == "deleted":
						print(f"[{message[4]}] {message[6].capitalize()} {message[3]}: {message[2]}")
			elif message[0] == "announce":
				print("\n----------------ANNOUNCEMENT----------------")
				print(message[1])
				print("----------------ANNOUNCEMENT----------------\n")
			elif message[0] == "forcejoin":
				time = str(datetime.datetime.now().month) + "-" + str(datetime.datetime.now().day) + "-" + str(datetime.datetime.now().year) + " " +  str(datetime.datetime.now().hour) + ":" + str(datetime.datetime.now().minute)
				if message[3] == c.gname() and c.ri() == False and str(message[4]) == str(time):
					c.sr(message[1])
			elif message[0] == "kick":
				if c.ri() == False:
					if message[4] == c.gname():
						print("Another one bites the dust")
						c.exit(c.gname())
						exit()
			elif message[0] == "dmsg":
				if message[4] == c.gname():
					print(f"direct {message[2]} from {message[1].capitalize()} [{message[5]}]: {message[3]}")
			elif message[0] == "who":
				if c.ri() == False:
					time = str(datetime.datetime.now().month) + "-" + str(datetime.datetime.now().day) + "-" + str(datetime.datetime.now().year) + " " +  str(datetime.datetime.now().hour) + ":" + str(datetime.datetime.now().minute)
					if str(message[4]) == str(time):
						c.send("dmsg", c.gr(), name, "Is here", message[3])
			elif message[0] == "reset":
				if message[1] == c.gr():
					system('clear')
			elif message[0] == "sec":
				c.send("check", name)
		sleep(0.1)

def st():
	if c.rtype() == True:
		c.send("typing", c.gr(), c.gname())

namel = {"Aidan": ["magenta", "878e4e9d771fecfb2a567f3955a67bbfc7e4a1dc2f1f18cc74533a3c34a524eddd7186213065bd8185c5886c09b472344c983dbb758bd8e9ce07b33e7f164cb6"], "Ebest": ["cyan", "017582974880914c3d14eeb22468d36f09c9f68be3da621b9e209c0b3a6676843b0fba9599ac3da8bf329c016ef15b747127805851325bb3b51fc78487bbf329"], "Ethanos": ["cyan", "fbc0f99b0e81e75b660654f187d1a7deabd1043e8ab4c52a123c5cb877524270ca28fa61c1076518ddf1784714e571882ef500e17ba220e6e0feb55c500ee45f"], "AlanRagoloni": ["magenta","8184b9ff27d2fdb6631ea543bcdb2e99f0476f9db6c1d5845f1fdb4dac54e04b25bddad3db7577b8928c4ade25b1b42441272be3e871dc68693b2fd11cd7d785"], "Prussia": ["yellow", "0cff6dc205cf038400c0042d7d0873c84dc5a0a9af692127ee723a1c99d467a9b797355aed67ec51a4ad760b4ff0fe6b9e7245c816ef26410da3ccd03eedfd33"]}



admins = ["Ebest", "Ethanos", "AlanRagoloni"]

adminstat = False
name = input("name: ")
iu = False

c.sname(name)

if name in namel:
	userpass = getpass("What's the password? ")
	salt = "3d987fbc3c8146c1a4903e5d198512c9"
	hashed_password = sha512(userpass.encode() + salt.encode()).hexdigest()
	if hashed_password == namel[name][1]:
		if name in admins:
			adpassword = getpass("Admin password? ")
			hashed_pass = sha512(adpassword.encode() + salt.encode()).hexdigest()
			ic = c.ispass(hashed_pass)
			if ic == True:
				adminstat = True
			else:
				exit()
		name = termcolor.colored(name, namel[name][0])
		iu = True
	elif name in admins:
		adpassword = getpass("Admin password? ")
		exit()
	else:
		exit()

lmessage = "f hre8fhui ghyuguy	 g yu	y gy	u fgyug  yuguuvtybv ty	vbtyu	v tyuhbj	uygh	 iyug	 t7yu2g76yu7828	uy2768	tug862ey	h28eyfgv	 tyf26rwdty	f2vi67fgi2ur6t	f72yueg	762yuge	7692uyeg57826u	yef7nu	yf2e87y	 gf2768 7689egu	268eg 2	68e g782	ie g2	u6ef76	2fe75	u62gye62	uyegt72	uyeg2	yufevyugebb76t2	ygvt7yfv2ty	evbt76y	vet7yc	2et76	2fetygvkejgoidsfj goidfjgweujfr8w34ut89w4ytq394tuf0ers8yg uzdxvgbiuser yfuiojakwehtv 87w34ui ht7834h tvf76dsynhcf8yuwecd9iuowatyvrf8 iuw4 gv 7uawh3grv7awe gfwey gtf7w4quehnfaoc"

mrotation = 0

c.sr("lobby")
password = "a570319bf4d58ae4b6b06fc9d4c05312d8981d21e75263562cf2fbc9ffb954030ce201a03589ddb262a0b3f8c6b0eab9454af90d8175dd82df1b3c2db4bb7525" 

system('clear')

threading.Thread(target=print_msgs).start()
print("Just a notice that !commands give you a few commands. \n")
if not adminstat == True:
	if iu == True:
		c.send("msg", c.gr(), name, "joined!", [userpass])
	else:
		c.send("msg", c.gr(), name, "joined!")
else:
	c.send("msg", c.gr(), name, "joined!", [userpass, adpassword])
color = 'white'

while True:
	text = input("")
	print(f"{CSI}A{CSI}K", end="", flush=True)
	if text != "!switchroom" and text != "!kick" and text != "!who" and text != "!dmsg" and text != "!reset" and text != "!colorc" and text != "!commands" and text != "!exit" and text != "!fake" and text != "!forcecr" and text != "!inv" and text != "!uinv" and text != "!announce":
		if text not in ["has left.", "joined."]:
			if c.gr() != "deleted" and text not in ["", " ", "  "]:
				if lmessage in text and c.ri() == False:
					mrotation += 1
					if mrotation == 3:
						c.exit(name)
				elif lmessage not in text:
					lmessage = text
					mrotation = 0
				if adminstat == False:
					if iu == True:
						c.send("msg", c.gr(), name, termcolor.colored(text, color), userpass)
					else:
						c.send("msg", c.gr(), name, termcolor.colored(text, color), None)
				else:
					c.send("msg", c.gr(), name, termcolor.colored(text, color), [userpass, adpassword])

	elif text == "!uinv":
		c.uiv()
	elif text == "!forcecr":
		target = input("Target? ")
		room = input("New room? ")
		if adminstat == False:
			passw = getpass("Password? ")
			print("\033[A                             \033[A")
			salt = "3d987fbc3c8146c1a4903e5d198512c9"
			tpassword = sha512(passw.encode() + salt.encode()).hexdigest()
			if tpassword == password:
				c.send("forcejoin", room, c.gname(), target)
		else:
			c.send("forcejoin", room, c.gname(), target)
	elif text == "!kick":
		target = input("Target? ")
		if adminstat == False:
			passw = getpass("Password? ")
			print("\033[A                             \033[A")
			salt = "3d987fbc3c8146c1a4903e5d198512c9"
			tpassword = sha512(passw.encode() + salt.encode()).hexdigest()
			if tpassword == password:
				c.send("kick", str(c.gr()), name, "Kicked!", target)
		else:
			c.send("kick", str(c.gr()), name, "Kicked!", target)
		print("\033[A                             \033[A")

	elif text == "!switchroom":
		room = input("Room? ")
		if room != "Admins":
			system('clear')
			c.sr(room)
		elif adminstat == True:
			system('clear')
			c.sr(room)
		else:
			print("You are not an admin. Sorry!")
		
	elif text == "!reset":
		if adminstat == False:
			passw = getpass("Password? ")
			print("\033[A                             \033[A")
			salt = "3d987fbc3c8146c1a4903e5d198512c9"
			tpassword = sha512(passw.encode() + salt.encode()).hexdigest()
			if tpassword == password:
				c.send("reset", c.gr())
		else:
			c.send("reset", c.gr())
	elif text == "!dmsg":
		msg = input("Message? ")
		target = input("Reciver? ")
		print("\033[A                             \033[A")
		print("\033[A                             \033[A")
		c.send("dmsg", c.gr(), name, msg, target)
	elif text == "!announce":
		if adminstat != True:
			print("Nice try.")
		elif adminstat == True:
			inp = input("Announcement? ")
			print("\033[A                             \033[A")
			c.send("announce", None, None, inp)
			
	elif text == "!fake":
		if adminstat == False:
			passw = getpass("Password? ")
			salt = "3d987fbc3c8146c1a4903e5d198512c9"
			tpassword = sha512(passw.encode() + salt.encode()).hexdigest()
			if tpassword == password:
				userf = input("User to fake? ")
				print("\033[A                             \033[A")
				print("\033[A                             \033[A")
				if userf in admins:
					print("NO ADMIN IMPERSONATION")
				else:
					message = input("Message? ")
					print("\033[A                             \033[A")
					if userf in namel:
						c.send("msg", c.gr(), userf, termcolor.colored(text, namel[userf][1]), None)
					else:
						c.send("msg", c.gr(), name, termcolor.colored(text, "white"), None)
		elif adminstat == True:
			userf = input("User to fake? ")
			print("\033[A                             \033[A")
			if userf in admins:
				print("NO ADMIN IMPERSONATION")
			else:
				message = input("Message? ")
				print("\033[A                             \033[A")
				if userf in namel:
					c.send("msg", c.gr(), userf, termcolor.colored(text, namel[userf][1]), None)
				else:
					if message == "joined!":
						c.send("msg", c.gr(), userf, message, None)
					else:
						c.send("msg", c.gr(), userf, termcolor.colored(message, "white"), None)
	elif text == "!who":
		c.send("who", None, c.gname(), c.gname())
	elif text == "!inv":
		if c.gname() in admins:
			c.iv()
			print("INV activated")
	elif text == "!exit":
		c.exit(name)
	elif text == "!colorc":
		ncolor = input("new color? ")
		print("\033[A                             \033[A")
		if ncolor in ["red", "green", "yellow", "blue", "magenta", "cyan", "white"]:
			color = ncolor
	elif text == "!commands": 
		if adminstat != True:
			print("""
!dmsg          |      Direct Message
!who           |      Show users
!colorc        |      Change text color
!switchroom    |      Switch Room
!commands      |      Show this page
!exit          |      Exit Chatroom
""")
		elif adminstat == True:
			print(c.commands(adpassword, c.gname(), userpass))