Support changing command responses from PRIVMSG to NOTICE

This commit is contained in:
jesopo 2018-10-02 21:40:34 +01:00
parent 74180cb103
commit 9118af5e9c
5 changed files with 63 additions and 20 deletions

View file

@ -4,12 +4,16 @@ from src import EventManager, ModuleManager, Utils
STR_MORE = "%s (more...)" % Utils.FONT_RESET STR_MORE = "%s (more...)" % Utils.FONT_RESET
STR_CONTINUED = "(...continued) " STR_CONTINUED = "(...continued) "
COMMAND_METHOD = "command-method"
COMMAND_METHODS = ["PRIVMSG", "NOTICE"]
OUT_CUTOFF = 400 OUT_CUTOFF = 400
REGEX_CUTOFF = re.compile("^.{1,%d}(?:\s|$)" % OUT_CUTOFF) REGEX_CUTOFF = re.compile("^.{1,%d}(?:\s|$)" % OUT_CUTOFF)
class Out(object): class Out(object):
def __init__(self, module_name, target, msgid): def __init__(self, server, module_name, target, msgid):
self.server = server
self.module_name = module_name self.module_name = module_name
self.target = target self.target = target
self._text = "" self._text = ""
@ -37,8 +41,16 @@ class Out(object):
if self._msgid: if self._msgid:
tags["+draft/reply"] = self._msgid tags["+draft/reply"] = self._msgid
self.target.send_message(text, prefix = Utils.FONT_RESET + "[%s] " % self.prefix()
prefix=Utils.FONT_RESET + "[%s] " % self.prefix(), tags=tags) method = self._get_method()
if method == "PRIVMSG":
self.target.send_message(text, prefix=prefix, tags=tags)
elif method == "NOTICE":
self.target.send_notice(text, prefix=prefix, tags=tags)
def _get_method(self):
return self.target.get_setting(COMMAND_METHOD,
self.server.get_setting(COMMAND_METHOD, "PRIVMSG")).upper()
def set_prefix(self, prefix): def set_prefix(self, prefix):
self.module_name = prefix self.module_name = prefix
@ -53,12 +65,22 @@ class StdErr(Out):
def prefix(self): def prefix(self):
return Utils.color(Utils.bold("!"+self.module_name), Utils.COLOR_RED) return Utils.color(Utils.bold("!"+self.module_name), Utils.COLOR_RED)
def _command_method_validate(s):
if s.upper() in COMMAND_METHODS:
return s.upper()
@Utils.export("channelset", {"setting": "command-prefix", @Utils.export("channelset", {"setting": "command-prefix",
"help": "Set the command prefix used in this channel"}) "help": "Set the command prefix used in this channel"})
@Utils.export("serverset", {"setting": "command-prefix", @Utils.export("serverset", {"setting": "command-prefix",
"help": "Set the command prefix used on this server"}) "help": "Set the command prefix used on this server"})
@Utils.export("serverset", {"setting": "identity-mechanism", @Utils.export("serverset", {"setting": "identity-mechanism",
"help": "Set the identity mechanism for this server"}) "help": "Set the identity mechanism for this server"})
@Utils.export("serverset", {"setting": "command-method",
"help": "Set the method used to respond to commands",
"validate": _command_method_validate})
@Utils.export("channelset", {"setting": "command-method",
"help": "Set the method used to respond to commands",
"validate": _command_method_validate})
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
@Utils.hook("new.user|channel") @Utils.hook("new.user|channel")
def new(self, event): def new(self, event):
@ -105,8 +127,8 @@ class Module(ModuleManager.BaseModule):
module_name = hook.function.__self__._name module_name = hook.function.__self__._name
msgid = event["tags"].get("draft/msgid", None) msgid = event["tags"].get("draft/msgid", None)
stdout = StdOut(module_name, target, msgid) stdout = StdOut(event["server"], module_name, target, msgid)
stderr = StdErr(module_name, target, msgid) stderr = StdErr(event["server"], module_name, target, msgid)
returns = self.events.on("preprocess.command").call_unsafe( returns = self.events.on("preprocess.command").call_unsafe(
hook=hook, user=event["user"], server=event["server"], hook=hook, user=event["user"], server=event["server"],
@ -260,15 +282,15 @@ class Module(ModuleManager.BaseModule):
@Utils.hook("send.stdout") @Utils.hook("send.stdout")
def send_stdout(self, event): def send_stdout(self, event):
stdout = StdOut(event["module_name"], event["target"], stdout = StdOut(event["server"], event["module_name"],
event.get("msgid", None)) event["target"], event.get("msgid", None))
stdout.write(event["message"]).send() stdout.write(event["message"]).send()
if stdout.has_text(): if stdout.has_text():
event["target"].last_stdout = stdout event["target"].last_stdout = stdout
@Utils.hook("send.stderr") @Utils.hook("send.stderr")
def send_stderr(self, event): def send_stderr(self, event):
stderr = StdErr(event["module_name"], event["target"], stderr = StdErr(event["server"], event["module_name"],
event.get("msgid", None)) event["target"], event.get("msgid", None))
stderr.write(event["message"]).send() stderr.write(event["message"]).send()
if stderr.has_text(): if stderr.has_text():
event["target"].last_stderr = stderr event["target"].last_stderr = stderr

View file

@ -400,12 +400,13 @@ class Module(ModuleManager.BaseModule):
channel = event["server"].get_channel(event["args"][0]) channel = event["server"].get_channel(event["args"][0])
self.events.on("received.message.channel").call( self.events.on("received.message.channel").call(
user=user, channel=channel, **kwargs) user=user, channel=channel, **kwargs)
channel.buffer.add_line(user.nickname, message, action, channel.buffer.add_message(user.nickname, message, action,
event["tags"]) event["tags"])
elif event["server"].is_own_nickname(target): elif event["server"].is_own_nickname(target):
self.events.on("received.message.private").call( self.events.on("received.message.private").call(
user=user, **kwargs) user=user, **kwargs)
user.buffer.add_line(user.nickname, message, action, event["tags"]) user.buffer.add_message(user.nickname, message, action,
event["tags"])
# we've received a notice # we've received a notice
@Utils.hook("raw.notice") @Utils.hook("raw.notice")

View file

@ -2,12 +2,13 @@ import re
from . import Utils from . import Utils
class BufferLine(object): class BufferLine(object):
def __init__(self, sender, message, action, tags, from_self): def __init__(self, sender, message, action, tags, from_self, method):
self.sender = sender self.sender = sender
self.message = message self.message = message
self.action = action self.action = action
self.tags = tags self.tags = tags
self.from_self = from_self self.from_self = from_self
self.method = method
class Buffer(object): class Buffer(object):
def __init__(self, bot, server): def __init__(self, bot, server):
@ -16,13 +17,19 @@ class Buffer(object):
self.lines = [] self.lines = []
self.max_lines = 64 self.max_lines = 64
self._skip_next = False self._skip_next = False
def add_line(self, sender, message, action, tags, from_self=False):
def _add_message(self, sender, message, action, tags, from_self, method):
if not self._skip_next: if not self._skip_next:
line = BufferLine(sender, message, action, tags, from_self) line = BufferLine(sender, message, action, tags, from_self, method)
self.lines.insert(0, line) self.lines.insert(0, line)
if len(self.lines) > self.max_lines: if len(self.lines) > self.max_lines:
self.lines.pop() self.lines.pop()
self._skip_next = False self._skip_next = False
def add_message(self, sender, message, action, tags, from_self=False):
self._add_message(sender, message, action, tags, from_self, "PRIVMSG")
def add_notice(self, sender, message, tags, from_self=False):
self._add_message(sender, message, False, tags, from_self, "NOTICE")
def get(self, index=0, **kwargs): def get(self, index=0, **kwargs):
from_self = kwargs.get("from_self", True) from_self = kwargs.get("from_self", True)
for line in self.lines: for line in self.lines:

View file

@ -107,6 +107,8 @@ class Channel(IRCObject.Object):
def send_message(self, text, prefix=None, tags={}): def send_message(self, text, prefix=None, tags={}):
self.server.send_message(self.name, text, prefix=prefix, tags=tags) self.server.send_message(self.name, text, prefix=prefix, tags=tags)
def send_notice(self, text, prefix=None, tags={}):
self.server.send_notice(self.name, text, prefix=prefix, tags=tags)
def send_mode(self, mode=None, target=None): def send_mode(self, mode=None, target=None):
self.server.send_mode(self.name, mode, target) self.server.send_mode(self.name, mode, target)
def send_kick(self, target, reason=None): def send_kick(self, target, reason=None):

View file

@ -351,7 +351,7 @@ class Server(IRCObject.Object):
def send_quit(self, reason="Leaving"): def send_quit(self, reason="Leaving"):
self.send("QUIT :%s" % reason) self.send("QUIT :%s" % reason)
def send_message(self, target, message, prefix=None, tags={}): def _tag_str(self, tags):
tag_str = "" tag_str = ""
for tag, value in tags.items(): for tag, value in tags.items():
if tag_str: if tag_str:
@ -361,9 +361,12 @@ class Server(IRCObject.Object):
tag_str += "=%s" % value tag_str += "=%s" % value
if tag_str: if tag_str:
tag_str = "@%s " % tag_str tag_str = "@%s " % tag_str
return tag_str
def send_message(self, target, message, prefix=None, tags={}):
full_message = message if not prefix else prefix+message full_message = message if not prefix else prefix+message
self.send("%sPRIVMSG %s :%s" % (tag_str, target, full_message)) self.send("%sPRIVMSG %s :%s" % (self._tag_str(tags), target,
full_message))
action = full_message.startswith("\01ACTION " action = full_message.startswith("\01ACTION "
) and full_message.endswith("\01") ) and full_message.endswith("\01")
@ -374,19 +377,27 @@ class Server(IRCObject.Object):
full_message_split = full_message.split() full_message_split = full_message.split()
if self.has_channel(target): if self.has_channel(target):
channel = self.get_channel(target) channel = self.get_channel(target)
channel.buffer.add_line(None, message, action, tags, True) channel.buffer.add_message(None, message, action, tags, True)
self.events.on("self.message.channel").call( self.events.on("self.message.channel").call(
message=full_message, message_split=full_message_split, message=full_message, message_split=full_message_split,
channel=channel, action=action, server=self) channel=channel, action=action, server=self)
else: else:
user = self.get_user(target) user = self.get_user(target)
user.buffer.add_line(None, message, action, tags, True) user.buffer.add_message(None, message, action, tags, True)
self.events.on("self.message.private").call( self.events.on("self.message.private").call(
message=full_message, message_split=full_message_split, message=full_message, message_split=full_message_split,
user=user, action=action, server=self) user=user, action=action, server=self)
def send_notice(self, target, message): def send_notice(self, target, message, prefix=None, tags={}):
self.send("NOTICE %s :%s" % (target, message)) full_message = message if not prefix else prefix+message
self.send("%sNOTICE %s :%s" % (self._tag_str(tags), target,
full_message))
if self.has_channel(target):
self.get_channel(target).buffer.add_notice(None, message, tags,
True)
else:
self.get_user(target).buffer.add_notice(None, message, tags,
True)
def send_mode(self, target, mode=None, args=None): def send_mode(self, target, mode=None, args=None):
self.send("MODE %s%s%s" % (target, "" if mode == None else " %s" % mode, self.send("MODE %s%s%s" % (target, "" if mode == None else " %s" % mode,