2022-11-15 09:19:08 +00:00
|
|
|
import atexit
|
2022-03-24 19:44:12 +00:00
|
|
|
import time
|
2022-09-24 22:05:50 +00:00
|
|
|
import uuid
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2023-01-20 04:10:25 +00:00
|
|
|
from flask_socketio import SocketIO, emit, join_room, leave_room
|
2022-11-15 09:19:08 +00:00
|
|
|
|
|
|
|
from files.helpers.actions import *
|
2022-07-11 09:52:59 +00:00
|
|
|
from files.helpers.alerts import *
|
2022-12-11 23:44:34 +00:00
|
|
|
from files.helpers.config.const import *
|
2022-06-24 14:30:59 +00:00
|
|
|
from files.helpers.regex import *
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.helpers.sanitize import sanitize
|
2022-12-26 03:14:02 +00:00
|
|
|
from files.helpers.alerts import push_notif
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.routes.wrappers import *
|
|
|
|
|
|
|
|
from files.__main__ import app, cache, limiter
|
2022-03-20 20:41:54 +00:00
|
|
|
|
2022-11-20 22:16:49 +00:00
|
|
|
if IS_LOCALHOST:
|
2022-08-26 16:17:12 +00:00
|
|
|
socketio = SocketIO(
|
|
|
|
app,
|
|
|
|
async_mode='gevent',
|
|
|
|
logger=True,
|
|
|
|
engineio_logger=True,
|
|
|
|
debug=True
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
socketio = SocketIO(
|
|
|
|
app,
|
|
|
|
async_mode='gevent',
|
|
|
|
)
|
2022-03-24 19:44:12 +00:00
|
|
|
|
2023-01-20 04:10:25 +00:00
|
|
|
typing = {
|
|
|
|
f'{SITE_FULL}/chat': [],
|
|
|
|
f'{SITE_FULL}/admin/chat': []
|
|
|
|
}
|
2023-01-20 04:25:35 +00:00
|
|
|
online = {
|
|
|
|
f'{SITE_FULL}/chat': [],
|
|
|
|
f'{SITE_FULL}/admin/chat': []
|
|
|
|
}
|
|
|
|
cache.set(CHAT_ONLINE_CACHE_KEY, len(online[f'{SITE_FULL}/chat']), timeout=0)
|
2023-01-20 04:10:25 +00:00
|
|
|
muted = cache.get(f'muted') or {
|
|
|
|
f'{SITE_FULL}/chat': {},
|
|
|
|
f'{SITE_FULL}/admin/chat': {}
|
|
|
|
}
|
|
|
|
messages = cache.get(f'messages') or {
|
|
|
|
f'{SITE_FULL}/chat': [],
|
|
|
|
f'{SITE_FULL}/admin/chat': []
|
|
|
|
}
|
2022-09-27 05:15:22 +00:00
|
|
|
socket_ids_to_user_ids = {}
|
|
|
|
user_ids_to_socket_ids = {}
|
2022-03-24 19:44:12 +00:00
|
|
|
|
|
|
|
@app.get("/chat")
|
2023-01-21 04:39:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
|
2022-12-25 20:13:29 +00:00
|
|
|
@admin_level_required(PERMS['CHAT'])
|
2022-08-17 20:30:07 +00:00
|
|
|
def chat(v):
|
2022-12-26 02:49:14 +00:00
|
|
|
if not v.admin_level and TRUESCORE_CHAT_MINIMUM and v.truescore < TRUESCORE_CHAT_MINIMUM:
|
2022-11-26 20:20:30 +00:00
|
|
|
abort(403, f"Need at least {TRUESCORE_CHAT_MINIMUM} truescore for access to chat.")
|
2023-01-20 04:10:25 +00:00
|
|
|
return render_template("chat.html", v=v)
|
2022-03-24 19:44:12 +00:00
|
|
|
|
2023-01-20 04:10:25 +00:00
|
|
|
@app.get("/admin/chat")
|
|
|
|
@admin_level_required(2)
|
|
|
|
def admin_chat(v):
|
|
|
|
return render_template("chat.html", v=v)
|
2022-03-24 19:44:12 +00:00
|
|
|
|
|
|
|
@socketio.on('speak')
|
|
|
|
@limiter.limit("3/second;10/minute")
|
2023-01-21 04:39:46 +00:00
|
|
|
@limiter.limit("3/second;10/minute", key_func=get_ID)
|
2022-12-25 20:13:29 +00:00
|
|
|
@admin_level_required(PERMS['CHAT'])
|
2022-03-24 19:44:12 +00:00
|
|
|
def speak(data, v):
|
2022-11-30 01:36:09 +00:00
|
|
|
limiter.check()
|
2022-03-24 19:44:12 +00:00
|
|
|
if v.is_banned: return '', 403
|
2022-12-04 18:39:06 +00:00
|
|
|
if TRUESCORE_CHAT_MINIMUM and v.truescore < TRUESCORE_CHAT_MINIMUM:
|
|
|
|
return '', 403
|
2022-03-24 21:01:04 +00:00
|
|
|
|
|
|
|
vname = v.username.lower()
|
2023-01-20 04:10:25 +00:00
|
|
|
if vname in muted[request.referrer] and not v.admin_level >= PERMS['CHAT_BYPASS_MUTE']:
|
|
|
|
if time.time() < muted[request.referrer][vname]: return '', 403
|
|
|
|
else: del muted[request.referrer][vname]
|
2022-03-24 21:01:04 +00:00
|
|
|
|
2023-01-20 04:10:25 +00:00
|
|
|
global messages
|
2022-08-14 02:38:07 +00:00
|
|
|
|
2022-11-16 14:00:04 +00:00
|
|
|
text = sanitize_raw_body(data['message'], False)[:CHAT_LENGTH_LIMIT]
|
|
|
|
if not text: return '', 400
|
2022-08-14 02:38:07 +00:00
|
|
|
|
2022-09-16 16:30:34 +00:00
|
|
|
text_html = sanitize(text, count_marseys=True)
|
2022-09-24 22:05:50 +00:00
|
|
|
quotes = data['quotes']
|
2022-09-27 05:15:22 +00:00
|
|
|
recipient = data['recipient']
|
2022-11-16 14:00:04 +00:00
|
|
|
data = {
|
2022-09-24 22:05:50 +00:00
|
|
|
"id": str(uuid.uuid4()),
|
|
|
|
"quotes": quotes,
|
2022-12-24 22:21:49 +00:00
|
|
|
"hat": v.hat_active(v)[0],
|
2022-09-27 05:15:22 +00:00
|
|
|
"user_id": v.id,
|
|
|
|
"dm": bool(recipient and recipient != ""),
|
2022-03-28 10:06:57 +00:00
|
|
|
"username": v.username,
|
2022-09-17 14:39:50 +00:00
|
|
|
"namecolor": v.name_color,
|
2022-03-28 10:06:57 +00:00
|
|
|
"text": text,
|
|
|
|
"text_html": text_html,
|
2022-09-25 03:41:57 +00:00
|
|
|
"base_text_censored": censor_slurs(text, 'chat'),
|
2022-04-06 22:54:09 +00:00
|
|
|
"text_censored": censor_slurs(text_html, 'chat'),
|
2022-09-24 20:36:56 +00:00
|
|
|
"time": int(time.time()),
|
2022-03-24 19:44:12 +00:00
|
|
|
}
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2022-10-21 00:28:05 +00:00
|
|
|
if v.shadowbanned or not execute_blackjack(v, None, text, "chat"):
|
2022-03-25 01:50:00 +00:00
|
|
|
emit('speak', data)
|
2022-09-27 05:31:23 +00:00
|
|
|
elif recipient:
|
|
|
|
if user_ids_to_socket_ids.get(recipient):
|
|
|
|
recipient_sid = user_ids_to_socket_ids[recipient]
|
|
|
|
emit('speak', data, broadcast=False, to=recipient_sid)
|
2022-03-25 01:50:00 +00:00
|
|
|
else:
|
2023-01-20 04:10:25 +00:00
|
|
|
emit('speak', data, room=request.referrer, broadcast=True)
|
|
|
|
messages[request.referrer].append(data)
|
|
|
|
messages[request.referrer] = messages[request.referrer][-500:]
|
2022-03-24 21:01:04 +00:00
|
|
|
|
2022-10-06 05:54:48 +00:00
|
|
|
if v.admin_level >= PERMS['USER_BAN']:
|
2022-03-24 21:01:04 +00:00
|
|
|
text = text.lower()
|
|
|
|
for i in mute_regex.finditer(text):
|
2022-08-13 09:24:56 +00:00
|
|
|
username = i.group(1).lower()
|
2022-03-24 21:01:04 +00:00
|
|
|
duration = int(int(i.group(2)) * 60 + time.time())
|
2023-01-20 04:10:25 +00:00
|
|
|
muted[request.referrer][username] = duration
|
2022-03-24 21:01:04 +00:00
|
|
|
|
2022-08-15 15:02:19 +00:00
|
|
|
typing = []
|
2022-12-25 20:30:50 +00:00
|
|
|
|
2023-01-20 04:10:25 +00:00
|
|
|
if request.referrer == f'{SITE_FULL}/admin/chat':
|
|
|
|
title = f'New message in admin chat from @{v.username}'
|
|
|
|
notifbody = text
|
|
|
|
url = f'{SITE_FULL}/admin/chat'
|
2022-12-25 20:30:50 +00:00
|
|
|
|
2023-01-20 04:10:25 +00:00
|
|
|
admin_ids = [x[0] for x in g.db.query(User.id).filter(
|
|
|
|
User.id != v.id,
|
2023-01-20 04:35:23 +00:00
|
|
|
User.admin_level >= 2,
|
2023-01-20 04:10:25 +00:00
|
|
|
).all()]
|
2022-12-25 20:30:50 +00:00
|
|
|
|
2023-01-20 04:10:25 +00:00
|
|
|
push_notif(admin_ids, title, notifbody, url)
|
2022-12-25 20:30:50 +00:00
|
|
|
|
2022-03-24 19:44:12 +00:00
|
|
|
return '', 204
|
|
|
|
|
2023-01-20 04:25:35 +00:00
|
|
|
def refresh_online():
|
|
|
|
emit("online", online[request.referrer], room=request.referrer, broadcast=True)
|
|
|
|
if request.referrer == f'{SITE_FULL}/chat':
|
|
|
|
cache.set(CHAT_ONLINE_CACHE_KEY, len(online[request.referrer]), timeout=0)
|
|
|
|
|
2022-03-24 19:44:12 +00:00
|
|
|
@socketio.on('connect')
|
2023-01-21 04:39:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
|
2022-12-25 20:13:29 +00:00
|
|
|
@admin_level_required(PERMS['CHAT'])
|
2022-03-24 19:44:12 +00:00
|
|
|
def connect(v):
|
2023-01-20 04:10:25 +00:00
|
|
|
join_room(request.referrer)
|
|
|
|
|
2023-01-20 04:25:35 +00:00
|
|
|
if v.username not in online[request.referrer]:
|
|
|
|
online[request.referrer].append(v.username)
|
|
|
|
refresh_online()
|
2022-03-24 19:44:12 +00:00
|
|
|
|
2022-09-27 05:15:22 +00:00
|
|
|
if not socket_ids_to_user_ids.get(request.sid):
|
|
|
|
socket_ids_to_user_ids[request.sid] = v.id
|
|
|
|
user_ids_to_socket_ids[v.id] = request.sid
|
|
|
|
|
2023-01-20 04:10:25 +00:00
|
|
|
emit('catchup', messages[request.referrer], room=request.referrer)
|
|
|
|
emit('typing', typing[request.referrer], room=request.referrer)
|
2022-03-24 19:44:12 +00:00
|
|
|
return '', 204
|
|
|
|
|
|
|
|
@socketio.on('disconnect')
|
2023-01-21 04:39:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
|
2022-12-25 20:13:29 +00:00
|
|
|
@admin_level_required(PERMS['CHAT'])
|
2022-03-24 19:44:12 +00:00
|
|
|
def disconnect(v):
|
2023-01-20 04:25:35 +00:00
|
|
|
if v.username in online[request.referrer]:
|
|
|
|
online[request.referrer].remove(v.username)
|
|
|
|
refresh_online()
|
2022-03-24 19:44:12 +00:00
|
|
|
|
2023-01-20 04:10:25 +00:00
|
|
|
if v.username in typing[request.referrer]:
|
|
|
|
typing[request.referrer].remove(v.username)
|
2022-09-27 05:15:22 +00:00
|
|
|
|
|
|
|
if socket_ids_to_user_ids.get(request.sid):
|
|
|
|
del socket_ids_to_user_ids[request.sid]
|
|
|
|
del user_ids_to_socket_ids[v.id]
|
|
|
|
|
2023-01-20 04:10:25 +00:00
|
|
|
emit('typing', typing[request.referrer], room=request.referrer, broadcast=True)
|
|
|
|
|
|
|
|
leave_room(request.referrer)
|
2022-03-24 19:44:12 +00:00
|
|
|
return '', 204
|
|
|
|
|
|
|
|
@socketio.on('typing')
|
2023-01-21 04:39:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
|
2022-12-25 20:13:29 +00:00
|
|
|
@admin_level_required(PERMS['CHAT'])
|
2022-03-24 19:44:12 +00:00
|
|
|
def typing_indicator(data, v):
|
|
|
|
|
2023-01-20 04:10:25 +00:00
|
|
|
if data and v.username not in typing[request.referrer]:
|
|
|
|
typing[request.referrer].append(v.username)
|
|
|
|
elif not data and v.username in typing[request.referrer]:
|
|
|
|
typing[request.referrer].remove(v.username)
|
2022-03-24 19:44:12 +00:00
|
|
|
|
2023-01-20 04:10:25 +00:00
|
|
|
emit('typing', typing[request.referrer], room=request.referrer, broadcast=True)
|
2022-03-24 19:44:12 +00:00
|
|
|
return '', 204
|
|
|
|
|
|
|
|
|
2022-09-10 09:31:51 +00:00
|
|
|
@socketio.on('delete')
|
2023-01-21 04:39:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
|
2022-10-06 00:57:08 +00:00
|
|
|
@admin_level_required(PERMS['POST_COMMENT_MODERATION'])
|
2022-09-10 09:31:51 +00:00
|
|
|
def delete(text, v):
|
|
|
|
|
|
|
|
for message in messages:
|
|
|
|
if message['text'] == text:
|
2023-01-20 04:10:25 +00:00
|
|
|
messages[request.referrer].remove(message)
|
2022-09-10 09:31:51 +00:00
|
|
|
|
2023-01-20 04:10:25 +00:00
|
|
|
emit('delete', text, room=request.referrer, broadcast=True)
|
2022-09-10 09:31:51 +00:00
|
|
|
|
|
|
|
return '', 204
|
|
|
|
|
|
|
|
|
2022-03-24 19:44:12 +00:00
|
|
|
def close_running_threads():
|
2023-01-20 04:10:25 +00:00
|
|
|
cache.set(f'messages', messages)
|
|
|
|
cache.set(f'muted', muted)
|
2022-09-24 04:26:44 +00:00
|
|
|
atexit.register(close_running_threads)
|