]>
Commit | Line | Data |
---|---|---|
b25d4368 | 1 | #!/usr/bin/python |
4477123d | 2 | # vim: fileencoding=utf-8 |
b25d4368 | 3 | |
931c88a4 | 4 | # Erebus IRC bot - Author: John Runyon |
5 | # main startup code | |
6 | ||
a28e2ae9 | 7 | from __future__ import print_function |
8 | ||
2ffef3ff | 9 | import os, sys, select, MySQLdb, MySQLdb.cursors, time, random, gc |
db50981b | 10 | import bot, config, ctlmod |
b25d4368 | 11 | |
a8553c45 | 12 | class Erebus(object): #singleton to pass around |
134c1193 | 13 | APIVERSION = 0 |
a76c4bd8 | 14 | RELEASE = 0 |
15 | ||
49a455aa | 16 | bots = {} |
17 | fds = {} | |
e4a4c762 | 18 | numhandlers = {} |
49a455aa | 19 | msghandlers = {} |
9557ee54 | 20 | chanhandlers = {} |
b2a896c8 | 21 | users = {} |
22 | chans = {} | |
49a455aa | 23 | |
24 | class User(object): | |
49a455aa | 25 | def __init__(self, nick, auth=None): |
26 | self.nick = nick | |
b2a896c8 | 27 | self.auth = auth |
676b2a85 | 28 | self.checklevel() |
a4eacae2 | 29 | |
5477b368 | 30 | self.chans = [] |
31 | ||
e80bf7de | 32 | def msg(self, *args, **kwargs): |
e64ac4a0 | 33 | main.randbot().msg(self, *args, **kwargs) |
2bb267e0 | 34 | def slowmsg(self, *args, **kwargs): |
35 | main.randbot().slowmsg(self, *args, **kwargs) | |
e64ac4a0 | 36 | def fastmsg(self, *args, **kwargs): |
37 | main.randbot().fastmsg(self, *args, **kwargs) | |
e80bf7de | 38 | |
b2a896c8 | 39 | def isauthed(self): |
40 | return self.auth is not None | |
41 | ||
49a455aa | 42 | def authed(self, auth): |
de89db13 | 43 | if auth == '0': self.auth = None |
44 | else: self.auth = auth.lower() | |
49a455aa | 45 | self.checklevel() |
a4eacae2 | 46 | |
676b2a85 | 47 | def checklevel(self): |
48 | if self.auth is None: | |
839d2b35 | 49 | self.glevel = -1 |
676b2a85 | 50 | else: |
2729abc8 | 51 | c = main.query("SELECT level FROM users WHERE auth = %s", (self.auth,)) |
52 | if c: | |
4fa1118b | 53 | row = c.fetchone() |
54 | if row is not None: | |
55 | self.glevel = row['level'] | |
56 | else: | |
57 | self.glevel = 0 | |
676b2a85 | 58 | else: |
839d2b35 | 59 | self.glevel = 0 |
60 | return self.glevel | |
43b98e4e | 61 | |
5477b368 | 62 | def join(self, chan): |
84b7c247 | 63 | if chan not in self.chans: self.chans.append(chan) |
5477b368 | 64 | def part(self, chan): |
3d724d3a | 65 | try: |
66 | self.chans.remove(chan) | |
67 | except: pass | |
d53d073b | 68 | return len(self.chans) == 0 |
c695f740 | 69 | def quit(self): |
d53d073b | 70 | pass |
124f114c | 71 | def nickchange(self, newnick): |
e80bf7de | 72 | self.nick = newnick |
5477b368 | 73 | |
49a455aa | 74 | def __str__(self): return self.nick |
71ef8273 | 75 | def __repr__(self): return "<User %r (%d)>" % (self.nick, self.glevel) |
43b98e4e | 76 | |
49a455aa | 77 | class Channel(object): |
586997a7 | 78 | def __init__(self, name, bot): |
49a455aa | 79 | self.name = name |
5477b368 | 80 | self.bot = bot |
586997a7 | 81 | self.levels = {} |
5477b368 | 82 | |
83 | self.users = [] | |
84 | self.voices = [] | |
85 | self.ops = [] | |
a4eacae2 | 86 | |
2729abc8 | 87 | c = main.query("SELECT user, level FROM chusers WHERE chan = %s", (self.name,)) |
88 | if c: | |
586997a7 | 89 | row = c.fetchone() |
4fa1118b | 90 | while row is not None: |
91 | self.levels[row['user']] = row['level'] | |
92 | row = c.fetchone() | |
586997a7 | 93 | |
94 | ||
fd52fb16 | 95 | def msg(self, *args, **kwargs): |
e64ac4a0 | 96 | self.bot.msg(self, *args, **kwargs) |
2bb267e0 | 97 | def slowmsg(self, *args, **kwargs): |
98 | self.bot.slowmsg(self, *args, **kwargs) | |
e64ac4a0 | 99 | def fastmsg(self, *args, **kwargs): |
100 | self.bot.fastmsg(self, *args, **kwargs) | |
fd52fb16 | 101 | |
586997a7 | 102 | def levelof(self, auth): |
a9ce8d6a | 103 | if auth is None: |
104 | return 0 | |
586997a7 | 105 | auth = auth.lower() |
106 | if auth in self.levels: | |
107 | return self.levels[auth] | |
108 | else: | |
109 | return 0 | |
110 | ||
111 | def setlevel(self, auth, level, savetodb=True): | |
112 | auth = auth.lower() | |
113 | if savetodb: | |
2729abc8 | 114 | c = main.query("REPLACE INTO chusers (chan, user, level) VALUES (%s, %s, %s)", (self.name, auth, level)) |
115 | if c: | |
4fa1118b | 116 | self.levels[auth] = level |
117 | return True | |
118 | else: | |
119 | return False | |
586997a7 | 120 | |
49a455aa | 121 | def userjoin(self, user, level=None): |
122 | if user not in self.users: self.users.append(user) | |
123 | if level == 'op' and user not in self.ops: self.ops.append(user) | |
124 | if level == 'voice' and user not in self.voices: self.voices.append(user) | |
125 | def userpart(self, user): | |
126 | if user in self.ops: self.ops.remove(user) | |
127 | if user in self.voices: self.voices.remove(user) | |
128 | if user in self.users: self.users.remove(user) | |
a4eacae2 | 129 | |
49a455aa | 130 | def userop(self, user): |
131 | if user in self.users and user not in self.ops: self.ops.append(user) | |
132 | def uservoice(self, user): | |
133 | if user in self.users and user not in self.voices: self.voices.append(user) | |
134 | def userdeop(self, user): | |
135 | if user in self.ops: self.ops.remove(user) | |
136 | def userdevoice(self, user): | |
137 | if user in self.voices: self.voices.remove(user) | |
138 | ||
139 | def __str__(self): return self.name | |
140 | def __repr__(self): return "<Channel %r>" % (self.name) | |
141 | ||
c0eee1b4 | 142 | def __init__(self, cfg): |
fc16e064 | 143 | self.starttime = time.time() |
c0eee1b4 | 144 | self.cfg = cfg |
145 | self.trigger = cfg.trigger | |
fd96a423 | 146 | if os.name == "posix": |
147 | self.potype = "poll" | |
148 | self.po = select.poll() | |
149 | else: # f.e. os.name == "nt" (Windows) | |
150 | self.potype = "select" | |
151 | self.fdlist = [] | |
49a455aa | 152 | |
2729abc8 | 153 | def query(self, *args, **kwargs): |
c728e51c | 154 | if 'noretry' in kwargs: |
155 | noretry = kwargs['noretry'] | |
156 | del kwargs['noretry'] | |
2729abc8 | 157 | else: |
c728e51c | 158 | noretry = False |
2729abc8 | 159 | |
160 | self.log("[SQL]", "?", "query(%s, %s)" % (', '.join([repr(i) for i in args]), ', '.join([str(key)+"="+repr(kwargs[key]) for key in kwargs]))) | |
161 | try: | |
162 | curs = self.db.cursor() | |
163 | res = curs.execute(*args, **kwargs) | |
164 | if res: | |
165 | return curs | |
166 | else: | |
167 | return res | |
168 | except MySQLdb.MySQLError as e: | |
169 | self.log("[SQL]", "!", "MySQL error! %r" % (e)) | |
c728e51c | 170 | if not noretry: |
2729abc8 | 171 | dbsetup() |
c728e51c | 172 | return self.query(*args, noretry=True, **kwargs) |
2729abc8 | 173 | else: |
174 | raise e | |
175 | ||
c728e51c | 176 | def querycb(self, cb, *args, **kwargs): |
177 | def run_query(): | |
178 | cb(self.query(*args, **kwargs)) | |
179 | threading.Thread(target=run_query).start() | |
180 | ||
0af282c6 | 181 | def newbot(self, nick, user, bind, authname, authpass, server, port, realname): |
49a455aa | 182 | if bind is None: bind = '' |
0af282c6 | 183 | obj = bot.Bot(self, nick, user, bind, authname, authpass, server, port, realname) |
49a455aa | 184 | self.bots[nick.lower()] = obj |
a4eacae2 | 185 | |
49a455aa | 186 | def newfd(self, obj, fileno): |
49a455aa | 187 | self.fds[fileno] = obj |
fd96a423 | 188 | if self.potype == "poll": |
189 | self.po.register(fileno, select.POLLIN) | |
190 | elif self.potype == "select": | |
191 | self.fdlist.append(fileno) | |
a4eacae2 | 192 | |
43b98e4e | 193 | def bot(self, name): #get Bot() by name (nick) |
49a455aa | 194 | return self.bots[name.lower()] |
43b98e4e | 195 | def fd(self, fileno): #get Bot() by fd/fileno |
49a455aa | 196 | return self.fds[fileno] |
8af0407d | 197 | def randbot(self): #get Bot() randomly |
71ef8273 | 198 | return self.bots[random.choice(list(self.bots.keys()))] |
49a455aa | 199 | |
3d724d3a | 200 | def user(self, _nick, justjoined=False, create=True): |
c695f740 | 201 | nick = _nick.lower() |
b2a896c8 | 202 | if nick in self.users: |
203 | return self.users[nick] | |
3d724d3a | 204 | elif create: |
c695f740 | 205 | user = self.User(_nick) |
b2a896c8 | 206 | self.users[nick] = user |
8af0407d | 207 | |
208 | if justjoined: | |
d53d073b | 209 | self.randbot().conn.send("WHO %s n%%ant,1" % (nick)) |
8af0407d | 210 | |
b2a896c8 | 211 | return user |
3d724d3a | 212 | else: |
213 | return None | |
5477b368 | 214 | def channel(self, name): #get Channel() by name |
215 | if name.lower() in self.chans: | |
216 | return self.chans[name.lower()] | |
217 | else: | |
218 | return None | |
219 | ||
586997a7 | 220 | def newchannel(self, bot, name): |
221 | chan = self.Channel(name.lower(), bot) | |
5477b368 | 222 | self.chans[name.lower()] = chan |
223 | return chan | |
49a455aa | 224 | |
225 | def poll(self): | |
fd96a423 | 226 | if self.potype == "poll": |
227 | return [fd for (fd, ev) in self.po.poll()] | |
228 | elif self.potype == "select": | |
229 | return select.select(self.fdlist, [], [])[0] | |
49a455aa | 230 | |
231 | def connectall(self): | |
a28e2ae9 | 232 | for bot in self.bots.values(): |
49a455aa | 233 | if bot.conn.state == 0: |
234 | bot.connect() | |
235 | ||
fadbf980 | 236 | def module(self, name): |
237 | return ctlmod.modules[name] | |
238 | ||
a8553c45 | 239 | def log(self, source, level, message): |
a28e2ae9 | 240 | print("%09.3f %s [%s] %s" % (time.time() % 100000, source, level, message)) |
a8553c45 | 241 | |
f560eb44 | 242 | def getuserbyauth(self, auth): |
a28e2ae9 | 243 | return [u for u in self.users.values() if u.auth == auth.lower()] |
f560eb44 | 244 | |
bffe0139 | 245 | def getdb(self): |
246 | """Get a DB object. The object must be returned to the pool after us, using returndb().""" | |
247 | return self.dbs.pop() | |
248 | ||
249 | def returndb(self, db): | |
250 | self.dbs.append(db) | |
251 | ||
49a455aa | 252 | #bind functions |
db50981b | 253 | def hook(self, word, handler): |
e4a4c762 | 254 | try: |
255 | self.msghandlers[word].append(handler) | |
256 | except: | |
257 | self.msghandlers[word] = [handler] | |
258 | def unhook(self, word, handler): | |
259 | if word in self.msghandlers and handler in self.msghandlers[word]: | |
260 | self.msghandlers[word].remove(handler) | |
db50981b | 261 | def hashook(self, word): |
e4a4c762 | 262 | return word in self.msghandlers and len(self.msghandlers[word]) != 0 |
db50981b | 263 | def gethook(self, word): |
264 | return self.msghandlers[word] | |
b25d4368 | 265 | |
e4a4c762 | 266 | def hooknum(self, word, handler): |
267 | try: | |
268 | self.numhandlers[word].append(handler) | |
269 | except: | |
270 | self.numhandlers[word] = [handler] | |
271 | def unhooknum(self, word, handler): | |
272 | if word in self.numhandlers and handler in self.numhandlers[word]: | |
273 | self.numhandlers[word].remove(handler) | |
274 | def hasnumhook(self, word): | |
275 | return word in self.numhandlers and len(self.numhandlers[word]) != 0 | |
276 | def getnumhook(self, word): | |
277 | return self.numhandlers[word] | |
278 | ||
2a1a69a6 | 279 | def hookchan(self, chan, handler): |
280 | try: | |
9557ee54 | 281 | self.chanhandlers[chan].append(handler) |
2a1a69a6 | 282 | except: |
9557ee54 | 283 | self.chanhandlers[chan] = [handler] |
2a1a69a6 | 284 | def unhookchan(self, chan, handler): |
285 | if chan in self.chanhandlers and handler in self.chanhandlers[chan]: | |
286 | self.chanhandlers[chan].remove(handler) | |
287 | def haschanhook(self, chan): | |
288 | return chan in self.chanhandlers and len(self.chanhandlers[chan]) != 0 | |
289 | def getchanhook(self, chan): | |
290 | return self.chanhandlers[chan] | |
586997a7 | 291 | |
292 | ||
de89db13 | 293 | def dbsetup(): |
4fa1118b | 294 | main.db = None |
bffe0139 | 295 | main.dbs = [] |
296 | for i in range(cfg.get('erebus', 'num_db_connections', 2)-1): | |
297 | main.dbs.append(MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MySQLdb.cursors.DictCursor)) | |
2729abc8 | 298 | main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MySQLdb.cursors.DictCursor) |
586997a7 | 299 | |
b25d4368 | 300 | def setup(): |
db50981b | 301 | global cfg, main |
302 | ||
48479459 | 303 | cfg = config.Config('bot.config') |
e64ac4a0 | 304 | |
dcc5bde3 | 305 | if cfg.getboolean('debug', 'gc'): |
2ffef3ff | 306 | gc.set_debug(gc.DEBUG_LEAK) |
307 | ||
e64ac4a0 | 308 | pidfile = open(cfg.pidfile, 'w') |
309 | pidfile.write(str(os.getpid())) | |
310 | pidfile.close() | |
311 | ||
c0eee1b4 | 312 | main = Erebus(cfg) |
bffe0139 | 313 | dbsetup() |
db50981b | 314 | |
315 | autoloads = [mod for mod, yes in cfg.items('autoloads') if int(yes) == 1] | |
316 | for mod in autoloads: | |
b9c6eb1d | 317 | ctlmod.load(main, mod) |
db50981b | 318 | |
2729abc8 | 319 | c = main.query("SELECT nick, user, bind, authname, authpass FROM bots WHERE active = 1") |
320 | if c: | |
4fa1118b | 321 | rows = c.fetchall() |
322 | c.close() | |
323 | for row in rows: | |
0af282c6 | 324 | main.newbot(row['nick'], row['user'], row['bind'], row['authname'], row['authpass'], cfg.host, cfg.port, cfg.realname) |
a12f7519 | 325 | main.connectall() |
b25d4368 | 326 | |
327 | def loop(): | |
49a455aa | 328 | poready = main.poll() |
fd96a423 | 329 | for fileno in poready: |
d1ea2946 | 330 | for line in main.fd(fileno).getdata(): |
331 | main.fd(fileno).parse(line) | |
b25d4368 | 332 | |
333 | if __name__ == '__main__': | |
963f2522 | 334 | try: os.rename('logfile', 'oldlogs/%s' % (time.time())) |
24b74bb3 | 335 | except: pass |
3d724d3a | 336 | sys.stdout = open('logfile', 'w', 1) |
24b74bb3 | 337 | sys.stderr = sys.stdout |
b25d4368 | 338 | setup() |
49a455aa | 339 | while True: loop() |