From: zonidjan Date: Tue, 17 Dec 2013 11:08:22 +0000 (-0600) Subject: Finished user authentication when bot joins channel (WHO #chan %ant,0) and added... X-Git-Url: https://jfr.im/git/erebus.git/commitdiff_plain/b2a896c852df233548af2c772bac07c76f184e3d?hp=931c88a4c3ec73d24c53baa4e8464bcb2e51b3b6 Finished user authentication when bot joins channel (WHO #chan %ant,0) and added level checks to hooks via modlib --- diff --git a/bot.config.example b/bot.config.example index 535de22..95b0029 100644 --- a/bot.config.example +++ b/bot.config.example @@ -8,4 +8,7 @@ dbuser = erebus dbpass = darkness dbname = erebus -trigger = . +trigger = ! + +[autoloads] +modtest = 1 diff --git a/bot.py b/bot.py index 68cd8ae..1737f93 100644 --- a/bot.py +++ b/bot.py @@ -18,8 +18,7 @@ class Bot(object): self.conn = BotConnection(self, bind, server, port) def connect(self): - -if self.conn.connect(): + if self.conn.connect(): self.parent.newfd(self, self.conn.socket.fileno()) def getdata(self): @@ -52,13 +51,25 @@ if self.conn.connect(): elif pieces[0] == "PING": self.conn.send("PONG %s" % (pieces[1])) + elif pieces[1] == "354": # WHOX + qt = pieces[3] + nick = pieces[4] + auth = pieces[5] + if auth != '0': + self.parent.user(nick).authed(auth) + elif pieces[1] == "JOIN": nick = pieces[0].split('!')[0][1:] user = self.parent.user(nick) - chan = self.parent.channel(pieces[2]) #TODO TODO TODO + chan = self.parent.channel(pieces[2]) + + if nick == self.nick: + self.conn.send("WHO %s %%ant,1" % (chan)) + else: + pass #TODO TODO TODO add to common chans! def parsemsg(self, user, chan, msg): - if msg[0] == '!': #TODO check config for trigger + if msg[0] == self.parent.trigger: msg = msg[1:] else: return @@ -67,7 +78,12 @@ if self.conn.connect(): cmd = pieces[0].lower() if self.parent.hashook(cmd): - self.parent.gethook(cmd)(self, user, chan, *pieces[1:]) + callback = self.parent.gethook(cmd) + if user.level >= callback.reqlevel: + callback(self, user, chan, *pieces[1:]) + return + + self.msg(user, "No such command, or you don't have access.") def msg(self, target, msg): if isinstance(target, self.parent.User): self.conn.send("NOTICE %s :%s" % (target.nick, msg)) diff --git a/config.py b/config.py index 92b08cb..c8a461b 100644 --- a/config.py +++ b/config.py @@ -16,6 +16,12 @@ class Config(object): def __setattr__(self, key, value): self.config.set('erebus', key, value) + def level(self, cmd): + return self.config.get('levels', cmd) + + def setlevel(self, cmd, level): + self.config.set('levels', cmd, level) + def items(self, section='erebus'): return self.config.items(section) diff --git a/erebus.py b/erebus.py index 2c489f6..68d6bc7 100644 --- a/erebus.py +++ b/erebus.py @@ -13,16 +13,23 @@ class Erebus(object): fds = {} mods = {} msghandlers = {} + users = {} + chans = {} class User(object): chans = [] def __init__(self, nick, auth=None): + print "parent.User(self, %r, %r)" % (nick, auth) self.nick = nick - self.auth = nick #TEMP + self.auth = auth self.checklevel() + def isauthed(self): + return self.auth is not None + def authed(self, auth): + if auth == '0': auth = None self.auth = auth self.checklevel() @@ -71,7 +78,8 @@ class Erebus(object): def __str__(self): return self.name def __repr__(self): return "" % (self.name) - def __init__(self): + def __init__(self, trigger): + self.trigger = trigger if os.name == "posix": self.potype = "poll" self.po = select.poll() @@ -96,8 +104,14 @@ class Erebus(object): def fd(self, fileno): #get Bot() by fd/fileno return self.fds[fileno] - def user(self, nick): #TODO #get User() by nick - return self.User(nick.lower()) + def user(self, nick): + nick = nick.lower() + if nick in self.users: + return self.users[nick] + else: + user = self.User(nick) + self.users[nick] = user + return user def channel(self, name): #TODO #get Channel() by name return self.Channel(name.lower()) @@ -126,9 +140,10 @@ def setup(): global cfg, main cfg = config.Config('bot.config') - main = Erebus() + main = Erebus(cfg.trigger) autoloads = [mod for mod, yes in cfg.items('autoloads') if int(yes) == 1] + print autoloads for mod in autoloads: ctlmod.load(main, mod) diff --git a/modlib.py b/modlib.py index 8ad0406..abf1a3d 100644 --- a/modlib.py +++ b/modlib.py @@ -13,10 +13,10 @@ class error(object): return self.errormsg class modlib(object): - #access levels - MANAGER = 3 - ADMIN = 2 - STAFF = 1 + # default access levels + MANAGER = 100 + ADMIN = 90 + STAFF = 80 AUTHED = 0 ANYONE = -1 @@ -34,8 +34,9 @@ class modlib(object): for cmd, func in self.hooks.iteritems(): self.parent.unhook(cmd, func) - def hook(self, cmd): + def hook(self, cmd, level=ANYONE): def realhook(func): + func.reqlevel = level self.hooks[cmd] = func if self.parent is not None: self.parent.hook(cmd, func)