<s> import sys <EOL> import time <EOL> import copy <EOL> from stewbot . components . modules . irclib import irclib <EOL> from stewbot . components . BaseClass import BaseClass <EOL> class IRC ( BaseClass ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> server , <EOL> port , <EOL> nick , <EOL> user , <EOL> password , <EOL> ssl , <EOL> chans = None , <EOL> debug = False , <EOL> default_quit_reason = '<STR_LIT>' , <EOL> default_reset_reason = '<STR_LIT>' , <EOL> max_message_size = <NUM_LIT> , <EOL> callback_pubmsg = None , <EOL> throttle_seconds = <NUM_LIT> , <EOL> logger = None <EOL> ) : <EOL> BaseClass . __init__ ( self , logger = logger ) <EOL> self . trace ( overrides = { '<STR_LIT:password>' : '<STR_LIT>' } ) <EOL> self . address = server <EOL> self . port = port <EOL> self . nick = nick <EOL> self . user = user <EOL> self . password = password <EOL> self . ssl = ssl <EOL> self . chans = chans <EOL> self . callback_pubmsg = callback_pubmsg <EOL> irclib . DEBUG = debug <EOL> self . default_quit_reason = default_quit_reason <EOL> self . default_reset_reason = default_reset_reason <EOL> self . max_message_size = max_message_size <EOL> self . throttle = throttle_seconds <EOL> self . queue = [ ] <EOL> self . next_send = time . time ( ) <EOL> self . last_size = <NUM_LIT:0> <EOL> self . irc = irclib . IRC ( ) <EOL> self . irc . add_global_handler ( '<STR_LIT>' , self . onPublicMessage ) <EOL> self . server = self . irc . server ( ) <EOL> self . server . add_global_handler ( '<STR_LIT>' , self . onConnect ) <EOL> self . server . add_global_handler ( '<STR_LIT>' , self . onDisonnect ) <EOL> def processForever ( self ) : <EOL> self . trace ( ) <EOL> self . irc . process_forever ( ) <EOL> def processOnce ( self ) : <EOL> self . trace ( ) <EOL> self . irc . process_once ( ) <EOL> class Message ( object ) : <EOL> def __init__ ( self , conn = None , event = None ) : <EOL> if conn and event : <EOL> self . conn = conn <EOL> self . event = event <EOL> self . channel = event . target ( ) <EOL> self . text = event . arguments ( ) [ <NUM_LIT:0> ] <EOL> self . mask = event . source ( ) <EOL> self . nick = None <EOL> self . ident = None <EOL> self . host = None <EOL> ( self . nick , self . host ) = self . mask . split ( '<STR_LIT:@>' , <NUM_LIT:2> ) <EOL> ( self . nick , self . ident ) = self . nick . split ( '<STR_LIT:!>' ) <EOL> def copy ( self ) : <EOL> _copy = IRC . Message ( ) <EOL> for ( key , item ) in self . __dict__ . items ( ) : <EOL> _copy . __dict__ [ key ] = copy ( item ) <EOL> return _copy <EOL> def connect ( self ) : <EOL> self . trace ( ) <EOL> if self . ssl : <EOL> self . server . connect ( self . address , self . port , self . nick , self . password , self . user , ssl = True ) <EOL> else : <EOL> self . server . connect ( self . address , self . port , self . nick , self . password , self . user ) <EOL> def onConnect ( self , conn , event ) : <EOL> self . trace ( ) <EOL> for chan in self . chans : <EOL> self . server . join ( chan ) <EOL> def disconnect ( self , msg = None ) : <EOL> self . trace ( ) <EOL> self . server . remove_global_handler ( '<STR_LIT>' , self . onDisonnect ) <EOL> self . server . add_global_handler ( '<STR_LIT>' , self . onChosenDisconnect ) <EOL> self . server . disconnect ( msg if msg else self . default_quit_reason ) <EOL> def onChosenDisconnect ( self , conn , event ) : <EOL> self . trace ( ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def reset ( self , msg = None ) : <EOL> self . trace ( ) <EOL> self . server . disconnect ( msg if msg else self . default_reset_reason ) <EOL> def onDisonnect ( self , conn , event ) : <EOL> self . trace ( ) <EOL> self . connect ( ) <EOL> def _sendMessage ( self , target , message ) : <EOL> self . trace ( ) <EOL> lines = [ message [ i : i + self . max_message_size ] for i in range ( <NUM_LIT:0> , len ( message ) , self . max_message_size ) ] <EOL> for line in lines : <EOL> self . traceMessage ( '<STR_LIT>' + line ) <EOL> self . queue . append ( [ target , line ] ) <EOL> if time . time ( ) >= self . next_send : <EOL> self . last_size = <NUM_LIT:0> <EOL> for i in range ( len ( self . queue ) ) : <EOL> ( target , line ) = self . queue . pop ( <NUM_LIT:0> ) <EOL> if time . time ( ) < self . next_send : <EOL> time . sleep ( self . next_send - time . time ( ) ) <EOL> if self . last_size > ( <NUM_LIT:0.5> * self . max_message_size ) : <EOL> time . sleep ( self . throttle ) <EOL> if self . last_size > ( <NUM_LIT> * self . max_message_size ) : <EOL> time . sleep ( self . throttle ) <EOL> self . server . privmsg ( target , self . Encode ( line ) ) <EOL> self . last_size = len ( line ) <EOL> self . next_send = time . time ( ) + self . throttle <EOL> def sendMessage ( self , chan , nick , msg ) : <EOL> self . trace ( ) <EOL> if nick : <EOL> msg = u'<STR_LIT>' % ( self . Decode ( nick ) , self . Decode ( msg ) ) <EOL> self . _sendMessage ( <EOL> target = chan , <EOL> message = msg <EOL> ) <EOL> def sendPrivateMessage ( self , nick , msg ) : <EOL> self . trace ( ) <EOL> self . _sendMessage ( <EOL> target = nick , <EOL> message = msg <EOL> ) <EOL> def onPublicMessage ( self , conn , event ) : <EOL> if self . callback_pubmsg : <EOL> self . callback_pubmsg ( self . Message ( conn , event ) ) <EOL> def getChannels ( self ) : <EOL> self . trace ( ) <EOL> return self . chans </s>
