From 235921fbabc9d5c90c99631908476f2de3f6343a Mon Sep 17 00:00:00 2001 From: dave Date: Thu, 2 Jul 2020 11:20:00 -0700 Subject: [PATCH] add replyto field to privmsg ircevent messages --- pyircbot/clipub.py | 12 +++++------- pyircbot/irccore.py | 15 +++++++++++---- tests/lib.py | 10 +++++----- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/pyircbot/clipub.py b/pyircbot/clipub.py index 6b634dc..5cca81a 100644 --- a/pyircbot/clipub.py +++ b/pyircbot/clipub.py @@ -9,7 +9,7 @@ from msgbus.client import MsgbusSubClient import pyircbot import traceback from pyircbot.pyircbot import PrimitiveBot -from pyircbot.irccore import IRCEvent, UserPrefix +from pyircbot.irccore import IRCEvent, UserPrefix, IRCCore from pyircbot.common import TouchReload from json import dumps @@ -57,12 +57,10 @@ class PyIRCBotSub(PrimitiveBot): args, sender, trailing, extras = loads(rest) nick, username, hostname = extras["prefix"] - msg = IRCEvent(command.upper(), - args, - UserPrefix(nick, - username, - hostname), - trailing) + msg = IRCCore.packetAsObject(command.upper(), + args, + f"{nick}!{username}@{hostname}", # hack + trailing) for module_name, module in self.moduleInstances.items(): for hook in module.irchooks: diff --git a/pyircbot/irccore.py b/pyircbot/irccore.py index d6c21a2..ebd9cbf 100644 --- a/pyircbot/irccore.py +++ b/pyircbot/irccore.py @@ -18,7 +18,7 @@ from io import StringIO from time import time -IRCEvent = namedtuple("IRCEvent", "command args prefix trailing") +IRCEvent = namedtuple("IRCEvent", "command args prefix trailing replyto") UserPrefix = namedtuple("UserPrefix", "nick username hostname") ServerPrefix = namedtuple("ServerPrefix", "hostname") @@ -258,6 +258,7 @@ class IRCCore(object): self.log.warning("Invalid hook - %s" % command) return False + @staticmethod def packetAsObject(command, args, prefix, trailing): """Given an irc message's args, prefix, and trailing data return an object with these properties @@ -269,9 +270,15 @@ class IRCCore(object): :type trailing: str :returns: object -- a IRCEvent object with the ``args``, ``prefix``, ``trailing``""" - return IRCEvent(command, args, - IRCCore.decodePrefix(prefix) if prefix else None, - trailing) + prefix = IRCCore.decodePrefix(prefix) if prefix else None + + replyto = None + if command == "PRIVMSG": + # prefix will always be set for PRIVMSG + # TODO server side fuzzing + replyto = args[0] if args[0].startswith("#") else prefix.nick + + return IRCEvent(command, args, prefix, trailing, replyto) " Utility methods " @staticmethod diff --git a/tests/lib.py b/tests/lib.py index a14561b..b273b4e 100644 --- a/tests/lib.py +++ b/tests/lib.py @@ -5,7 +5,7 @@ from threading import Thread from random import randint from pyircbot import PyIRCBot from pyircbot.pyircbot import PrimitiveBot -from pyircbot.irccore import IRCEvent, UserPrefix +from pyircbot.irccore import IRCEvent, UserPrefix, IRCCore from unittest.mock import MagicMock from tests.miniircd import Server as MiniIrcServer @@ -27,10 +27,10 @@ class FakeBaseBot(PrimitiveBot): """ Feed a message into the bot. """ - msg = IRCEvent(cmd, - args, - UserPrefix(*sender), - trailing) + msg = IRCCore.packetAsObject(cmd, + args, + f"{sender[0]}!{sender[1]}@{sender[2]}", # hack + trailing) for module_name, module in self.moduleInstances.items():# TODO dedupe this block across the various base classes for hook in module.irchooks: