From 32df9230ea15ce62eb43e3e575c2eabbba1d695b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 24 Jul 2018 11:05:29 +0200 Subject: [PATCH] server: Handle malformed server responses. --- main.py | 8 ++++++-- matrix/server.py | 10 +++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 1b9066d..381bfd1 100644 --- a/main.py +++ b/main.py @@ -35,7 +35,7 @@ import logbook from logbook import Logger, StderrHandler, StreamHandler import nio -from nio import TransportType +from nio import TransportType, RemoteTransportError from matrix.colors import Formatted from matrix.utf import utf8_decode @@ -296,7 +296,11 @@ def receive_cb(server_name, file_descriptor): server.disconnect() break - server.client.receive(data) + try: + server.client.receive(data) + except RemoteTransportError as e: + server.error(str(e)) + server.disconnect() response = server.client.next_response() diff --git a/matrix/server.py b/matrix/server.py index 18b0927..dd45754 100644 --- a/matrix/server.py +++ b/matrix/server.py @@ -30,7 +30,7 @@ from matrix.plugin_options import Option, DebugType from matrix.utils import (key_from_value, prnt_debug, server_buffer_prnt, create_server_buffer) from matrix.utf import utf8_decode -from matrix.globals import W, SERVERS +from matrix.globals import W, SERVERS, SCRIPT_NAME from .buffer import RoomBuffer, OwnMessage, OwnAction try: @@ -279,6 +279,14 @@ class MatrixServer: # type: (MatrixServer) -> None self.send_buffer = b"" + def error(self, message): + buf = "" + if self.server_buffer: + buf = self.server_buffer + + msg = "{prefix}{}: {}".format(SCRIPT_NAME, message) + W.prnt(buf, msg) + def send(self, data): # type: (bytes) -> bool self.try_send(data)