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