]>
Commit | Line | Data |
---|---|---|
f4f6442e JR |
1 | #!/usr/bin/python |
2 | ||
db75daab JR |
3 | # Erebus IRC bot - Author: John Runyon |
4 | # main startup code | |
5 | ||
f4f6442e JR |
6 | #TODO: tons |
7 | ||
82696467 | 8 | import os, sys, select, MySQLdb, MySQLdb.cursors |
d1ea05b0 | 9 | import bot, config, ctlmod |
f4f6442e JR |
10 | |
11 | class Erebus(object): | |
9c935294 JR |
12 | bots = {} |
13 | fds = {} | |
14 | mods = {} | |
ac617f8b | 15 | numhandlers = {} |
9c935294 | 16 | msghandlers = {} |
1743224e JR |
17 | users = {} |
18 | chans = {} | |
9c935294 JR |
19 | |
20 | class User(object): | |
9c935294 JR |
21 | def __init__(self, nick, auth=None): |
22 | self.nick = nick | |
1743224e | 23 | self.auth = auth |
7d69441a | 24 | self.checklevel() |
ab7c0c0b | 25 | |
87b8b5ef JR |
26 | self.chans = [] |
27 | ||
1743224e JR |
28 | def isauthed(self): |
29 | return self.auth is not None | |
30 | ||
9c935294 | 31 | def authed(self, auth): |
1743224e | 32 | if auth == '0': auth = None |
9c935294 JR |
33 | self.auth = auth |
34 | self.checklevel() | |
ab7c0c0b | 35 | |
7d69441a JR |
36 | def checklevel(self): |
37 | if self.auth is None: | |
67e04ca5 | 38 | self.glevel = -1 |
7d69441a JR |
39 | else: |
40 | c = main.db.cursor() | |
41 | c.execute("SELECT level FROM users WHERE auth = %s", (self.auth,)) | |
42 | row = c.fetchone() | |
43 | if row is not None: | |
67e04ca5 | 44 | self.glevel = row['level'] |
7d69441a | 45 | else: |
67e04ca5 JR |
46 | self.glevel = 0 |
47 | return self.glevel | |
f9767f79 | 48 | |
87b8b5ef JR |
49 | def join(self, chan): |
50 | self.chans.append(chan) | |
51 | def part(self, chan): | |
52 | self.chans.remove(chan) | |
53 | ||
9c935294 | 54 | def __str__(self): return self.nick |
67e04ca5 | 55 | def __repr__(self): return "<User %r (%d)>" % (self.nick,self.glevel) |
f9767f79 | 56 | |
9c935294 | 57 | class Channel(object): |
87b8b5ef | 58 | def __init__(self, name, bot, levels={}): |
9c935294 | 59 | self.name = name |
87b8b5ef JR |
60 | self.bot = bot |
61 | self.levels = levels | |
62 | ||
63 | self.users = [] | |
64 | self.voices = [] | |
65 | self.ops = [] | |
ab7c0c0b | 66 | |
9c935294 JR |
67 | def userjoin(self, user, level=None): |
68 | if user not in self.users: self.users.append(user) | |
69 | if level == 'op' and user not in self.ops: self.ops.append(user) | |
70 | if level == 'voice' and user not in self.voices: self.voices.append(user) | |
71 | def userpart(self, user): | |
72 | if user in self.ops: self.ops.remove(user) | |
73 | if user in self.voices: self.voices.remove(user) | |
74 | if user in self.users: self.users.remove(user) | |
ab7c0c0b | 75 | |
9c935294 JR |
76 | def userop(self, user): |
77 | if user in self.users and user not in self.ops: self.ops.append(user) | |
78 | def uservoice(self, user): | |
79 | if user in self.users and user not in self.voices: self.voices.append(user) | |
80 | def userdeop(self, user): | |
81 | if user in self.ops: self.ops.remove(user) | |
82 | def userdevoice(self, user): | |
83 | if user in self.voices: self.voices.remove(user) | |
84 | ||
85 | def __str__(self): return self.name | |
86 | def __repr__(self): return "<Channel %r>" % (self.name) | |
87 | ||
1743224e JR |
88 | def __init__(self, trigger): |
89 | self.trigger = trigger | |
c5f1f47b JR |
90 | if os.name == "posix": |
91 | self.potype = "poll" | |
92 | self.po = select.poll() | |
93 | else: # f.e. os.name == "nt" (Windows) | |
94 | self.potype = "select" | |
95 | self.fdlist = [] | |
9c935294 | 96 | |
87b8b5ef | 97 | def newbot(self, nick, user, bind, server, port, realname): |
9c935294 | 98 | if bind is None: bind = '' |
87b8b5ef | 99 | obj = bot.Bot(self, nick, user, bind, server, port, realname) |
9c935294 | 100 | self.bots[nick.lower()] = obj |
ab7c0c0b | 101 | |
9c935294 | 102 | def newfd(self, obj, fileno): |
9c935294 | 103 | self.fds[fileno] = obj |
c5f1f47b JR |
104 | if self.potype == "poll": |
105 | self.po.register(fileno, select.POLLIN) | |
106 | elif self.potype == "select": | |
107 | self.fdlist.append(fileno) | |
ab7c0c0b | 108 | |
f9767f79 | 109 | def bot(self, name): #get Bot() by name (nick) |
9c935294 | 110 | return self.bots[name.lower()] |
f9767f79 | 111 | def fd(self, fileno): #get Bot() by fd/fileno |
9c935294 | 112 | return self.fds[fileno] |
06858271 JR |
113 | def randbot(self): #get Bot() randomly |
114 | for b in self.bots.itervalues(): return b #TODO | |
9c935294 | 115 | |
06858271 | 116 | def user(self, nick, justjoined=False): |
1743224e JR |
117 | nick = nick.lower() |
118 | if nick in self.users: | |
119 | return self.users[nick] | |
120 | else: | |
121 | user = self.User(nick) | |
122 | self.users[nick] = user | |
06858271 JR |
123 | |
124 | if justjoined: | |
125 | self.randbot().conn.send("WHO %s %%ant,2" % (nick)) | |
126 | ||
1743224e | 127 | return user |
87b8b5ef JR |
128 | def channel(self, name): #get Channel() by name |
129 | if name.lower() in self.chans: | |
130 | return self.chans[name.lower()] | |
131 | else: | |
132 | return None | |
133 | ||
134 | def newchannel(self, bot, name, levels={}): | |
135 | chan = self.Channel(name.lower(), bot, levels) | |
136 | self.chans[name.lower()] = chan | |
137 | return chan | |
9c935294 JR |
138 | |
139 | def poll(self): | |
c5f1f47b JR |
140 | if self.potype == "poll": |
141 | return [fd for (fd, ev) in self.po.poll()] | |
142 | elif self.potype == "select": | |
143 | return select.select(self.fdlist, [], [])[0] | |
9c935294 JR |
144 | |
145 | def connectall(self): | |
146 | for bot in self.bots.itervalues(): | |
147 | if bot.conn.state == 0: | |
148 | bot.connect() | |
149 | ||
9c935294 | 150 | #bind functions |
d1ea05b0 | 151 | def hook(self, word, handler): |
ac617f8b JR |
152 | try: |
153 | self.msghandlers[word].append(handler) | |
154 | except: | |
155 | self.msghandlers[word] = [handler] | |
156 | def unhook(self, word, handler): | |
157 | if word in self.msghandlers and handler in self.msghandlers[word]: | |
158 | self.msghandlers[word].remove(handler) | |
d1ea05b0 | 159 | def hashook(self, word): |
ac617f8b | 160 | return word in self.msghandlers and len(self.msghandlers[word]) != 0 |
d1ea05b0 JR |
161 | def gethook(self, word): |
162 | return self.msghandlers[word] | |
f4f6442e | 163 | |
ac617f8b JR |
164 | def hooknum(self, word, handler): |
165 | try: | |
166 | self.numhandlers[word].append(handler) | |
167 | except: | |
168 | self.numhandlers[word] = [handler] | |
169 | def unhooknum(self, word, handler): | |
170 | if word in self.numhandlers and handler in self.numhandlers[word]: | |
171 | self.numhandlers[word].remove(handler) | |
172 | def hasnumhook(self, word): | |
173 | return word in self.numhandlers and len(self.numhandlers[word]) != 0 | |
174 | def getnumhook(self, word): | |
175 | return self.numhandlers[word] | |
176 | ||
f4f6442e | 177 | def setup(): |
d1ea05b0 JR |
178 | global cfg, main |
179 | ||
180 | cfg = config.Config('bot.config') | |
1743224e | 181 | main = Erebus(cfg.trigger) |
d1ea05b0 JR |
182 | |
183 | autoloads = [mod for mod, yes in cfg.items('autoloads') if int(yes) == 1] | |
184 | for mod in autoloads: | |
32cabc3d | 185 | print "Loading %s" % (mod) |
d1ea05b0 JR |
186 | ctlmod.load(main, mod) |
187 | ||
82696467 JR |
188 | main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MySQLdb.cursors.DictCursor) |
189 | c = main.db.cursor() | |
190 | c.execute("SELECT nick, user, bind FROM bots WHERE active = 1") | |
191 | rows = c.fetchall() | |
192 | c.close() | |
193 | for row in rows: | |
87b8b5ef | 194 | main.newbot(row['nick'], row['user'], row['bind'], cfg.host, cfg.port, cfg.realname) |
82696467 | 195 | main.connectall() |
f4f6442e JR |
196 | |
197 | def loop(): | |
9c935294 | 198 | poready = main.poll() |
c5f1f47b | 199 | for fileno in poready: |
711444b7 JR |
200 | for line in main.fd(fileno).getdata(): |
201 | main.fd(fileno).parse(line) | |
f4f6442e JR |
202 | |
203 | if __name__ == '__main__': | |
204 | setup() | |
9c935294 | 205 | while True: loop() |