-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
122 lines (94 loc) · 3.88 KB
/
app.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
from flask import Flask, render_template, request, session, redirect, url_for
from flask_socketio import SocketIO, emit, join_room
import json
import zlib # Add zlib for compression
from time import time
import os
app = Flask(__name__)
app.config['SECRET_KEY'] = 'cv4u8fvj8wr5tijm8345tjiwuj-5t80tu'
socketio = SocketIO(app)
# Define the folder path
JSON_FOLDER = 'chatjson'
# Define Blocked Usernames
blocked = ["Server", "Admin", "BlockedUser"]
# Specify the paths to your SSL certificate and private key
CERT_PATH = '/path/to/your/certificate.crt'
KEY_PATH = '/path/to/your/private.key'
# Create the folder if it doesn't exist
os.makedirs(JSON_FOLDER, exist_ok=True)
def write_data_to_json(room, data):
filename = os.path.join(JSON_FOLDER, f'{room}_data.json')
# Compress the messages before writing to JSON file
compressed_data = zlib.compress(json.dumps(data).encode('utf-8'))
with open(filename, 'wb') as json_file:
json_file.write(compressed_data)
def read_data_from_json(room):
filename = os.path.join(JSON_FOLDER, f'{room}_data.json')
try:
with open(filename, 'rb') as json_file:
compressed_data = json_file.read()
# Decompress the data before loading it
data = json.loads(zlib.decompress(compressed_data).decode('utf-8'))
return data
except FileNotFoundError:
return {'messages': []}
@app.route('/')
def index():
return render_template('index.html')
@app.route('/chat')
def chat():
username = request.args.get('username')
# Check for blocked username
if username.lower() in map(str.lower, blocked):
print(username + " was blocked from use")
return redirect(url_for('index'))
room = request.args.get('room')
if username and room:
session['username'] = username
session['room'] = room
data = read_data_from_json(room)
return render_template('chat.html', room=room, data=data)
else:
return redirect(url_for('index'))
@socketio.on('join')
def handle_join(data):
username = data['username']
room = data['room']
if username.lower() in map(str.lower, blocked):
print("Blocked username join message")
else:
session['username'] = username
session['room'] = room
join_room(room)
# Load previous messages for the chat room
data = read_data_from_json(room)
messages = data.get('messages', [])
emit('load_messages', {'messages': messages})
# Log connect message as a regular message
connect_message = f'{username} has joined the room.'
data['messages'].append({'username': 'Server', 'text': connect_message, 'timestamp': int(time())})
write_data_to_json(room, data)
emit('message', {'username': 'Server', 'text': connect_message}, room=room)
print('Client connected')
@socketio.on('disconnect')
def handle_disconnect():
username = session.get('username', 'Anonymous')
room = session.get('room', 'default')
leave_message = f'{username} has left the room.'
# Log disconnect message as a regular message
data = read_data_from_json(room)
data['messages'].append({'username': 'Server', 'text': leave_message, 'timestamp': int(time())})
write_data_to_json(room, data)
emit('message', {'username': 'Server', 'text': leave_message}, room=room)
print('Client disconnected')
@socketio.on('message')
def handle_message(data):
username = session.get('username', 'Anonymous')
room = session.get('room', 'default')
text = data['text']
data = read_data_from_json(room)
data['messages'].append({'username': username, 'text': text, 'timestamp': int(time())})
write_data_to_json(room, data)
emit('message', {'username': username, 'text': text}, room=room)
socketio.run(app, debug=False, host='0.0.0.0', port=5000)
# add ssl_context=(CERT_PATH, KEY_PATH) and change file paths above