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