<s> import time <EOL> import collections <EOL> class CaseInsensitiveDict ( collections . MutableMapping ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data = None , ** kwargs ) : <EOL> self . _store = dict ( ) <EOL> if data is None : <EOL> data = { } <EOL> self . update ( data , ** kwargs ) <EOL> def __setitem__ ( self , key , value ) : <EOL> self . _store [ key . casefold ( ) ] = ( key , value ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . _store [ key . casefold ( ) ] [ <NUM_LIT:1> ] <EOL> def __delitem__ ( self , key ) : <EOL> del self . _store [ key . casefold ( ) ] <EOL> def __iter__ ( self ) : <EOL> return ( casedkey for casedkey , mappedvalue in self . _store . values ( ) ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _store ) <EOL> def lower_items ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( <EOL> ( lowerkey , keyval [ <NUM_LIT:1> ] ) <EOL> for ( lowerkey , keyval ) <EOL> in self . _store . items ( ) <EOL> ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , collections . Mapping ) : <EOL> other = CaseInsensitiveDict ( other ) <EOL> else : <EOL> return NotImplemented <EOL> return dict ( self . lower_items ( ) ) == dict ( other . lower_items ( ) ) <EOL> def copy ( self ) : <EOL> return CaseInsensitiveDict ( self . _store . values ( ) ) <EOL> def __repr__ ( self ) : <EOL> return str ( dict ( self . items ( ) ) ) <EOL> class ExpiringDict ( collections . OrderedDict ) : <EOL> def __init__ ( self , max_len , max_age_seconds ) : <EOL> collections . OrderedDict . __init__ ( self ) <EOL> self . max_len = max_len <EOL> self . max_age = max_age_seconds <EOL> def __contains__ ( self , key ) : <EOL> try : <EOL> item = collections . OrderedDict . __getitem__ ( self , key . casefold ( ) ) <EOL> if time . time ( ) - item [ <NUM_LIT:1> ] < self . max_age : <EOL> return True <EOL> else : <EOL> del self [ key . casefold ( ) ] <EOL> except KeyError : <EOL> pass <EOL> return False <EOL> def __getitem__ ( self , key , with_age = False , max_age = None ) : <EOL> item = collections . OrderedDict . __getitem__ ( self , key . casefold ( ) ) <EOL> item_age = time . time ( ) - item [ <NUM_LIT:1> ] <EOL> if not max_age : <EOL> max_age = self . max_age <EOL> if item_age < max_age : <EOL> if with_age : <EOL> return item [ <NUM_LIT:0> ] , item_age <EOL> else : <EOL> return item [ <NUM_LIT:0> ] <EOL> else : <EOL> del self [ key . casefold ( ) ] <EOL> raise KeyError ( key . casefold ( ) ) <EOL> def __setitem__ ( self , key , value ) : <EOL> if len ( self ) == self . max_len : <EOL> self . popitem ( last = False ) <EOL> collections . OrderedDict . __setitem__ ( self , key . casefold ( ) , ( value , time . time ( ) ) ) <EOL> def pop ( self , key , default = None ) : <EOL> try : <EOL> item = collections . OrderedDict . __getitem__ ( self , key . casefold ( ) ) <EOL> del self [ key . casefold ( ) ] <EOL> return item [ <NUM_LIT:0> ] <EOL> except KeyError : <EOL> return default <EOL> def get ( self , key , default = None , with_age = False , max_age = None ) : <EOL> try : <EOL> return self . __getitem__ ( key . casefold ( ) , with_age , max_age ) <EOL> except KeyError : <EOL> if with_age : <EOL> return default , None <EOL> else : <EOL> return default <EOL> def put ( self , key , value , ts = None ) : <EOL> if len ( self ) == self . max_len : <EOL> self . popitem ( last = False ) <EOL> if not ts : <EOL> ts = time . time ( ) <EOL> collections . OrderedDict . __setitem__ ( self , key . casefold ( ) , ( value , ts ) ) <EOL> def items ( self ) : <EOL> r = [ ] <EOL> for key in self : <EOL> try : <EOL> r . append ( ( key , self [ key ] ) ) <EOL> except KeyError : <EOL> pass <EOL> return r <EOL> def values ( self ) : <EOL> r = [ ] <EOL> for key in self : <EOL> try : <EOL> r . append ( self [ key ] ) <EOL> except KeyError : <EOL> pass <EOL> return r <EOL> def fromkeys ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def iteritems ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def itervalues ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def viewitems ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def viewkeys ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def viewvalues ( self ) : <EOL> raise NotImplementedError ( ) <EOL> class CaseInsensitiveList ( collections . MutableSequence ) : <EOL> @ staticmethod <EOL> def _check_value ( value ) : <EOL> if not isinstance ( value , object ) : <EOL> raise TypeError ( ) <EOL> def __init__ ( self , data = None ) : <EOL> self . __store = [ ] <EOL> if data : <EOL> self . extend ( data ) <EOL> def __getitem__ ( self , key ) : <EOL> try : <EOL> return self . __store [ key ] <EOL> except KeyError : <EOL> raise IndexError <EOL> def __setitem__ ( self , key , value ) : <EOL> if isinstance ( value , str ) : <EOL> value = value . casefold ( ) <EOL> self . _check_value ( value ) <EOL> self . __store [ key ] = value <EOL> def __delitem__ ( self , key ) : <EOL> del self . __store [ key ] <EOL> def __len__ ( self ) : <EOL> return len ( self . __store ) <EOL> def insert ( self , key , value ) : <EOL> if isinstance ( value , str ) : <EOL> value = value . casefold ( ) <EOL> self . _check_value ( value ) <EOL> self . __store . insert ( key , value ) <EOL> def __contains__ ( self , value ) : <EOL> if isinstance ( value , str ) : <EOL> value = value . casefold ( ) <EOL> return value in self . __store <EOL> def __add__ ( self , other ) : <EOL> self . extend ( other ) <EOL> return self <EOL> class CaseInsensitiveSet ( collections . MutableSet ) : <EOL> @ staticmethod <EOL> def _check_value ( value ) : <EOL> if not isinstance ( value , object ) : <EOL> raise TypeError ( ) <EOL> def __init__ ( self , data = None ) : <EOL> self . __store = set ( ) <EOL> if data : <EOL> self . update ( data ) <EOL> def __len__ ( self ) : <EOL> return len ( self . __store ) <EOL> def add ( self , value ) : <EOL> if isinstance ( value , str ) : <EOL> value = value . casefold ( ) <EOL> self . _check_value ( value ) <EOL> self . __store . add ( value ) <EOL> def discard ( self , value ) : <EOL> if isinstance ( value , str ) : <EOL> value = value . casefold ( ) <EOL> self . __store . discard ( value ) <EOL> def __contains__ ( self , value ) : <EOL> if isinstance ( value , str ) : <EOL> value = value . casefold ( ) <EOL> return value in self . __store <EOL> def __iter__ ( self ) : <EOL> return iter ( self . __store ) <EOL> import string <EOL> hostname_allowed_chars = string . ascii_letters + string . digits + '<STR_LIT:->' <EOL> hostname_allowed_chars_tbl = str . maketrans ( '<STR_LIT>' , '<STR_LIT>' , hostname_allowed_chars ) <EOL> def validate_hostname ( hostname ) : <EOL> if hostname . endswith ( '<STR_LIT:.>' ) : <EOL> hostname = hostname [ : - <NUM_LIT:1> ] <EOL> if len ( hostname ) < <NUM_LIT:1> or len ( hostname ) > <NUM_LIT> : <EOL> return False <EOL> for part in hostname . split ( '<STR_LIT:.>' ) : <EOL> if len ( part ) < <NUM_LIT:1> or len ( part ) > <NUM_LIT> or part . startswith ( '<STR_LIT:->' ) or part . endswith ( '<STR_LIT:->' ) : <EOL> return False <EOL> if part . translate ( hostname_allowed_chars_tbl ) != '<STR_LIT>' : <EOL> return False <EOL> return True <EOL> from . server import get_context <EOL> special = '<STR_LIT>' <EOL> nick_allowed_chars = string . ascii_letters + string . digits + special <EOL> nick_allowed_chars_tbl = str . maketrans ( '<STR_LIT>' , '<STR_LIT>' , nick_allowed_chars ) <EOL> first_nick_allowed_chars = string . ascii_letters + special <EOL> def validate_nick ( nick ) : <EOL> if len ( nick ) < <NUM_LIT:1> or nick [ <NUM_LIT:0> ] not in first_nick_allowed_chars : <EOL> return False <EOL> nicklen = get_context ( ) . conf . limits . get ( '<STR_LIT>' , None ) <EOL> if nicklen and len ( nick ) > nicklen : <EOL> return False <EOL> remainder = nick [ <NUM_LIT:1> : ] <EOL> badchars = remainder . translate ( nick_allowed_chars_tbl ) <EOL> return badchars == '<STR_LIT>' <EOL> chan_allowed_chars = string . ascii_letters + string . digits + special + '<STR_LIT>' <EOL> chan_allowed_chars_tbl = str . maketrans ( '<STR_LIT>' , '<STR_LIT>' , chan_allowed_chars ) <EOL> def validate_chan ( chan_name ) : <EOL> if len ( chan_name ) < <NUM_LIT:1> or chan_name [ <NUM_LIT:0> ] != '<STR_LIT:#>' : <EOL> return False <EOL> channellen = get_context ( ) . conf . limits . get ( '<STR_LIT>' , None ) <EOL> if channellen and len ( chan_name ) > channellen : <EOL> return False <EOL> badchars = chan_name [ <NUM_LIT:1> : ] . translate ( chan_allowed_chars_tbl ) <EOL> return badchars == '<STR_LIT>' <EOL> def uniq ( input ) : <EOL> output = [ ] <EOL> for x in input : <EOL> if x not in output : <EOL> output . append ( x ) <EOL> return output <EOL> class UserHost : <EOL> def __init__ ( self , nuh ) : <EOL> self . nuh = nuh <EOL> @ property <EOL> def nickname ( self ) : <EOL> return self . nuh . split ( '<STR_LIT:!>' ) [ <NUM_LIT:0> ] <EOL> @ property <EOL> def username ( self ) : <EOL> return self . nuh . split ( '<STR_LIT:!>' ) [ <NUM_LIT:1> ] . split ( '<STR_LIT:@>' ) [ <NUM_LIT:0> ] <EOL> @ property <EOL> def hostname ( self ) : <EOL> return self . nug . split ( '<STR_LIT:@>' ) [ <NUM_LIT:1> ] </s>
