# 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."""

    if not irc.connected.is_set():
        # Break if the IRC network isn't ready.
        return

    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!
        log.warning("(%s) Fantasy prefix was not set in configuration - "
                    "fantasy commands will not work!", irc.name)
        return

    channel = args['target']
    text = args['text']
    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 \
                irc.isInternalClient(source):

            # 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

utils.add_hook(handle_fantasy, 'PRIVMSG')

@@ -187,16 +187,29 @@
 t utils%0A
+import world%0A
 from log
@@ -1122,24 +1122,29 @@
 arget'%5D%0A    
+orig_
 text = args%5B
@@ -1155,88 +1155,78 @@
 t'%5D%0A
+%0A
     
-for prefix in prefixes:  # Cycle through the prefixes list we finished with.
+if utils.isChannel(channel) and not irc.isInternalClient(source):
 %0A   
@@ -1394,160 +1394,121 @@
  2) 
-The message starts with one of our fantasy prefixes.%0A        #   3) The main PyLink client is in the channel where the command was%0A        #      called
+A PyLink service client exists in the channel.%0A        #   3) The message starts with one of our fantasy prefixes
 .%0A  
@@ -1621,195 +1621,357 @@
     
-if utils.isChannel(channel
+for botname, sbot in world.services.items():%0A            log.debug('(%25s
 ) 
+f
 an
-d text.startswith(prefix) and %5C%0A                irc.pseudoclient.uid in irc.channels%5Bchannel%5D.users and not %5C%0A                irc.isInternalClient(source):%0A%0A
+tasy: checking bot %25s', irc.name, botname)%0A            if sbot.uids.get(irc.name) in irc.channels%5Bchannel%5D.users:%0A                for prefix in prefixes:  # Cycle through the prefixes list we finished with.%0A                     if orig_text.startswith(prefix):%0A%0A           
     
@@ -1978,16 +1978,17 @@
         
+ 
 # Cut of
@@ -2029,16 +2029,28 @@
 e text.%0A
+            
         
@@ -2060,16 +2060,21 @@
  text = 
+orig_
 text%5Blen
@@ -2101,203 +2101,135 @@
     
+      
-# Set the %22place last command was called in%22 variable to the%0A            # channel in question, so that replies from fantasy-supporting%0A            # plugins get forwarded to it.%0A            irc.
+      # Finally, call the bot command and loop to the next bot.%0A                        sbot.call_cmd(irc, source, text, 
 call
@@ -2237,117 +2237,45 @@
 d_by
- = 
+=
 channel
-%0A%0A            # Finally, call the bot command and break.%0A            irc.callCommand(source, text)%0A
+, notice=False)%0A             
     
@@ -2285,14 +2285,16 @@
     
- break
+continue
 %0A%0Aut

rework to support service bots (#216)