]> jfr.im git - erebus.git/commitdiff
Added support for PM commands (with&without chan)
authorzonidjan <redacted>
Wed, 18 Dec 2013 16:53:57 +0000 (10:53 -0600)
committerzonidjan <redacted>
Wed, 18 Dec 2013 16:53:57 +0000 (10:53 -0600)
- Also added new trigger (asterisk followed by botnick, i.e. *erebus)
- Also started channel-level support
- Also added new args to modlib.modlib.hook()
  - needchan = does a channel HAVE to be specified? (one can be specified even if not)
  - level changed to glevel
  - clevel (chanel-level) (not used yet, saved to func.reqclevel)

bot.py
erebus.py
modlib.py
modules/eval.py

diff --git a/bot.py b/bot.py
index 1737f93debd4914999aadb10ebcd984983a909ae..a57b0d439e4b69b67aa7d5223b0fe2d3668069cd 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -44,9 +44,9 @@ class Bot(object):
                if pieces[1] == "PRIVMSG":
                        nick = pieces[0].split('!')[0][1:]
                        user = self.parent.user(nick)
-                       chan = self.parent.channel(pieces[2])
+                       target = pieces[2]
                        msg = ' '.join(pieces[3:])[1:]
-                       self.parsemsg(user, chan, msg)
+                       self.parsemsg(user, target, msg)
 
                elif pieces[0] == "PING":
                        self.conn.send("PONG %s" % (pieces[1]))
@@ -68,18 +68,37 @@ class Bot(object):
                        else:
                                pass #TODO TODO TODO add to common chans!
                        
-       def parsemsg(self, user, chan, msg):
-               if msg[0] == self.parent.trigger:
-                       msg = msg[1:]
-               else:
-                       return
-
+       def parsemsg(self, user, target, msg):
+               chan = None
+               triggerused = msg[0] == self.parent.trigger
+               if triggerused: msg = msg[1:]
                pieces = msg.split()
+
+               if target == self.nick:
+                       chanword = pieces[1]
+                       if chanword[0] == '#':
+                               chan = self.parent.channel(chanword)
+                               pieces.pop(1)
+
+               else: # message was sent to a channel
+                       chan = self.parent.channel(target) #TODO check if bot's on channel --- in Erebus.channel() maybe?
+                       if msg[0] == '*': # 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:
+                               return # not to bot, don't process!
+
                cmd = pieces[0].lower()
 
+               print "%r %r %r %r" % (cmd, user, target, msg)
+
                if self.parent.hashook(cmd):
                        callback = self.parent.gethook(cmd)
-                       if user.level >= callback.reqlevel:
+                       if chan is None and callback.needchan:
+                               self.msg(user, "You need to specify a channel for that command.")
+                               return
+                       if user.glevel >= callback.reqglevel: #TODO TODO TODO check reqclevel
                                callback(self, user, chan, *pieces[1:])
                                return
 
index 68d6bc78fadb40f1c1e30f14065a9dafdb16c5b9..e8a1ca9246a4a01b9fdef96f48cb68e5de3080b1 100644 (file)
--- a/erebus.py
+++ b/erebus.py
@@ -35,19 +35,19 @@ class Erebus(object):
 
                def checklevel(self):
                        if self.auth is None:
-                               self.level = -1
+                               self.glevel = -1
                        else:
                                c = main.db.cursor()
                                c.execute("SELECT level FROM users WHERE auth = %s", (self.auth,))
                                row = c.fetchone()
                                if row is not None:
-                                       self.level = row['level']
+                                       self.glevel = row['level']
                                else:
-                                       self.level = 0
-                       return self.level
+                                       self.glevel = 0
+                       return self.glevel
 
                def __str__(self): return self.nick
-               def __repr__(self): return "<User %r (%d)>" % (self.nick,self.level)
+               def __repr__(self): return "<User %r (%d)>" % (self.nick,self.glevel)
 
        class Channel(object):
                users = []
index abf1a3d8e9c45a9586ab5809c222ea35ee23d773..743e36dbf334482b1560cb18b397140aa722d42a 100644 (file)
--- a/modlib.py
+++ b/modlib.py
@@ -13,13 +13,21 @@ class error(object):
                return self.errormsg
 
 class modlib(object):
-       # default access levels
+       # default (global) access levels
        MANAGER = 100
        ADMIN = 90
        STAFF = 80
        AUTHED = 0
        ANYONE = -1
 
+       # (channel) access levels
+       OWNER = -10
+       MASTER = -8 #master is {-8,-9}
+       OP = -5 #op is {-5,-6,-7}
+       VOICE = -4
+       KNOWN = -3
+       PUBLIC = -2 #anyone (use glevel to control auth-needed)
+
        def __init__(self, name):
                self.hooks = {}
                self.parent = None
@@ -34,9 +42,12 @@ class modlib(object):
                for cmd, func in self.hooks.iteritems():
                        self.parent.unhook(cmd, func)
 
-       def hook(self, cmd, level=ANYONE):
+       def hook(self, cmd, needchan=True, glevel=ANYONE, clevel=PUBLIC):
                def realhook(func):
-                       func.reqlevel = level
+                       func.needchan = needchan
+                       func.reqglevel = glevel
+                       func.reqclevel = clevel
+
                        self.hooks[cmd] = func
                        if self.parent is not None:
                                self.parent.hook(cmd, func)
index 830600c3ebcd9338f51b8f5b1ec6bfd9751e8a59..9ad387ca7ba37c8aaf6c1c8cc93e471345af3856 100644 (file)
@@ -19,15 +19,21 @@ modstop = lib.modstop
 import sys
 
 
-@lib.hook('eval', lib.MANAGER)
+@lib.hook('eval', needchan=False, glevel=lib.MANAGER)
 def cmd_eval(bot, user, chan, *args):
+       if chan is not None: replyto = chan
+       else: replyto = user
+
        try: ret = eval(' '.join(args))
-       except: bot.msg(chan, "Error (%s): %s" % (sys.exc_info()[0], sys.exc_info()[1]))
-       else: bot.msg(chan, "Done: %r" % (ret))
+       except: bot.msg(replyto, "Error (%s): %s" % (sys.exc_info()[0], sys.exc_info()[1]))
+       else: bot.msg(replyto, "Done: %r" % (ret))
 
 
-@lib.hook('exec', lib.MANAGER)
+@lib.hook('exec', needchan=False, glevel=lib.MANAGER)
 def cmd_exec(bot, user, chan, *args):
+       if chan is not None: replyto = chan
+       else: replyto = user
+
        try: exec ' '.join(args)
-       except: bot.msg(chan, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
-       else: bot.msg(chan, "Done.")
+       except: bot.msg(replyto, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1]))
+       else: bot.msg(replyto, "Done.")