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