Add modules/strip_color.py

This commit is contained in:
jesopo 2018-09-30 20:12:11 +01:00
parent c87728a87d
commit 43740d40f4
3 changed files with 25 additions and 3 deletions

10
modules/strip_color.py Normal file
View file

@ -0,0 +1,10 @@
from src import ModuleManager, Utils
@Utils.export("serverset", {"setting": "strip-color",
"help": "Set whether I strip colors from my messages on this server",
"validate": Utils.bool_or_none})
class Module(ModuleManager.BaseModule):
@Utils.hook("preprocess.send")
def preprocess(self, event):
if event["server"].get_setting("strip-color", False):
return Utils.strip_font(event["line"])

View file

@ -250,12 +250,16 @@ class Server(object):
return self.until_read_timeout == 0 return self.until_read_timeout == 0
def send(self, data): def send(self, data):
encoded = data.split("\n")[0].strip("\r").encode("utf8") returned = self.events.on("preprocess.send").call_for_result(
server=self, line=data)
line = returned or data
encoded = line.split("\n")[0].strip("\r").encode("utf8")
if len(encoded) > 450: if len(encoded) > 450:
encoded = encoded[:450] encoded = encoded[:450]
self.buffered_lines.append(encoded + b"\r\n") self.buffered_lines.append(encoded + b"\r\n")
if self.bot.args.verbose:
self.bot.log.info(">%s | %s", [str(self), encoded.decode("utf8")]) self.bot.log.debug(">%s | %s", [str(self), encoded.decode("utf8")])
def _send(self): def _send(self):
if not len(self.write_buffer): if not len(self.write_buffer):
self.write_buffer = self.buffered_lines.pop(0) self.write_buffer = self.buffered_lines.pop(0)

View file

@ -155,6 +155,7 @@ COLOR_LIGHTBLUE, COLOR_PINK, COLOR_GREY, COLOR_LIGHTGREY = (12, 13,
FONT_BOLD, FONT_ITALIC, FONT_UNDERLINE, FONT_INVERT = ("\x02", "\x1D", FONT_BOLD, FONT_ITALIC, FONT_UNDERLINE, FONT_INVERT = ("\x02", "\x1D",
"\x1F", "\x16") "\x1F", "\x16")
FONT_COLOR, FONT_RESET = "\x03", "\x0F" FONT_COLOR, FONT_RESET = "\x03", "\x0F"
REGEX_COLOR = re.compile("%s\d\d(?:,\d\d)?" % FONT_COLOR)
def color(s, foreground, background=None): def color(s, foreground, background=None):
foreground = str(foreground).zfill(2) foreground = str(foreground).zfill(2)
@ -169,6 +170,13 @@ def bold(s):
def underline(s): def underline(s):
return "%s%s%s" % (FONT_UNDERLINE, s, FONT_UNDERLINE) return "%s%s%s" % (FONT_UNDERLINE, s, FONT_UNDERLINE)
def strip_font(s):
s = s.replace(FONT_BOLD, "")
s = s.replace(FONT_ITALIC, "")
s = REGEX_COLOR.sub("", s)
s = s.replace(FONT_COLOR, "")
return s
TIME_SECOND = 1 TIME_SECOND = 1
TIME_MINUTE = TIME_SECOND*60 TIME_MINUTE = TIME_SECOND*60
TIME_HOUR = TIME_MINUTE*60 TIME_HOUR = TIME_MINUTE*60