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
|
|
|
|
|
2017-03-05 09:10:33 +01:00
|
|
|
respondtonick = conf.conf['bot'].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:
|
|
|
|
|
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',
|
|
|
|
conf.conf['bot'].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-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):
|
2017-03-11 08:42:45 +01:00
|
|
|
# No prefixes were set, so skip.
|
2016-05-15 20:45:32 +02:00
|
|
|
continue
|
|
|
|
|
2017-02-19 06:21:19 +01:00
|
|
|
lowered_text = irc.toLower(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):]
|
|
|
|
|
|
|
|
# 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')
|