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