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