def userRenamed(self, oldname, newname): ''' Method called when other user has changed their nick. ''' # Notify plugins and log event logging.debug("[NICK] %s -> %s", oldname, newname) ext.notify(self, 'nick', old = oldname, new = newname)
def nickChanged(self, nick): ''' Method called when bot's nick has changed. ''' old = self.nickname self.nickname = nick logging.debug("[NICK] %s -> %s", old, nick) ext.notify(self, 'nick', old=old, new=nick)
def joined(self, channel): ''' Method called when bot has joined a channel. ''' # Notify plugins and log the event logging.debug("[JOIN] %s to %s", self.nickname, channel) ext.notify(self, 'join', channel = channel, user = self.nickname)
def userQuit(self, user, message): ''' Method called when other user has disconnected from IRC. ''' # Notify plugins and log event logging.debug("[QUIT] %s (%s)", user, message) ext.notify(self, 'quit', user = user, message = message)
def privmsg(self, user, channel, message): ''' Method called upon receiving a message on a channel or private message. ''' if channel == "*": logging.debug("[SERVER] %s", message) return is_priv = channel == self.nickname ext.notify(self, 'message', user=user, channel=None if is_priv else channel, message=message, type=ext.MSG_SAY) if is_priv: is_command = True # on priv, everything's a command if config.cmd_prefix and message.startswith(config.cmd_prefix): message = message[len(config.cmd_prefix):] # remove prefix if present anyway else: if config.cmd_prefix: is_command = message.startswith(config.cmd_prefix) if is_command: message = message[len(config.cmd_prefix):] else: is_command = True # if no prefix is defined, everything is a command logging.info("[%s] <%s/%s> %s", "COMMAND" if is_command else "MESSAGE", user, channel if not is_priv else '__priv__', message) if is_command: resp = self._command(user, message, channel=None if is_priv else channel) if resp: logging.info("[RESPONSE] %s", resp) irc.say(self, user if is_priv else channel, resp)
def topicUpdated(self, user, channel, newTopic): ''' Method called when topic of channel changes or upon joining the channel. ''' # Notify plugins and log message logging.debug("[TOPIC] <%s/%s> %s", user, channel, newTopic) ext.notify(self, 'topic', channel = channel, topic = newTopic, user = user)
def privmsg(self, user, channel, message): ''' Method called upon receiving a message on a channel or private message. ''' # Discard server messages if channel == "*": logging.debug("[SERVER] %s", message) ; return # Notify plugins ext.notify(self, 'message', user = user, channel = (channel if channel != self.nickname else None), message = message, type = ext.MSG_SAY) # First, check whether this is a private message and whether we shall interpret # it as a command invocation is_priv = channel == self.nickname if is_priv: is_command = True # On priv, everything's a command if config.cmd_prefix and message.startswith(config.cmd_prefix): # Prefix is optional; get rid of it if present message = message[len(config.cmd_prefix):] else: if config.cmd_prefix: is_command = message.startswith(config.cmd_prefix) if is_command: message = message[len(config.cmd_prefix):] # Get rid of the prefix if present else: is_command = True # If no prefix defined, everything is a command # Log message/command logging.info ("[%s] <%s/%s> %s", "COMMAND" if is_command else "MESSAGE", user, channel if not is_priv else '__priv__', message) if is_command: resp = self._command(user, message) if resp: logging.info("[RESPONSE] %s", resp) irc.say(self, user if is_priv else channel, resp)
def userLeft(self, user, channel): ''' Method called when other user has left a channel. ''' # Notify plugins and log event logging.debug("[PART] %s from %s", self.nickname, channel) ext.notify(self, 'part', user = self.nickname, channel = channel)
def userJoined(self, user, channel): ''' Method called when other user has joined a channel. ''' # Notify plugins and log event logging.debug("[JOIN] %s to %s", user, channel) ext.notify(self, 'join', channel = channel, user = user)
def myInfo(self, servername, version, umodes, cmodes): ''' Method called with information about the server. ''' # Log and notify plugins logging.debug("[SERVER] %s running %s; usermodes=%s, channelmodes=%s", servername, version, umodes, cmodes) ext.notify(self, 'connect', host = servername)
def kickedFrom(self, channel, kicker, message): ''' Method called when bot is kicked from a channel. ''' self.channels.remove(channel) logging.debug("[KICK] %s from %s by %s (%s)", self.nickname, channel, kicker, message) ext.notify(self, 'kick', channel=channel, kicker=kicker, kickee=self.nickname, reason=message)
def kickedFrom(self, channel, kicker, message): ''' Method called when bot is kicked from a channel. ''' # Notify plugins and log event logging.debug("[KICK] %s from %s by %s (%s)", self.nickname, channel, kicker, message) ext.notify(self, 'kick', channel = channel, kicker = kicker, kickee = self.nickname, reason = message)
def userKicked(self, kickee, channel, kicker, message): ''' Method called when other user is kicked from a channel. ''' # Notify plugins and log event logging.debug("[KICK] %s from %s by %s (%s)", kickee, channel, kicker, message) ext.notify(self, 'kick', channel = channel, kicker = kicker, kickee = kickee, reason = message)
def modeChanged(self, user, channel, set, modes, args): ''' Method called when user changes mode(s) for a channel. ''' # Notify plugins and log the message logging.debug("[MODE] %s sets %s%s %s for %s", user, "+" if set else "-", modes, args, channel) ext.notify(self, 'mode', user = user, channel = channel, set = set, modes = modes, args = args)
def noticed(self, user, channel, message): ''' Method called upon recieving notice message (either channel or private). ''' is_priv = channel == self.nickname logging.debug("[NOTICE] <%s/%s> %s", user, channel if not is_priv else '__priv__', message) ext.notify(self, 'message', user=user, channel=(channel if not is_priv else None), message=message, type=ext.MSG_NOTICE)
def action(self, user, channel, message): ''' Method called when user performs and action (/me) in channel. ''' is_priv = channel == self.nickname # Notify plugins and log message logging.debug("[ACTION] <%s/%s> %s", user, channel if not is_priv else '__priv__', message) ext.notify(self, 'message', user=user, channel=(channel if not is_priv else None), message=message, type=ext.MSG_ACTION)
def noticed(self, user, channel, message): ''' Method called upon recieving notice message (either channel or private). ''' is_priv = channel == self.nickname # Notify plugins and log the message logging.debug("[NOTICE] <%s/%s> %s", user, channel if not is_priv else '__priv__', message) ext.notify(self, 'message', user = user, channel = (channel if not is_priv else None), message = message, type = ext.MSG_NOTICE)
def action(self, user, channel, message): ''' Method called when user performs and action (/me) in channel. ''' is_priv = channel == self.nickname # Notify plugins and log message logging.debug("[ACTION] <%s/%s> %s", user, channel if not is_priv else '__priv__', message) ext.notify(self, 'message', user = user, channel = (channel if not is_priv else None), message = message, type = ext.MSG_ACTION)
def nickChanged(self, nick): ''' Method called when bot's nick has changed. ''' # Remember new nick old = self.nickname self.nickname = nick # Notify plugins and log event logging.debug("[NICK] %s -> %s", old, nick) ext.notify(self, 'nick', old = old, new = nick)
def _command(self, user, command, channel=None): '''Internal function that handles the processing of commands. Returns the result of processing as a text response to be "said" by the bot, or None if it wasn't actually a command. :param channel: Channel where the command was issued (can be None if it was private message command) ''' m = COMMAND_RE.match(command) if not m: return cmd = m.group('cmd') args = m.groupdict().get('args') # Poll plugins for command result resp = ext.notify(self, 'command', channel=channel, user=user, cmd=cmd, args=args) if resp: return resp # Plugins didn't care so find a command and invoke it if present cmd_object = ext.get_command(cmd) if cmd_object: if callable(cmd_object): try: resp = cmd_object(args) except Exception, e: resp = type(e).__name__ + ": " + str(e) resp = [resp] # Since we expect response to be iterable else: return ["Invalid command '%s'; likely indicates faulty plugin" % cmd]
def _command(self, user, command): ''' Internal function that handles the processing of commands. Returns the result of processing as a text response to be "said" by the bot, or None if it wasn't actually a command. ''' m = COMMAND_RE.match(command) if not m: return cmd = m.group('cmd') args = m.groupdict().get('args') # Poll plugins for command result resp = ext.notify(self, 'command', user = user, cmd = cmd, args = args) if resp: return resp # Plugins didn't care so find a command and invoke it if present cmd_object = ext.get_command(cmd) if cmd_object: if callable(cmd_object): try: resp = cmd_object(args) except Exception, e: resp = type(e).__name__ + ": " + str(e) resp = [resp] # Since we expect response to be iterable else: return ["Invalid command '%s'; likely indicates faulty plugin" % cmd] else: # Check whether the command can be unambiguously resolved completions = ext._commands.search(cmd).keys() if len(completions) == 1: command = completions[0] if args: command += " %s" % args return self._command(user, command) # Otherwise, suggest other variants suggestions = set() for i in range(1, len(cmd) + 1): completions = ext._commands.search(cmd[:i]).keys() suggestions = suggestions.union(set(completions)) if len(suggestions) == 0: resp = ["Unrecognized command '%s'." % cmd] else: # If there are too many suggestions, filter them out MAX_SUGGESTIONS = 5 suggestions = filter(None, suggestions) more = None if len(suggestions) > MAX_SUGGESTIONS: more = len(suggestions) - MAX_SUGGESTIONS suggestions = suggestions[:MAX_SUGGESTIONS] # Format them if config.cmd_prefix: suggestions = map(lambda s: config.cmd_prefix + s, suggestions) suggestions = str.join(" ", suggestions) if more: suggestions += " ... (%s more)" % more resp = ["Did you mean one of: %s ?" % suggestions] return resp
def tick(self): ''' Method called every second. Provides a way for plugins to perform actions based on time. ''' ext.notify(self, 'tick')
def joined(self, channel): ''' Method called when bot has joined a channel. ''' self.channels.add(channel) logging.debug("[JOIN] %s to %s", self.nickname, channel) ext.notify(self, 'join', channel=channel, user=self.nickname)
def left(self, channel): ''' Method called when bot has left a channel. ''' self.channels.remove(channel) logging.debug("[PART] %s from %s", self.nickname, channel) ext.notify(self, 'part', user=self.nickname, channel=channel)