]> jfr.im git - erebus.git/blame - erebus.py
Finished user authentication when bot joins channel (WHO #chan %ant,0) and added...
[erebus.git] / erebus.py
CommitLineData
b25d4368 1#!/usr/bin/python
2
931c88a4 3# Erebus IRC bot - Author: John Runyon
4# main startup code
5
b25d4368 6#TODO: tons
7
a12f7519 8import os, sys, select, MySQLdb, MySQLdb.cursors
db50981b 9import bot, config, ctlmod
b25d4368 10
11class Erebus(object):
49a455aa 12 bots = {}
13 fds = {}
14 mods = {}
15 msghandlers = {}
b2a896c8 16 users = {}
17 chans = {}
49a455aa 18
19 class User(object):
20 chans = []
a4eacae2 21
49a455aa 22 def __init__(self, nick, auth=None):
b2a896c8 23 print "parent.User(self, %r, %r)" % (nick, auth)
49a455aa 24 self.nick = nick
b2a896c8 25 self.auth = auth
676b2a85 26 self.checklevel()
a4eacae2 27
b2a896c8 28 def isauthed(self):
29 return self.auth is not None
30
49a455aa 31 def authed(self, auth):
b2a896c8 32 if auth == '0': auth = None
49a455aa 33 self.auth = auth
34 self.checklevel()
a4eacae2 35
676b2a85 36 def checklevel(self):
37 if self.auth is None:
38 self.level = -1
39 else:
40 c = main.db.cursor()
41 c.execute("SELECT level FROM users WHERE auth = %s", (self.auth,))
42 row = c.fetchone()
43 if row is not None:
44 self.level = row['level']
45 else:
46 self.level = 0
47 return self.level
43b98e4e 48
49a455aa 49 def __str__(self): return self.nick
676b2a85 50 def __repr__(self): return "<User %r (%d)>" % (self.nick,self.level)
43b98e4e 51
49a455aa 52 class Channel(object):
53 users = []
54 voices = []
55 ops = []
a4eacae2 56
49a455aa 57 def __init__(self, name):
58 self.name = name
a4eacae2 59
49a455aa 60 def userjoin(self, user, level=None):
61 if user not in self.users: self.users.append(user)
62 if level == 'op' and user not in self.ops: self.ops.append(user)
63 if level == 'voice' and user not in self.voices: self.voices.append(user)
64 def userpart(self, user):
65 if user in self.ops: self.ops.remove(user)
66 if user in self.voices: self.voices.remove(user)
67 if user in self.users: self.users.remove(user)
a4eacae2 68
49a455aa 69 def userop(self, user):
70 if user in self.users and user not in self.ops: self.ops.append(user)
71 def uservoice(self, user):
72 if user in self.users and user not in self.voices: self.voices.append(user)
73 def userdeop(self, user):
74 if user in self.ops: self.ops.remove(user)
75 def userdevoice(self, user):
76 if user in self.voices: self.voices.remove(user)
77
78 def __str__(self): return self.name
79 def __repr__(self): return "<Channel %r>" % (self.name)
80
b2a896c8 81 def __init__(self, trigger):
82 self.trigger = trigger
fd96a423 83 if os.name == "posix":
84 self.potype = "poll"
85 self.po = select.poll()
86 else: # f.e. os.name == "nt" (Windows)
87 self.potype = "select"
88 self.fdlist = []
49a455aa 89
90 def newbot(self, nick, user, bind, server, port, realname, chans):
91 if bind is None: bind = ''
92 obj = bot.Bot(self, nick, user, bind, server, port, realname, chans)
93 self.bots[nick.lower()] = obj
a4eacae2 94
49a455aa 95 def newfd(self, obj, fileno):
49a455aa 96 self.fds[fileno] = obj
fd96a423 97 if self.potype == "poll":
98 self.po.register(fileno, select.POLLIN)
99 elif self.potype == "select":
100 self.fdlist.append(fileno)
a4eacae2 101
43b98e4e 102 def bot(self, name): #get Bot() by name (nick)
49a455aa 103 return self.bots[name.lower()]
43b98e4e 104 def fd(self, fileno): #get Bot() by fd/fileno
49a455aa 105 return self.fds[fileno]
106
b2a896c8 107 def user(self, nick):
108 nick = nick.lower()
109 if nick in self.users:
110 return self.users[nick]
111 else:
112 user = self.User(nick)
113 self.users[nick] = user
114 return user
43b98e4e 115 def channel(self, name): #TODO #get Channel() by name
49a455aa 116 return self.Channel(name.lower())
117
118 def poll(self):
fd96a423 119 if self.potype == "poll":
120 return [fd for (fd, ev) in self.po.poll()]
121 elif self.potype == "select":
122 return select.select(self.fdlist, [], [])[0]
49a455aa 123
124 def connectall(self):
125 for bot in self.bots.itervalues():
126 if bot.conn.state == 0:
127 bot.connect()
128
49a455aa 129 #bind functions
db50981b 130 def hook(self, word, handler):
db50981b 131 self.msghandlers[word] = handler
132 def unhook(self, word):
133 del self.msghandlers[word]
134 def hashook(self, word):
135 return word in self.msghandlers
136 def gethook(self, word):
137 return self.msghandlers[word]
b25d4368 138
139def setup():
db50981b 140 global cfg, main
141
142 cfg = config.Config('bot.config')
b2a896c8 143 main = Erebus(cfg.trigger)
db50981b 144
145 autoloads = [mod for mod, yes in cfg.items('autoloads') if int(yes) == 1]
b2a896c8 146 print autoloads
db50981b 147 for mod in autoloads:
148 ctlmod.load(main, mod)
149
a12f7519 150 main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MySQLdb.cursors.DictCursor)
151 c = main.db.cursor()
152 c.execute("SELECT nick, user, bind FROM bots WHERE active = 1")
153 rows = c.fetchall()
154 c.close()
155 for row in rows:
156 c2 = main.db.cursor()
157 c2.execute("SELECT chname FROM chans WHERE bot = %s AND active = 1", (row['nick'],))
158 chans = [chdic['chname'] for chdic in c2.fetchall()]
159 c2.close()
160 main.newbot(row['nick'], row['user'], row['bind'], cfg.host, cfg.port, cfg.realname, chans)
161 main.connectall()
b25d4368 162
163def loop():
49a455aa 164 poready = main.poll()
fd96a423 165 for fileno in poready:
49a455aa 166 main.fd(fileno).getdata()
b25d4368 167
168if __name__ == '__main__':
169 setup()
49a455aa 170 while True: loop()