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