]>
Commit | Line | Data |
---|---|---|
b25d4368 | 1 | #!/usr/bin/python |
2 | ||
3 | #TODO: tons | |
4 | ||
a12f7519 | 5 | import os, sys, select, MySQLdb, MySQLdb.cursors |
6 | import bot, config | |
b25d4368 | 7 | |
8 | class Erebus(object): | |
49a455aa | 9 | bots = {} |
10 | fds = {} | |
11 | mods = {} | |
12 | msghandlers = {} | |
13 | ||
14 | class User(object): | |
15 | chans = [] | |
a4eacae2 | 16 | |
49a455aa | 17 | def __init__(self, nick, auth=None): |
18 | self.nick = nick | |
19 | self.auth = auth | |
a4eacae2 | 20 | |
49a455aa | 21 | if auth is not None: |
22 | self.checklevel() | |
a4eacae2 | 23 | |
49a455aa | 24 | def authed(self, auth): |
25 | self.auth = auth | |
26 | self.checklevel() | |
a4eacae2 | 27 | |
49a455aa | 28 | def checklevel(self): self.level = 9999 #TODO get level from db |
43b98e4e | 29 | |
49a455aa | 30 | def __str__(self): return self.nick |
31 | def __repr__(self): return "<User %r>" % (self.nick) | |
43b98e4e | 32 | |
49a455aa | 33 | class Channel(object): |
34 | users = [] | |
35 | voices = [] | |
36 | ops = [] | |
a4eacae2 | 37 | |
49a455aa | 38 | def __init__(self, name): |
39 | self.name = name | |
a4eacae2 | 40 | |
49a455aa | 41 | def userjoin(self, user, level=None): |
42 | if user not in self.users: self.users.append(user) | |
43 | if level == 'op' and user not in self.ops: self.ops.append(user) | |
44 | if level == 'voice' and user not in self.voices: self.voices.append(user) | |
45 | def userpart(self, user): | |
46 | if user in self.ops: self.ops.remove(user) | |
47 | if user in self.voices: self.voices.remove(user) | |
48 | if user in self.users: self.users.remove(user) | |
a4eacae2 | 49 | |
49a455aa | 50 | def userop(self, user): |
51 | if user in self.users and user not in self.ops: self.ops.append(user) | |
52 | def uservoice(self, user): | |
53 | if user in self.users and user not in self.voices: self.voices.append(user) | |
54 | def userdeop(self, user): | |
55 | if user in self.ops: self.ops.remove(user) | |
56 | def userdevoice(self, user): | |
57 | if user in self.voices: self.voices.remove(user) | |
58 | ||
59 | def __str__(self): return self.name | |
60 | def __repr__(self): return "<Channel %r>" % (self.name) | |
61 | ||
62 | def __init__(self): | |
fd96a423 | 63 | if os.name == "posix": |
64 | self.potype = "poll" | |
65 | self.po = select.poll() | |
66 | else: # f.e. os.name == "nt" (Windows) | |
67 | self.potype = "select" | |
68 | self.fdlist = [] | |
49a455aa | 69 | |
70 | def newbot(self, nick, user, bind, server, port, realname, chans): | |
71 | if bind is None: bind = '' | |
72 | obj = bot.Bot(self, nick, user, bind, server, port, realname, chans) | |
73 | self.bots[nick.lower()] = obj | |
a4eacae2 | 74 | |
49a455aa | 75 | def newfd(self, obj, fileno): |
49a455aa | 76 | self.fds[fileno] = obj |
fd96a423 | 77 | if self.potype == "poll": |
78 | self.po.register(fileno, select.POLLIN) | |
79 | elif self.potype == "select": | |
80 | self.fdlist.append(fileno) | |
a4eacae2 | 81 | |
43b98e4e | 82 | def bot(self, name): #get Bot() by name (nick) |
49a455aa | 83 | return self.bots[name.lower()] |
43b98e4e | 84 | def fd(self, fileno): #get Bot() by fd/fileno |
49a455aa | 85 | return self.fds[fileno] |
86 | ||
43b98e4e | 87 | def user(self, nick): #TODO #get User() by nick |
49a455aa | 88 | return self.User(nick.lower()) |
43b98e4e | 89 | def channel(self, name): #TODO #get Channel() by name |
49a455aa | 90 | return self.Channel(name.lower()) |
91 | ||
92 | def poll(self): | |
fd96a423 | 93 | if self.potype == "poll": |
94 | return [fd for (fd, ev) in self.po.poll()] | |
95 | elif self.potype == "select": | |
96 | return select.select(self.fdlist, [], [])[0] | |
49a455aa | 97 | |
98 | def connectall(self): | |
99 | for bot in self.bots.itervalues(): | |
100 | if bot.conn.state == 0: | |
101 | bot.connect() | |
102 | ||
103 | #module functions | |
104 | def modlist(self): pass | |
105 | def hasmod(self, name): pass | |
106 | def loadmod(self, name): pass | |
107 | def unloadmod(self, name): pass | |
108 | def reloadmod(self, name): pass | |
109 | ||
110 | #bind functions | |
111 | def bind(self, word, handler): pass | |
112 | def addbind(self, word, handler): pass | |
113 | def rmbind(self, word, handler): pass | |
114 | def getbind(self, word, handler): pass | |
115 | ||
a12f7519 | 116 | cfg = config.Config('bot.config') |
b25d4368 | 117 | main = Erebus() |
b25d4368 | 118 | |
119 | def setup(): | |
a12f7519 | 120 | main.db = MySQLdb.connect(host=cfg.dbhost, user=cfg.dbuser, passwd=cfg.dbpass, db=cfg.dbname, cursorclass=MySQLdb.cursors.DictCursor) |
121 | c = main.db.cursor() | |
122 | c.execute("SELECT nick, user, bind FROM bots WHERE active = 1") | |
123 | rows = c.fetchall() | |
124 | c.close() | |
125 | for row in rows: | |
126 | c2 = main.db.cursor() | |
127 | c2.execute("SELECT chname FROM chans WHERE bot = %s AND active = 1", (row['nick'],)) | |
128 | chans = [chdic['chname'] for chdic in c2.fetchall()] | |
129 | c2.close() | |
130 | main.newbot(row['nick'], row['user'], row['bind'], cfg.host, cfg.port, cfg.realname, chans) | |
131 | main.connectall() | |
b25d4368 | 132 | |
133 | def loop(): | |
49a455aa | 134 | poready = main.poll() |
fd96a423 | 135 | for fileno in poready: |
49a455aa | 136 | main.fd(fileno).getdata() |
b25d4368 | 137 | |
138 | if __name__ == '__main__': | |
139 | setup() | |
49a455aa | 140 | while True: loop() |