<commit_before>import os
import sys
import time
import signal
import logging
import re
from datetime import datetime

from twisted.words.protocols import irc
from twisted.internet import protocol, reactor

from cardinal.plugins import PluginManager, EventManager
from cardinal.exceptions import *


class CardinalBot(irc.IRCClient):
    logger = None
    """Logging object for CardinalBot"""

    factory = None
    """Should contain an instance of CardinalBotFactory"""

    network = None
    """Currently connected network (e.g. irc.freenode.net)"""

    @property
    def nickname(self):
        """This is the nickname property of CardinalBot"""
        return self.factory.nickname

    @property
    def password(self):
        return self.factory.server_password

    user_regex = re.compile(r'^(.*?)!(.*?)@(.*?)$')
    """Regex for identifying a user's nick, ident, and vhost"""

    plugin_manager = None
    """Instance of PluginManager"""

    event_manager = None
    """Instance of EventManager"""

    storage_path = None
    """Location of storage directory"""

    uptime = None
    """Time that Cardinal connected to the network"""

    booted  = None
    """Time that Cardinal was first launched"""

    reloads = 0
    """Number of plugin reloads performed"""

    def __init__(self):
        """Initializes the logging and sets storage directory"""
        self.logger = logging.getLogger(__name__)

        self.storage_path = os.path.join(
            os.path.dirname(os.path.realpath(sys.argv[0])),
            'storage'
        )

    def signedOn(self):
        """Called once we've connected to a network"""
        self.logger.info("Signed on as %s" % self.nickname)

        # Give the factory access to the bot
        if self.factory is None:
            raise InternalError("Factory must be set on CardinalBot instance")

        # Give the factory the instance it created in case it needs to
        # interface for error handling or metadata retention.
        self.factory.cardinal = self

        # Set the currently connected network
        self.network = self.factory.network

        # Attempt to identify with NickServ, if a password was given
        if self.factory.password:
            self.logger.info("Attempting to identify with NickServ")
            self.msg("NickServ", "IDENTIFY %s" % (self.factory.password,))

        # Creates an instance of EventManager
        self.logger.debug("Creating new EventManager instance")
        self.event_manager = EventManager(self)

        # Register events
        try:
            self.event_manager.register("irc.invite", 2)
            self.event_manager.register("irc.privmsg", 3)
            self.event_manager.register("irc.notice", 3)
            self.event_manager.register("irc.nick", 2)
            self.event_manager.register("irc.mode", 3)
            self.event_manager.register("irc.join", 2)
            self.event_manager.register("irc.part", 3)
            self.event_manager.register("irc.kick", 4)
            self.event_manager.register("irc.quit", 2)
        except EventAlreadyExistsError, e:
            self.logger.error("Could not register core IRC events", exc_info=True)

        # Create an instance of PluginManager, giving it an instance of ourself
        # to pass to plugins, as well as a list of initial plugins to load.
        self.logger.debug("Creating new PluginManager instance")
        self.plugin_manager = PluginManager(self, self.factory.plugins)

        # Attempt to join channels
        for channel in self.factory.channels:
            self.join(channel)

        # Set the uptime as now and grab the  boot time from the factory
        self.uptime = datetime.now()
        self.booted = self.factory.booted<commit_msg>Fix missing irc.topic event registration<commit_after>import os
import sys
import time
import signal
import logging
import re
from datetime import datetime

from twisted.words.protocols import irc
from twisted.internet import protocol, reactor

from cardinal.plugins import PluginManager, EventManager
from cardinal.exceptions import *


class CardinalBot(irc.IRCClient):
    logger = None
    """Logging object for CardinalBot"""

    factory = None
    """Should contain an instance of CardinalBotFactory"""

    network = None
    """Currently connected network (e.g. irc.freenode.net)"""

    @property
    def nickname(self):
        """This is the nickname property of CardinalBot"""
        return self.factory.nickname

    @property
    def password(self):
        return self.factory.server_password

    user_regex = re.compile(r'^(.*?)!(.*?)@(.*?)$')
    """Regex for identifying a user's nick, ident, and vhost"""

    plugin_manager = None
    """Instance of PluginManager"""

    event_manager = None
    """Instance of EventManager"""

    storage_path = None
    """Location of storage directory"""

    uptime = None
    """Time that Cardinal connected to the network"""

    booted  = None
    """Time that Cardinal was first launched"""

    reloads = 0
    """Number of plugin reloads performed"""

    def __init__(self):
        """Initializes the logging and sets storage directory"""
        self.logger = logging.getLogger(__name__)

        self.storage_path = os.path.join(
            os.path.dirname(os.path.realpath(sys.argv[0])),
            'storage'
        )

    def signedOn(self):
        """Called once we've connected to a network"""
        self.logger.info("Signed on as %s" % self.nickname)

        # Give the factory access to the bot
        if self.factory is None:
            raise InternalError("Factory must be set on CardinalBot instance")

        # Give the factory the instance it created in case it needs to
        # interface for error handling or metadata retention.
        self.factory.cardinal = self

        # Set the currently connected network
        self.network = self.factory.network

        # Attempt to identify with NickServ, if a password was given
        if self.factory.password:
            self.logger.info("Attempting to identify with NickServ")
            self.msg("NickServ", "IDENTIFY %s" % (self.factory.password,))

        # Creates an instance of EventManager
        self.logger.debug("Creating new EventManager instance")
        self.event_manager = EventManager(self)

        # Register events
        try:
            self.event_manager.register("irc.invite", 2)
            self.event_manager.register("irc.privmsg", 3)
            self.event_manager.register("irc.notice", 3)
            self.event_manager.register("irc.nick", 2)
            self.event_manager.register("irc.mode", 3)
            self.event_manager.register("irc.topic", 3)
            self.event_manager.register("irc.join", 2)
            self.event_manager.register("irc.part", 3)
            self.event_manager.register("irc.kick", 4)
            self.event_manager.register("irc.quit", 2)
        except EventAlreadyExistsError, e:
            self.logger.error("Could not register core IRC events", exc_info=True)

        # Create an instance of PluginManager, giving it an instance of ourself
        # to pass to plugins, as well as a list of initial plugins to load.
        self.logger.debug("Creating new PluginManager instance")
        self.plugin_manager = PluginManager(self, self.factory.plugins)

        # Attempt to join channels
        for channel in self.factory.channels:
            self.join(channel)

        # Set the uptime as now and grab the  boot time from the factory
        self.uptime = datetime.now()
        self.booted = self.factory.booted
{'fraction_non_alphanumeric': 0.09664694280078895, 'fraction_numerical': 0.0030243261012491782, 'mean_word_length': 2.6409765438008614, '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': '3548482', 'n_tokens_mistral': 2104, 'n_tokens_neox': 1949, 'n_words': 647}