<commit_before>
from random import choice

from gnotty.client import BaseIRCClient


class BaseBot(BaseIRCClient):
    """
    Base bot class. Defines the log and webook methods for subclasses
    to override.
    """

    def log(self, **kwargs):
        """
        Log handler - override me.
        """
        print "[%(server)s%(channel)s] %(nickname)s: %(message)s" % kwargs

    def log_args(self, event, message):
        return {
            "server": self.connection.server,
            "channel": self.channel,
            "nickname": self.get_nickname(event),
            "message": message,
        }

    def on_join(self, connection, event):
        self.log(**self.log_args(event, "joins"))

    def on_quit(self, connection, event):
        self.log(**self.log_args(event, "leaves"))

    def on_pubmsg(self, connection, event):
        for message in event.arguments():
            self.log(**self.log_args(event, message))

    def on_webhook(self, environ):
        raise NotImplementedError


class DjangoBot(BaseBot):
    """
    Bot for Django integration that logs messages to a database.
    """

    def log(self, **kwargs):
        from gnotty.models import IRCMessage
        IRCMessage.objects.create(**kwargs)
        super(DjangoBot, self).log(**kwargs)


class ChatMixin(object):

    def __init__(self, *args, **kwargs):
        super(ChatterMixin, self).__init__(*args, **kwargs)
        try:
            from nltk.chat.eliza import eliza_chatbot
            from nltk.chat.iesha import iesha_chatbot
            from nltk.chat.rude import rude_chatbot
            from nltk.chat.suntsu import suntsu_chatbot
            from nltk.chat.zen import zen_chatbot
        except ImportError:
            print "ChatterMixin requires nltk installed"
        else:
            self.chatbots = [eliza_chatbot, iesha_chatbot, rude_chatbot,
                             suntsu_chatbot, zen_chatbot]

    def on_join(self, connection, event):
        nickname = self.get_nickname(event)
        greetings = ("Hi", "Hello", "Howdy", "Welcome")
        if nickname != self.nickname:
            self.message_channel("%s: %s" % (nickname, choice(greetings)))
        super(GreeterMixin, self).on_join(connection, event)

    def on_pubmsg(self, connection, event):
        from nltk.chat.rude import rude_chatbot
        for message in event.arguments():
            prefix = "%s: " % self.nickname
            if message.startswith(prefix):
                nickname = self.get_nickname(event)
                chatbot = choice(self.chatbots)
                reply = chatbot.respond(message.replace(prefix, "", 1))
                self.message_channel("%s: %s" % (nickname, reply))<commit_msg>Clean up the chat bot.<commit_after>
from random import choice, randint

from gevent import sleep

from gnotty.client import BaseIRCClient


class BaseBot(BaseIRCClient):
    """
    Base bot class. Defines the log and webook methods for subclasses
    to override.
    """

    def log(self, **kwargs):
        """
        Log handler - override me.
        """
        print "[%(server)s%(channel)s] %(nickname)s: %(message)s" % kwargs

    def log_args(self, event, message, nickname=None):
        return {
            "server": self.connection.server,
            "channel": self.channel,
            "nickname": nickname or self.get_nickname(event),
            "message": message,
        }

    def message_channel(self, message):
        """
        We won't receive our own messages, so log them manually.
        """
        self.log(**self.log_args(None, message, self.nickname))
        super(BaseBot, self).message_channel(message)

    def on_join(self, connection, event):
        self.log(**self.log_args(event, "joins"))

    def on_quit(self, connection, event):
        self.log(**self.log_args(event, "leaves"))

    def on_pubmsg(self, connection, event):
        for message in event.arguments():
            self.log(**self.log_args(event, message))

    def on_webhook(self, environ):
        raise NotImplementedError


class DjangoBot(BaseBot):
    """
    Bot for Django integration that logs messages to a database.
    """

    def log(self, **kwargs):
        from gnotty.models import IRCMessage
        IRCMessage.objects.create(**kwargs)
        super(DjangoBot, self).log(**kwargs)


class ChatMixin(object):
    """
    A demo bot mixin that greets and responds to people.
    """

    def __init__(self, *args, **kwargs):
        super(ChatMixin, self).__init__(*args, **kwargs)
        self.chatbots = []
        self.greetings = ("Hi", "Hello", "Howdy", "Welcome")
        try:
            from nltk.chat import bots
        except ImportError:
            from warnings import warn
            warn("ChatMixin requires nltk installed")
        else:
            get_bot = lambda x: x[0].func_globals["%sbot" % x[0].__name__
            self.chatbots = map(get_bot, bots)

    def message_channel_delayed(self, message):
        """
        Pause for a random few seconds before messaging, to seem less
        bot like.
        """
        sleep(randint(2, 5))
        self.message_channel(message)

    def on_join(self, connection, event):
        super(ChatMixin, self).on_join(connection, event)
        nickname = self.get_nickname(event)
        greeting = choice(self.greetings)
        if nickname != self.nickname:
            self.message_channel_delayed("%s: %s" % (nickname, greeting))

    def on_pubmsg(self, connection, event):
        super(ChatMixin, self).on_pubmsg(connection, event)
        if not self.chatbots:
            return
        for message in event.arguments():
            prefix = "%s: " % self.nickname
            if message.startswith(prefix):
                nickname = self.get_nickname(event)
                chatbot = choice(self.chatbots)
                reply = chatbot.respond(message.replace(prefix, "", 1))
                self.message_channel_delayed("%s: %s" % (nickname, reply))
{'fraction_non_alphanumeric': 0.11538461538461539, 'fraction_numerical': 0.001003344481605351, 'mean_word_length': 2.381006218202374, 'pattern_counts': {'":': 8, '<': 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': '6908826', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1753, 'n_words': 435}