<commit_before>import logging
import re
import random

from zope.interface import implements

from hamper.interfaces import Command, ChatCommandPlugin


log = logging.getLogger('hamper.plugins')


class Quit(ChatCommandPlugin):
    """Know when hamper isn't wanted."""
    name = 'quit'

    class QuitCommand(Command):
        regex = 'quit'

        def command(self, bot, comm, groups):
            if comm['pm']:
                bot.msg(comm['channel'], "You can't do that from PM.")
                return False

            bot.reply(comm, 'Bye!')
            bot.quit()
            return True


class Sed(ChatCommandPlugin):
    """To be honest, I feel strange in channels that don't have this."""
    name = 'sed'
    priority = -1

    class SedCommand(Command):
        name = 'sed'
        regex = r's/(.*)/(.*)/(g?i?m?)'
        onlyDirected = False

        short_desc = 's/find/replace/ - Perform sed style find and replace.'
        long_desc = ('Use like "!s/foo/bar/" to search for "foo" and replace it '
                     'with "bar". \n'
                     'Flags: Add these flags to the end of the command: \n'
                     'm - Restrict the search to only your messages\n'
                     'i - Case insensitive searching.')

        def command(self, bot, comm, groups):
            options = groups[2]

            regex_opts = re.I if 'i' in options else 0
            usr_regex = re.compile(groups[0], regex_opts)
            usr_replace = groups[1]

            g = 0 if 'g' in options else 1

            key = comm['channel']
            if key not in bot.factory.history:
                bot.reply(comm, 'Who are you?! How did you get in my house?!')
                return False

            for hist in reversed(bot.factory.history[key]):
                if 'm' in options and hist['user'] != comm['user']:
                    # Only look at the user's messages
                    continue

                # Don't look at other sed commands
                if hist['directed'] and hist['raw_message'].startswith('s/'):
                    continue

                if usr_regex.search(hist['raw_message']):
                    new_msg = usr_regex.sub(usr_replace, hist['raw_message'], g)
                    bot.reply(comm, '{0} actually meant: {1}'
                                .format(hist['user'], new_msg))
                    break
            else:
                bot.reply(comm, "Sorry, I couldn't match /{0}/."
                        .format(usr_regex.pattern))


class LetMeGoogleThatForYou(ChatCommandPlugin):
    """Link to the sarcastic letmegooglethatforyou.com."""
    name = 'lmgtfy'

    class LMGTFYCommand(Command):
        name = 'lmgtfy'
        regex = '^lmgtfy\s+(.*)'
        onlyDirected = False

        short_desc = 'lmgtfy - Show someone where to find something.'
        long_desc = ('This command will be triggered at the beginning of a '
                     'message to anyone, so you can use it like "Bob: lmgtfy '
                     'rtfm" to show Bob how to search for "rtfm".')

        def command(self, bot, comm, groups):
            target = ''
            if comm['target']:
                target = comm['target'] + ': '
            args = groups[0].replace(' ', '+')
            bot.reply(comm, target + 'http://lmgtfy.com/?q=' + args)

class Rot13(ChatCommandPlugin):
    """Encode secret messages."""
    name = 'rot13'<commit_msg>Fix sed.<commit_after>import logging
import re
import random

from zope.interface import implements

from hamper.interfaces import Command, ChatCommandPlugin


log = logging.getLogger('hamper.plugins')


class Quit(ChatCommandPlugin):
    """Know when hamper isn't wanted."""
    name = 'quit'

    class QuitCommand(Command):
        regex = 'quit'

        def command(self, bot, comm, groups):
            if comm['pm']:
                bot.msg(comm['channel'], "You can't do that from PM.")
                return False

            bot.reply(comm, 'Bye!')
            bot.quit()
            return True


class Sed(ChatCommandPlugin):
    """To be honest, I feel strange in channels that don't have this."""
    name = 'sed'
    priority = -1

    class SedCommand(Command):
        name = 'sed'
        regex = r's/(.*)/(.*)/(.*)'
        onlyDirected = False

        short_desc = 's/find/replace/ - Perform sed style find and replace.'
        long_desc = ('Use like "s/foo/bar/" to search for "foo" and replace it '
                     'with "bar". \n'
                     'Flags: Add these flags to the end of the command:\n'
                     'm - Restrict the search to only your messages\n'
                     'i - Case insensitive searching.\n'
                     'g - Match an unlimited number of times.')

        def command(self, bot, comm, groups):
            usr_replace = groups[1]
            options = groups[2]
            regex_opts = re.I if 'i' in options else 0
            usr_regex = re.compile(groups[0], regex_opts)

            g = 0 if 'g' in options else 1

            key = comm['channel']
            if key not in bot.factory.history:
                bot.reply(comm, 'Who are you?! How did you get in my house?!')
                return False

            for hist in reversed(bot.factory.history[key]):
                if 'm' in options and hist['user'] != comm['user']:
                    # Only look at the user's messages
                    continue

                # Don't look at other sed commands
                if 's/' in hist['raw_message']:
                    continue

                if usr_regex.search(hist['raw_message']):
                    new_msg = usr_regex.sub(usr_replace, hist['raw_message'], g)
                    bot.reply(comm, '{0} actually meant: {1}'
                                .format(hist['user'], new_msg))
                    break
            else:
                bot.reply(comm, "Sorry, I couldn't match '{0}'."
                        .format(usr_regex.pattern))


class LetMeGoogleThatForYou(ChatCommandPlugin):
    """Link to the sarcastic letmegooglethatforyou.com."""
    name = 'lmgtfy'

    class LMGTFYCommand(Command):
        name = 'lmgtfy'
        regex = '^lmgtfy\s+(.*)'
        onlyDirected = False

        short_desc = 'lmgtfy - Show someone where to find something.'
        long_desc = ('This command will be triggered at the beginning of a '
                     'message to anyone, so you can use it like "Bob: lmgtfy '
                     'rtfm" to show Bob how to search for "rtfm".')

        def command(self, bot, comm, groups):
            target = ''
            if comm['target']:
                target = comm['target'] + ': '
            args = groups[0].replace(' ', '+')
            bot.reply(comm, target + 'http://lmgtfy.com/?q=' + args)

class Rot13(ChatCommandPlugin):
    """Encode secret messages."""
    name = 'rot13'
{'fraction_non_alphanumeric': 0.11627231818841631, 'fraction_numerical': 0.004354768471476266, 'mean_word_length': 1.7009016072128578, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4409322', 'n_tokens_mistral': 1990, 'n_tokens_neox': 1841, 'n_words': 606}