]> jfr.im git - erebus.git/blobdiff - erebus.py
Preparing to develop autotests
[erebus.git] / erebus.py
index b38820b6fe5cfe2d0020d6dd0995c856c2270173..f8c71437dd9f9c6968e8febac93ef63042a19ba4 100644 (file)
--- a/erebus.py
+++ b/erebus.py
@@ -1,15 +1,20 @@
 #!/usr/bin/python
 
+# Erebus IRC bot - Author: John Runyon
+# main startup code
+
 #TODO: tons
 
 import os, sys, select, MySQLdb, MySQLdb.cursors
-import bot, config
+import bot, config, ctlmod
 
 class Erebus(object):
        bots = {}
        fds = {}
        mods = {}
        msghandlers = {}
+       users = {}
+       chans = {}
 
        class User(object):
                chans = []
@@ -17,18 +22,31 @@ class Erebus(object):
                def __init__(self, nick, auth=None):
                        self.nick = nick
                        self.auth = auth
+                       self.checklevel()
 
-                       if auth is not None:
-                               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()
 
-               def checklevel(self): self.level = 9999 #TODO get level from db
+               def checklevel(self):
+                       if self.auth is None:
+                               self.glevel = -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.glevel = row['level']
+                               else:
+                                       self.glevel = 0
+                       return self.glevel
 
                def __str__(self): return self.nick
-               def __repr__(self): return "<User %r>" % (self.nick)
+               def __repr__(self): return "<User %r (%d)>" % (self.nick,self.glevel)
 
        class Channel(object):
                users = []
@@ -59,7 +77,8 @@ class Erebus(object):
                def __str__(self): return self.name
                def __repr__(self): return "<Channel %r>" % (self.name)
 
-       def __init__(self):
+       def __init__(self, trigger):
+               self.trigger = trigger
                if os.name == "posix":
                        self.potype = "poll"
                        self.po = select.poll()
@@ -84,8 +103,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())
 
@@ -100,23 +125,26 @@ 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
-
-cfg = config.Config('bot.config')
-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():
+       global cfg, main
+
+       cfg = config.Config('bot.config')
+       main = Erebus(cfg.trigger)
+
+       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")
@@ -133,7 +161,8 @@ def setup():
 def loop():
        poready = main.poll()
        for fileno in poready:
-               main.fd(fileno).getdata()
+               for line in main.fd(fileno).getdata():
+                       main.fd(fileno).parse(line)
 
 if __name__ == '__main__':
        setup()