Return truncted data from encode_truncate and hold it in IRCLine.Line

This commit is contained in:
jesopo 2019-02-10 18:16:19 +00:00
parent 7952d4c366
commit bd9eaad0b2
3 changed files with 12 additions and 6 deletions

View file

@ -2,9 +2,11 @@ import datetime, typing
from src import IRCObject
class Line(IRCObject.Object):
def __init__(self, send_time: datetime.datetime, data: bytes):
def __init__(self, send_time: datetime.datetime, data: bytes,
truncated: str):
self.send_time = send_time
self.data = data
self.truncated = truncated
self._on_send = [] # type: typing.List[typing.Callable[[], None]]

View file

@ -315,10 +315,11 @@ class Server(IRCObject.Object):
break
line_stripped = line.split("\n", 1)[0].strip("\r")
encoded = utils.encode_truncate(line_stripped, "utf8", LINE_CUTOFF)
encoded, truncated = utils.encode_truncate(
line_stripped, "utf8", LINE_CUTOFF)
encoded = b"%s\r\n" % encoded
line_obj = IRCLine.Line(datetime.datetime.utcnow(), encoded)
line_obj = IRCLine.Line(datetime.datetime.utcnow(), encoded, truncated)
self.queued_lines.append(line_obj)
self.bot.log.debug("%s (raw send) | %s", [str(self), line])

View file

@ -190,12 +190,15 @@ def is_ip(s: str) -> bool:
return False
return True
def encode_truncate(s: str, encoding: str, byte_max: int) -> bytes:
def encode_truncate(s: str, encoding: str, byte_max: int
) -> typing.Tuple[bytes, str]:
encoded = b""
for character in s:
truncated = ""
for i, character in enumerate(s):
encoded_character = character.encode(encoding)
if len(encoded + encoded_character) > byte_max:
truncated = s[i:]
break
else:
encoded += encoded_character
return encoded
return encoded, truncated