#!/usr/bin/env python3 """ SocketIO events. """ import time import bleach from flask_socketio import SocketIO, emit, join_room import database as db 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"] name = data["name"] date = int(time.time()) data["date"] = date 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 = '' + line + '' lines.append(line) message = "
".join(lines) data["message"] = message emit("message", data, room=room)