diff options
-rwxr-xr-x | baitv-daemon.py | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/baitv-daemon.py b/baitv-daemon.py index c4caffe..3e4f25d 100755 --- a/baitv-daemon.py +++ b/baitv-daemon.py @@ -11,6 +11,7 @@ import utils VERSION = "baitv-daemon v0.2.4" USERS = set() +HISTORY = [] current_time = lambda: int(round(time.time() * 1000)) timestamp = lambda: int(round(time.time())) @@ -34,6 +35,7 @@ class Client: self.cmds = { "msg": (self.on_msg, 2, False), "notice": (self.on_notice, 2, True), + "history":(self.on_history, 1, False), "login": (self.on_login, 1, False), "logout": (self.on_logout, 0, True), "kick": (self.on_kick, 1, True), @@ -57,7 +59,9 @@ class Client: @asyncio.coroutine def on_msg(self, args): - if current_time() - self.last_chat > settings.flood_time: + t = current_time() + + if t - self.last_chat > settings.flood_time: color, msg = args for user in USERS: @@ -67,7 +71,11 @@ class Client: else: yield from user.ws.send("MSG:{}:{}".format(color, msg)) - self.last_chat = current_time() + self.last_chat = t + + HISTORY.append((t, color, msg)) + if len(HISTORY) > settings.history_length: + HISTORY.pop(0) else: yield from self.ws.send("FLOOD") @@ -81,6 +89,15 @@ class Client: yield from self.broadcast("NOTICE::{}".format(msg), [self]) @asyncio.coroutine + def on_history(self, args): + if HISTORY: + msgs = min(len(HISTORY), int(args[0])) * -1 + for msg in HISTORY[msgs:]: + yield from self.ws.send("HMSG:{}:{}:{}".format(*msg)) + + yield from self.ws.send("HISTORY_OK") + + @asyncio.coroutine def on_login(self, args): valid_psks = utils.parse_streamers_file(settings.streamers_file) psk = args[0] @@ -190,8 +207,9 @@ def unregister(websocket): def baitv_daemon(websocket, path): print("New client ({})".format(websocket.remote_address)) - real_ip = websocket.request_headers['X-Real-IP'] - if not real_ip: + try: + real_ip = websocket.request_headers['X-Real-IP'] + except ValueError: real_ip = websocket.remote_address[0] this_client = Client(websocket, real_ip) |