<s> from __future__ import unicode_literals <EOL> import time <EOL> import venusian <EOL> from os import path <EOL> from irc3 import base <EOL> from irc3 import utils <EOL> from irc3 import config <EOL> from irc3 . compat import text_type <EOL> from irc3 . compat import asyncio <EOL> from irc3 . compat import Queue <EOL> from collections import defaultdict <EOL> from . plugins . command import command <EOL> from . dec import plugin <EOL> from . dec import extend <EOL> from . dec import event <EOL> class IrcClient ( asyncio . Protocol ) : <EOL> """<STR_LIT>""" <EOL> def connection_made ( self , transport ) : <EOL> self . closed = False <EOL> self . transport = transport <EOL> self . queue = Queue ( ) <EOL> self . uuid = self . transport . get_extra_info ( '<STR_LIT>' ) <EOL> self . factory . clients [ self . uuid ] = self <EOL> self . encoding = self . factory . encoding <EOL> self . data = { } <EOL> self . modes = set ( ) <EOL> self . channels = set ( ) <EOL> t = time . time ( ) <EOL> self . data . update ( <EOL> uuid = self . uuid , host = self . uuid [ <NUM_LIT:0> ] , <EOL> connection_made = t , data_received = t , <EOL> srv = self . factory . config . servername , <EOL> version = self . factory . config . version ) <EOL> def __getattr__ ( self , attr ) : <EOL> return self . data [ attr ] <EOL> def get_nick ( self ) : <EOL> return self . data . get ( '<STR_LIT>' ) <EOL> def set_nick ( self , nick ) : <EOL> self . data [ '<STR_LIT>' ] = nick <EOL> self . data [ '<STR_LIT>' ] = utils . IrcString ( <EOL> '<STR_LIT>' . format ( ** self . data ) ) <EOL> nick = property ( get_nick , set_nick ) <EOL> @ property <EOL> def registered ( self ) : <EOL> return bool ( '<STR_LIT>' in self . data and '<STR_LIT:username>' in self . data ) <EOL> def decode ( self , data ) : <EOL> """<STR_LIT>""" <EOL> encoding = getattr ( self , '<STR_LIT>' , '<STR_LIT:ascii>' ) <EOL> return data . decode ( encoding , '<STR_LIT:ignore>' ) <EOL> def data_received ( self , data ) : <EOL> self . data [ '<STR_LIT>' ] = time . time ( ) <EOL> data = self . decode ( data ) <EOL> if not self . queue . empty ( ) : <EOL> data = self . queue . get_nowait ( ) + data <EOL> lines = data . split ( '<STR_LIT:\r\n>' ) <EOL> self . queue . put_nowait ( lines . pop ( - <NUM_LIT:1> ) ) <EOL> for line in lines : <EOL> self . factory . dispatch ( line , client = self ) <EOL> def encode ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( data , text_type ) : <EOL> data = data . encode ( self . encoding ) <EOL> return data <EOL> def fwrite ( self , messages , ** kwargs ) : <EOL> kwargs [ '<STR_LIT:c>' ] = self <EOL> if not isinstance ( messages , ( list , tuple ) ) : <EOL> fmt = getattr ( messages , '<STR_LIT>' , messages ) <EOL> else : <EOL> fmt = '<STR_LIT:\r\n>' . join ( [ getattr ( m , '<STR_LIT>' , m ) for m in messages ] ) <EOL> self . write ( fmt . format ( ** kwargs ) ) <EOL> def write ( self , data ) : <EOL> if data is not None : <EOL> self . factory . dispatch ( data , client = self , iotype = '<STR_LIT>' ) <EOL> data = self . encode ( data ) <EOL> if not data . endswith ( b'<STR_LIT:\r\n>' ) : <EOL> data = data + b'<STR_LIT:\r\n>' <EOL> self . transport . write ( data ) <EOL> def connection_lost ( self , exc ) : <EOL> self . factory . log . critical ( '<STR_LIT>' , <EOL> id ( self . transport ) , <EOL> exc ) <EOL> self . factory . notify ( '<STR_LIT>' , client = self ) <EOL> del self . factory . clients [ self . uuid ] <EOL> if not self . closed : <EOL> self . closed = True <EOL> self . close ( ) <EOL> def close ( self ) : <EOL> if not self . closed : <EOL> self . factory . log . critical ( '<STR_LIT>' , <EOL> id ( self . transport ) ) <EOL> try : <EOL> self . transport . close ( ) <EOL> finally : <EOL> self . closed = True <EOL> def __str__ ( self ) : <EOL> if '<STR_LIT>' in self . data : <EOL> return '<STR_LIT>' . format ( ** self . data ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> __repr__ = __str__ <EOL> class IrcServer ( base . IrcObject ) : <EOL> """<STR_LIT>""" <EOL> nick = None <EOL> server = True <EOL> plugin_category = '<STR_LIT>' <EOL> _pep8 = [ config , extend , plugin , event , command ] <EOL> venusian = venusian <EOL> venusian_categories = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> server_config = { <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:1>' , '<STR_LIT>' : '<STR_LIT:D>' , '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT:#>' , '<STR_LIT>' : <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' : True , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:g>' , '<STR_LIT>' : True , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <EOL> '<STR_LIT:ascii>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : True , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : True , '<STR_LIT>' : <EOL> '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <EOL> True , '<STR_LIT>' : True , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <EOL> '<STR_LIT>' , '<STR_LIT>' : True , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <EOL> True , '<STR_LIT>' : '<STR_LIT:100>' } <EOL> defaults = dict ( <EOL> base . IrcObject . defaults , <EOL> motd = path . join ( path . dirname ( __file__ ) , '<STR_LIT>' ) , <EOL> cmd = '<STR_LIT>' , <EOL> host = '<STR_LIT>' , <EOL> port = <NUM_LIT> , <EOL> connection = IrcClient , <EOL> server_config = server_config , <EOL> servername = '<STR_LIT:localhost>' , <EOL> ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . clients = defaultdict ( dict ) <EOL> super ( IrcServer , self ) . __init__ ( * args , ** kwargs ) <EOL> def connection_made ( self , f ) : <EOL> if getattr ( self , '<STR_LIT>' , None ) : <EOL> self . protocol . close ( ) <EOL> try : <EOL> f . result ( ) <EOL> except Exception as e : <EOL> self . log . exception ( e ) <EOL> self . loop . call_later ( <NUM_LIT:3> , self . create_connection ) <EOL> else : <EOL> self . log . info ( '<STR_LIT>' ) <EOL> def notice ( self , client , message ) : <EOL> """<STR_LIT>""" <EOL> if client and message : <EOL> messages = utils . split_message ( message , self . config . max_length ) <EOL> for msg in messages : <EOL> client . fwrite ( '<STR_LIT>' , msg = msg ) <EOL> privmsg = notice <EOL> def SIGHUP ( self , * args ) : <EOL> self . loop . stop ( ) <EOL> def SIGINT ( self , * args ) : <EOL> self . loop . stop ( ) <EOL> def run ( argv = None ) : <EOL> return IrcServer . from_argv ( argv ) </s>
