<s> import re <EOL> import time <EOL> import socket <EOL> import ssl <EOL> import threading <EOL> import Queue <EOL> import irc . client <EOL> from utils import logger <EOL> from utils import Hash <EOL> from version import VERSION <EOL> out_msg = [ ] <EOL> class IrcThread ( threading . Thread ) : <EOL> def __init__ ( self , processor , config ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . processor = processor <EOL> self . daemon = True <EOL> options = dict ( config . items ( '<STR_LIT>' ) ) <EOL> self . stratum_tcp_port = options . get ( '<STR_LIT>' ) <EOL> self . stratum_tcp_ssl_port = options . get ( '<STR_LIT>' ) <EOL> self . report_stratum_tcp_port = options . get ( '<STR_LIT>' ) <EOL> self . report_stratum_tcp_ssl_port = options . get ( '<STR_LIT>' ) <EOL> self . irc_bind_ip = options . get ( '<STR_LIT>' ) <EOL> self . host = options . get ( '<STR_LIT:host>' ) <EOL> self . report_host = options . get ( '<STR_LIT>' ) <EOL> self . nick = options . get ( '<STR_LIT>' ) <EOL> if self . report_stratum_tcp_port : <EOL> self . stratum_tcp_port = self . report_stratum_tcp_port <EOL> if self . report_stratum_tcp_ssl_port : <EOL> self . stratum_tcp_ssl_port = self . report_stratum_tcp_ssl_port <EOL> if self . report_host : <EOL> self . host = self . report_host <EOL> if not self . nick : <EOL> self . nick = Hash ( self . host ) [ : <NUM_LIT:5> ] . encode ( "<STR_LIT>" ) <EOL> self . pruning = True <EOL> self . pruning_limit = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . nick = '<STR_LIT>' + self . nick <EOL> self . password = None <EOL> self . who_queue = Queue . Queue ( ) <EOL> def getname ( self ) : <EOL> s = '<STR_LIT:v>' + VERSION + '<STR_LIT:U+0020>' <EOL> if self . pruning : <EOL> s += '<STR_LIT:p>' + self . pruning_limit + '<STR_LIT:U+0020>' <EOL> def add_port ( letter , number ) : <EOL> DEFAULT_PORTS = { '<STR_LIT:t>' : '<STR_LIT>' , '<STR_LIT:s>' : '<STR_LIT>' } <EOL> if not number : return '<STR_LIT>' <EOL> if DEFAULT_PORTS [ letter ] == number : <EOL> return letter + '<STR_LIT:U+0020>' <EOL> else : <EOL> return letter + number + '<STR_LIT:U+0020>' <EOL> s += add_port ( '<STR_LIT:t>' , self . stratum_tcp_port ) <EOL> s += add_port ( '<STR_LIT:s>' , self . stratum_tcp_ssl_port ) <EOL> return s <EOL> def start ( self , queue ) : <EOL> self . queue = queue <EOL> threading . Thread . start ( self ) <EOL> def on_connect ( self , connection , event ) : <EOL> connection . join ( "<STR_LIT>" ) <EOL> def on_join ( self , connection , event ) : <EOL> m = re . match ( "<STR_LIT>" , event . source ) <EOL> if m : <EOL> self . who_queue . put ( ( connection , m . group ( <NUM_LIT:1> ) ) ) <EOL> def on_quit ( self , connection , event ) : <EOL> m = re . match ( "<STR_LIT>" , event . source ) <EOL> if m : <EOL> self . queue . put ( ( '<STR_LIT>' , [ m . group ( <NUM_LIT:1> ) ] ) ) <EOL> def on_kick ( self , connection , event ) : <EOL> m = re . match ( "<STR_LIT>" , event . arguments [ <NUM_LIT:0> ] ) <EOL> if m : <EOL> self . queue . put ( ( '<STR_LIT>' , [ m . group ( <NUM_LIT:1> ) ] ) ) <EOL> def on_disconnect ( self , connection , event ) : <EOL> logger . error ( "<STR_LIT>" ) <EOL> raise BaseException ( "<STR_LIT>" ) <EOL> def on_who ( self , connection , event ) : <EOL> line = str ( event . arguments [ <NUM_LIT:6> ] ) . split ( ) <EOL> try : <EOL> ip = socket . gethostbyname ( line [ <NUM_LIT:1> ] ) <EOL> except : <EOL> ip = line [ <NUM_LIT:1> ] <EOL> nick = event . arguments [ <NUM_LIT:4> ] <EOL> host = line [ <NUM_LIT:1> ] <EOL> ports = line [ <NUM_LIT:2> : ] <EOL> self . queue . put ( ( '<STR_LIT>' , [ nick , ip , host , ports ] ) ) <EOL> def on_name ( self , connection , event ) : <EOL> for s in event . arguments [ <NUM_LIT:2> ] . split ( ) : <EOL> if s . startswith ( "<STR_LIT>" ) : <EOL> self . who_queue . put ( ( connection , s ) ) <EOL> def who_thread ( self ) : <EOL> while not self . processor . shared . stopped ( ) : <EOL> try : <EOL> connection , s = self . who_queue . get ( timeout = <NUM_LIT:1> ) <EOL> except Queue . Empty : <EOL> continue <EOL> connection . who ( s ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> def run ( self ) : <EOL> while self . processor . shared . paused ( ) : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> self . ircname = self . host + '<STR_LIT:U+0020>' + self . getname ( ) <EOL> irc . client . ServerConnection . buffer_class = irc . buffer . LenientDecodingLineBuffer <EOL> logger . info ( "<STR_LIT>" ) <EOL> t = threading . Thread ( target = self . who_thread ) <EOL> t . start ( ) <EOL> while not self . processor . shared . stopped ( ) : <EOL> client = irc . client . Reactor ( ) <EOL> try : <EOL> c = client . server ( ) . connect ( '<STR_LIT>' , <NUM_LIT> , self . nick , self . password , ircname = self . ircname ) <EOL> except irc . client . ServerConnectionError : <EOL> logger . error ( '<STR_LIT>' , exc_info = True ) <EOL> time . sleep ( <NUM_LIT:10> ) <EOL> continue <EOL> c . add_global_handler ( "<STR_LIT>" , self . on_connect ) <EOL> c . add_global_handler ( "<STR_LIT>" , self . on_join ) <EOL> c . add_global_handler ( "<STR_LIT>" , self . on_quit ) <EOL> c . add_global_handler ( "<STR_LIT>" , self . on_kick ) <EOL> c . add_global_handler ( "<STR_LIT>" , self . on_who ) <EOL> c . add_global_handler ( "<STR_LIT>" , self . on_name ) <EOL> c . add_global_handler ( "<STR_LIT>" , self . on_disconnect ) <EOL> c . set_keepalive ( <NUM_LIT> ) <EOL> self . connection = c <EOL> try : <EOL> client . process_forever ( ) <EOL> except BaseException as e : <EOL> logger . error ( '<STR_LIT>' , exc_info = True ) <EOL> time . sleep ( <NUM_LIT:10> ) <EOL> continue <EOL> logger . info ( "<STR_LIT>" ) </s>
