jaraco / irc

Full-featured Python IRC library for Python.
MIT License
390 stars 84 forks source link
irc python

.. image:: https://img.shields.io/pypi/v/irc.svg :target: https://pypi.org/project/irc

.. image:: https://img.shields.io/pypi/pyversions/irc.svg

.. image:: https://github.com/jaraco/irc/actions/workflows/main.yml/badge.svg :target: https://github.com/jaraco/irc/actions?query=workflow%3A%22tests%22 :alt: tests

.. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json :target: https://github.com/astral-sh/ruff :alt: Ruff

.. image:: https://readthedocs.org/projects/irc/badge/?version=latest :target: https://irc.readthedocs.io/en/latest/?badge=latest

.. image:: https://img.shields.io/badge/skeleton-2024-informational :target: https://blog.jaraco.com/skeleton

.. image:: https://badges.gitter.im/jaraco/irc.svg :alt: Join the chat at https://gitter.im/jaraco/irc :target: https://gitter.im/jaraco/irc?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge

.. image:: https://tidelift.com/badges/github/jaraco/irc :target: https://tidelift.com/subscription/pkg/pypi-irc?utm_source=pypi-irc&utm_medium=referral&utm_campaign=readme

Full-featured Python IRC library for Python.

Overview

This library provides a low-level implementation of the IRC protocol for Python. It provides an event-driven IRC client framework. It has a fairly thorough support for the basic IRC protocol, CTCP, and DCC connections.

In order to understand how to make an IRC client, it's best to read up first on the IRC specifications <http://web.archive.org/web/20160628193730/http://www.irchelp.org/irchelp/rfc/>_.

Client Features

The main features of the IRC client framework are:

Current limitations:

Unfortunately, this library isn't as well-documented as I would like it to be. I think the best way to get started is to read and understand the example program irccat, which is included in the distribution.

The following modules might be of interest:

Examples

Example scripts in the scripts directory:

NOTE: If you're running one of the examples on a unix command line, you need to escape the # symbol in the channel. For example, use \\#test or "#test" instead of #test.

Scheduling Events

The library includes a default event Scheduler as irc.schedule.DefaultScheduler, but this scheduler can be replaced with any other scheduler. For example, to use the schedule <https://pypi.org/project/schedule>_ package, include it in your dependencies and install it into the IRC library as so:

class ScheduleScheduler(irc.schedule.IScheduler):
    def execute_every(self, period, func):
        schedule.every(period).do(func)

    def execute_at(self, when, func):
        schedule.at(when).do(func)

    def execute_after(self, delay, func):
        raise NotImplementedError("Not supported")

    def run_pending(self):
        schedule.run_pending()

irc.client.Reactor.scheduler_class = ScheduleScheduler

Decoding Input

By default, the IRC library attempts to decode all incoming streams as UTF-8, even though the IRC spec stipulates that no specific encoding can be expected. Since assuming UTF-8 is not reasonable in the general case, the IRC library provides options to customize decoding of input by customizing the ServerConnection class. The buffer_class attribute on the ServerConnection determines which class is used for buffering lines from the input stream, using the buffer module in jaraco.stream <https://pypi.python.org/pypi/jaraco.stream>_. By default it is buffer.DecodingLineBuffer, but may be re-assigned with another class, following the interface of buffer.LineBuffer. The buffer_class attribute may be assigned for all instances of ServerConnection by overriding the class attribute.

For example:

.. code:: python

from jaraco.stream import buffer

irc.client.ServerConnection.buffer_class = buffer.LenientDecodingLineBuffer

The LenientDecodingLineBuffer attempts UTF-8 but falls back to latin-1, which will avoid UnicodeDecodeError in all cases (but may produce unexpected behavior if an IRC user is using another encoding).

The buffer may be overridden on a per-instance basis (as long as it's overridden before the connection is established):

.. code:: python

server = irc.client.Reactor().server()
server.buffer_class = buffer.LenientDecodingLineBuffer
server.connect()

Alternatively, some clients may still want to decode the input using a different encoding. To decode all input as latin-1 (which decodes any input), use the following:

.. code:: python

irc.client.ServerConnection.buffer_class.encoding = "latin-1"

Or decode to UTF-8, but use a replacement character for unrecognized byte sequences:

.. code:: python

irc.client.ServerConnection.buffer_class.errors = "replace"

Or, to simply ignore all input that cannot be decoded:

.. code:: python

class IgnoreErrorsBuffer(buffer.DecodingLineBuffer):
    def handle_exception(self):
        pass

irc.client.ServerConnection.buffer_class = IgnoreErrorsBuffer

The library requires text for message processing, so a decoding buffer must be used. Clients must use one of the above techniques for decoding input to text.

Notes and Contact Info

Enjoy.

Maintainer: Jason R. Coombs jaraco@jaraco.com

Original Author: Joel Rosdahl joel@rosdahl.net

Copyright © 1999-2002 Joel Rosdahl Copyright © 2011-2016 Jason R. Coombs Copyright © 2009 Ferry Boender

For Enterprise

Available as part of the Tidelift Subscription.

This project and the maintainers of thousands of other packages are working with Tidelift to deliver one enterprise subscription that covers all of the open source you use.

Learn more <https://tidelift.com/subscription/pkg/pypi-irc?utm_source=pypi-irc&utm_medium=referral&utm_campaign=github>_.