2015-09-26 18:23:30 +02:00
|
|
|
# fantasy.py: Adds FANTASY command support, to allow calling commands in channels
|
2016-06-21 03:18:54 +02:00
|
|
|
from pylinkirc import utils, world
|
|
|
|
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
|
|
|
|
|
2016-05-15 20:45:32 +02:00
|
|
|
respondtonick = irc.botdata.get("respondtonick")
|
2015-11-29 05:46:53 +01:00
|
|
|
|
2015-09-26 18:23:30 +02:00
|
|
|
channel = args['target']
|
2016-05-15 20:27:51 +02:00
|
|
|
orig_text = args['text']
|
|
|
|
|
|
|
|
if utils.isChannel(channel) and not irc.isInternalClient(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
|
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:
|
|
|
|
|
|
|
|
# Try to look up a prefix specific for this bot in
|
|
|
|
# bot: prefixes: <botname>, falling back to the default prefix if not
|
|
|
|
# specified.
|
|
|
|
prefixes = [irc.botdata.get('prefixes', {}).get(botname) or
|
|
|
|
irc.botdata.get('prefix')]
|
|
|
|
|
|
|
|
# If responding to nick is enabled, add variations of the current nick
|
|
|
|
# to the prefix list: "<nick>," and "<nick>:"
|
2017-02-19 06:21:19 +01:00
|
|
|
nick = irc.toLower(irc.users[servuid].nick)
|
2016-05-15 20:45:32 +02:00
|
|
|
|
|
|
|
if respondtonick:
|
|
|
|
prefixes += [nick+',', nick+':']
|
|
|
|
|
|
|
|
if not any(prefixes):
|
|
|
|
# We finished with an empty prefixes list, meaning fantasy is misconfigured!
|
|
|
|
log.warning("(%s) Fantasy prefix for bot %s was not set in configuration - "
|
|
|
|
"fantasy commands will not work!", irc.name, botname)
|
|
|
|
continue
|
|
|
|
|
2017-02-19 06:21:19 +01:00
|
|
|
lowered_text = irc.toLower(orig_text)
|
2016-05-15 20:27:51 +02:00
|
|
|
for prefix in prefixes: # Cycle through the prefixes list we finished with.
|
2017-02-19 06:21:19 +01:00
|
|
|
if prefix and 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):]
|
|
|
|
|
|
|
|
# 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')
|