Better support for msgtags with PRIVMSGs; put functionality in to
modules/commands.py to reply to specific messages by use of msgids
This commit is contained in:
parent
3201f198da
commit
976b6ae335
6 changed files with 43 additions and 18 deletions
|
@ -9,15 +9,18 @@ OUT_CUTOFF = 400
|
|||
REGEX_CUTOFF = re.compile("^.{1,%d}(?:\s|$)" % OUT_CUTOFF)
|
||||
|
||||
class Out(object):
|
||||
def __init__(self, module_name, target):
|
||||
def __init__(self, module_name, target, msgid):
|
||||
self.module_name = module_name
|
||||
self.target = target
|
||||
self._text = ""
|
||||
self.written = False
|
||||
self._msgid = None
|
||||
|
||||
def write(self, text):
|
||||
self._text += text
|
||||
self.written = True
|
||||
return self
|
||||
|
||||
def send(self):
|
||||
if self.has_text():
|
||||
text = self._text
|
||||
|
@ -29,10 +32,17 @@ class Out(object):
|
|||
].decode("utf8").lstrip())
|
||||
else:
|
||||
self._text = ""
|
||||
self.target.send_message(text, prefix=Utils.FONT_RESET + "[%s] " %
|
||||
self.prefix())
|
||||
|
||||
tags = {}
|
||||
if self._msgid:
|
||||
tags["reply"] = self._msgid
|
||||
|
||||
self.target.send_message(text,
|
||||
prefix=Utils.FONT_RESET + "[%s] " % self.prefix(), tags=tags)
|
||||
|
||||
def set_prefix(self, prefix):
|
||||
self.module_name = prefix
|
||||
|
||||
def has_text(self):
|
||||
return bool(self._text)
|
||||
|
||||
|
@ -93,8 +103,10 @@ class Module(ModuleManager.BaseModule):
|
|||
module_name = ""
|
||||
if hasattr(hook.function, "__self__"):
|
||||
module_name = hook.function.__self__._name
|
||||
stdout, stderr = StdOut(module_name, target), StdErr(module_name,
|
||||
target)
|
||||
|
||||
msgid = event["tags"].get("msgid", None)
|
||||
stdout = StdOut(module_name, target, msgid)
|
||||
stderr = StdErr(module_name, target, msgid)
|
||||
|
||||
returns = self.events.on("preprocess.command").call_unsafe(
|
||||
hook=hook, user=event["user"], server=event["server"],
|
||||
|
|
|
@ -414,11 +414,12 @@ class Module(ModuleManager.BaseModule):
|
|||
channel = event["server"].get_channel(event["args"][0])
|
||||
self.events.on("received.message.channel").call(
|
||||
user=user, channel=channel, **kwargs)
|
||||
channel.buffer.add_line(user.nickname, message, action)
|
||||
channel.buffer.add_line(user.nickname, message, action,
|
||||
event["tags"])
|
||||
elif event["server"].is_own_nickname(target):
|
||||
self.events.on("received.message.private").call(
|
||||
user=user, **kwargs)
|
||||
user.buffer.add_line(user.nickname, message, action)
|
||||
user.buffer.add_line(user.nickname, message, action, event["tags"])
|
||||
|
||||
# we've received a notice
|
||||
@Utils.hook("raw.notice")
|
||||
|
|
|
@ -2,10 +2,11 @@ import re
|
|||
from . import Utils
|
||||
|
||||
class BufferLine(object):
|
||||
def __init__(self, sender, message, action, from_self):
|
||||
def __init__(self, sender, message, action, tags, from_self):
|
||||
self.sender = sender
|
||||
self.message = message
|
||||
self.action = action
|
||||
self.tags = tags
|
||||
self.from_self = from_self
|
||||
|
||||
class Buffer(object):
|
||||
|
@ -15,9 +16,9 @@ class Buffer(object):
|
|||
self.lines = []
|
||||
self.max_lines = 64
|
||||
self._skip_next = False
|
||||
def add_line(self, sender, message, action, from_self=False):
|
||||
def add_line(self, sender, message, action, tags, from_self=False):
|
||||
if not self._skip_next:
|
||||
line = BufferLine(sender, message, action, from_self)
|
||||
line = BufferLine(sender, message, action, tags, from_self)
|
||||
self.lines.insert(0, line)
|
||||
if len(self.lines) > self.max_lines:
|
||||
self.lines.pop()
|
||||
|
|
|
@ -105,8 +105,8 @@ class Channel(IRCObject.Object):
|
|||
return self.bot.database.user_channel_settings.find_all_by_setting(
|
||||
self.id, setting, default)
|
||||
|
||||
def send_message(self, text, prefix=None):
|
||||
self.server.send_message(self.name, text, prefix=prefix)
|
||||
def send_message(self, text, prefix=None, tags={}):
|
||||
self.server.send_message(self.name, text, prefix=prefix, tags=tags)
|
||||
def send_mode(self, mode=None, target=None):
|
||||
self.server.send_mode(self.name, mode, target)
|
||||
def send_kick(self, target, reason=None):
|
||||
|
|
|
@ -351,9 +351,19 @@ class Server(IRCObject.Object):
|
|||
def send_quit(self, reason="Leaving"):
|
||||
self.send("QUIT :%s" % reason)
|
||||
|
||||
def send_message(self, target, message, prefix=None):
|
||||
def send_message(self, target, message, prefix=None, tags={}):
|
||||
tag_str = ""
|
||||
for tag, value in tags.items():
|
||||
if tag_str:
|
||||
tag_str += ","
|
||||
tag_str += tag
|
||||
if value:
|
||||
tag_str += "=%s" % value
|
||||
if tag_str:
|
||||
tag_str = "@%s " % tag_str
|
||||
|
||||
full_message = message if not prefix else prefix+message
|
||||
self.send("PRIVMSG %s :%s" % (target, full_message))
|
||||
self.send("%sPRIVMSG %s :%s" % (tag_str, target, full_message))
|
||||
|
||||
action = full_message.startswith("\01ACTION "
|
||||
) and full_message.endswith("\01")
|
||||
|
@ -364,13 +374,13 @@ class Server(IRCObject.Object):
|
|||
full_message_split = full_message.split()
|
||||
if self.has_channel(target):
|
||||
channel = self.get_channel(target)
|
||||
channel.buffer.add_line(None, message, action, True)
|
||||
channel.buffer.add_line(None, message, action, tags, True)
|
||||
self.events.on("self.message.channel").call(
|
||||
message=full_message, message_split=full_message_split,
|
||||
channel=channel, action=action, server=self)
|
||||
else:
|
||||
user = self.get_user(target)
|
||||
user.buffer.add_line(None, message, action, True)
|
||||
user.buffer.add_line(None, message, action, tags, True)
|
||||
self.events.on("self.message.private").call(
|
||||
message=full_message, message_split=full_message_split,
|
||||
user=user, action=action, server=self)
|
||||
|
|
|
@ -56,8 +56,9 @@ class User(IRCObject.Object):
|
|||
return self.bot.database.user_channel_settings.find_by_setting(
|
||||
self.get_id(), setting, default)
|
||||
|
||||
def send_message(self, message, prefix=None):
|
||||
self.server.send_message(self.nickname, message, prefix=prefix)
|
||||
def send_message(self, message, prefix=None, tags={}):
|
||||
self.server.send_message(self.nickname, message, prefix=prefix,
|
||||
tags=tags)
|
||||
def send_notice(self, message):
|
||||
self.server.send_notice(self.nickname, message)
|
||||
def send_ctcp_response(self, command, args):
|
||||
|
|
Loading…
Reference in a new issue