<s> '''<STR_LIT>''' <EOL> import sys <EOL> import socket <EOL> import re <EOL> import threading <EOL> import logging <EOL> import time <EOL> if sys . hexversion < <NUM_LIT> : <EOL> import Queue as queue <EOL> BlockingIOError = socket . error <EOL> else : <EOL> import queue <EOL> from . ircclient import IRCClient <EOL> user_re = re . compile ( r'<STR_LIT>' ) <EOL> class IRCBot ( IRCClient ) : <EOL> '''<STR_LIT>''' <EOL> _handlers = { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> _process_thread = None <EOL> def _async_process ( self ) : <EOL> while not self . _stop_event . is_set ( ) : <EOL> time . sleep ( <NUM_LIT> ) <EOL> try : <EOL> args = self . _in_queue . get_nowait ( ) <EOL> logging . debug ( args ) <EOL> userhost = user_re . search ( args [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] ) <EOL> if userhost : <EOL> nick , host , user = userhost . groups ( ) <EOL> command = args [ <NUM_LIT:1> ] <EOL> if command == '<STR_LIT>' : <EOL> channel = args [ <NUM_LIT:2> ] [ <NUM_LIT:1> : ] <EOL> for handler in self . _handlers [ '<STR_LIT>' ] : <EOL> handler ( self , nick , host , channel ) <EOL> elif command == '<STR_LIT>' : <EOL> channel = args [ <NUM_LIT:2> ] <EOL> topic = '<STR_LIT:U+0020>' . join ( args [ <NUM_LIT:3> : ] ) <EOL> for handler in self . _handlers [ '<STR_LIT>' ] : <EOL> handler ( self , nick , host , channel , topic ) <EOL> elif command == '<STR_LIT>' : <EOL> channel = args [ <NUM_LIT:2> ] <EOL> message = '<STR_LIT:U+0020>' . join ( args [ <NUM_LIT:3> : ] ) <EOL> for handler in self . _handlers [ '<STR_LIT>' ] : <EOL> handler ( self , nick , host , channel , message ) <EOL> elif command == '<STR_LIT>' : <EOL> channel = args [ <NUM_LIT:2> ] <EOL> message = '<STR_LIT:U+0020>' . join ( args [ <NUM_LIT:3> : ] ) [ <NUM_LIT:1> : ] <EOL> for handler in self . _handlers [ '<STR_LIT>' ] : <EOL> handler ( self , nick , host , channel , message ) <EOL> if channel [ <NUM_LIT:0> ] == '<STR_LIT:#>' : <EOL> for handler in self . _handlers [ '<STR_LIT>' ] : <EOL> handler ( self , nick , host , channel , message ) <EOL> else : <EOL> for handler in self . _handlers [ '<STR_LIT>' ] : <EOL> handler ( self , nick , host , message ) <EOL> elif command == '<STR_LIT>' : <EOL> channel = args [ <NUM_LIT:2> ] <EOL> kicked_nick = args [ <NUM_LIT:3> ] <EOL> reason = '<STR_LIT:U+0020>' . join ( args [ <NUM_LIT:4> : ] ) [ <NUM_LIT:1> : ] <EOL> for handler in self . _handlers [ '<STR_LIT>' ] : <EOL> handler ( self , nick , host , channel , kicked_nick , reason ) <EOL> elif command == '<STR_LIT>' : <EOL> new_nick = args [ <NUM_LIT:2> ] [ <NUM_LIT:1> : ] <EOL> for handler in self . _handlers [ '<STR_LIT>' ] : <EOL> handler ( self , nick , new_nick , host ) <EOL> elif command == '<STR_LIT>' : <EOL> channel = args [ <NUM_LIT:2> ] <EOL> message = '<STR_LIT:U+0020>' . join ( args [ <NUM_LIT:3> : ] ) <EOL> for handler in self . _handlers [ '<STR_LIT>' ] : <EOL> handler ( self , nick , host , channel , message ) <EOL> else : <EOL> logging . warning ( "<STR_LIT>" % command ) <EOL> self . _in_queue . task_done ( ) <EOL> except queue . Empty as e : pass <EOL> except Exception as e : <EOL> logging . debug ( e . args ) <EOL> def start ( self ) : <EOL> IRCClient . start ( self ) <EOL> self . _process_thread = threading . Thread ( target = self . _async_process ) <EOL> self . _process_thread . start ( ) <EOL> def on ( self , type ) : <EOL> '''<STR_LIT>''' <EOL> def decorator ( self , func ) : <EOL> '''<STR_LIT>''' <EOL> self . _handlers [ type ] . append ( func ) <EOL> return func <EOL> return decorator <EOL> def on_join ( self , func ) : <EOL> self . _handlers [ '<STR_LIT>' ] . append ( func ) <EOL> return func <EOL> def on_part ( self , func ) : <EOL> self . _handlers [ '<STR_LIT>' ] . append ( func ) <EOL> return func <EOL> def on_kick ( self , func ) : <EOL> self . _handlers [ '<STR_LIT>' ] . append ( func ) <EOL> return func <EOL> def on_msg ( self , func ) : <EOL> self . _handlers [ '<STR_LIT>' ] . append ( func ) <EOL> return func <EOL> def on_privmsg ( self , func ) : <EOL> self . _handlers [ '<STR_LIT>' ] . append ( func ) <EOL> return func <EOL> def on_chanmsg ( self , func ) : <EOL> self . _handlers [ '<STR_LIT>' ] . append ( func ) <EOL> return func <EOL> def on_notice ( self , func ) : <EOL> self . _handlers [ '<STR_LIT>' ] . append ( func ) <EOL> return func <EOL> def on_nick ( self , func ) : <EOL> self . _handlers [ '<STR_LIT>' ] . append ( func ) <EOL> return func <EOL> __all__ = [ '<STR_LIT>' ] </s>
