]> jfr.im git - erebus.git/commitdiff
Added channel levels to DB.
authorzonidjan <redacted>
Sun, 22 Dec 2013 19:07:25 +0000 (13:07 -0600)
committerzonidjan <redacted>
Sun, 22 Dec 2013 19:07:25 +0000 (13:07 -0600)
Also moved channel listing from erebus.py setup() to bot.py __init__

Channel levels are not yet checked when using a command.

bot.py
dump.sql
erebus.py

diff --git a/bot.py b/bot.py
index e468dc11d7b0e71d85271cef4366e232f64984fd..ab97605ee52f16bcb91d0f611cd5eff6e76e8eb5 100644 (file)
--- 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
index 399169cd1c9c79b32718706c46072f8bea0259da..8f9323e60f9e32a5637d5777215b7efa91b2f14e 100644 (file)
--- 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
index f8c71437dd9f9c6968e8febac93ef63042a19ba4..4e1934b35f3ef3596206ef51aa8e07edd49032b9 100644 (file)
--- 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 "<User %r (%d)>" % (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():