<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from future . utils import viewkeys <EOL> import re <EOL> from twisted . application import internet <EOL> from twisted . words . protocols import irc <EOL> from twisted . internet import protocol , reactor <EOL> from evennia . server . session import Session <EOL> from evennia . utils import logger , utils <EOL> IRC_BOLD = "<STR_LIT>" <EOL> IRC_COLOR = "<STR_LIT>" <EOL> IRC_RESET = "<STR_LIT>" <EOL> IRC_ITALIC = "<STR_LIT>" <EOL> IRC_NORMAL = "<STR_LIT>" <EOL> IRC_UNDERLINE = "<STR_LIT>" <EOL> IRC_WHITE = "<STR_LIT:0>" <EOL> IRC_BLACK = "<STR_LIT:1>" <EOL> IRC_DBLUE = "<STR_LIT:2>" <EOL> IRC_DGREEN = "<STR_LIT:3>" <EOL> IRC_RED = "<STR_LIT:4>" <EOL> IRC_DRED = "<STR_LIT:5>" <EOL> IRC_DMAGENTA = "<STR_LIT>" <EOL> IRC_DYELLOW = "<STR_LIT>" <EOL> IRC_YELLOW = "<STR_LIT>" <EOL> IRC_GREEN = "<STR_LIT>" <EOL> IRC_DCYAN = "<STR_LIT>" <EOL> IRC_CYAN = "<STR_LIT>" <EOL> IRC_BLUE = "<STR_LIT>" <EOL> IRC_MAGENTA = "<STR_LIT>" <EOL> IRC_DGREY = "<STR_LIT>" <EOL> IRC_GRAY = "<STR_LIT>" <EOL> IRC_COLOR_MAP = dict ( [ <EOL> ( r'<STR_LIT>' , IRC_RESET ) , <EOL> ( r'<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( r'<STR_LIT>' , "<STR_LIT:U+0020>" ) , <EOL> ( r'<STR_LIT>' , "<STR_LIT:U+0020>" ) , <EOL> ( r'<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( r'<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_RED ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_GREEN ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_YELLOW ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_BLUE ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_MAGENTA ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_CYAN ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_WHITE ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_DGREY ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_DRED ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_DGREEN ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_DYELLOW ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_DBLUE ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_DMAGENTA ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_DCYAN ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_GRAY ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_BLACK ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_NORMAL + "<STR_LIT:U+002C>" + IRC_DRED ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_NORMAL + "<STR_LIT:U+002C>" + IRC_DGREEN ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_NORMAL + "<STR_LIT:U+002C>" + IRC_DYELLOW ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_NORMAL + "<STR_LIT:U+002C>" + IRC_DBLUE ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_NORMAL + "<STR_LIT:U+002C>" + IRC_DMAGENTA ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_NORMAL + "<STR_LIT:U+002C>" + IRC_DCYAN ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_NORMAL + "<STR_LIT:U+002C>" + IRC_GRAY ) , <EOL> ( r'<STR_LIT>' , IRC_COLOR + IRC_NORMAL + "<STR_LIT:U+002C>" + IRC_BLACK ) <EOL> ] ) <EOL> RE_IRC_COLOR = re . compile ( r"<STR_LIT:|>" . join ( [ re . escape ( key ) for key in viewkeys ( IRC_COLOR_MAP ) ] ) , re . DOTALL ) <EOL> RE_MXP = re . compile ( r'<STR_LIT>' , re . DOTALL ) <EOL> RE_ANSI_ESCAPES = re . compile ( r"<STR_LIT>" % "<STR_LIT:|>" . join ( ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) ) , re . DOTALL ) <EOL> def sub_irc ( ircmatch ) : <EOL> """<STR_LIT>""" <EOL> return IRC_COLOR_MAP . get ( ircmatch . group ( ) , "<STR_LIT>" ) <EOL> def parse_irc_colors ( string ) : <EOL> """<STR_LIT>""" <EOL> in_string = utils . to_str ( string ) <EOL> parsed_string = "<STR_LIT>" <EOL> parts = RE_ANSI_ESCAPES . split ( in_string ) + [ "<STR_LIT:U+0020>" ] <EOL> for part , sep in zip ( parts [ : : <NUM_LIT:2> ] , parts [ <NUM_LIT:1> : : <NUM_LIT:2> ] ) : <EOL> pstring = RE_IRC_COLOR . sub ( sub_irc , part ) <EOL> parsed_string += "<STR_LIT>" % ( pstring , sep [ <NUM_LIT:0> ] . strip ( ) ) <EOL> parsed_string = RE_MXP . sub ( r'<STR_LIT>' , parsed_string ) <EOL> return parsed_string <EOL> class IRCBot ( irc . IRCClient , Session ) : <EOL> """<STR_LIT>""" <EOL> lineRate = <NUM_LIT:1> <EOL> nickname = None <EOL> logger = None <EOL> factory = None <EOL> channel = None <EOL> def signedOn ( self ) : <EOL> """<STR_LIT>""" <EOL> self . join ( self . channel ) <EOL> self . stopping = False <EOL> self . factory . bot = self <EOL> address = "<STR_LIT>" % ( self . channel , self . network ) <EOL> self . init_session ( "<STR_LIT>" , address , self . factory . sessionhandler ) <EOL> self . uid = int ( self . factory . uid ) <EOL> self . logged_in = True <EOL> self . factory . sessionhandler . connect ( self ) <EOL> logger . log_info ( "<STR_LIT>" % ( self . nickname , self . channel , <EOL> self . network , self . port ) ) <EOL> def disconnect ( self , reason = None ) : <EOL> """<STR_LIT>""" <EOL> self . sessionhandler . disconnect ( self ) <EOL> self . stopping = True <EOL> self . transport . loseConnection ( ) <EOL> def privmsg ( self , user , channel , msg ) : <EOL> """<STR_LIT>""" <EOL> if not msg . startswith ( '<STR_LIT>' ) : <EOL> user = user . split ( '<STR_LIT:!>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> self . data_in ( "<STR_LIT>" % ( user , channel , msg ) ) <EOL> def action ( self , user , channel , msg ) : <EOL> """<STR_LIT>""" <EOL> if not msg . startswith ( '<STR_LIT>' ) : <EOL> user = user . split ( '<STR_LIT:!>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> self . data_in ( "<STR_LIT>" % ( user , channel , msg ) ) <EOL> def data_in ( self , text = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . sessionhandler . data_in ( self , text = text , ** kwargs ) <EOL> def data_out ( self , text = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if text . startswith ( "<STR_LIT>" ) : <EOL> text = text . split ( "<STR_LIT:U+0020>" , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> text = parse_irc_colors ( text ) <EOL> self . say ( self . channel , text ) <EOL> class IRCBotFactory ( protocol . ReconnectingClientFactory ) : <EOL> """<STR_LIT>""" <EOL> initialDelay = <NUM_LIT:1> <EOL> factor = <NUM_LIT> <EOL> maxDelay = <NUM_LIT> <EOL> def __init__ ( self , sessionhandler , uid = None , botname = None , channel = None , network = None , port = None , ssl = None ) : <EOL> """<STR_LIT>""" <EOL> self . sessionhandler = sessionhandler <EOL> self . uid = uid <EOL> self . nickname = str ( botname ) <EOL> self . channel = str ( channel ) <EOL> self . network = str ( network ) <EOL> self . port = port <EOL> self . ssl = ssl <EOL> self . bot = None <EOL> def buildProtocol ( self , addr ) : <EOL> """<STR_LIT>""" <EOL> protocol = IRCBot ( ) <EOL> protocol . factory = self <EOL> protocol . nickname = self . nickname <EOL> protocol . channel = self . channel <EOL> protocol . network = self . network <EOL> protocol . port = self . port <EOL> protocol . ssl = self . ssl <EOL> return protocol <EOL> def startedConnecting ( self , connector ) : <EOL> """<STR_LIT>""" <EOL> logger . log_info ( "<STR_LIT>" % self . channel ) <EOL> def clientConnectionFailed ( self , connector , reason ) : <EOL> """<STR_LIT>""" <EOL> self . retry ( connector ) <EOL> def clientConnectionLost ( self , connector , reason ) : <EOL> """<STR_LIT>""" <EOL> if not self . bot . stopping : <EOL> self . retry ( connector ) <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . port : <EOL> if self . ssl : <EOL> try : <EOL> from twisted . internet import ssl <EOL> service = reactor . connectSSL ( self . network , int ( self . port ) , self , ssl . ClientContextFactory ( ) ) <EOL> except ImportError : <EOL> self . caller . msg ( "<STR_LIT>" ) <EOL> else : <EOL> service = internet . TCPClient ( self . network , int ( self . port ) , self ) <EOL> self . sessionhandler . portal . services . addService ( service ) </s>
