]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python | |
2 | ||
3 | # Erebus IRC bot - Author: John Runyon | |
4 | # main startup code | |
5 | ||
6 | #TODO: tons | |
7 | ||
8 | import os, sys, select, MySQLdb, MySQLdb.cursors | |
9 | import bot, config, ctlmod | |
10 | ||
11 | class Erebus(object): | |
12 | bots = {} | |
13 | fds = {} | |
14 | mods = {} | |
15 | msghandlers = {} | |
16 | users = {} | |
17 | chans = {} | |
18 | ||
19 | class User(object): | |
20 | chans = [] | |
21 | ||
22 | def __init__(self, nick, auth=None): | |
23 | print "parent.User(self, %r, %r)" % (nick, auth) | |
24 | self.nick = nick | |
25 | self.auth = auth | |
26 | self.checklevel() | |
27 | ||
28 | def isauthed(self): | |
29 | return self.auth is not None | |
30 | ||
31 | def authed(self, auth): | |
32 | if auth == '0': auth = None | |
33 | self.auth = auth | |
34 | self.checklevel() | |
35 | ||
36 | def checklevel(self): | |
37 | if self.auth is None: | |
38 | self.glevel = -1 | |
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: | |
44 | self.glevel = row['level'] | |
45 | else: | |
46 | self.glevel = 0 | |
47 | return self.glevel | |
48 | ||
49 | def __str__(self): return self.nick | |
50 | def __repr__(self): return "<User %r (%d)>" % (self.nick,self.glevel) | |
51 | ||
52 | class Channel(object): | |
53 | users = [] | |
54 | voices = [] | |
55 | ops = [] | |
56 | ||
57 | def __init__(self, name): | |
58 | self.name = name | |
59 | ||
60 | def userjoin(self, user, level=None): | |
61 | if user not in self.users: self.users.append(user) | |
62 | if level == 'op' and user not in self.ops: self.ops.append(user) | |
63 | if level == 'voice' and user not in self.voices: self.voices.append(user) | |
64 | def userpart(self, user): | |
65 | if user in self.ops: self.ops.remove(user) | |
66 | if user in self.voices: self.voices.remove(user) | |
67 | if user in self.users: self.users.remove(user) | |
68 | ||
69 | def userop(self, user): | |
70 | if user in self.users and user not in self.ops: self.ops.append(user) | |
71 | def uservoice(self, user): | |
72 | if user in self.users and user not in self.voices: self.voices.append(user) | |
73 | def userdeop(self, user): | |
74 | if user in self.ops: self.ops.remove(user) | |
75 | def userdevoice(self, user): | |
76 | if user in self.voices: self.voices.remove(user) | |
77 | ||
78 | def __str__(self): return self.name | |
79 | def __repr__(self): return "<Channel %r>" % (self.name) | |
80 | ||
81 | def __init__(self, trigger): | |
82 | self.trigger = trigger | |
83 | if os.name == "posix": | |
84 | self.potype = "poll" | |
85 | self.po = select.poll() | |
86 | else: # f.e. os.name == "nt" (Windows) | |
87 | self.potype = "select" | |
88 | self.fdlist = [] | |
89 | ||
90 | def newbot(self, nick, user, bind, server, port, realname, chans): | |
91 | if bind is None: bind = '' | |
92 | obj = bot.Bot(self, nick, user, bind, server, port, realname, chans) | |
93 | self.bots[nick.lower()] = obj | |
94 | ||
95 | def newfd(self, obj, fileno): | |
96 | self.fds[fileno] = obj | |
97 | if self.potype == "poll": | |
98 | self.po.register(fileno, select.POLLIN) | |
99 | elif self.potype == "select": | |
100 | self.fdlist.append(fileno) | |
101 | ||
102 | def bot(self, name): #get Bot() by name (nick) | |
103 | return self.bots[name.lower()] | |
104 | def fd(self, fileno): #get Bot() by fd/fileno | |
105 | return self.fds[fileno] | |
106 | ||
107 | def user(self, nick): | |
108 | nick = nick.lower() | |
109 | if nick in self.users: | |
110 | return self.users[nick] | |
111 | else: | |
112 | user = self.User(nick) | |
113 | self.users[nick] = user | |
114 | return user | |
115 | def channel(self, name): #TODO #get Channel() by name | |
116 | return self.Channel(name.lower()) | |
117 | ||
118 | def poll(self): | |
119 | if self.potype == "poll": | |
120 | return [fd for (fd, ev) in self.po.poll()] | |
121 | elif self.potype == "select": | |
122 | return select.select(self.fdlist, [], [])[0] | |
123 | ||
124 | def connectall(self): | |
125 | for bot in self.bots.itervalues(): | |
126 | if bot.conn.state == 0: | |
127 | bot.connect() | |
128 | ||
129 | #bind functions | |
130 | def hook(self, word, handler): | |
131 | self.msghandlers[word] = handler | |
132 | def unhook(self, word): | |
133 | del self.msghandlers[word] | |
134 | def hashook(self, word): | |
135 | return word in self.msghandlers | |
136 | def gethook(self, word): | |
137 | return self.msghandlers[word] | |
138 | ||
139 | def setup(): | |
140 | global cfg, main | |
141 | ||
142 | cfg = config.Config('bot.config') | |
143 | main = Erebus(cfg.trigger) | |
144 | ||
145 | autoloads = [mod for mod, yes in cfg.items('autoloads') if int(yes) == 1] | |
146 | print autoloads | |
147 | for mod in autoloads: | |
148 | ctlmod.load(main, mod) | |
149 | ||
150 | main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MySQLdb.cursors.DictCursor) | |
151 | c = main.db.cursor() | |
152 | c.execute("SELECT nick, user, bind FROM bots WHERE active = 1") | |
153 | rows = c.fetchall() | |
154 | c.close() | |
155 | for row in rows: | |
156 | c2 = main.db.cursor() | |
157 | c2.execute("SELECT chname FROM chans WHERE bot = %s AND active = 1", (row['nick'],)) | |
158 | chans = [chdic['chname'] for chdic in c2.fetchall()] | |
159 | c2.close() | |
160 | main.newbot(row['nick'], row['user'], row['bind'], cfg.host, cfg.port, cfg.realname, chans) | |
161 | main.connectall() | |
162 | ||
163 | def loop(): | |
164 | poready = main.poll() | |
165 | for fileno in poready: | |
166 | main.fd(fileno).getdata() | |
167 | ||
168 | if __name__ == '__main__': | |
169 | setup() | |
170 | while True: loop() |