From: zonidjan Date: Sun, 22 Dec 2013 19:07:25 +0000 (-0600) Subject: Added channel levels to DB. X-Git-Url: https://jfr.im/git/erebus.git/commitdiff_plain/5477b368b6f982ae96db41d62bbd3ce7abbc9d5d?hp=d1ea29460cb89ae52cad1c26f5f95b82d9b21320 Added channel levels to DB. Also moved channel listing from erebus.py setup() to bot.py __init__ Channel levels are not yet checked when using a command. --- diff --git a/bot.py b/bot.py index e468dc1..ab97605 100644 --- a/bot.py +++ b/bot.py @@ -9,12 +9,29 @@ import socket, sys #bots = {'erebus': bot.Bot(nick='Erebus', user='erebus', bind='', server='irc.quakenet.org', port=6667, realname='Erebus')} class Bot(object): - def __init__(self, parent, nick, user, bind, server, port, realname, chans): + def __init__(self, parent, nick, user, bind, server, port, realname): self.parent = parent self.nick = nick self.user = user self.realname = realname - self.chans = chans + + self.chans = [] + + 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.conn = BotConnection(self, bind, server, port) def connect(self): @@ -33,7 +50,7 @@ class Bot(object): elif pieces[1] == "001": self.conn.registered(True) for c in self.chans: - self.join(c) + self.join(c.name) elif pieces[1] == "PRIVMSG": nick = pieces[0].split('!')[0][1:] @@ -60,7 +77,8 @@ class Bot(object): if nick == self.nick: self.conn.send("WHO %s %%ant,1" % (chan)) else: - pass #TODO TODO TODO add to common chans! + chan.userjoin(user) + user.join(chan) def parsemsg(self, user, target, msg): chan = None diff --git a/dump.sql b/dump.sql index 399169c..8f9323e 100644 --- a/dump.sql +++ b/dump.sql @@ -70,6 +70,31 @@ INSERT INTO `chans` VALUES ('#dimetest','Erebus',1); /*!40000 ALTER TABLE `chans` ENABLE KEYS */; UNLOCK TABLES; +-- +-- Table structure for table `chusers` +-- + +DROP TABLE IF EXISTS `chusers`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `chusers` ( + `chan` varchar(100) NOT NULL, + `user` varchar(30) NOT NULL, + `level` int(11) NOT NULL, + PRIMARY KEY (`chan`,`user`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `chusers` +-- + +LOCK TABLES `chusers` WRITE; +/*!40000 ALTER TABLE `chusers` DISABLE KEYS */; +INSERT INTO `chusers` VALUES ('#dimetest','dimecadmium',10); +/*!40000 ALTER TABLE `chusers` ENABLE KEYS */; +UNLOCK TABLES; + -- -- Table structure for table `users` -- @@ -103,4 +128,4 @@ UNLOCK TABLES; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2013-12-19 16:28:56 +-- Dump completed on 2013-12-22 13:07:13 diff --git a/erebus.py b/erebus.py index f8c7143..4e1934b 100644 --- a/erebus.py +++ b/erebus.py @@ -17,13 +17,13 @@ class Erebus(object): chans = {} class User(object): - chans = [] - def __init__(self, nick, auth=None): self.nick = nick self.auth = auth self.checklevel() + self.chans = [] + def isauthed(self): return self.auth is not None @@ -45,16 +45,23 @@ class Erebus(object): self.glevel = 0 return self.glevel + def join(self, chan): + self.chans.append(chan) + def part(self, chan): + self.chans.remove(chan) + def __str__(self): return self.nick def __repr__(self): return "" % (self.nick,self.glevel) class Channel(object): - users = [] - voices = [] - ops = [] - - def __init__(self, name): + def __init__(self, name, bot, levels={}): self.name = name + self.bot = bot + self.levels = levels + + self.users = [] + self.voices = [] + self.ops = [] def userjoin(self, user, level=None): if user not in self.users: self.users.append(user) @@ -86,9 +93,9 @@ class Erebus(object): self.potype = "select" self.fdlist = [] - def newbot(self, nick, user, bind, server, port, realname, chans): + def newbot(self, nick, user, bind, server, port, realname): if bind is None: bind = '' - obj = bot.Bot(self, nick, user, bind, server, port, realname, chans) + obj = bot.Bot(self, nick, user, bind, server, port, realname) self.bots[nick.lower()] = obj def newfd(self, obj, fileno): @@ -111,8 +118,16 @@ class Erebus(object): user = self.User(nick) self.users[nick] = user return user - def channel(self, name): #TODO #get Channel() by name - return self.Channel(name.lower()) + def channel(self, name): #get Channel() by name + if name.lower() in self.chans: + return self.chans[name.lower()] + else: + return None + + def newchannel(self, bot, name, levels={}): + chan = self.Channel(name.lower(), bot, levels) + self.chans[name.lower()] = chan + return chan def poll(self): if self.potype == "poll": @@ -151,11 +166,7 @@ def setup(): rows = c.fetchall() c.close() for row in rows: - c2 = main.db.cursor() - c2.execute("SELECT chname FROM chans WHERE bot = %s AND active = 1", (row['nick'],)) - chans = [chdic['chname'] for chdic in c2.fetchall()] - c2.close() - main.newbot(row['nick'], row['user'], row['bind'], cfg.host, cfg.port, cfg.realname, chans) + main.newbot(row['nick'], row['user'], row['bind'], cfg.host, cfg.port, cfg.realname) main.connectall() def loop():