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