diff --git a/src/IRCLine.py b/src/IRCLine.py index 88c1cfe4..e5a58707 100644 --- a/src/IRCLine.py +++ b/src/IRCLine.py @@ -1,22 +1,30 @@ import datetime, typing -from src import IRCObject +from src import IRCObject, utils + +LINE_CUTOFF = 450 class Line(IRCObject.Object): - def __init__(self, send_time: datetime.datetime, data: bytes, - truncated: str): + def __init__(self, send_time: datetime.datetime, line: str): + self._line = line self.send_time = send_time - self.data = data - self.truncated = truncated + + data, truncated = utils.encode_truncate(line, "utf8", LINE_CUTOFF) + + self._data = data + self._truncated = truncated self._on_send = [] # type: typing.List[typing.Callable[[], None]] def __repr__(self) -> str: return "IRCLine.Line(%s)" % self.__str__() def __str__(self) -> str: - return self.data + return self._data def on_send(self, func: typing.Callable[[], None]): self._on_send.append(func) def sent(self): for func in self._on_send[:]: func() + + def data(self) -> bytes: + return b"%s\r\n" % self._data diff --git a/src/IRCServer.py b/src/IRCServer.py index 0b9f694c..f3f9d47b 100644 --- a/src/IRCServer.py +++ b/src/IRCServer.py @@ -9,8 +9,6 @@ UNTHROTTLED_MAX_LINES = 10 READ_TIMEOUT_SECONDS = 120 PING_INTERVAL_SECONDS = 30 -LINE_CUTOFF = 450 - class Server(IRCObject.Object): def __init__(self, bot: "IRCBot.Bot", @@ -315,11 +313,7 @@ class Server(IRCObject.Object): break line_stripped = line.split("\n", 1)[0].strip("\r") - encoded, truncated = utils.encode_truncate( - line_stripped, "utf8", LINE_CUTOFF) - encoded = b"%s\r\n" % encoded - - line_obj = IRCLine.Line(datetime.datetime.utcnow(), encoded, truncated) + line_obj = IRCLine.Line(datetime.datetime.utcnow(), line_stripped) self.queued_lines.append(line_obj) self.bot.log.debug("%s (raw send) | %s", [str(self), line]) @@ -332,7 +326,7 @@ class Server(IRCObject.Object): to_buffer = self.queued_lines[:throttle_space] self.queued_lines = self.queued_lines[throttle_space:] for line in to_buffer: - self.write_buffer += line.data + self.write_buffer += line.data() self.buffered_lines.append(line) bytes_written_i = self.socket.send(self.write_buffer)