]> jfr.im git - erebus.git/blame - erebus.py
more work on trivia module
[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
4fa1118b 8import os, sys, select, MySQLdb, MySQLdb.cursors, time
db50981b 9import bot, config, ctlmod
b25d4368 10
11class Erebus(object):
49a455aa 12 bots = {}
13 fds = {}
14 mods = {}
e4a4c762 15 numhandlers = {}
49a455aa 16 msghandlers = {}
9557ee54 17 chanhandlers = {}
b2a896c8 18 users = {}
19 chans = {}
49a455aa 20
21 class User(object):
49a455aa 22 def __init__(self, nick, auth=None):
23 self.nick = nick
b2a896c8 24 self.auth = auth
676b2a85 25 self.checklevel()
a4eacae2 26
5477b368 27 self.chans = []
28
b2a896c8 29 def isauthed(self):
30 return self.auth is not None
31
49a455aa 32 def authed(self, auth):
de89db13 33 if auth == '0': self.auth = None
34 else: self.auth = auth.lower()
49a455aa 35 self.checklevel()
a4eacae2 36
676b2a85 37 def checklevel(self):
38 if self.auth is None:
839d2b35 39 self.glevel = -1
676b2a85 40 else:
41 c = main.db.cursor()
4fa1118b 42 if c.execute("SELECT level FROM users WHERE auth = %s", (self.auth,)):
43 row = c.fetchone()
44 if row is not None:
45 self.glevel = row['level']
46 else:
47 self.glevel = 0
676b2a85 48 else:
839d2b35 49 self.glevel = 0
50 return self.glevel
43b98e4e 51
5477b368 52 def join(self, chan):
53 self.chans.append(chan)
54 def part(self, chan):
55 self.chans.remove(chan)
c695f740 56 def quit(self):
57 for chan in self.chans:
58 self.chans.remove(chan)
5477b368 59
49a455aa 60 def __str__(self): return self.nick
839d2b35 61 def __repr__(self): return "<User %r (%d)>" % (self.nick,self.glevel)
43b98e4e 62
49a455aa 63 class Channel(object):
586997a7 64 def __init__(self, name, bot):
49a455aa 65 self.name = name
5477b368 66 self.bot = bot
586997a7 67 self.levels = {}
5477b368 68
69 self.users = []
70 self.voices = []
71 self.ops = []
a4eacae2 72
586997a7 73 c = main.db.cursor()
4fa1118b 74 if c.execute("SELECT user, level FROM chusers WHERE chan = %s", (self.name,)):
586997a7 75 row = c.fetchone()
4fa1118b 76 while row is not None:
77 self.levels[row['user']] = row['level']
78 row = c.fetchone()
586997a7 79
80
81 def levelof(self, auth):
82 auth = auth.lower()
83 if auth in self.levels:
84 return self.levels[auth]
85 else:
86 return 0
87
88 def setlevel(self, auth, level, savetodb=True):
89 auth = auth.lower()
90 if savetodb:
91 c = main.db.cursor()
4fa1118b 92 if c.execute("REPLACE INTO chusers (chan, user, level) VALUES (%s, %s, %s)", (self.name, auth, level)):
93 self.levels[auth] = level
94 return True
95 else:
96 return False
586997a7 97
49a455aa 98 def userjoin(self, user, level=None):
99 if user not in self.users: self.users.append(user)
100 if level == 'op' and user not in self.ops: self.ops.append(user)
101 if level == 'voice' and user not in self.voices: self.voices.append(user)
102 def userpart(self, user):
103 if user in self.ops: self.ops.remove(user)
104 if user in self.voices: self.voices.remove(user)
105 if user in self.users: self.users.remove(user)
a4eacae2 106
49a455aa 107 def userop(self, user):
108 if user in self.users and user not in self.ops: self.ops.append(user)
109 def uservoice(self, user):
110 if user in self.users and user not in self.voices: self.voices.append(user)
111 def userdeop(self, user):
112 if user in self.ops: self.ops.remove(user)
113 def userdevoice(self, user):
114 if user in self.voices: self.voices.remove(user)
115
116 def __str__(self): return self.name
117 def __repr__(self): return "<Channel %r>" % (self.name)
118
b2a896c8 119 def __init__(self, trigger):
120 self.trigger = trigger
fd96a423 121 if os.name == "posix":
122 self.potype = "poll"
123 self.po = select.poll()
124 else: # f.e. os.name == "nt" (Windows)
125 self.potype = "select"
126 self.fdlist = []
49a455aa 127
5477b368 128 def newbot(self, nick, user, bind, server, port, realname):
49a455aa 129 if bind is None: bind = ''
5477b368 130 obj = bot.Bot(self, nick, user, bind, server, port, realname)
49a455aa 131 self.bots[nick.lower()] = obj
a4eacae2 132
49a455aa 133 def newfd(self, obj, fileno):
49a455aa 134 self.fds[fileno] = obj
fd96a423 135 if self.potype == "poll":
136 self.po.register(fileno, select.POLLIN)
137 elif self.potype == "select":
138 self.fdlist.append(fileno)
a4eacae2 139
43b98e4e 140 def bot(self, name): #get Bot() by name (nick)
49a455aa 141 return self.bots[name.lower()]
43b98e4e 142 def fd(self, fileno): #get Bot() by fd/fileno
49a455aa 143 return self.fds[fileno]
8af0407d 144 def randbot(self): #get Bot() randomly
145 for b in self.bots.itervalues(): return b #TODO
49a455aa 146
c695f740 147 def user(self, _nick, justjoined=False):
148 nick = _nick.lower()
b2a896c8 149 if nick in self.users:
150 return self.users[nick]
151 else:
c695f740 152 user = self.User(_nick)
b2a896c8 153 self.users[nick] = user
8af0407d 154
155 if justjoined:
156 self.randbot().conn.send("WHO %s %%ant,2" % (nick))
157
b2a896c8 158 return user
5477b368 159 def channel(self, name): #get Channel() by name
160 if name.lower() in self.chans:
161 return self.chans[name.lower()]
162 else:
163 return None
164
586997a7 165 def newchannel(self, bot, name):
166 chan = self.Channel(name.lower(), bot)
5477b368 167 self.chans[name.lower()] = chan
168 return chan
49a455aa 169
170 def poll(self):
fd96a423 171 if self.potype == "poll":
172 return [fd for (fd, ev) in self.po.poll()]
173 elif self.potype == "select":
174 return select.select(self.fdlist, [], [])[0]
49a455aa 175
176 def connectall(self):
177 for bot in self.bots.itervalues():
178 if bot.conn.state == 0:
179 bot.connect()
180
49a455aa 181 #bind functions
db50981b 182 def hook(self, word, handler):
e4a4c762 183 try:
184 self.msghandlers[word].append(handler)
185 except:
186 self.msghandlers[word] = [handler]
187 def unhook(self, word, handler):
188 if word in self.msghandlers and handler in self.msghandlers[word]:
189 self.msghandlers[word].remove(handler)
db50981b 190 def hashook(self, word):
e4a4c762 191 return word in self.msghandlers and len(self.msghandlers[word]) != 0
db50981b 192 def gethook(self, word):
193 return self.msghandlers[word]
b25d4368 194
e4a4c762 195 def hooknum(self, word, handler):
196 try:
197 self.numhandlers[word].append(handler)
198 except:
199 self.numhandlers[word] = [handler]
200 def unhooknum(self, word, handler):
201 if word in self.numhandlers and handler in self.numhandlers[word]:
202 self.numhandlers[word].remove(handler)
203 def hasnumhook(self, word):
204 return word in self.numhandlers and len(self.numhandlers[word]) != 0
205 def getnumhook(self, word):
206 return self.numhandlers[word]
207
2a1a69a6 208 def hookchan(self, chan, handler):
209 try:
9557ee54 210 self.chanhandlers[chan].append(handler)
2a1a69a6 211 except:
9557ee54 212 self.chanhandlers[chan] = [handler]
2a1a69a6 213 def unhookchan(self, chan, handler):
214 if chan in self.chanhandlers and handler in self.chanhandlers[chan]:
215 self.chanhandlers[chan].remove(handler)
216 def haschanhook(self, chan):
217 return chan in self.chanhandlers and len(self.chanhandlers[chan]) != 0
218 def getchanhook(self, chan):
219 return self.chanhandlers[chan]
586997a7 220
221
de89db13 222class MyCursor(MySQLdb.cursors.DictCursor):
223 def execute(self, *args, **kwargs):
224 print "[SQL] [#] MyCursor.execute(self, %s, %s)" % (', '.join([repr(i) for i in args]), ', '.join([str(key)+"="+repr(kwargs[key]) for key in kwargs]))
225 try:
226 super(self.__class__, self).execute(*args, **kwargs)
227 except MySQLdb.MySQLError as e:
228 print "[SQL] [!] MySQL error! %r" % (e)
4fa1118b 229 dbsetup()
230 return False
231 return True
de89db13 232
233
234def dbsetup():
4fa1118b 235 main.db = None
de89db13 236 main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MyCursor)
586997a7 237
b25d4368 238def setup():
db50981b 239 global cfg, main
240
241 cfg = config.Config('bot.config')
b2a896c8 242 main = Erebus(cfg.trigger)
db50981b 243
244 autoloads = [mod for mod, yes in cfg.items('autoloads') if int(yes) == 1]
245 for mod in autoloads:
d431e543 246 print "Loading %s" % (mod)
db50981b 247 ctlmod.load(main, mod)
248
de89db13 249 dbsetup()
a12f7519 250 c = main.db.cursor()
4fa1118b 251 if c.execute("SELECT nick, user, bind FROM bots WHERE active = 1"):
252 rows = c.fetchall()
253 c.close()
254 for row in rows:
255 main.newbot(row['nick'], row['user'], row['bind'], cfg.host, cfg.port, cfg.realname)
a12f7519 256 main.connectall()
b25d4368 257
258def loop():
49a455aa 259 poready = main.poll()
fd96a423 260 for fileno in poready:
d1ea2946 261 for line in main.fd(fileno).getdata():
262 main.fd(fileno).parse(line)
b25d4368 263
264if __name__ == '__main__':
265 setup()
49a455aa 266 while True: loop()