bitbot-3.11-fork/src/IRCLine.py

50 lines
1.5 KiB
Python
Raw Normal View History

2019-02-10 14:18:33 +00:00
import datetime, typing
from src import IRCObject, IRCServer, utils
# this should be 510 (RFC1459, 512 with \r\n) but a server BitBot uses is broken
LINE_CUTOFF = 470
2019-02-10 14:18:33 +00:00
class Line(IRCObject.Object):
def __init__(self, server: "IRCServer.Server", send_time: datetime.datetime,
line: str):
self.server = server
self._line = line
2019-02-10 14:18:33 +00:00
self.send_time = send_time
data, truncated = utils.encode_truncate(line, "utf8",
self._char_limit())
self._data = data
self._truncated = truncated
2019-02-10 14:18:33 +00:00
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
2019-02-10 14:18:33 +00:00
def _char_limit(self) -> int:
return LINE_CUTOFF-len(":%s " % self.server.hostmask())
def data(self) -> bytes:
return b"%s\r\n" % self._data
def decoded_data(self) -> str:
return self._data.decode("utf8")
def truncated(self) -> str:
return self._truncated
2019-02-10 14:18:33 +00:00
def on_send(self, func: typing.Callable[[], None]):
self._on_send.append(func)
def sent(self):
for func in self._on_send[:]:
func()
def end_replace(self, s: str):
s_encoded = s.encode("utf8")
s_len = len(s_encoded)
removed = self._data[-s_len:].decode("utf8")
self._truncated = removed+self._truncated
self._data = self._data[:-s_len]+s_encoded