]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python | |
2 | ||
3 | # Erebus IRC bot - Author: John Runyon | |
4 | # main startup code | |
5 | ||
6 | import os, sys, select, MySQLdb, MySQLdb.cursors, time, random | |
7 | import bot, config, ctlmod | |
8 | ||
9 | class Erebus(object): | |
10 | APIVERSION = 1 | |
11 | RELEASE = 0 | |
12 | ||
13 | bots = {} | |
14 | fds = {} | |
15 | numhandlers = {} | |
16 | msghandlers = {} | |
17 | chanhandlers = {} | |
18 | users = {} | |
19 | chans = {} | |
20 | ||
21 | class User(object): | |
22 | def __init__(self, nick, auth=None): | |
23 | self.nick = nick | |
24 | self.auth = auth | |
25 | self.checklevel() | |
26 | ||
27 | self.chans = [] | |
28 | ||
29 | def msg(self, *args, **kwargs): | |
30 | main.randbot.msg(self, *args, **kwargs) | |
31 | ||
32 | def isauthed(self): | |
33 | return self.auth is not None | |
34 | ||
35 | def authed(self, auth): | |
36 | if auth == '0': self.auth = None | |
37 | else: self.auth = auth.lower() | |
38 | self.checklevel() | |
39 | ||
40 | def checklevel(self): | |
41 | if self.auth is None: | |
42 | self.glevel = -1 | |
43 | else: | |
44 | c = main.db.cursor() | |
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 | |
51 | else: | |
52 | self.glevel = 0 | |
53 | return self.glevel | |
54 | ||
55 | def join(self, chan): | |
56 | self.chans.append(chan) | |
57 | def part(self, chan): | |
58 | self.chans.remove(chan) | |
59 | def quit(self): | |
60 | for chan in self.chans: | |
61 | self.chans.remove(chan) | |
62 | def nickchange(self, newnick): | |
63 | self.nick = newnick | |
64 | ||
65 | def __str__(self): return self.nick | |
66 | def __repr__(self): return "<User %r (%d)>" % (self.nick,self.glevel) | |
67 | ||
68 | class Channel(object): | |
69 | def __init__(self, name, bot): | |
70 | self.name = name | |
71 | self.bot = bot | |
72 | self.levels = {} | |
73 | ||
74 | self.users = [] | |
75 | self.voices = [] | |
76 | self.ops = [] | |
77 | ||
78 | c = main.db.cursor() | |
79 | if c.execute("SELECT user, level FROM chusers WHERE chan = %s", (self.name,)): | |
80 | row = c.fetchone() | |
81 | while row is not None: | |
82 | self.levels[row['user']] = row['level'] | |
83 | row = c.fetchone() | |
84 | ||
85 | ||
86 | def msg(self, *args, **kwargs): | |
87 | self.bot.msg(self.name, *args, **kwargs) | |
88 | ||
89 | def levelof(self, auth): | |
90 | if auth is None: | |
91 | return 0 | |
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() | |
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 | |
107 | ||
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) | |
116 | ||
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 | ||
129 | def __init__(self, cfg): | |
130 | self.cfg = cfg | |
131 | self.trigger = cfg.trigger | |
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 = [] | |
138 | ||
139 | def newbot(self, nick, user, bind, authname, authpass, server, port, realname): | |
140 | if bind is None: bind = '' | |
141 | obj = bot.Bot(self, nick, user, bind, authname, authpass, server, port, realname) | |
142 | self.bots[nick.lower()] = obj | |
143 | ||
144 | def newfd(self, obj, fileno): | |
145 | self.fds[fileno] = obj | |
146 | if self.potype == "poll": | |
147 | self.po.register(fileno, select.POLLIN) | |
148 | elif self.potype == "select": | |
149 | self.fdlist.append(fileno) | |
150 | ||
151 | def bot(self, name): #get Bot() by name (nick) | |
152 | return self.bots[name.lower()] | |
153 | def fd(self, fileno): #get Bot() by fd/fileno | |
154 | return self.fds[fileno] | |
155 | def randbot(self): #get Bot() randomly | |
156 | return self.bots[random.choice(self.bots.keys())] | |
157 | ||
158 | def user(self, _nick, justjoined=False): | |
159 | nick = _nick.lower() | |
160 | if nick in self.users: | |
161 | return self.users[nick] | |
162 | else: | |
163 | user = self.User(_nick) | |
164 | self.users[nick] = user | |
165 | ||
166 | if justjoined: | |
167 | self.randbot().conn.send("WHO %s n%%ant,2" % (nick)) | |
168 | ||
169 | return user | |
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 | ||
176 | def newchannel(self, bot, name): | |
177 | chan = self.Channel(name.lower(), bot) | |
178 | self.chans[name.lower()] = chan | |
179 | return chan | |
180 | ||
181 | def poll(self): | |
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] | |
186 | ||
187 | def connectall(self): | |
188 | for bot in self.bots.itervalues(): | |
189 | if bot.conn.state == 0: | |
190 | bot.connect() | |
191 | ||
192 | def module(self, name): | |
193 | return ctlmod.modules[name] | |
194 | ||
195 | #bind functions | |
196 | def hook(self, word, handler): | |
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) | |
204 | def hashook(self, word): | |
205 | return word in self.msghandlers and len(self.msghandlers[word]) != 0 | |
206 | def gethook(self, word): | |
207 | return self.msghandlers[word] | |
208 | ||
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 | ||
222 | def hookchan(self, chan, handler): | |
223 | try: | |
224 | self.chanhandlers[chan].append(handler) | |
225 | except: | |
226 | self.chanhandlers[chan] = [handler] | |
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] | |
234 | ||
235 | ||
236 | class MyCursor(MySQLdb.cursors.DictCursor): | |
237 | def execute(self, *args, **kwargs): | |
238 | print "[SQL] [#] MyCursor.execute(self, %s, %s)" % (', '.join([repr(i) for i in args]), ', '.join([str(key)+"="+repr(kwargs[key]) for key in kwargs])) | |
239 | try: | |
240 | super(self.__class__, self).execute(*args, **kwargs) | |
241 | except MySQLdb.MySQLError as e: | |
242 | print "[SQL] [!] MySQL error! %r" % (e) | |
243 | dbsetup() | |
244 | return False | |
245 | return True | |
246 | ||
247 | ||
248 | def dbsetup(): | |
249 | main.db = None | |
250 | main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MyCursor) | |
251 | ||
252 | def setup(): | |
253 | global cfg, main | |
254 | ||
255 | cfg = config.Config('bot.config') | |
256 | main = Erebus(cfg) | |
257 | ||
258 | autoloads = [mod for mod, yes in cfg.items('autoloads') if int(yes) == 1] | |
259 | for mod in autoloads: | |
260 | print "Loading %s ... " % (mod), | |
261 | modstatus = ctlmod.load(main, mod) | |
262 | if not modstatus: | |
263 | print str(modstatus) | |
264 | elif modstatus == True: | |
265 | print "OK" | |
266 | else: | |
267 | print modstatus | |
268 | ||
269 | dbsetup() | |
270 | c = main.db.cursor() | |
271 | if c.execute("SELECT nick, user, bind, authname, authpass FROM bots WHERE active = 1"): | |
272 | rows = c.fetchall() | |
273 | c.close() | |
274 | for row in rows: | |
275 | main.newbot(row['nick'], row['user'], row['bind'], row['authname'], row['authpass'], cfg.host, cfg.port, cfg.realname) | |
276 | main.connectall() | |
277 | ||
278 | def loop(): | |
279 | poready = main.poll() | |
280 | for fileno in poready: | |
281 | for line in main.fd(fileno).getdata(): | |
282 | main.fd(fileno).parse(line) | |
283 | ||
284 | if __name__ == '__main__': | |
285 | setup() | |
286 | while True: loop() |