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