2018-06-16 18:52:46 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
SocketIO events.
|
|
|
|
"""
|
|
|
|
import time
|
|
|
|
|
2018-06-17 17:49:49 -04:00
|
|
|
import bleach
|
2018-06-16 18:52:46 -04:00
|
|
|
from flask_socketio import SocketIO, emit, join_room
|
|
|
|
|
2018-06-17 17:49:49 -04:00
|
|
|
import database as db
|
|
|
|
|
2018-06-16 18:52:46 -04:00
|
|
|
socketio = SocketIO()
|
|
|
|
|
|
|
|
@socketio.on('joined', namespace="/chat")
|
|
|
|
def joined(data):
|
|
|
|
"""
|
|
|
|
Sent by clients when they enter a room.
|
|
|
|
"""
|
|
|
|
room = data["room"]
|
|
|
|
join_room(room)
|
|
|
|
|
|
|
|
|
|
|
|
@socketio.on('message', namespace="/chat")
|
|
|
|
def text(data):
|
|
|
|
"""
|
|
|
|
Sent by a client when the user entered a new message.
|
|
|
|
"""
|
|
|
|
room = data["room"]
|
|
|
|
|
|
|
|
message = data["message"]
|
|
|
|
date = int(time.time())
|
|
|
|
data["date"] = date
|
2018-06-17 17:49:49 -04:00
|
|
|
|
|
|
|
message = message.strip()
|
|
|
|
if not message:
|
|
|
|
return
|
|
|
|
tags = ["b", "code", "i", "s"]
|
|
|
|
message = bleach.clean(message, tags=tags)
|
|
|
|
lines = []
|
|
|
|
for line in message.splitlines():
|
|
|
|
if line.startswith(">"):
|
|
|
|
line = '<span class="greenText">' + line + '</span>'
|
|
|
|
lines.append(line)
|
|
|
|
message = "<br />".join(lines)
|
|
|
|
data["message"] = message
|
2018-06-16 18:52:46 -04:00
|
|
|
|
2018-06-17 21:10:41 -04:00
|
|
|
db.log_chat_message(data)
|
2018-06-17 17:49:49 -04:00
|
|
|
emit("message", data, room=room)
|