2015-09-26 18:23:30 +02:00
|
|
|
# fantasy.py: Adds FANTASY command support, to allow calling commands in channels
|
2017-03-05 09:10:33 +01:00
|
|
|
from pylinkirc import utils, world, conf
|
2016-06-21 03:18:54 +02:00
|
|
|
from pylinkirc.log import log
|
2015-09-26 18:23:30 +02:00
|
|
|
|
|
|
|
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-09-26 18:23:30 +02:00
|
|
|
channel = args['target']
|
2016-05-15 20:27:51 +02:00
|
|
|
orig_text = args['text']
|
|
|
|
|
2017-08-29 05:14:14 +02:00
|
|
|
if irc.is_channel(channel) and not irc.is_internal_client(source):
|
2015-11-29 05:46:53 +01:00
|
|
|
# The following conditions must be met for an incoming message for
|
|
|
|
# fantasy to trigger:
|
|
|
|
# 1) The message target is a channel.
|
2016-05-15 20:27:51 +02:00
|
|
|
# 2) A PyLink service client exists in the channel.
|
|
|
|
# 3) The message starts with one of our fantasy prefixes.
|
2015-11-29 05:46:53 +01:00
|
|
|
# 4) The sender is NOT a PyLink client (this prevents infinite
|
|
|
|
# message loops).
|
2016-09-07 03:06:29 +02:00
|
|
|
for botname, sbot in world.services.copy().items():
|
|
|
|
if botname not in world.services: # Bot was removed during iteration
|
|
|
|
continue
|
2017-03-26 23:03:31 +02:00
|
|
|
|
|
|
|
# Check respond to nick options in this order:
|
|
|
|
# 1) The service specific "respond_to_nick" option
|
|
|
|
# 2) The global "pylink::respond_to_nick" option
|
|
|
|
# 3) The (deprecated) global "bot::respondtonick" option.
|
|
|
|
respondtonick = conf.conf.get(botname, {}).get('respond_to_nick',
|
2018-02-19 08:26:39 +01:00
|
|
|
conf.conf['pylink'].get("respond_to_nick", conf.conf['pylink'].get("respondtonick")))
|
2017-03-26 23:03:31 +02:00
|
|
|
|
2016-05-15 20:27:51 +02:00
|
|
|
log.debug('(%s) fantasy: checking bot %s', irc.name, botname)
|
2016-05-15 20:45:32 +02:00
|
|
|
servuid = sbot.uids.get(irc.name)
|
|
|
|
if servuid in irc.channels[channel].users:
|
|
|
|
|
2017-03-11 08:42:45 +01:00
|
|
|
# Look up a string prefix for this bot in either its own configuration block, or
|
|
|
|
# in bot::prefixes::<botname>.
|
|
|
|
prefixes = [conf.conf.get(botname, {}).get('prefix',
|
2018-02-19 08:26:39 +01:00
|
|
|
conf.conf['pylink'].get('prefixes', {}).get(botname))]
|
2016-05-15 20:45:32 +02:00
|
|
|
|
|
|
|
# If responding to nick is enabled, add variations of the current nick
|
|
|
|
# to the prefix list: "<nick>," and "<nick>:"
|
2017-06-30 08:01:39 +02:00
|
|
|
nick = irc.to_lower(irc.users[servuid].nick)
|
2016-05-15 20:45:32 +02:00
|
|
|
|
2017-03-29 07:00:41 +02:00
|
|
|
nick_prefixes = [nick+',', nick+':']
|
2016-05-15 20:45:32 +02:00
|
|
|
if respondtonick:
|
2017-03-29 07:00:41 +02:00
|
|
|
prefixes += nick_prefixes
|
2016-05-15 20:45:32 +02:00
|
|
|
|
|
|
|
if not any(prefixes):
|
2017-03-11 08:42:45 +01:00
|
|
|
# No prefixes were set, so skip.
|
2016-05-15 20:45:32 +02:00
|
|
|
continue
|
|
|
|
|
2017-06-30 08:01:39 +02:00
|
|
|
lowered_text = irc.to_lower(orig_text)
|
2017-03-11 08:42:45 +01:00
|
|
|
for prefix in filter(None, prefixes): # Cycle through the prefixes list we finished with.
|
|
|
|
if lowered_text.startswith(prefix):
|
2016-05-15 20:27:51 +02:00
|
|
|
|
|
|
|
# Cut off the length of the prefix from the text.
|
|
|
|
text = orig_text[len(prefix):]
|
|
|
|
|
2017-03-29 07:00:41 +02:00
|
|
|
# HACK: don't trigger on commands like "& help" to prevent false positives.
|
|
|
|
# Weird spacing like "PyLink: help" and "/msg PyLink help" should still
|
|
|
|
# work though.
|
|
|
|
if text.startswith(' ') and prefix not in nick_prefixes:
|
|
|
|
log.debug('(%s) fantasy: skipping trigger with text prefix followed by space', irc.name)
|
|
|
|
continue
|
|
|
|
|
2016-05-15 20:27:51 +02:00
|
|
|
# Finally, call the bot command and loop to the next bot.
|
2016-07-01 03:22:45 +02:00
|
|
|
sbot.call_cmd(irc, source, text, called_in=channel)
|
2016-05-15 20:27:51 +02:00
|
|
|
continue
|
2015-11-29 05:46:53 +01:00
|
|
|
|
2015-09-26 18:23:30 +02:00
|
|
|
utils.add_hook(handle_fantasy, 'PRIVMSG')
|