2015-09-26 18:23:30 +02:00
|
|
|
# fantasy.py: Adds FANTASY command support, to allow calling commands in channels
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
|
|
|
import utils
|
|
|
|
from log import log
|
|
|
|
|
|
|
|
def handle_fantasy(irc, source, command, args):
|
|
|
|
"""Fantasy command handler."""
|
2016-03-28 06:41:35 +02:00
|
|
|
|
|
|
|
if not irc.connected.is_set():
|
|
|
|
# Break if the IRC network isn't ready.
|
|
|
|
return
|
|
|
|
|
2015-11-29 05:46:53 +01:00
|
|
|
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: "<nick>," and "<nick>:"
|
|
|
|
nick = irc.pseudoclient.nick
|
|
|
|
prefixes += [nick+',', nick+':']
|
|
|
|
|
|
|
|
if not prefixes:
|
|
|
|
# We finished with an empty prefixes list, meaning fantasy is misconfigured!
|
2015-09-26 18:23:30 +02:00
|
|
|
log.warning("(%s) Fantasy prefix was not set in configuration - "
|
2015-11-02 05:55:01 +01:00
|
|
|
"fantasy commands will not work!", irc.name)
|
2015-09-26 18:23:30 +02:00
|
|
|
return
|
2015-11-29 05:46:53 +01:00
|
|
|
|
2015-09-26 18:23:30 +02:00
|
|
|
channel = args['target']
|
|
|
|
text = args['text']
|
2015-11-29 05:46:53 +01:00
|
|
|
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 \
|
2016-01-01 02:28:47 +01:00
|
|
|
irc.isInternalClient(source):
|
2015-11-29 05:46:53 +01:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2015-09-26 18:23:30 +02:00
|
|
|
utils.add_hook(handle_fantasy, 'PRIVMSG')
|