From 0d402af17e51ea601ec79b04460e2dc2cb4f82a3 Mon Sep 17 00:00:00 2001 From: James Lu Date: Sat, 28 Nov 2015 20:46:53 -0800 Subject: [PATCH] fantasy: support nick triggers (close #134) --- config.yml.example | 7 +++++- plugins/fantasy.py | 56 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/config.yml.example b/config.yml.example index 622c627..bdf7d88 100644 --- a/config.yml.example +++ b/config.yml.example @@ -15,9 +15,14 @@ bot: # Console log verbosity: see https://docs.python.org/3/library/logging.html#logging-levels loglevel: DEBUG - # Fantasy command prefix, if the fantasy plugin is loaded. + # Sets the fantasy command prefix for calling commands inside channels + # (requires fantasy plugin). prefix: "." + # Determines whether the bot will reply to commands prefixed with its nick + # (case sensitive and requires the fantasy plugin). + respondtonick: true + login: # PyLink administrative login - Change this, or the service will not start! user: admin diff --git a/plugins/fantasy.py b/plugins/fantasy.py index 78ee29d..1de767c 100644 --- a/plugins/fantasy.py +++ b/plugins/fantasy.py @@ -8,26 +8,48 @@ from log import log def handle_fantasy(irc, source, command, args): """Fantasy command handler.""" - try: - prefix = irc.botdata["prefix"] - except KeyError: + try: # First, try to fetch the config-defined prefix. + prefixes = [irc.botdata["prefix"]] + except KeyError: # Config option is missing. + prefixes = [] + + if irc.botdata.get("respondtonick"): + # If responding to nick is enabled, add variations of the current nick + # to the prefix list: "," and ":" + nick = irc.pseudoclient.nick + prefixes += [nick+',', nick+':'] + + if not prefixes: + # We finished with an empty prefixes list, meaning fantasy is misconfigured! log.warning("(%s) Fantasy prefix was not set in configuration - " "fantasy commands will not work!", irc.name) return + channel = args['target'] text = args['text'] - # Conditions: - # 1) Message target is a channel, - # 2) Message starts with our fantasy prefix, - # 3) The main PyLink client is in the channel. - # 4) The sender is NOT a PyLink client (prevents message loops). - if utils.isChannel(channel) and text.startswith(prefix) and \ - irc.pseudoclient.uid in irc.channels[channel].users and not \ - utils.isInternalClient(irc, source): - # Cut off the length of the prefix from the text. - text = text[len(prefix):] - # Set the last called in variable to the channel, so replies (from - # supporting plugins) get forwarded to it. - irc.called_by = channel - irc.callCommand(source, text) + for prefix in prefixes: # Cycle through the prefixes list we finished with. + # The following conditions must be met for an incoming message for + # fantasy to trigger: + # 1) The message target is a channel. + # 2) The message starts with one of our fantasy prefixes. + # 3) The main PyLink client is in the channel where the command was + # called. + # 4) The sender is NOT a PyLink client (this prevents infinite + # message loops). + if utils.isChannel(channel) and text.startswith(prefix) and \ + irc.pseudoclient.uid in irc.channels[channel].users and not \ + utils.isInternalClient(irc, source): + + # Cut off the length of the prefix from the text. + text = text[len(prefix):] + + # Set the "place last command was called in" variable to the + # channel in question, so that replies from fantasy-supporting + # plugins get forwarded to it. + irc.called_by = channel + + # Finally, call the bot command and break. + irc.callCommand(source, text) + break + utils.add_hook(handle_fantasy, 'PRIVMSG')