]> jfr.im git - erebus.git/commitdiff
add wantchan - fixes #13 - will consume channel if available, but not require it
authorzonidjan <redacted>
Wed, 4 Oct 2017 23:18:01 +0000 (18:18 -0500)
committerzonidjan <redacted>
Wed, 4 Oct 2017 23:18:01 +0000 (18:18 -0500)
bot.py
modlib.py
modules/control.py

diff --git a/bot.py b/bot.py
index d1eed6c6e43d6f20a7e16df7ee863d1e61faa5b7..060c172cd84383e82e8ee13e07d8d066900c0f04 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -270,37 +270,41 @@ class Bot(object):
                                if msg == "VERSION":
                                        self.msg(user, "\001VERSION Erebus v%d.%d - http://github.com/zonidjan/erebus" % (self.parent.APIVERSION, self.parent.RELEASE))
                                return
-               if len(pieces) > 1:
-                       chanword = pieces[1]
-                       if chanword.startswith('#'):
-                               chanparam = self.parent.channel(chanword)
 
                if target != self.nick: # message was sent to a channel
-                       chan = self.parent.channel(target)
                        try:
                                if msg.startswith('*'): # message may be addressed to bot by "*BOTNICK" trigger?
                                        if pieces[0][1:].lower() == self.nick.lower():
                                                pieces.pop(0) # command actually starts with next word
                                                msg = ' '.join(pieces) # command actually starts with next word
-                               elif not triggerused:
-                                       if self.parent.haschanhook(target.lower()):
-                                               for callback in self.parent.getchanhook(target.lower()):
-                                                       try:
-                                                               cbret = callback(self, user, chan, *pieces)
-                                                       except NotImplementedError:
-                                                               self.msg(user, "Command not implemented.")
-                                                       except:
-                                                               self.msg(user, "Command failed. Code: CBEXC%09.3f" % (time.time() % 100000))
-                                                               self.__debug_cbexception("chanhook", user=user, target=target, msg=msg)
-                                       return # not to bot, don't process!
+                                               triggerused = True
                        except IndexError:
                                return # "message" is empty
 
+               if len(pieces) > 1:
+                       chanword = pieces[1]
+                       if chanword.startswith('#'):
+                               chanparam = self.parent.channel(chanword)
+
+               if target != self.nick: # message was sent to a channel
+                       chan = self.parent.channel(target)
+                       if not triggerused:
+                               if self.parent.haschanhook(target.lower()):
+                                       for callback in self.parent.getchanhook(target.lower()):
+                                               try:
+                                                       cbret = callback(self, user, chan, *pieces)
+                                               except NotImplementedError:
+                                                       self.msg(user, "Command not implemented.")
+                                               except:
+                                                       self.msg(user, "Command failed. Code: CBEXC%09.3f" % (time.time() % 100000))
+                                                       self.__debug_cbexception("chanhook", user=user, target=target, msg=msg)
+                               return # not to bot, don't process!
+
                cmd = pieces[0].lower()
                rancmd = False
                if self.parent.hashook(cmd):
                        for callback in self.parent.gethook(cmd):
-                               if chanparam is not None and callback.needchan:
+                               if chanparam is not None and (callback.needchan or callback.wantchan):
                                        chan = chanparam
                                        pieces.pop(1)
                                if chan is None and callback.needchan:
index 7225ac7833f46ce46ae53f2b9e502e7b8d3fdb6e..ff63a07612a9fd0fcf6157bb820275a7d635ace6 100644 (file)
--- a/modlib.py
+++ b/modlib.py
@@ -100,7 +100,8 @@ class modlib(object):
                        return func
                return realhook
 
-       def hook(self, cmd=None, needchan=True, glevel=ANYONE, clevel=PUBLIC):
+       def hook(self, cmd=None, needchan=True, glevel=ANYONE, clevel=PUBLIC, wantchan=None):
+               if wantchan is None: wantchan = needchan
                _cmd = cmd #save this since it gets wiped out...
                def realhook(func):
                        cmd = _cmd #...and restore it
@@ -110,6 +111,7 @@ class modlib(object):
                                cmd = (cmd,)
 
                        func.needchan = needchan
+                       func.wantchan = wantchan
                        func.reqglevel = glevel
                        func.reqclevel = clevel
                        func.cmd = cmd
index 34018fc0e775f6b87b9323f38b4d43c94267d6ff..bd2460cf823a0264d7e54caba92883f259b05657 100644 (file)
@@ -99,7 +99,7 @@ def _whois(user, chan, showglevel=True, showclevel=True):
                        fmt += " (not a channel user)"
        return fmt % fillers
 
-@lib.hook(needchan=False)
+@lib.hook(needchan=False, wantchan=True)
 @lib.help("<user|#auth>", "shows who someone is")
 @lib.argsEQ(1)
 def whois(bot, user, chan, realtarget, *args):