3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-30 14:49:28 +01:00
PyLink/plugins/fantasy.py
James Lu bf42109d81 Split fantasy prefix definitions into service-specific blocks
Closes #426.

This makes the pylink::prefix (aka bot::prefix) option only affect the main PyLink bot, and deprecates the pylink::prefixes::<...> options.
As the bot: config block is no longer checked, this commit depends on commit 45ed5b962e215763f7659631d1b98a5031dbab92 (ref #343) to alias it to conf::pylink.
2017-03-10 23:47:07 -08:00

60 lines
2.5 KiB
Python

# fantasy.py: Adds FANTASY command support, to allow calling commands in channels
from pylinkirc import utils, world, conf
from pylinkirc.log import log
def handle_fantasy(irc, source, command, args):
"""Fantasy command handler."""
if not irc.connected.is_set():
# Break if the IRC network isn't ready.
return
respondtonick = conf.conf['bot'].get("respondtonick")
channel = args['target']
orig_text = args['text']
if utils.isChannel(channel) and not irc.isInternalClient(source):
# The following conditions must be met for an incoming message for
# fantasy to trigger:
# 1) The message target is a channel.
# 2) A PyLink service client exists in the channel.
# 3) The message starts with one of our fantasy prefixes.
# 4) The sender is NOT a PyLink client (this prevents infinite
# message loops).
for botname, sbot in world.services.copy().items():
if botname not in world.services: # Bot was removed during iteration
continue
log.debug('(%s) fantasy: checking bot %s', irc.name, botname)
servuid = sbot.uids.get(irc.name)
if servuid in irc.channels[channel].users:
# 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))]
# If responding to nick is enabled, add variations of the current nick
# to the prefix list: "<nick>," and "<nick>:"
nick = irc.toLower(irc.users[servuid].nick)
if respondtonick:
prefixes += [nick+',', nick+':']
if not any(prefixes):
# No prefixes were set, so skip.
continue
lowered_text = irc.toLower(orig_text)
for prefix in filter(None, prefixes): # Cycle through the prefixes list we finished with.
if lowered_text.startswith(prefix):
# 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.
sbot.call_cmd(irc, source, text, called_in=channel)
continue
utils.add_hook(handle_fantasy, 'PRIVMSG')