2019-02-09 17:52:51 +00:00
|
|
|
import re
|
2019-02-09 17:49:01 +00:00
|
|
|
from src import utils
|
|
|
|
|
2019-02-10 20:49:59 +00:00
|
|
|
STR_MORE = " (more...)"
|
2019-06-18 13:14:37 +00:00
|
|
|
STR_MORE_LEN = len(STR_MORE.encode("utf8"))
|
2019-02-09 17:50:40 +00:00
|
|
|
STR_CONTINUED = "(...continued) "
|
|
|
|
|
2019-02-09 17:49:01 +00:00
|
|
|
class Out(object):
|
2019-05-21 22:09:32 +00:00
|
|
|
def __init__(self, server, module_name, target, target_str, tags):
|
2019-02-09 17:49:01 +00:00
|
|
|
self.server = server
|
2019-07-03 13:04:24 +00:00
|
|
|
self._prefix = self._default_prefix(module_name)
|
2019-02-09 17:49:01 +00:00
|
|
|
self._hide_prefix = False
|
|
|
|
self.target = target
|
2019-05-21 22:09:32 +00:00
|
|
|
self._target_str = target_str
|
2019-02-09 17:49:01 +00:00
|
|
|
self._text = ""
|
|
|
|
self.written = False
|
2019-05-21 11:56:05 +00:00
|
|
|
self._tags = tags
|
2019-06-10 09:45:02 +00:00
|
|
|
self._assured = False
|
|
|
|
|
|
|
|
def assure(self):
|
|
|
|
self._assured = True
|
2019-02-09 17:49:01 +00:00
|
|
|
|
|
|
|
def write(self, text):
|
|
|
|
self._text += text
|
|
|
|
self.written = True
|
|
|
|
return self
|
|
|
|
|
|
|
|
def send(self, method):
|
|
|
|
if self.has_text():
|
|
|
|
prefix = ""
|
|
|
|
if not self._hide_prefix:
|
2019-07-03 13:04:24 +00:00
|
|
|
prefix = utils.consts.RESET + "[%s] " % self._prefix
|
2019-02-09 17:49:01 +00:00
|
|
|
|
2019-02-10 20:49:59 +00:00
|
|
|
full_text = "%s%s" % (prefix, self._text)
|
2019-06-10 09:45:02 +00:00
|
|
|
line_factory = None
|
2019-02-09 17:49:01 +00:00
|
|
|
if method == "PRIVMSG":
|
2019-06-10 09:45:02 +00:00
|
|
|
line_factory = utils.irc.protocol.privmsg
|
2019-02-09 17:49:01 +00:00
|
|
|
elif method == "NOTICE":
|
2019-06-10 09:45:02 +00:00
|
|
|
line_factory = utils.irc.protocol.notice
|
2019-02-10 20:49:59 +00:00
|
|
|
else:
|
2019-04-30 21:45:37 +00:00
|
|
|
raise ValueError("Unknown command method '%s'" % method)
|
2019-02-10 20:49:59 +00:00
|
|
|
|
2019-06-10 09:45:02 +00:00
|
|
|
line = line_factory(self._target_str, full_text, tags=self._tags)
|
|
|
|
if self._assured:
|
|
|
|
line.assure()
|
|
|
|
|
2019-06-19 09:34:42 +00:00
|
|
|
valid, truncated = line.truncate(self.server.hostmask(),
|
|
|
|
margin=STR_MORE_LEN)
|
2019-06-18 13:14:37 +00:00
|
|
|
|
|
|
|
if truncated:
|
2019-06-19 09:34:42 +00:00
|
|
|
line = utils.irc.parse_line(valid+STR_MORE)
|
2019-06-18 13:14:37 +00:00
|
|
|
self._text = "%s%s" % (STR_CONTINUED, truncated)
|
|
|
|
else:
|
|
|
|
self._text = ""
|
|
|
|
|
|
|
|
sent_line = self.server.send(line)
|
2019-02-09 17:49:01 +00:00
|
|
|
|
2019-07-03 13:04:24 +00:00
|
|
|
def _default_prefix(self, s: str):
|
|
|
|
return s
|
2019-02-09 17:49:01 +00:00
|
|
|
def set_prefix(self, prefix):
|
2019-07-03 13:04:24 +00:00
|
|
|
self._prefix = self._default_prefix(prefix)
|
|
|
|
def append_prefix(self, s: str):
|
|
|
|
self._prefix = "%s%s" % (self._prefix, s)
|
2019-02-09 17:49:01 +00:00
|
|
|
def hide_prefix(self):
|
|
|
|
self._hide_prefix = True
|
|
|
|
|
|
|
|
def has_text(self):
|
|
|
|
return bool(self._text)
|
|
|
|
|
|
|
|
class StdOut(Out):
|
2019-07-03 13:04:24 +00:00
|
|
|
def _default_prefix(self, s: str):
|
|
|
|
return utils.irc.color(s, utils.consts.GREEN)
|
2019-02-09 17:49:01 +00:00
|
|
|
class StdErr(Out):
|
2019-07-03 13:04:24 +00:00
|
|
|
def _default_prefix(self, s: str):
|
|
|
|
return utils.irc.color(s, utils.consts.RED)
|
2019-02-09 17:49:01 +00:00
|
|
|
|