2019-02-10 14:18:33 +00:00
|
|
|
import datetime, typing
|
2019-02-10 23:51:28 +00:00
|
|
|
from src import IRCObject, IRCServer, utils
|
2019-02-10 18:43:04 +00:00
|
|
|
|
2019-02-10 22:52:23 +00:00
|
|
|
# this should be 510 (RFC1459, 512 with \r\n) but a server BitBot uses is broken
|
2019-02-10 20:54:12 +00:00
|
|
|
LINE_CUTOFF = 470
|
2019-02-10 14:18:33 +00:00
|
|
|
|
|
|
|
class Line(IRCObject.Object):
|
2019-02-10 20:49:59 +00:00
|
|
|
def __init__(self, server: "IRCServer.Server", send_time: datetime.datetime,
|
|
|
|
line: str):
|
|
|
|
self.server = server
|
2019-02-10 18:43:04 +00:00
|
|
|
self._line = line
|
2019-02-10 14:18:33 +00:00
|
|
|
self.send_time = send_time
|
2019-02-10 18:43:04 +00:00
|
|
|
|
2019-02-10 20:49:59 +00:00
|
|
|
data, truncated = utils.encode_truncate(line, "utf8",
|
|
|
|
self._char_limit())
|
2019-02-10 18:43:04 +00:00
|
|
|
|
|
|
|
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:
|
2019-02-10 18:43:04 +00:00
|
|
|
return self._data
|
2019-02-10 14:18:33 +00:00
|
|
|
|
2019-02-10 20:49:59 +00:00
|
|
|
def _char_limit(self):
|
|
|
|
return LINE_CUTOFF-len(":%s " % self.server.hostmask())
|
|
|
|
|
|
|
|
def data(self) -> bytes:
|
|
|
|
return b"%s\r\n" % self._data
|
|
|
|
def decoded_data(self) -> bytes:
|
|
|
|
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()
|
2019-02-10 18:43:04 +00:00
|
|
|
|
2019-02-10 20:49:59 +00:00
|
|
|
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
|