+ else: # message was sent to a channel
+ chan = self.parent.channel(target)
+ try:
+ if msg[0] == '*': # message may be addressed to bot by "*BOTNICK" trigger?
+ if pieces[0][1:].lower() == self.nick.lower():
+ pieces.pop(0) # command actually starts with next word
+ msg = ' '.join(pieces) # command actually starts with next word
+ elif not triggerused:
+ if self.parent.haschanhook(target.lower()):
+ for callback in self.parent.getchanhook(target.lower()):
+ cbret = callback(self, user, chan, *pieces)
+ if cbret is NotImplemented:
+ self.msg(user, "Command not implemented.")
+ return # not to bot, don't process!
+ except IndexError:
+ return # "message" is empty
+
+ cmd = pieces[0].lower()
+
+ if self.parent.hashook(cmd):
+ for callback in self.parent.gethook(cmd):
+ if chan is None and callback.needchan:
+ self.msg(user, "You need to specify a channel for that command.")
+ elif user.glevel >= callback.reqglevel and (not callback.needchan or chan.levelof(user.auth) >= callback.reqclevel):
+ cbret = callback(self, user, chan, target, *pieces[1:])
+ if cbret is NotImplemented:
+ self.msg(user, "Command not implemented.")
+
+ def msg(self, target, msg):
+ if target is None or msg is None: return
+
+ self.msgqueue.append((target,msg))
+ if not self.msgtimer.is_alive():
+ self.msgtimer.start()
+
+ def fastmsg(self, target, msg):
+
+ if isinstance(target, self.parent.User): target = target.nick
+ elif isinstance(target, self.parent.Channel): target = target.name
+ elif not isinstance(target, basestring): raise TypeError('Bot.msg() "target" must be Erebus.User, Erebus.Channel, or string')
+
+ if target[0] == '#': command = "PRIVMSG %s :%s" % (target, msg)
+ else: command = "NOTICE %s :%s" % (target, msg)
+
+ self.conn.send(command)
+
+ def _popmsg(self):
+ self.makemsgtimer()
+
+ try:
+ self.fastmsg(*self.msgqueue.popleft())
+ self.msgtimer.start()
+ except IndexError: pass
+
+ def makemsgtimer(self):
+ self.msgtimer = threading.Timer(2, self._popmsg)
+ self.msgtimer.daemon = True
+
+ def join(self, chan):
+ self.conn.send("JOIN %s" % (chan))
+
+ def part(self, chan):
+ self.conn.send("PART %s" % (chan))
+
+ def quit(self, reason="Shutdown"):
+ self.conn.send("QUIT :%s" % (reason))
+
+ def __str__(self): return self.nick
+ def __repr__(self): return "<Bot %r>" % (self.nick)
+
+class BotConnection(object):
+ def __init__(self, parent, bind, server, port):