#!/usr/bin/python

###
# Copyright (c) 2004, James Vega
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#     this list of conditions, and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions, and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   * Neither the name of the author of this software nor the name of
#     contributors to this software may be used to endorse or promote products
#     derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###

"""
Some commands common to #grasshoppers.
"""

import supybot

__revision__ = "$Id$"
__author__ = supybot.authors.jamessan

import re

from itertools import ifilter

import supybot.conf as conf
import supybot.utils as utils
from supybot.commands import *
import supybot.ircmsgs as ircmsgs
import supybot.ircutils as ircutils
import supybot.privmsgs as privmsgs
import supybot.callbacks as callbacks


def configure(advanced):
    # This will be called by setup.py to configure this module.  Advanced is
    # a bool that specifies whether the user identified himself as an advanced
    # user or not.  You should effect your configuration by manipulating the
    # registry as appropriate.
    from supybot.questions import expect, anything, something, yn
    conf.registerPlugin('Grasshoppaz', True)


class Grasshoppaz(callbacks.Privmsg):
    def __init__(self):
        self.__parent = super(Grasshoppaz, self)
        self.__parent.__init__()

    def _validLastMsg(self, msg, chan):
        return msg.prefix and \
               msg.command == 'PRIVMSG' and \
               ircutils.isChannel(msg.args[0]) and \
               ircutils.strEqual(chan, msg.args[0])

    def _normalize(self, m):
        if ircmsgs.isAction(m):
            n = ircmsgs.prettyPrint(m).split(None, 2)
            try:
                return [' '.join(n[:2]), n[2]]
            except IndexError:
                return [' '.join(n[:2]), '\x00']
        else:
            pretty = ircmsgs.prettyPrint(m).split(None, 1)
            if len(pretty) == 1:
                pretty.append('\x00')
            return pretty

    def s(self, irc, msg, args, channel, s, r):
        """<search> <replace>

        Searches for the last message with <search> and replaces all instances
        of <search> with <replace>.
        """
        try:
            search = utils.perlReToPythonRe('m/%s/i' % s)
            replacer = utils.perlReToReplacer('s/%s/%s/gi' % (s, r))
        except ValueError, e:
            irc.error(str(e))
            return
        iterable = ifilter(lambda m, c=channel: self._validLastMsg(m, c),
                           reversed(irc.state.history))
        iterable.next()
        for msg in iterable:
            (p, m) = self._normalize(msg)
            if search.search(m):
                irc.reply(' '.join([p, replacer(m)]), prefixName=False)
                return
        irc.error('I couldn\'t find a message with that phrase.')
    s = wrap(s, ['onlyInChannel', 'something', 'text'])

    def win(self, irc, msg, args, channel, text):
        """<phrase>

        Useful when two people say similar things at the same time.
        Determines who was the first person to say <phrase>.
        """
        try:
            s = utils.perlReToPythonRe('m/%s/i' % text)
        except ValueError, e:
            irc.error(str(e))
            return
        iterable = ifilter(lambda m, c=channel: self._validLastMsg(m, c),
                           reversed(irc.state.history))
        iterable.next()
        foundLast = False
        for msg in iterable:
            (p, m) = self._normalize(msg)
            if s.search(m):
                if foundLast:
                    if ' ' in p:
                        irc.reply('%s wins!' % p.split()[1], prefixName=False)
                        return
                    else:
                        irc.reply('%s wins!' % p.strip('<>'), prefixName=False)
                        return
                else:
                    foundLast = True
        if foundLast:
            irc.error('I only found one message with that phrase.')
        else:
            irc.error('I couldn\'t find a message with that phrase.')
    win = wrap(win, ['onlyInChannel', 'text'])

Class = Grasshoppaz

# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
