]> jfr.im git - erebus.git/commitdiff
Added channel level checking
authorJohn Runyon <redacted>
Tue, 4 Feb 2014 09:19:35 +0000 (03:19 -0600)
committerJohn Runyon <redacted>
Tue, 4 Feb 2014 09:19:35 +0000 (03:19 -0600)
bot.py
erebus.py
modlib.py
modules/foo.py
modules/module.py

diff --git a/bot.py b/bot.py
index 2a4dd14e14c2cc5d0225d0d4e551baf0fbb55315..ec471e3dbd549d39c880e8925c9de165ddd79bd9 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -1,5 +1,4 @@
 #!/usr/bin/python
-# -*- coding: latin-1 -*-
 
 # Erebus IRC bot - Author: John Runyon
 # "Bot" and "BotConnection" classes (handling a specific "arm")
@@ -16,23 +15,12 @@ class Bot(object):
                self.user = user
                self.realname = realname
 
-               self.chans = []
+               curs = self.parent.db.cursor()
+               curs.execute("SELECT chname FROM chans WHERE bot = %s AND active = 1", (self.nick,))
+               chansres = curs.fetchall()
+               curs.close()
 
-               chcurs = self.parent.db.cursor()
-               chcurs.execute("SELECT chname FROM chans WHERE bot = %s AND active = 1", (self.nick,))
-               chans = chcurs.fetchall()
-               chcurs.close()
-
-               for chrow in chans:
-                       uscurs = self.parent.db.cursor()
-                       uscurs.execute("SELECT user, level FROM chusers WHERE chan = %s", (chrow['chname'],))
-                       usrow = uscurs.fetchone()
-                       levels = {}
-                       while usrow is not None:
-                               levels[usrow['user']] = -usrow['level']
-                               usrow = uscurs.fetchone()
-                       uscurs.close()
-                       self.chans.append(self.parent.newchannel(self, chrow['chname'], levels))
+               self.chans = [self.parent.newchannel(self, row['chname']) for row in chansres]
 
                self.conn = BotConnection(self, bind, server, port)
        def connect(self):
@@ -121,15 +109,12 @@ class Bot(object):
                        for callback in self.parent.gethook(cmd):
                                if chan is None and callback.needchan:
                                        self.msg(user, "You need to specify a channel for that command.")
-                                       continue
-                               if user.glevel >= callback.reqglevel:
-                                       #TODO TODO TODO check reqclevel
+                               elif user.glevel >= callback.reqglevel and (not callback.needchan or chan.levelof(user.auth) >= callback.reqclevel):
                                        cbret = callback(self, user, chan, target, *pieces[1:])
                                        if cbret is NotImplemented:
                                                self.msg(user, "Command not implemented.")
-                                       continue
                else:
-                       self.msg(user, "No such command, or you don't have access.")
+                       self.msg(user, "No such command.")
 
        def msg(self, target, msg):
                if isinstance(target, self.parent.User): self.conn.send("NOTICE %s :%s" % (target.nick, msg))
index ebd24f6a88daf474b26ae9226825733ca17b23c6..fb4239a97306f9c46963733e3aa7068e5bb4921d 100644 (file)
--- a/erebus.py
+++ b/erebus.py
@@ -1,5 +1,4 @@
 #!/usr/bin/python
-# -*- coding: latin-1 -*-
 
 # Erebus IRC bot - Author: John Runyon
 # main startup code
@@ -31,7 +30,7 @@ class Erebus(object):
 
                def authed(self, auth):
                        if auth == '0': auth = None
-                       self.auth = auth
+                       self.auth = auth.lower()
                        self.checklevel()
 
                def checklevel(self):
@@ -56,15 +55,37 @@ class Erebus(object):
                def __repr__(self): return "<User %r (%d)>" % (self.nick,self.glevel)
 
        class Channel(object):
-               def __init__(self, name, bot, levels={}):
+               def __init__(self, name, bot):
                        self.name = name
                        self.bot = bot
-                       self.levels = levels
+                       self.levels = {}
 
                        self.users = []
                        self.voices = []
                        self.ops = []
 
+                       c = main.db.cursor()
+                       c.execute("SELECT user, level FROM chusers WHERE chan = %s", (self.name,))
+                       row = c.fetchone()
+                       while row is not None:
+                               self.levels[row['user']] = row['level']
+                               row = c.fetchone()
+
+
+               def levelof(self, auth):
+                       auth = auth.lower()
+                       if auth in self.levels:
+                               return self.levels[auth]
+                       else:
+                               return 0
+
+               def setlevel(self, auth, level, savetodb=True):
+                       auth = auth.lower()
+                       if savetodb:
+                               c = main.db.cursor()
+                               c.execute("REPLACE INTO chusers (chan, user, level) VALUES (%s, %s, %s)", (self.name, auth, level))
+                       self.levels[auth] = level
+
                def userjoin(self, user, level=None):
                        if user not in self.users: self.users.append(user)
                        if level == 'op' and user not in self.ops: self.ops.append(user)
@@ -132,8 +153,8 @@ class Erebus(object):
                else:
                        return None
 
-       def newchannel(self, bot, name, levels={}):
-               chan = self.Channel(name.lower(), bot, levels)
+       def newchannel(self, bot, name):
+               chan = self.Channel(name.lower(), bot)
                self.chans[name.lower()] = chan
                return chan
 
@@ -175,6 +196,9 @@ class Erebus(object):
        def getnumhook(self, word):
                return self.numhandlers[word]
 
+
+
+
 def setup():
        global cfg, main
 
index 4c349c0f6c55f9e30657af3c89ecdb18ccfcc8b6..7e9a742506545fa27ae1b88d0a53cbc0e7d779bb 100644 (file)
--- a/modlib.py
+++ b/modlib.py
@@ -21,12 +21,12 @@ class modlib(object):
        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)
+       OWNER = 5
+       MASTER = 4
+       OP = 3
+       VOICE = 2
+       KNOWN = 1
+       PUBLIC = 0 #anyone (use glevel to control auth-needed)
 
        # messages
        WRONGARGS = "Wrong number of arguments."
index 3da5d62fedac0b9f63e35618c35e5d7f5b0c0835..276b9a364ad3eb6711a1c2f8607e9b0754a40a28 100644 (file)
@@ -17,6 +17,9 @@ modstart = lib.modstart
 modstop = lib.modstop
 
 # module code
-@lib.hook('test')
-def cmd_test(bot, user, chan, realtarget, *args):
+@lib.hook('test', needchan=False)
+def cmd_gtest(bot, user, chan, realtarget, *args):
+       if chan is not None: replyto = chan
+       else: replyto = user
+
        bot.msg(chan, "You said: %s" % (' '.join([str(arg) for arg in args])))
index 1e3e3e156ede2c6280ffb915a72ee65a5c64ee0e..8a2989c63d21a0175d080877995c13ca041c16e6 100644 (file)
@@ -1,5 +1,5 @@
 # Erebus IRC bot - Author: John Runyon
-# simple module example
+# module control through irc
 # This file is released into the public domain; see http://unlicense.org/
 
 # module info