From: zonidjan Date: Tue, 4 Feb 2014 09:19:35 +0000 (-0600) Subject: Added channel level checking X-Git-Url: https://jfr.im/git/erebus.git/commitdiff_plain/586997a7668236c42527b70e4da93edcf4fa4af4?hp=a4097c44e68577b2314bc6de52d0e61db57bf509 Added channel level checking --- diff --git a/bot.py b/bot.py index 2a4dd14..ec471e3 100644 --- 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)) diff --git a/erebus.py b/erebus.py index ebd24f6..fb4239a 100644 --- 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 "" % (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 diff --git a/modlib.py b/modlib.py index 4c349c0..7e9a742 100644 --- 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." diff --git a/modules/foo.py b/modules/foo.py index 3da5d62..276b9a3 100644 --- a/modules/foo.py +++ b/modules/foo.py @@ -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]))) diff --git a/modules/module.py b/modules/module.py index 1e3e3e1..8a2989c 100644 --- a/modules/module.py +++ b/modules/module.py @@ -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