]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python | |
2 | ||
3 | # Erebus IRC bot - Author: John Runyon | |
4 | # "Bot" and "BotConnection" classes (handling a specific "arm") | |
5 | ||
6 | #TODO: error checking | |
7 | ||
8 | import socket, sys | |
9 | ||
10 | #bots = {'erebus': bot.Bot(nick='Erebus', user='erebus', bind='', server='irc.quakenet.org', port=6667, realname='Erebus')} | |
11 | class Bot(object): | |
12 | def __init__(self, parent, nick, user, bind, server, port, realname): | |
13 | self.parent = parent | |
14 | self.nick = nick | |
15 | self.user = user | |
16 | self.realname = realname | |
17 | ||
18 | self.chans = [] | |
19 | ||
20 | chcurs = self.parent.db.cursor() | |
21 | chcurs.execute("SELECT chname FROM chans WHERE bot = %s AND active = 1", (self.nick,)) | |
22 | chans = chcurs.fetchall() | |
23 | chcurs.close() | |
24 | ||
25 | for chrow in chans: | |
26 | uscurs = self.parent.db.cursor() | |
27 | uscurs.execute("SELECT user, level FROM chusers WHERE chan = %s", (chrow['chname'],)) | |
28 | usrow = uscurs.fetchone() | |
29 | levels = {} | |
30 | while usrow is not None: | |
31 | levels[usrow['user']] = -usrow['level'] | |
32 | usrow = uscurs.fetchone() | |
33 | uscurs.close() | |
34 | self.chans.append(self.parent.newchannel(self, chrow['chname'], levels)) | |
35 | ||
36 | self.conn = BotConnection(self, bind, server, port) | |
37 | def connect(self): | |
38 | if self.conn.connect(): | |
39 | self.parent.newfd(self, self.conn.socket.fileno()) | |
40 | ||
41 | def getdata(self): | |
42 | return self.conn.read() | |
43 | ||
44 | def parse(self, line): | |
45 | pieces = line.split() | |
46 | ||
47 | if not self.conn.registered() and pieces[0] == "NOTICE": | |
48 | self.conn.register() | |
49 | ||
50 | elif pieces[1] == "001": | |
51 | self.conn.registered(True) | |
52 | for c in self.chans: | |
53 | self.join(c.name) | |
54 | ||
55 | elif pieces[1] == "PRIVMSG": | |
56 | nick = pieces[0].split('!')[0][1:] | |
57 | user = self.parent.user(nick) | |
58 | target = pieces[2] | |
59 | msg = ' '.join(pieces[3:])[1:] | |
60 | self.parsemsg(user, target, msg) | |
61 | ||
62 | elif pieces[0] == "PING": | |
63 | self.conn.send("PONG %s" % (pieces[1])) | |
64 | ||
65 | elif pieces[1] == "354": # WHOX | |
66 | qt = pieces[3] | |
67 | nick = pieces[4] | |
68 | auth = pieces[5] | |
69 | if auth != '0': | |
70 | self.parent.user(nick).authed(auth) | |
71 | ||
72 | elif pieces[1] == "JOIN": | |
73 | nick = pieces[0].split('!')[0][1:] | |
74 | chan = self.parent.channel(pieces[2]) | |
75 | ||
76 | if nick == self.nick: | |
77 | self.conn.send("WHO %s %%ant,1" % (chan)) | |
78 | else: | |
79 | user = self.parent.user(nick, justjoined=True) | |
80 | chan.userjoin(user) | |
81 | user.join(chan) | |
82 | ||
83 | def parsemsg(self, user, target, msg): | |
84 | chan = None | |
85 | triggerused = msg[0] == self.parent.trigger | |
86 | if triggerused: msg = msg[1:] | |
87 | pieces = msg.split() | |
88 | ||
89 | if target == self.nick: | |
90 | if len(pieces) > 1: | |
91 | chanword = pieces[1] | |
92 | if chanword[0] == '#': | |
93 | chan = self.parent.channel(chanword) | |
94 | pieces.pop(1) | |
95 | ||
96 | else: # message was sent to a channel | |
97 | chan = self.parent.channel(target) #TODO check if bot's on channel --- in Erebus.channel() maybe? | |
98 | if msg[0] == '*': # message may be addressed to bot by "*BOTNICK" trigger? | |
99 | if pieces[0][1:].lower() == self.nick.lower(): | |
100 | pieces.pop(0) # command actually starts with next word | |
101 | msg = ' '.join(pieces) # command actually starts with next word | |
102 | elif not triggerused: | |
103 | return # not to bot, don't process! | |
104 | ||
105 | cmd = pieces[0].lower() | |
106 | ||
107 | if self.parent.hashook(cmd): | |
108 | callback = self.parent.gethook(cmd) | |
109 | if chan is None and callback.needchan: | |
110 | self.msg(user, "You need to specify a channel for that command.") | |
111 | return | |
112 | if user.glevel >= callback.reqglevel: | |
113 | #TODO TODO TODO check reqclevel | |
114 | cbret = callback(self, user, chan, target, *pieces[1:]) | |
115 | if cbret is NotImplemented: | |
116 | self.msg(user, "Command not implemented.") | |
117 | return | |
118 | ||
119 | self.msg(user, "No such command, or you don't have access.") | |
120 | ||
121 | def msg(self, target, msg): | |
122 | if isinstance(target, self.parent.User): self.conn.send("NOTICE %s :%s" % (target.nick, msg)) | |
123 | elif isinstance(target, self.parent.Channel): self.conn.send("PRIVMSG %s :%s" % (target.name, msg)) | |
124 | elif isinstance(target, basestring): | |
125 | if target[0] == '#': self.conn.send("PRIVMSG %s :%s" % (target, msg)) | |
126 | else: self.conn.send("NOTICE %s :%s" % (target, msg)) | |
127 | else: raise TypeError('Bot.msg() "target" must be Erebus.User, Erebus.Channel, or string') | |
128 | ||
129 | def join(self, chan): | |
130 | self.conn.send("JOIN %s" % (chan)) | |
131 | ||
132 | def part(self, chan): | |
133 | self.conn.send("PART %s" % (chan)) | |
134 | ||
135 | def quit(self, reason="Shutdown"): | |
136 | self.conn.send("QUIT :%s" % (reason)) | |
137 | ||
138 | def __str__(self): return self.nick | |
139 | def __repr__(self): return "<Bot %r>" % (self.nick) | |
140 | ||
141 | class BotConnection(object): | |
142 | state = 0 # 0=disconnected, 1=registering, 2=connected | |
143 | ||
144 | def __init__(self, parent, bind, server, port): | |
145 | self.parent = parent | |
146 | self.buffer = '' | |
147 | self.socket = None | |
148 | ||
149 | self.bind = bind | |
150 | self.server = server | |
151 | self.port = int(port) | |
152 | ||
153 | def connect(self): | |
154 | self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
155 | self.socket.bind((self.bind, 0)) | |
156 | self.socket.connect((self.server, self.port)) | |
157 | return True | |
158 | def register(self): | |
159 | if self.state == 0: | |
160 | self.send("NICK %s" % (self.parent.nick)) | |
161 | self.send("USER %s 0 * :%s" % (self.parent.user, self.parent.realname)) | |
162 | self.state = 1 | |
163 | return True | |
164 | ||
165 | def registered(self, done=False): | |
166 | if done: self.state = 2 | |
167 | return self.state == 2 | |
168 | ||
169 | #TODO: rewrite send() to queue | |
170 | def send(self, line): | |
171 | print self.parent.nick, '[O]', line | |
172 | self.write(line) | |
173 | ||
174 | def write(self, line): | |
175 | self.socket.sendall(line+"\r\n") | |
176 | ||
177 | def read(self): | |
178 | self.buffer += self.socket.recv(8192) | |
179 | lines = [] | |
180 | ||
181 | while '\r\n' in self.buffer: | |
182 | pieces = self.buffer.split('\r\n', 1) | |
183 | print self.parent.nick, '[I]', pieces[0] | |
184 | lines.append(pieces[0]) | |
185 | self.buffer = pieces[1] | |
186 | ||
187 | return lines | |
188 | ||
189 | def __str__(self): return self.nick | |
190 | def __repr__(self): return "<BotConnection %r (%r)>" % (self.socket.fileno(), self.parent.nick) |