]>
Commit | Line | Data |
---|---|---|
b25d4368 | 1 | #!/usr/bin/python |
2 | ||
931c88a4 | 3 | # Erebus IRC bot - Author: John Runyon |
4 | # "Bot" and "BotConnection" classes (handling a specific "arm") | |
5 | ||
b25d4368 | 6 | #TODO: error checking |
7 | ||
49a455aa | 8 | import socket, sys |
b25d4368 | 9 | |
10 | #bots = {'erebus': bot.Bot(nick='Erebus', user='erebus', bind='', server='irc.quakenet.org', port=6667, realname='Erebus')} | |
11 | class Bot(object): | |
0af282c6 | 12 | def __init__(self, parent, nick, user, bind, authname, authpass, server, port, realname): |
b25d4368 | 13 | self.parent = parent |
14 | self.nick = nick | |
a12f7519 | 15 | self.user = user |
16 | self.realname = realname | |
5477b368 | 17 | |
0af282c6 | 18 | self.authname = authname |
19 | self.authpass = authpass | |
20 | ||
586997a7 | 21 | curs = self.parent.db.cursor() |
4fa1118b | 22 | if curs.execute("SELECT chname FROM chans WHERE bot = %s AND active = 1", (self.nick,)): |
23 | chansres = curs.fetchall() | |
24 | curs.close() | |
25 | self.chans = [self.parent.newchannel(self, row['chname']) for row in chansres] | |
b25d4368 | 26 | |
a12f7519 | 27 | self.conn = BotConnection(self, bind, server, port) |
b25d4368 | 28 | def connect(self): |
b2a896c8 | 29 | if self.conn.connect(): |
49a455aa | 30 | self.parent.newfd(self, self.conn.socket.fileno()) |
31 | ||
b25d4368 | 32 | def getdata(self): |
d1ea2946 | 33 | return self.conn.read() |
a4eacae2 | 34 | |
b25d4368 | 35 | def parse(self, line): |
36 | pieces = line.split() | |
a4eacae2 | 37 | |
d1ea2946 | 38 | if not self.conn.registered() and pieces[0] == "NOTICE": |
e4a4c762 | 39 | self.conn.register() |
40 | return | |
d1ea2946 | 41 | |
e4a4c762 | 42 | if self.parent.hasnumhook(pieces[1]): |
43 | hooks = self.parent.getnumhook(pieces[1]) | |
44 | for callback in hooks: | |
45 | callback(self, line) | |
46 | ||
47 | if pieces[1] == "001": | |
d1ea2946 | 48 | self.conn.registered(True) |
0af282c6 | 49 | self.conn.send("MODE %s +x" % (pieces[2])) |
50 | if self.authname is not None and self.authpass is not None: | |
51 | self.conn.send("AUTH %s %s" % (self.authname, self.authpass)) | |
d1ea2946 | 52 | for c in self.chans: |
5477b368 | 53 | self.join(c.name) |
d1ea2946 | 54 | |
55 | elif pieces[1] == "PRIVMSG": | |
49a455aa | 56 | nick = pieces[0].split('!')[0][1:] |
57 | user = self.parent.user(nick) | |
839d2b35 | 58 | target = pieces[2] |
49a455aa | 59 | msg = ' '.join(pieces[3:])[1:] |
839d2b35 | 60 | self.parsemsg(user, target, msg) |
a4eacae2 | 61 | |
49a455aa | 62 | elif pieces[0] == "PING": |
63 | self.conn.send("PONG %s" % (pieces[1])) | |
a4eacae2 | 64 | |
b2a896c8 | 65 | elif pieces[1] == "354": # WHOX |
66 | qt = pieces[3] | |
67 | nick = pieces[4] | |
68 | auth = pieces[5] | |
de89db13 | 69 | self.parent.user(nick).authed(auth) |
b2a896c8 | 70 | |
49a455aa | 71 | elif pieces[1] == "JOIN": |
72 | nick = pieces[0].split('!')[0][1:] | |
b2a896c8 | 73 | chan = self.parent.channel(pieces[2]) |
74 | ||
75 | if nick == self.nick: | |
fadbf980 | 76 | self.conn.send("WHO %s c%%ant,1" % (chan)) |
b2a896c8 | 77 | else: |
8af0407d | 78 | user = self.parent.user(nick, justjoined=True) |
5477b368 | 79 | chan.userjoin(user) |
80 | user.join(chan) | |
a5ceff24 | 81 | |
82 | elif pieces[1] == "PART": | |
83 | nick = pieces[0].split('!')[0][1:] | |
84 | chan = self.parent.channel(pieces[2]) | |
49a455aa | 85 | |
a5ceff24 | 86 | if nick != self.nick: |
87 | self.parent.user(nick).part(chan) | |
88 | chan.userpart(self.parent.user(nick)) | |
89 | ||
90 | elif pieces[1] == "QUIT": | |
91 | nick = pieces[0].split('!')[0][1:] | |
92 | if nick != self.nick: | |
93 | self.parent.user(nick).quit() | |
94 | del self.parent.users[nick.lower()] | |
95 | ||
96 | elif pieces[1] == "MODE": #TODO | |
97 | pass | |
98 | ||
99 | ||
839d2b35 | 100 | def parsemsg(self, user, target, msg): |
101 | chan = None | |
877cd61d | 102 | if len(msg) == 0: |
103 | return | |
104 | ||
105 | triggerused = msg[0] == self.parent.trigger | |
839d2b35 | 106 | if triggerused: msg = msg[1:] |
49a455aa | 107 | pieces = msg.split() |
839d2b35 | 108 | |
109 | if target == self.nick: | |
a76c4bd8 | 110 | if msg[0] == "\001": #ctcp |
111 | msg = msg.strip("\001") | |
112 | if msg == "VERSION": | |
113 | self.msg(user, "\001VERSION Erebus v%d.%d - http://github.com/zonidjan/erebus" % (self.parent.APIVERSION, self.parent.RELEASE)) | |
114 | return | |
d1ea2946 | 115 | if len(pieces) > 1: |
116 | chanword = pieces[1] | |
117 | if chanword[0] == '#': | |
118 | chan = self.parent.channel(chanword) | |
f5e736ee | 119 | if chan is not None: #if chan is still none, there's no bot on "chanword", and chanword is used as a parameter. |
120 | pieces.pop(1) | |
839d2b35 | 121 | |
122 | else: # message was sent to a channel | |
123 | chan = self.parent.channel(target) #TODO check if bot's on channel --- in Erebus.channel() maybe? | |
90b64dc0 CS |
124 | try: |
125 | if msg[0] == '*': # message may be addressed to bot by "*BOTNICK" trigger? | |
126 | if pieces[0][1:].lower() == self.nick.lower(): | |
127 | pieces.pop(0) # command actually starts with next word | |
128 | msg = ' '.join(pieces) # command actually starts with next word | |
129 | elif not triggerused: | |
9557ee54 | 130 | if self.parent.haschanhook(target.lower()): |
2a1a69a6 | 131 | for callback in self.parent.getchanhook(target.lower()): |
9557ee54 | 132 | cbret = callback(self, user, chan, *pieces) |
2a1a69a6 | 133 | if cbret is NotImplemented: |
134 | self.msg(user, "Command not implemented.") | |
a76c4bd8 | 135 | return # not to bot, don't process! |
90b64dc0 | 136 | except IndexError: |
a76c4bd8 | 137 | return # "message" is empty |
839d2b35 | 138 | |
db50981b | 139 | cmd = pieces[0].lower() |
a4eacae2 | 140 | |
db50981b | 141 | if self.parent.hashook(cmd): |
e4a4c762 | 142 | for callback in self.parent.gethook(cmd): |
143 | if chan is None and callback.needchan: | |
144 | self.msg(user, "You need to specify a channel for that command.") | |
586997a7 | 145 | elif user.glevel >= callback.reqglevel and (not callback.needchan or chan.levelof(user.auth) >= callback.reqclevel): |
e4a4c762 | 146 | cbret = callback(self, user, chan, target, *pieces[1:]) |
147 | if cbret is NotImplemented: | |
148 | self.msg(user, "Command not implemented.") | |
49a455aa | 149 | |
150 | def msg(self, target, msg): | |
a83e1f9c | 151 | if target is None or msg is None: return |
152 | ||
49a455aa | 153 | if isinstance(target, self.parent.User): self.conn.send("NOTICE %s :%s" % (target.nick, msg)) |
154 | elif isinstance(target, self.parent.Channel): self.conn.send("PRIVMSG %s :%s" % (target.name, msg)) | |
155 | elif isinstance(target, basestring): | |
156 | if target[0] == '#': self.conn.send("PRIVMSG %s :%s" % (target, msg)) | |
157 | else: self.conn.send("NOTICE %s :%s" % (target, msg)) | |
158 | else: raise TypeError('Bot.msg() "target" must be Erebus.User, Erebus.Channel, or string') | |
a4eacae2 | 159 | |
49a455aa | 160 | def join(self, chan): |
161 | self.conn.send("JOIN %s" % (chan)) | |
a4eacae2 | 162 | |
49a455aa | 163 | def part(self, chan): |
164 | self.conn.send("PART %s" % (chan)) | |
a4eacae2 | 165 | |
49a455aa | 166 | def quit(self, reason="Shutdown"): |
167 | self.conn.send("QUIT :%s" % (reason)) | |
b25d4368 | 168 | |
a12f7519 | 169 | def __str__(self): return self.nick |
170 | def __repr__(self): return "<Bot %r>" % (self.nick) | |
171 | ||
b25d4368 | 172 | class BotConnection(object): |
173 | state = 0 # 0=disconnected, 1=registering, 2=connected | |
174 | ||
a12f7519 | 175 | def __init__(self, parent, bind, server, port): |
b25d4368 | 176 | self.parent = parent |
177 | self.buffer = '' | |
178 | self.socket = None | |
179 | ||
b25d4368 | 180 | self.bind = bind |
181 | self.server = server | |
182 | self.port = int(port) | |
b25d4368 | 183 | |
184 | def connect(self): | |
185 | self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
186 | self.socket.bind((self.bind, 0)) | |
187 | self.socket.connect((self.server, self.port)) | |
d1ea2946 | 188 | return True |
189 | def register(self): | |
190 | if self.state == 0: | |
191 | self.send("NICK %s" % (self.parent.nick)) | |
192 | self.send("USER %s 0 * :%s" % (self.parent.user, self.parent.realname)) | |
193 | self.state = 1 | |
49a455aa | 194 | return True |
b25d4368 | 195 | |
196 | def registered(self, done=False): | |
197 | if done: self.state = 2 | |
198 | return self.state == 2 | |
199 | ||
b25d4368 | 200 | #TODO: rewrite send() to queue |
201 | def send(self, line): | |
381f9fc4 | 202 | print self.parent.nick, '[O]', str(line) |
b25d4368 | 203 | self.write(line) |
a4eacae2 | 204 | |
b25d4368 | 205 | def write(self, line): |
b25d4368 | 206 | self.socket.sendall(line+"\r\n") |
a4eacae2 | 207 | |
b25d4368 | 208 | def read(self): |
209 | self.buffer += self.socket.recv(8192) | |
210 | lines = [] | |
a4eacae2 | 211 | |
b25d4368 | 212 | while '\r\n' in self.buffer: |
213 | pieces = self.buffer.split('\r\n', 1) | |
d1ea2946 | 214 | print self.parent.nick, '[I]', pieces[0] |
b25d4368 | 215 | lines.append(pieces[0]) |
216 | self.buffer = pieces[1] | |
a4eacae2 | 217 | |
b25d4368 | 218 | return lines |
a12f7519 | 219 | |
220 | def __str__(self): return self.nick | |
221 | def __repr__(self): return "<BotConnection %r (%r)>" % (self.socket.fileno(), self.parent.nick) |