-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCHATBOX.py
More file actions
257 lines (228 loc) · 10.1 KB
/
CHATBOX.py
File metadata and controls
257 lines (228 loc) · 10.1 KB
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
from prompt_toolkit import print_formatted_text, HTML
from prompt_toolkit.patch_stdout import patch_stdout
from prompt_toolkit import PromptSession
from colorama import Fore, Style, init
from datetime import datetime
import threading
import platform
import random
import socket
import time
import sys
import os
server = '0.0.0.0'
host = '127.0.0.1'
port = 8080
addr = (host, port)
client = None
init(autoreset=True)
ascii_art_list = [r"""
_________ .__ __ __________
\_ ___ \| |__ _____ _/ |\______ \ _______ ___
/ \ \/| | \\__ \\ __\ | _// _ \ \/ /
\ \___| Y \/ __ \| | | | ( <_> > <
\______ /___| (____ /__| |______ /\____/__/\_ \
\/ \/ \/ \/ \/
""",
r"""
/$$$$$$ /$$ /$$ /$$$$$$$
/$$__ $$| $$ | $$ | $$__ $$
| $$ \__/| $$$$$$$ /$$$$$$ /$$$$$$ | $$ \ $$ /$$$$$$ /$$ /$$
| $$ | $$__ $$ |____ $$|_ $$_/ | $$$$$$$ /$$__ $$| $$ /$$/
| $$ | $$ \ $$ /$$$$$$$ | $$ | $$__ $$| $$ \ $$ \ $$$$/
| $$ $$| $$ | $$ /$$__ $$ | $$ /$$| $$ \ $$| $$ | $$ >$$ $$
| $$$$$$/| $$ | $$| $$$$$$$ | $$$$/| $$$$$$$/| $$$$$$/ /$$/\ $$
\______/ |__/ |__/ \_______/ \___/ |_______/ \______/ |__/ \__/
""",
r"""
__ __ __ ____ ______ ____ ___ __ __
/ ]| | | / || || \ / \ | | |
/ / | | || o || || o )| || | |
/ / | _ || ||_| |_|| || O ||_ _|
/ \_ | | || _ | | | | O || || |
\ || | || | | | | | || || | |
\____||__|__||__|__| |__| |_____| \___/ |__|__|
""",
r"""
_______ __ __ _______ _______ _______ _______ __ __
| || | | || _ || || _ || || |_| |
| || |_| || |_| ||_ _|| |_| || _ || |
| || || | | | | || | | || |
| _|| || | | | | _ | | |_| | | |
| |_ | _ || _ | | | | |_| || || _ |
|_______||__| |__||__| |__| |___| |_______||_______||__| |__|
""",
r"""
______ __ __ ___ .___________..______ ______ ___ ___
/ || | | | / \ | || _ \ / __ \ \ \ / /
| ,----'| |__| | / ^ \ `---| |----`| |_) | | | | | \ V /
| | | __ | / /_\ \ | | | _ < | | | | > <
| `----.| | | | / _____ \ | | | |_) | | `--' | / . \
\______||__| |__| /__/ \__\ |__| |______/ \______/ /__/ \__\
""",
r"""
________ ___ ___ ________ _________ ________ ________ ___ ___
|\ ____\|\ \|\ \|\ __ \|\___ ___\\ __ \|\ __ \ |\ \ / /|
\ \ \___|\ \ \\\ \ \ \|\ \|___ \ \_\ \ \|\ /\ \ \|\ \ \ \ \/ / /
\ \ \ \ \ __ \ \ __ \ \ \ \ \ \ __ \ \ \\\ \ \ \ / /
\ \ \____\ \ \ \ \ \ \ \ \ \ \ \ \ \ \|\ \ \ \\\ \ / \/
\ \_______\ \__\ \__\ \__\ \__\ \ \__\ \ \_______\ \_______\/ /\ \
\|_______|\|__|\|__|\|__|\|__| \|__| \|_______|\|_______/__/ /\ __\
|__|/ \|__|
""",
r"""
█████████ █████ █████ ███████████
███▒▒▒▒▒███▒▒███ ▒▒███ ▒▒███▒▒▒▒▒███
███ ▒▒▒ ▒███████ ██████ ███████ ▒███ ▒███ ██████ █████ █████
▒███ ▒███▒▒███ ▒▒▒▒▒███ ▒▒▒███▒ ▒██████████ ███▒▒███▒▒███ ▒▒███
▒███ ▒███ ▒███ ███████ ▒███ ▒███▒▒▒▒▒███▒███ ▒███ ▒▒▒█████▒
▒▒███ ███ ▒███ ▒███ ███▒▒███ ▒███ ███ ▒███ ▒███▒███ ▒███ ███▒▒▒███
▒▒█████████ ████ █████▒▒████████ ▒▒█████ ███████████ ▒▒██████ █████ █████
▒▒▒▒▒▒▒▒▒ ▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒▒▒▒▒▒▒ ▒▒▒▒▒▒ ▒▒▒▒▒ ▒▒▒▒▒
""",
r"""
e88~-_ 888 d8 888~~\
d888 \ 888-~88e /~~~8e _d88__ 888 | e88~-_ Y88b /
8888 888 888 88b 888 888 _/ d888 i Y88b/
8888 888 888 e88~-888 888 888 \ 8888 | Y88b
Y888 / 888 888 C888 888 888 888 | Y888 ' /Y88b
"88_-~ 888 888 "88_-888 "88_/ 888__/ "88_-~ / Y88b
""",
r"""
.d8888b. 888 888 888888b.
d88P Y88b 888 888 888 "88b
888 888 888 888 888 .88P
888 88888b. 8888b. 888888 8888888K. .d88b. 888 888
888 888 "88b "88b 888 888 "Y88b d88""88b `Y8bd8P'
888 888 888 888 .d888888 888 888 888 888 888 X88K
Y88b d88P 888 888 888 888 Y88b. 888 d88P Y88..88P .d8""8b.
"Y8888P" 888 888 "Y888888 "Y888 8888888P" "Y88P" 888 888
""",
r"""
_______ _______ _______ _______ _______ _______ _______
|\ /|\ /|\ /|\ /|\ /|\ /|\ /|
| +---+ | +---+ | +---+ | +---+ | +---+ | +---+ | +---+ |
| | | | | | | | | | | | | | | | | | | | | |
| |C | | |h | | |a | | |t | | |B | | |o | | |x | |
| +---+ | +---+ | +---+ | +---+ | +---+ | +---+ | +---+ |
|/_____\|/_____\|/_____\|/_____\|/_____\|/_____\|/_____\|
"""
]
session = PromptSession()
current_hour = datetime.now()
formatted_current_hour = current_hour.strftime("%H:%M:%S")
if platform.system() == "Windows":
os.system("title ChatBox")
else:
os.system("echo -ne '\033]0;ChatBox\007'")
def xor_encrypt_decrypt(data, key):
return ''.join(chr(ord(c) ^ key) for c in data)
def clear_terminal():
if platform.system() == "Windows":
os.system("cls")
else:
os.system("clear")
def exit_app():
print(f"\n{Fore.RED}See you soon!{Fore.RESET}")
time.sleep(1)
sys.exit()
def make_log_file():
if platform.system() == "Windows":
os.system("type nul > error_log.txt")
else:
os.system("touch error_log.txt")
def write_in_error_log(e):
with open("error_log.txt", "a") as log_file:
log_file.write("[" + formatted_current_hour + "] " + str(e) + "\n")
if not log_file:
make_log_file()
log_file.write("[" + formatted_current_hour + "] " + str(e) + "\n")
chosen_ascii = random.choice(ascii_art_list)
def ASCII():
print(chosen_ascii)
def Main_menu():
ASCII()
print("CMD Powered Chatroom")
print("\n[1] Start server")
print("[2] Connect to server")
print("[3] Exit")
print("")
User_Choice = input("@ChatBox> ")
if User_Choice == "1":
clear_terminal()
start_server()
elif User_Choice == "2":
clear_terminal()
connect()
elif User_Choice == "3":
exit_app()
else:
clear_terminal()
Main_menu()
def start_server():
serverpart = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
serverpart.bind((server, port))
serverpart.listen()
ASCII()
print(f"{Fore.RED}Listening for new connections. . .{Fore.RESET}")
conn, addr = serverpart.accept()
print(f"{Fore.RED}There's a connection!")
listen_thread = threading.Thread(target=listen_text, args=(conn,))
listen_thread.start()
send_text(conn)
return
except Exception as e:
clear_terminal()
time.sleep(3)
start_server()
def connect():
ASCII()
print(f"{Fore.RED}Trying the connection. . .{Fore.RESET}")
global client
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
client.connect(addr)
print(f"{Fore.RED}Connected!{Fore.RESET}")
listen_thread = threading.Thread(target=listen_text, args=(client,))
listen_thread.start()
send_text(client)
except Exception as e:
while True:
time.sleep(1)
clear_terminal()
connect()
def send_text(conn):
try:
with patch_stdout():
nickname = session.prompt("\nNickname: ")
enc_nickname = xor_encrypt_decrypt(nickname, 934028)
conn.send(enc_nickname.encode())
while True:
message = session.prompt(nickname + "> ")
enc_message = xor_encrypt_decrypt(message, 934028)
conn.send(enc_message.encode())
except Exception as e:
write_in_error_log(e)
input("\nAll users left or there was a problem, press ENTER to go back. . .")
clear_terminal()
Main_menu()
def listen_text(conn):
try:
with patch_stdout():
nickname = conn.recv(1024).decode()
decrypted_nick = xor_encrypt_decrypt(nickname, 934028)
print_formatted_text(HTML(f'<style fg="red">{decrypted_nick}</style> has entered the chat!'))
while True:
with patch_stdout():
data = conn.recv(1024).decode()
decrypted_data = xor_encrypt_decrypt(data, 934028)
print(f"\n" + decrypted_nick + "> " + decrypted_data)
except Exception as e:
write_in_error_log(e)
input("\nAll users left or there was a problem, press ENTER to go back. . .")
clear_terminal()
Main_menu()
if __name__ == '__main__':
Main_menu()
conn = start_server()