Shift encoding/truncating IRC lines to src/IRCLine.py
This commit is contained in:
parent
bd9eaad0b2
commit
a3b9be3d3d
2 changed files with 16 additions and 14 deletions
|
@ -1,22 +1,30 @@
|
||||||
import datetime, typing
|
import datetime, typing
|
||||||
from src import IRCObject
|
from src import IRCObject, utils
|
||||||
|
|
||||||
|
LINE_CUTOFF = 450
|
||||||
|
|
||||||
class Line(IRCObject.Object):
|
class Line(IRCObject.Object):
|
||||||
def __init__(self, send_time: datetime.datetime, data: bytes,
|
def __init__(self, send_time: datetime.datetime, line: str):
|
||||||
truncated: str):
|
self._line = line
|
||||||
self.send_time = send_time
|
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]]
|
self._on_send = [] # type: typing.List[typing.Callable[[], None]]
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return "IRCLine.Line(%s)" % self.__str__()
|
return "IRCLine.Line(%s)" % self.__str__()
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return self.data
|
return self._data
|
||||||
|
|
||||||
def on_send(self, func: typing.Callable[[], None]):
|
def on_send(self, func: typing.Callable[[], None]):
|
||||||
self._on_send.append(func)
|
self._on_send.append(func)
|
||||||
def sent(self):
|
def sent(self):
|
||||||
for func in self._on_send[:]:
|
for func in self._on_send[:]:
|
||||||
func()
|
func()
|
||||||
|
|
||||||
|
def data(self) -> bytes:
|
||||||
|
return b"%s\r\n" % self._data
|
||||||
|
|
|
@ -9,8 +9,6 @@ UNTHROTTLED_MAX_LINES = 10
|
||||||
READ_TIMEOUT_SECONDS = 120
|
READ_TIMEOUT_SECONDS = 120
|
||||||
PING_INTERVAL_SECONDS = 30
|
PING_INTERVAL_SECONDS = 30
|
||||||
|
|
||||||
LINE_CUTOFF = 450
|
|
||||||
|
|
||||||
class Server(IRCObject.Object):
|
class Server(IRCObject.Object):
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
bot: "IRCBot.Bot",
|
bot: "IRCBot.Bot",
|
||||||
|
@ -315,11 +313,7 @@ class Server(IRCObject.Object):
|
||||||
break
|
break
|
||||||
|
|
||||||
line_stripped = line.split("\n", 1)[0].strip("\r")
|
line_stripped = line.split("\n", 1)[0].strip("\r")
|
||||||
encoded, truncated = utils.encode_truncate(
|
line_obj = IRCLine.Line(datetime.datetime.utcnow(), line_stripped)
|
||||||
line_stripped, "utf8", LINE_CUTOFF)
|
|
||||||
encoded = b"%s\r\n" % encoded
|
|
||||||
|
|
||||||
line_obj = IRCLine.Line(datetime.datetime.utcnow(), encoded, truncated)
|
|
||||||
self.queued_lines.append(line_obj)
|
self.queued_lines.append(line_obj)
|
||||||
|
|
||||||
self.bot.log.debug("%s (raw send) | %s", [str(self), line])
|
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]
|
to_buffer = self.queued_lines[:throttle_space]
|
||||||
self.queued_lines = self.queued_lines[throttle_space:]
|
self.queued_lines = self.queued_lines[throttle_space:]
|
||||||
for line in to_buffer:
|
for line in to_buffer:
|
||||||
self.write_buffer += line.data
|
self.write_buffer += line.data()
|
||||||
self.buffered_lines.append(line)
|
self.buffered_lines.append(line)
|
||||||
|
|
||||||
bytes_written_i = self.socket.send(self.write_buffer)
|
bytes_written_i = self.socket.send(self.write_buffer)
|
||||||
|
|
Loading…
Reference in a new issue