"""
Alias - have internal shortcuts to commands
Copyright (C) 2010  Brian S. Stephan

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from ConfigParser import NoOptionError, NoSectionError
import re

from extlib import irclib

from Module import Module

class Alias(Module):
    """
    Allows for commands to be aliased as !command, circumventing bot addressing stuff.
    """

    def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
        """
        See if there is an alias ("!command") in the text, and if so, translate it into
        an internal bot command and run it.
        """

        # first see if the aliases are being directly manipulated via add/remove
        whats = what.split(' ')
        try:
            if whats[0] == '!alias' and whats[1] == 'add' and len(whats) >= 4:
                if not self.config.has_section(self.__class__.__name__):
                    self.config.add_section(self.__class__.__name__)

                self.config.set(self.__class__.__name__, whats[2], ' '.join(whats[3:]))
                replystr = 'Added alias ' + whats[2] + '.'
                return self.reply(connection, replypath, replystr)
            if whats[0] == '!alias' and whats[1] == 'remove' and len(whats) >= 3:
                if not self.config.has_section(self.__class__.__name__):
                    self.config.add_section(self.__class__.__name__)

                if self.config.remove_option(self.__class__.__name__, whats[2]):
                    replystr = 'Removed alias ' + whats[2] + '.'
                    return self.reply(connection, replypath, replystr)
            elif whats[0] == '!alias' and whats[1] == 'list':
                try:
                    if len(whats) > 2:
                        alias = self.config.get(self.__class__.__name__, whats[2])
                        return self.reply(connection, replypath, alias)
                    else:
                        alist = self.config.options(self.__class__.__name__)
                        self.remove_metaoptions(alist)
                        alist.sort()
                        liststr = ', '.join(alist)
                        return self.reply(connection, replypath, liststr)
                except NoSectionError: pass
        except NoOptionError: pass
        except NoSectionError: pass

        # search for recursions, which will search for recursions, which ...
        what = self.try_recursion(connection, event, nick, userhost, None, what, admin_unlocked)

        # done searching for recursions in this level. we will now operate on
        # whatever recursion-satisfied string we have, checking for alias and
        # running module commands

        # now that that's over, try doing alias work
        try:
            alias_list = self.config.options(self.__class__.__name__)
            self.remove_metaoptions(alias_list)

            for alias in alias_list:
                alias_re = re.compile(alias)
                if alias_re.search(what):
                    command = re.sub(alias, self.config.get(self.__class__.__name__, alias), what)
                    # we found an alias for our given string, doing a replace

                    # need to do another recursion scan
                    command = self.try_recursion(connection, event, nick, userhost, None, command, admin_unlocked)

                    # running it against each module
                    for module in self.modlist:
                        ret = module.do(connection, event, nick, userhost, None, command, admin_unlocked)
                        if ret is not None:
                            # a module had a result for us, post-alias, so return it
                            # TODO: scan all modules with compounding results
                            return self.reply(connection, replypath, ret)

        except NoOptionError: pass
        except NoSectionError: pass

    def try_recursion(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
        """
        Scan message for subcommands to execute and use as part of this command.
        Upon seeing a line intended for this module, see if there are subcommands
        that we should do what is basically a text replacement on. The intent is to
        allow things like the following:

        command arg1 [anothercommand arg1 arg2]

        where the output of anothercommand is command's arg2..n.
        """

        start_idx = what.find('[')
        subcmd = what[start_idx+1:]
        end_idx = subcmd.rfind(']')
        subcmd = subcmd[:end_idx]

        attempt = what

        if start_idx == -1 or end_idx == -1 or len(subcmd) == 0:
            # no alias, just returning what we got
            return attempt
        else:
            # we have a subcmd, see if there's another one nested
            # this will include more calls to Alias, which will try recursing again
            for module in self.modlist:
                ret = module.do(connection, event, nick, userhost, None, subcmd, admin_unlocked)
                if ret is not None:
                    # some module had a change, so we replace [subcmd] with ret and return it
                    return attempt.replace('['+subcmd+']', ret)

            # we got here, no one had a replacement. return what we had
            return attempt

# vi:tabstop=4:expandtab:autoindent
# kate: indent-mode python;indent-width 4;replace-tabs on;