]>
jfr.im git - erebus.git/blob - bot.py
3 # Erebus IRC bot - Author: John Runyon
4 # "Bot" and "BotConnection" classes (handling a specific "arm")
6 import socket
, sys
, time
, threading
, os
, random
7 from collections
import deque
9 #bots = {'erebus': bot.Bot(nick='Erebus', user='erebus', bind='', server='irc.quakenet.org', port=6667, realname='Erebus')}
11 def __init__(self
, parent
, nick
, user
, bind
, authname
, authpass
, server
, port
, realname
):
16 self
.realname
= realname
18 self
.authname
= authname
19 self
.authpass
= authpass
21 curs
= self
.parent
.db
.cursor()
22 if curs
.execute("SELECT chname FROM chans WHERE bot = %s AND active = 1", (self
.nick
,)):
23 chansres
= curs
.fetchall()
25 self
.chans
= [self
.parent
.newchannel(self
, row
['chname']) for row
in chansres
]
27 self
.conn
= BotConnection(self
, bind
, server
, port
)
29 self
.msgqueue
= deque()
30 self
.slowmsgqueue
= deque()
34 curs
= self
.parent
.db
.cursor()
35 curs
.execute("UPDATE bots SET connected = 0 WHERE nick = %s", (self
.nick
,))
40 if self
.conn
.connect():
41 self
.parent
.newfd(self
, self
.conn
.socket
.fileno())
44 return self
.conn
.read()
46 def _checknick(self
): # check if we're using the right nick, try changing
47 if self
.nick
!= self
.permnick
and self
.conn
.registered():
48 self
.conn
.send("NICK %s" % (self
.permnick
))
50 def parse(self
, line
):
54 zero
= { #things to look for without source
55 'NOTICE': self
._gotconnected
,
56 'PING': self
._gotping
,
57 'ERROR': self
._goterror
,
59 one
= { #things to look for after source
61 '376': self
._gotRegistered
,
62 '422': self
._gotRegistered
,
63 'PRIVMSG': self
._gotprivmsg
,
64 '353': self
._got
353, #NAMES
65 '354': self
._got
354, #WHO
66 '433': self
._got
433, #nick in use
67 'JOIN': self
._gotjoin
,
68 'PART': self
._gotpart
,
69 'QUIT': self
._gotquit
,
70 'NICK': self
._gotnick
,
71 'MODE': self
._gotmode
,
74 if self
.parent
.hasnumhook(pieces
[1]):
75 hooks
= self
.parent
.getnumhook(pieces
[1])
76 for callback
in hooks
:
80 self
.__debug
_cbexception
("numhook", line
)
83 zero
[pieces
[0]](pieces
)
84 elif pieces
[1] in one
:
85 one
[pieces
[1]](pieces
)
87 def _gotconnected(self
, pieces
):
88 if not self
.conn
.registered():
90 def _gotping(self
, pieces
):
91 self
.conn
.send("PONG %s" % (pieces
[1]))
93 def _goterror(self
, pieces
): #TODO handle more gracefully
94 curs
= self
.parent
.db
.cursor()
95 curs
.execute("UPDATE bots SET connected = 0")
99 def _got001(self
, pieces
):
100 pass # wait until the end of MOTD instead
101 def _gotRegistered(self
, pieces
):
102 self
.conn
.registered(True)
104 curs
= self
.parent
.db
.cursor()
105 curs
.execute("UPDATE bots SET connected = 1 WHERE nick = %s", (self
.nick
,))
108 self
.conn
.send("MODE %s +x" % (pieces
[2]))
109 if self
.authname
is not None and self
.authpass
is not None:
110 self
.conn
.send("AUTH %s %s" % (self
.authname
, self
.authpass
))
113 def _gotprivmsg(self
, pieces
):
114 nick
= pieces
[0].split('!')[0][1:]
115 user
= self
.parent
.user(nick
)
117 msg
= ' '.join(pieces
[3:])[1:]
118 self
.parsemsg(user
, target
, msg
)
119 def _got353(self
, pieces
):
120 chan
= self
.parent
.channel(pieces
[4])
122 names
[0] = names
[0][1:] #remove colon
124 user
= self
.parent
.user(n
.lstrip('@+'))
126 chan
.userjoin(user
, 'op')
128 chan
.userjoin(user
, 'voice')
132 def _got354(self
, pieces
):
135 nick
, auth
= pieces
[4:6]
138 chan
, nick
, auth
= pieces
[4:7]
139 chan
= self
.parent
.channel(chan
)
140 user
= self
.parent
.user(nick
)
147 if qt
== 2: # triggered by !auth
150 self
.msg(nick
, "You are now known as #%s (access level: %s)" % (auth
, user
.glevel
))
152 self
.msg(nick
, "You are now known as #%s (not staff)" % (auth
))
154 self
.msg(nick
, "I tried, but you're not authed!")
155 def _got433(self
, pieces
):
156 if not self
.conn
.registered(): #we're trying to connect
157 newnick
= "%s%d" % (self
.nick
, random
.randint(111,999))
158 self
.conn
.send("NICK %s" % (newnick
))
160 def _gotjoin(self
, pieces
):
161 nick
= pieces
[0].split('!')[0][1:]
162 chan
= self
.parent
.channel(pieces
[2])
164 if nick
== self
.nick
:
165 self
.conn
.send("WHO %s c%%cant,3" % (chan
))
167 user
= self
.parent
.user(nick
, justjoined
=True)
170 def _gotpart(self
, pieces
):
171 nick
= pieces
[0].split('!')[0][1:]
172 chan
= self
.parent
.channel(pieces
[2])
174 if nick
!= self
.nick
:
175 gone
= self
.parent
.user(nick
).part(chan
)
176 chan
.userpart(self
.parent
.user(nick
))
178 self
.parent
.user(nick
).quit()
179 del self
.parent
.users
[nick
.lower()]
180 def _gotquit(self
, pieces
):
181 nick
= pieces
[0].split('!')[0][1:]
182 if nick
!= self
.nick
:
183 for chan
in self
.parent
.user(nick
).chans
:
184 chan
.userpart(self
.parent
.user(nick
))
185 self
.parent
.user(nick
).quit()
186 del self
.parent
.users
[nick
.lower()]
187 def _gotnick(self
, pieces
):
188 oldnick
= pieces
[0].split('!')[0][1:]
189 newnick
= pieces
[2][1:]
190 if newnick
.lower() != oldnick
.lower():
191 self
.parent
.users
[newnick
.lower()] = self
.parent
.users
[oldnick
.lower()]
192 del self
.parent
.users
[oldnick
.lower()]
193 self
.parent
.users
[newnick
.lower()].nickchange(newnick
)
194 def _gotmode(self
, pieces
):
195 source
= pieces
[0].split('!')[0][1:]
196 chan
= self
.parent
.channel(pieces
[2])
208 chan
.userop(self
.parent
.user(args
.pop(0)))
210 chan
.userdeop(self
.parent
.user(args
.pop(0)))
213 chan
.uservoice(self
.parent
.user(args
.pop(0)))
215 chan
.userdevoice(self
.parent
.user(args
.pop(0)))
217 pass # don't care about other modes
219 def __debug_cbexception(self
, source
, *args
, **kwargs
):
220 if int(self
.parent
.cfg
.get('debug', 'cbexc', default
=0)) == 1:
221 self
.conn
.send("PRIVMSG %s :%09.3f ^C4^B!!!^B^C CBEXC %s" % (self
.parent
.cfg
.get('debug', 'owner'), time
.time() % 100000, source
))
222 __import__('traceback').print_exc()
223 print "%09.3f %s [!] CBEXC %s %r %r" % (time
.time() % 100000, self
.nick
, source
, args
, kwargs
)
226 def parsemsg(self
, user
, target
, msg
):
231 triggerused
= msg
[0] == self
.parent
.trigger
232 if triggerused
: msg
= msg
[1:]
235 if target
== self
.nick
:
236 if msg
[0] == "\001": #ctcp
237 msg
= msg
.strip("\001")
239 self
.msg(user
, "\001VERSION Erebus v%d.%d - http://github.com/zonidjan/erebus" % (self
.parent
.APIVERSION
, self
.parent
.RELEASE
))
243 if chanword
[0] == '#':
244 chan
= self
.parent
.channel(chanword
)
245 if chan
is not None: #if chan is still none, there's no bot on "chanword", and chanword is used as a parameter.
248 else: # message was sent to a channel
249 chan
= self
.parent
.channel(target
)
251 if msg
[0] == '*': # message may be addressed to bot by "*BOTNICK" trigger?
252 if pieces
[0][1:].lower() == self
.nick
.lower():
253 pieces
.pop(0) # command actually starts with next word
254 msg
= ' '.join(pieces
) # command actually starts with next word
255 elif not triggerused
:
256 if self
.parent
.haschanhook(target
.lower()):
257 for callback
in self
.parent
.getchanhook(target
.lower()):
259 cbret
= callback(self
, user
, chan
, *pieces
)
260 except NotImplementedError:
261 self
.msg(user
, "Command not implemented.")
263 self
.msg(user
, "Command failed. Code: CBEXC%09.3f" % (time
.time() % 100000))
264 self
.__debug
_cbexception
("chanhook", user
=user
, target
=target
, msg
=msg
)
265 return # not to bot, don't process!
267 return # "message" is empty
269 cmd
= pieces
[0].lower()
271 if self
.parent
.hashook(cmd
):
272 for callback
in self
.parent
.gethook(cmd
):
273 if chan
is None and callback
.needchan
:
274 self
.msg(user
, "You need to specify a channel for that command.")
275 elif user
.glevel
>= callback
.reqglevel
and (not callback
.needchan
or chan
.levelof(user
.auth
) >= callback
.reqclevel
):
277 cbret
= callback(self
, user
, chan
, target
, *pieces
[1:])
278 except NotImplementedError:
279 self
.msg(user
, "Command not implemented.")
281 self
.msg(user
, "Command failed. Code: CBEXC%09.3f" % (time
.time() % 100000))
282 self
.__debug
_cbexception
("hook", user
=user
, target
=target
, msg
=msg
)
283 except SystemExit as e
:
284 curs
= self
.parent
.db
.cursor()
285 curs
.execute("UPDATE bots SET connected = 0")
289 def __debug_nomsg(self
, target
, msg
):
290 if int(self
.parent
.cfg
.get('debug', 'nomsg', default
=0)) == 1:
291 self
.conn
.send("PRIVMSG %s :%09.3f \ 34\ 2!!!\ 2\ 3 NOMSG %r, %r" % (self
.parent
.cfg
.get('debug', 'owner'), time
.time() % 100000, target
, msg
))
292 print "%09.3f %s [!] %s" % (time
.time() % 100000, self
.nick
, "!!! NOMSG")
293 __import__('traceback').print_stack()
295 def msg(self
, target
, msg
):
296 if target
is None or msg
is None:
297 return self
.__debug
_nomsg
(target
, msg
)
299 self
.msgqueue
.append((target
, msg
))
300 if not self
.msgtimer
.is_alive():
301 self
.msgtimer
.start()
303 def slowmsg(self
, target
, msg
):
304 if target
is None or msg
is None:
305 return self
.__debug
_nomsg
(target
, msg
)
307 self
.slowmsgqueue
.append((target
, msg
))
308 if not self
.msgtimer
.is_alive():
309 self
.msgtimer
.start()
311 def fastmsg(self
, target
, msg
):
312 if target
is None or msg
is None:
313 return self
.__debug
_nomsg
(target
, msg
)
317 if target
[0] == '#': command
= "PRIVMSG %s :%s" % (target
, msg
)
318 else: command
= "NOTICE %s :%s" % (target
, msg
)
320 self
.conn
.send(command
)
326 self
.fastmsg(*self
.msgqueue
.popleft())
327 self
.msgtimer
.start()
330 self
.fastmsg(*self
.slowmsgqueue
.popleft())
331 self
.msgtimer
.start()
335 def makemsgtimer(self
):
336 self
.msgtimer
= threading
.Timer(2, self
._popmsg
)
337 self
.msgtimer
.daemon
= True
339 def join(self
, chan
):
340 self
.conn
.send("JOIN %s" % (chan
))
342 def part(self
, chan
):
343 self
.conn
.send("PART %s" % (chan
))
345 def quit(self
, reason
="Shutdown"):
346 self
.conn
.send("QUIT :%s" % (reason
))
348 def __str__(self
): return self
.nick
349 def __repr__(self
): return "<Bot %r>" % (self
.nick
)
351 class BotConnection(object):
352 def __init__(self
, parent
, bind
, server
, port
):
359 self
.port
= int(port
)
361 self
.state
= 0 # 0=disconnected, 1=registering, 2=connected
364 self
.socket
= socket
.socket(socket
.AF_INET
, socket
.SOCK_STREAM
)
365 self
.socket
.bind((self
.bind
, 0))
366 self
.socket
.connect((self
.server
, self
.port
))
370 self
.send("NICK %s" % (self
.parent
.nick
))
371 self
.send("USER %s 0 * :%s" % (self
.parent
.user
, self
.parent
.realname
))
375 def registered(self
, done
=False):
376 if done
: self
.state
= 2
377 return self
.state
== 2
379 def send(self
, line
):
380 print "%09.3f %s [O] %s" % (time
.time() % 100000, self
.parent
.nick
, line
)
383 def _write(self
, line
):
384 self
.socket
.sendall(line
+"\r\n")
387 self
.buffer += self
.socket
.recv(8192)
390 while "\r\n" in self
.buffer:
391 pieces
= self
.buffer.split("\r\n", 1)
392 print "%09.3f %s [I] %s" % (time
.time() % 100000, self
.parent
.nick
, pieces
[0])
393 # print (time.time() % 1460000000), self.parent.nick, '[I]', pieces[0]
394 lines
.append(pieces
[0])
395 self
.buffer = pieces
[1]
399 def __str__(self
): return self
.parent
.nick
400 def __repr__(self
): return "<BotConnection %r (%r)>" % (self
.socket
.fileno(), self
.parent
.nick
)