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