Source code for irc3.plugins.core

# -*- coding: utf-8 -*-
from irc3 import event
from irc3 import rfc
__doc__ = '''
:mod:`irc3.plugins.core` Core plugin

Core events

.. autoclass:: Core

    >>> from irc3.testing import IrcBot


    >>> bot = IrcBot()
    >>> bot.include('irc3.plugins.core')

[docs]class Core: def __init__(self, bot): = bot self.timeout = int('timeout')) self.max_lag = int('max_lag')) self.reconn_handle = None self.ping_handle = None self.nick_handle = None self.before_connect_events = [ event(rfc.CONNECTED, self.connected), event(r"^:\S+ 005 \S+ (?P<data>.+) :\S+.*", self.set_config), ] def connection_made(self, client=None): # handle server config config =['server_config'].copy()['server_config'] = config*self.before_connect_events), *self.before_connect_events) # ping/ping self.connection_made_at = self.pong(event='CONNECT', data='')
[docs] def connected(self, **kwargs): """triger the server_ready event"""'Server config: %r', # recompile when I'm sure of my nickname['nick'] = kwargs['me'] # Let all plugins know that server can handle commands'server_ready') # detach useless events*self.before_connect_events)
def reconnect(self): # pragma: no cover "We're waiting a ping for too long. Trying to reconnect..."), 'No pong reply' ) self.pong(event='RECONNECT', data='')
[docs] @event(rfc.PONG) def pong(self, event='PONG', data='', **kw): # pragma: no cover """P0NG/PING"""'%s ping-pong (%s)', event, data) if self.reconn_handle is not None: self.reconn_handle.cancel() self.reconn_handle =, self.reconnect) if self.ping_handle is not None: self.ping_handle.cancel() self.ping_handle = self.timeout - self.max_lag,, 'PING :%s' % int(
[docs] @event(rfc.PING) def ping(self, data): """PING reply"""'PONG :' + data) self.pong(event='PING', data=data)
[docs] @event(rfc.NEW_NICK) def recompile(self, nick=None, new_nick=None, **kw): """recompile regexp on new nick""" if == nick.nick:['nick'] = new_nick
[docs] @event(rfc.ERR_NICK) def badnick(self, me=None, nick=None, **kw): """Use alt nick on nick error""" if me == '*': + '_')'Trying to regain nickname in 30s...') self.nick_handle = 30,,
[docs] def set_config(self, data=None, **kwargs): """Store server config""" config =['server_config'] for opt in data.split(' '): if '=' in opt: opt, value = opt.split('=', 1) else: value = True if opt.isupper(): config[opt] = value