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