Let IRCLine.Line handle truncating command output

This commit is contained in:
jesopo 2019-02-10 20:49:59 +00:00
parent abf5679b68
commit bb4ec082dd
3 changed files with 34 additions and 23 deletions

View file

@ -1,10 +1,7 @@
import re
from src import utils
OUT_CUTOFF = 400
REGEX_CUTOFF = re.compile(r"^.{1,%d}(?:\s|$)" % OUT_CUTOFF)
STR_MORE = "%s (more...)" % utils.consts.RESET
STR_MORE = " (more...)"
STR_CONTINUED = "(...continued) "
class Out(object):
@ -25,17 +22,6 @@ class Out(object):
def send(self, method):
if self.has_text():
text = self._text
text_encoded = text.encode("utf8")
if len(text_encoded) > OUT_CUTOFF:
text = "%s%s" % (text_encoded[:OUT_CUTOFF].decode("utf8"
).rstrip(), STR_MORE)
self._text = "%s%s" % (STR_CONTINUED, text_encoded[OUT_CUTOFF:
].decode("utf8").lstrip())
else:
self._text = ""
tags = {}
if self._msgid:
tags["+draft/reply"] = self._msgid
@ -45,11 +31,18 @@ class Out(object):
prefix = utils.consts.RESET + "[%s] " % self.prefix()
target_str = "%s%s" % (self._statusmsg, self.target.name)
full_text = "%s%s" % (prefix, text)
full_text = "%s%s" % (prefix, self._text)
if method == "PRIVMSG":
self.server.send_message(target_str, full_text, tags=tags)
line = self.server.send_message(target_str, full_text,
tags=tags)
elif method == "NOTICE":
self.server.send_notice(target_str, full_text, tags=tags)
line = self.server.send_notice(target_str, full_text, tags=tags)
else:
raise ValuError("Unknown command methd '%s'" % method)
if line.truncated():
line.end_replace(STR_MORE)
self._text = "%s%s" % (STR_CONTINUED, line.truncated())
def set_prefix(self, prefix):
self.module_name = prefix

View file

@ -4,11 +4,14 @@ from src import IRCObject, utils
LINE_CUTOFF = 450
class Line(IRCObject.Object):
def __init__(self, send_time: datetime.datetime, line: str):
def __init__(self, server: "IRCServer.Server", send_time: datetime.datetime,
line: str):
self.server = server
self._line = line
self.send_time = send_time
data, truncated = utils.encode_truncate(line, "utf8", LINE_CUTOFF)
data, truncated = utils.encode_truncate(line, "utf8",
self._char_limit())
self._data = data
self._truncated = truncated
@ -20,11 +23,26 @@ class Line(IRCObject.Object):
def __str__(self) -> str:
return self._data
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
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
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

View file

@ -316,7 +316,7 @@ class Server(IRCObject.Object):
break
line_stripped = line.split("\n", 1)[0].strip("\r")
line_obj = IRCLine.Line(datetime.datetime.utcnow(), line_stripped)
line_obj = IRCLine.Line(self, datetime.datetime.utcnow(), line_stripped)
self.queued_lines.append(line_obj)
return line_obj