2022-03-24 19:44:12 +00:00
|
|
|
import time
|
|
|
|
from files.helpers.wrappers import auth_required
|
|
|
|
from files.helpers.sanitize import sanitize
|
|
|
|
from files.helpers.const import *
|
2022-07-11 09:52:59 +00:00
|
|
|
from files.helpers.alerts import *
|
2022-06-24 14:30:59 +00:00
|
|
|
from files.helpers.regex import *
|
2022-03-24 19:44:12 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from flask_socketio import SocketIO, emit
|
|
|
|
from files.__main__ import app, limiter, cache
|
|
|
|
from flask import render_template, make_response, send_from_directory
|
2022-03-22 14:17:43 +00:00
|
|
|
import sys
|
2022-03-24 19:44:12 +00:00
|
|
|
import atexit
|
2022-03-20 20:41:54 +00:00
|
|
|
|
2022-03-24 19:44:12 +00:00
|
|
|
if SITE == 'localhost':
|
|
|
|
socketio = SocketIO(app, async_mode='gevent', cors_allowed_origins=[SITE_FULL], logger=True, engineio_logger=True, debug=True)
|
|
|
|
else:
|
2022-03-22 14:11:33 +00:00
|
|
|
socketio = SocketIO(app, async_mode='gevent', cors_allowed_origins=[SITE_FULL])
|
2022-03-24 19:44:12 +00:00
|
|
|
|
|
|
|
typing = []
|
|
|
|
online = []
|
2022-04-26 00:38:52 +00:00
|
|
|
muted = cache.get(f'{SITE}_muted') or {}
|
|
|
|
messages = cache.get(f'{SITE}_chat') or []
|
|
|
|
total = cache.get(f'{SITE}_total') or 0
|
2022-03-24 19:44:12 +00:00
|
|
|
|
|
|
|
@app.get("/chat")
|
|
|
|
@auth_required
|
2022-08-17 20:30:07 +00:00
|
|
|
def chat(v):
|
2022-03-24 19:44:12 +00:00
|
|
|
return render_template("chat.html", v=v, messages=messages)
|
|
|
|
|
|
|
|
|
|
|
|
@app.get('/chat.js')
|
|
|
|
def chatjs():
|
|
|
|
resp = make_response(send_from_directory('assets', 'js/chat.js'))
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
|
|
@socketio.on('speak')
|
|
|
|
@limiter.limit("3/second;10/minute")
|
2022-07-13 18:14:37 +00:00
|
|
|
@limiter.limit("3/second;10/minute", key_func=lambda:f'{SITE}-{session.get("lo_user")}')
|
2022-03-24 19:44:12 +00:00
|
|
|
@auth_required
|
|
|
|
def speak(data, v):
|
|
|
|
if v.is_banned: return '', 403
|
2022-03-24 21:01:04 +00:00
|
|
|
|
|
|
|
vname = v.username.lower()
|
|
|
|
if vname in muted:
|
2022-03-25 01:47:07 +00:00
|
|
|
if time.time() < muted[vname]: return '', 403
|
2022-03-24 21:01:04 +00:00
|
|
|
else: del muted[vname]
|
|
|
|
|
2022-03-24 19:44:12 +00:00
|
|
|
global messages, total
|
2022-08-14 02:38:07 +00:00
|
|
|
|
|
|
|
if SITE == 'rdrama.net': text = data[:200].strip()
|
|
|
|
else: text = data[:1000].strip()
|
|
|
|
|
2022-03-24 19:44:12 +00:00
|
|
|
if not text: return '', 403
|
|
|
|
text_html = sanitize(text)
|
|
|
|
|
|
|
|
data={
|
|
|
|
"avatar": v.profile_url,
|
2022-03-28 10:06:57 +00:00
|
|
|
"username": v.username,
|
|
|
|
"namecolor": v.namecolor,
|
|
|
|
"text": text,
|
|
|
|
"text_html": text_html,
|
2022-04-06 22:54:09 +00:00
|
|
|
"text_censored": censor_slurs(text_html, 'chat'),
|
|
|
|
"time": int(time.time())
|
2022-03-24 19:44:12 +00:00
|
|
|
}
|
2022-03-25 01:47:07 +00:00
|
|
|
|
2022-03-25 01:50:00 +00:00
|
|
|
if v.shadowbanned:
|
|
|
|
emit('speak', data)
|
2022-05-22 08:58:42 +00:00
|
|
|
elif blackjack and any(i in text.lower() for i in blackjack.split()):
|
|
|
|
emit('speak', data)
|
|
|
|
v.shadowbanned = 'AutoJanny'
|
|
|
|
g.db.add(v)
|
2022-07-11 09:52:59 +00:00
|
|
|
send_repeatable_notification(CARP_ID, f"{v.username} has been shadowbanned because of a chat message.")
|
2022-07-13 15:16:53 +00:00
|
|
|
send_repeatable_notification(CARP_ID, f"{v.username} has been shadowbanned because of a chat message.")
|
2022-03-25 01:50:00 +00:00
|
|
|
else:
|
|
|
|
emit('speak', data, broadcast=True)
|
|
|
|
messages.append(data)
|
2022-08-13 11:08:38 +00:00
|
|
|
messages = messages[-100:]
|
2022-03-24 19:44:12 +00:00
|
|
|
|
|
|
|
total += 1
|
2022-03-24 21:01:04 +00:00
|
|
|
|
|
|
|
if v.admin_level > 1:
|
|
|
|
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())
|
|
|
|
muted[username] = duration
|
|
|
|
|
2022-08-15 15:02:19 +00:00
|
|
|
typing = []
|
2022-03-24 19:44:12 +00:00
|
|
|
return '', 204
|
|
|
|
|
|
|
|
@socketio.on('connect')
|
|
|
|
@auth_required
|
|
|
|
def connect(v):
|
|
|
|
if v.username not in online:
|
|
|
|
online.append(v.username)
|
|
|
|
emit("online", online, broadcast=True)
|
2022-08-22 20:47:01 +00:00
|
|
|
cache.set(ONLINE_STR, len(online), timeout=0)
|
2022-03-24 19:44:12 +00:00
|
|
|
|
|
|
|
emit('typing', typing)
|
|
|
|
return '', 204
|
|
|
|
|
|
|
|
@socketio.on('disconnect')
|
|
|
|
@auth_required
|
|
|
|
def disconnect(v):
|
|
|
|
if v.username in online:
|
|
|
|
online.remove(v.username)
|
|
|
|
emit("online", online, broadcast=True)
|
2022-08-22 20:47:01 +00:00
|
|
|
cache.set(ONLINE_STR, len(online), timeout=0)
|
2022-03-24 19:44:12 +00:00
|
|
|
|
|
|
|
if v.username in typing: typing.remove(v.username)
|
|
|
|
emit('typing', typing, broadcast=True)
|
|
|
|
return '', 204
|
|
|
|
|
|
|
|
@socketio.on('typing')
|
|
|
|
@auth_required
|
|
|
|
def typing_indicator(data, v):
|
|
|
|
|
|
|
|
if data and v.username not in typing: typing.append(v.username)
|
|
|
|
elif not data and v.username in typing: typing.remove(v.username)
|
|
|
|
|
|
|
|
emit('typing', typing, broadcast=True)
|
|
|
|
return '', 204
|
|
|
|
|
|
|
|
|
|
|
|
def close_running_threads():
|
2022-04-26 00:38:52 +00:00
|
|
|
cache.set(f'{SITE}_chat', messages)
|
|
|
|
cache.set(f'{SITE}_total', total)
|
|
|
|
cache.set(f'{SITE}_muted', muted)
|
2022-03-24 19:44:12 +00:00
|
|
|
atexit.register(close_running_threads)
|