]> jfr.im git - erebus.git/blobdiff - erebus.py
Added a few module featuresm depends needs testing!
[erebus.git] / erebus.py
index 31ed1c966b2d5653f08c0bd5f44632f0fe3123bf..c75183ca31fe6a5239b7d6c6723a41e01e982b84 100644 (file)
--- a/erebus.py
+++ b/erebus.py
@@ -2,8 +2,8 @@
 
 #TODO: tons
 
-import os, sys, select
-import bot
+import os, sys, select, MySQLdb, MySQLdb.cursors
+import bot, config, ctlmod
 
 class Erebus(object):
        bots = {}
@@ -16,19 +16,28 @@ class Erebus(object):
 
                def __init__(self, nick, auth=None):
                        self.nick = nick
-                       self.auth = auth
-
-                       if auth is not None:
-                               self.checklevel()
+                       self.auth = nick #TEMP
+                       self.checklevel()
 
                def authed(self, auth):
                        self.auth = auth
                        self.checklevel()
 
-               def checklevel(self): self.level = 9999 #TODO get level from db
+               def checklevel(self):
+                       if self.auth is None:
+                               self.level = -1
+                       else:
+                               c = main.db.cursor()
+                               c.execute("SELECT level FROM users WHERE auth = %s", (self.auth,))
+                               row = c.fetchone()
+                               if row is not None:
+                                       self.level = row['level']
+                               else:
+                                       self.level = 0
+                       return self.level
 
                def __str__(self): return self.nick
-               def __repr__(self): return "<User %r>" % (self.nick)
+               def __repr__(self): return "<User %r (%d)>" % (self.nick,self.level)
 
        class Channel(object):
                users = []
@@ -73,7 +82,6 @@ class Erebus(object):
                self.bots[nick.lower()] = obj
 
        def newfd(self, obj, fileno):
-               print "newfd(Erebus(), %r, %r)" % (obj, fileno)
                self.fds[fileno] = obj
                if self.potype == "poll":
                        self.po.register(fileno, select.POLLIN)
@@ -101,25 +109,38 @@ class Erebus(object):
                        if bot.conn.state == 0:
                                bot.connect()
 
-       #module functions
-       def modlist(self): pass
-       def hasmod(self, name): pass
-       def loadmod(self, name): pass
-       def unloadmod(self, name): pass
-       def reloadmod(self, name): pass
-
        #bind functions
-       def bind(self, word, handler): pass
-       def addbind(self, word, handler): pass
-       def rmbind(self, word, handler): pass
-       def getbind(self, word, handler): pass
-
-
-main = Erebus()
+       def hook(self, word, handler):
+               self.msghandlers[word] = handler
+       def unhook(self, word):
+               del self.msghandlers[word]
+       def hashook(self, word):
+               return word in self.msghandlers
+       def gethook(self, word):
+               return self.msghandlers[word]
 
 def setup():
-       main.newbot('Erebus', 'erebus', None, 'irc.quakenet.org', 6667, 'Erebus', ['#dimetest'])
-       main.bot('erebus').connect()
+       global cfg, main
+
+       cfg = config.Config('bot.config')
+       main = Erebus()
+
+       autoloads = [mod for mod, yes in cfg.items('autoloads') if int(yes) == 1]
+       for mod in autoloads:
+               ctlmod.load(main, mod)
+
+       main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MySQLdb.cursors.DictCursor)
+       c = main.db.cursor()
+       c.execute("SELECT nick, user, bind FROM bots WHERE active = 1")
+       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.connectall()
 
 def loop():
        poready = main.poll()