<commit_before>#! -*- coding: utf-8 -*-

# Copyright (C) 1999-2002  <NAME>
# Portions Copyright © 2011-2012 <NAME>

"""
Simple IRC bot library.

This module contains a single-server IRC bot class that can be used to
write simpler bots.
"""

from __future__ import absolute_import

import sys

import irc.client
import irc.modes
from .dict import IRCDict

class ServerSpec(object):
    """
    An IRC server specification.

    >>> spec = ServerSpec('localhost')
    >>> spec.host
    'localhost'
    >>> spec.port
    6667
    >>> spec.password

    >>> spec = ServerSpec('127.0.0.1', 6697, '<PASSWORD>')
    >>> spec.password
    '<PASSWORD>'
    """
    def __init__(self, host, port=6667, password=None):
        self.host = host
        self.port = port
        self.password = password

class SingleServerIRCBot(irc.client.SimpleIRCClient):
    """A single-server IRC bot class.

    The bot tries to reconnect if it is disconnected.

    The bot keeps track of the channels it has joined, the other
    clients that are present in the channels and which of those that
    have operator or voice modes.  The "database" is kept in the
    self.channels attribute, which is an IRCDict of Channels.
    """
    def __init__(self, server_list, nickname, realname,
            reconnection_interval=60, **connect_params):
        """Constructor for SingleServerIRCBot objects.

        Arguments:

            server_list -- A list of ServerSpec objects or tuples of
                           parameters suitable for constructing ServerSpec
                           objects. Defines the list of servers the bot will
                           use (in order).

            nickname -- The bot's nickname.

            realname -- The bot's realname.

            reconnection_interval -- How long the bot should wait
                                     before trying to reconnect.

            dcc_connections -- A list of initiated/accepted DCC
            connections.

            **connect_params -- parameters to pass through to the connect
                                method.
        """

        super(SingleServerIRCBot, self).__init__()
        self.__connect_params = connect_params
        self.channels = IRCDict()
        self.server_list = [
            ServerSpec(*server)
                if isinstance(server, (tuple, list))
                else server
            for server in server_list
        ]
        assert all(
            isinstance(server, ServerSpec)
            for server in self.server_list
        )
        if not reconnection_interval or reconnection_interval < 0:
            reconnection_interval = 2 ** 31
        self.reconnection_interval = reconnection_interval

        self._nickname = nickname
        self._realname = realname
        for i in ["disconnect", "join", "kick", "mode",
                  "namreply", "nick", "part", "quit"]:
            self.connection.add_global_handler(i, getattr(self, "_on_" + i),
                -20)

        self.connection.features.prefix.update({
            '%': 'h',
            '~': 'q',
        })
<commit_msg>Turns out we don't need backward compatibility because the code with these modes was never released.<commit_after>#! -*- coding: utf-8 -*-

# Copyright (C) 1999-2002  <NAME>
# Portions Copyright © 2011-2012 <NAME>

"""
Simple IRC bot library.

This module contains a single-server IRC bot class that can be used to
write simpler bots.
"""

from __future__ import absolute_import

import sys

import irc.client
import irc.modes
from .dict import IRCDict

class ServerSpec(object):
    """
    An IRC server specification.

    >>> spec = ServerSpec('localhost')
    >>> spec.host
    'localhost'
    >>> spec.port
    6667
    >>> spec.password

    >>> spec = ServerSpec('127.0.0.1', 6697, 'fooP455')
    >>> spec.password
    '<PASSWORD>'
    """
    def __init__(self, host, port=6667, password=None):
        self.host = host
        self.port = port
        self.password = password

class SingleServerIRCBot(irc.client.SimpleIRCClient):
    """A single-server IRC bot class.

    The bot tries to reconnect if it is disconnected.

    The bot keeps track of the channels it has joined, the other
    clients that are present in the channels and which of those that
    have operator or voice modes.  The "database" is kept in the
    self.channels attribute, which is an IRCDict of Channels.
    """
    def __init__(self, server_list, nickname, realname,
            reconnection_interval=60, **connect_params):
        """Constructor for SingleServerIRCBot objects.

        Arguments:

            server_list -- A list of ServerSpec objects or tuples of
                           parameters suitable for constructing ServerSpec
                           objects. Defines the list of servers the bot will
                           use (in order).

            nickname -- The bot's nickname.

            realname -- The bot's realname.

            reconnection_interval -- How long the bot should wait
                                     before trying to reconnect.

            dcc_connections -- A list of initiated/accepted DCC
            connections.

            **connect_params -- parameters to pass through to the connect
                                method.
        """

        super(SingleServerIRCBot, self).__init__()
        self.__connect_params = connect_params
        self.channels = IRCDict()
        self.server_list = [
            ServerSpec(*server)
                if isinstance(server, (tuple, list))
                else server
            for server in server_list
        ]
        assert all(
            isinstance(server, ServerSpec)
            for server in self.server_list
        )
        if not reconnection_interval or reconnection_interval < 0:
            reconnection_interval = 2 ** 31
        self.reconnection_interval = reconnection_interval

        self._nickname = nickname
        self._realname = realname
        for i in ["disconnect", "join", "kick", "mode",
                  "namreply", "nick", "part", "quit"]:
            self.connection.add_global_handler(i, getattr(self, "_on_" + i),
                -20)

{'fraction_non_alphanumeric': 0.08563580546130231, 'fraction_numerical': 0.01438035223784133, 'mean_word_length': 1.9816955684007707, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3244895', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1576, 'n_words': 548}