<s> import time <EOL> import random <EOL> import eventlet <EOL> from irc . bot import SingleServerIRCBot <EOL> from st2reactor . sensor . base import Sensor <EOL> eventlet . monkey_patch ( <EOL> os = True , <EOL> select = True , <EOL> socket = True , <EOL> thread = True , <EOL> time = True ) <EOL> class StackStormSensorBot ( SingleServerIRCBot ) : <EOL> def __init__ ( self , server_host , server_port , nickname , channels , handlers , logger ) : <EOL> server_list = [ ( server_host , server_port ) ] <EOL> super ( StackStormSensorBot , self ) . __init__ ( server_list = server_list , nickname = nickname , <EOL> realname = nickname ) <EOL> self . _channels = channels <EOL> self . _handlers = handlers <EOL> self . _logger = logger <EOL> def on_welcome ( self , connection , event ) : <EOL> self . _logger . debug ( '<STR_LIT>' ) <EOL> for channel in self . _channels : <EOL> self . _logger . debug ( '<STR_LIT>' % ( channel ) ) <EOL> connection . join ( channel ) <EOL> def on_nicknameinuse ( self , connection , event ) : <EOL> new_nickname = '<STR_LIT>' % ( connection . get_nickname ( ) , random . randint ( <NUM_LIT:1> , <NUM_LIT:1000> ) ) <EOL> connection . nick ( new_nickname ) <EOL> def on_pubmsg ( self , connection , event ) : <EOL> event . timestamp = int ( time . time ( ) ) <EOL> handler = self . _handlers . get ( '<STR_LIT>' , lambda connection , event : connection ) <EOL> handler ( connection = connection , event = event ) <EOL> def on_privmsg ( self , connection , event ) : <EOL> event . timestamp = int ( time . time ( ) ) <EOL> handler = self . _handlers . get ( '<STR_LIT>' , lambda connection , event : connection ) <EOL> handler ( connection = connection , event = event ) <EOL> def on_join ( self , connection , event ) : <EOL> event . timestamp = int ( time . time ( ) ) <EOL> handler = self . _handlers . get ( '<STR_LIT>' , lambda connection , event : connection ) <EOL> handler ( connection = connection , event = event ) <EOL> def on_part ( self , connection , event ) : <EOL> event . timestamp = int ( time . time ( ) ) <EOL> handler = self . _handlers . get ( '<STR_LIT>' , lambda connection , event : connection ) <EOL> handler ( connection = connection , event = event ) <EOL> class IRCSensor ( Sensor ) : <EOL> def __init__ ( self , sensor_service , config = None ) : <EOL> super ( IRCSensor , self ) . __init__ ( sensor_service = sensor_service , <EOL> config = config ) <EOL> self . _logger = self . _sensor_service . get_logger ( __name__ ) <EOL> split = self . _config [ '<STR_LIT>' ] . split ( '<STR_LIT::>' ) <EOL> self . _server_host = split [ <NUM_LIT:0> ] <EOL> self . _server_port = int ( split [ <NUM_LIT:1> ] ) <EOL> self . _nickname = self . _config [ '<STR_LIT>' ] <EOL> self . _channels = self . _config [ '<STR_LIT>' ] <EOL> def setup ( self ) : <EOL> handlers = { <EOL> '<STR_LIT>' : self . _handle_pubmsg , <EOL> '<STR_LIT>' : self . _handle_privmsg , <EOL> '<STR_LIT>' : self . _handle_join , <EOL> '<STR_LIT>' : self . _handle_part <EOL> } <EOL> self . _bot = StackStormSensorBot ( server_host = self . _server_host , <EOL> server_port = self . _server_port , <EOL> nickname = self . _nickname , channels = self . _channels , <EOL> handlers = handlers , <EOL> logger = self . _logger ) <EOL> def run ( self ) : <EOL> self . _bot . start ( ) <EOL> def cleanup ( self ) : <EOL> self . _bot . disconnect ( msg = '<STR_LIT>' ) <EOL> def add_trigger ( self , trigger ) : <EOL> pass <EOL> def update_trigger ( self , trigger ) : <EOL> pass <EOL> def remove_trigger ( self , trigger ) : <EOL> pass <EOL> def _handle_pubmsg ( self , connection , event ) : <EOL> trigger = '<STR_LIT>' <EOL> payload = { <EOL> '<STR_LIT:source>' : { <EOL> '<STR_LIT>' : event . source . nick , <EOL> '<STR_LIT:host>' : event . source . host <EOL> } , <EOL> '<STR_LIT>' : event . target , <EOL> '<STR_LIT>' : event . timestamp , <EOL> '<STR_LIT:message>' : event . arguments [ <NUM_LIT:0> ] <EOL> } <EOL> self . _sensor_service . dispatch ( trigger = trigger , payload = payload ) <EOL> def _handle_privmsg ( self , connection , event ) : <EOL> trigger = '<STR_LIT>' <EOL> payload = { <EOL> '<STR_LIT:source>' : { <EOL> '<STR_LIT>' : event . source . nick , <EOL> '<STR_LIT:host>' : event . source . host <EOL> } , <EOL> '<STR_LIT>' : event . timestamp , <EOL> '<STR_LIT:message>' : event . arguments [ <NUM_LIT:0> ] <EOL> } <EOL> self . _sensor_service . dispatch ( trigger = trigger , payload = payload ) <EOL> def _handle_join ( self , connection , event ) : <EOL> trigger = '<STR_LIT>' <EOL> payload = { <EOL> '<STR_LIT:source>' : { <EOL> '<STR_LIT>' : event . source . nick , <EOL> '<STR_LIT:host>' : event . source . host <EOL> } , <EOL> '<STR_LIT>' : event . timestamp , <EOL> '<STR_LIT>' : event . target <EOL> } <EOL> self . _sensor_service . dispatch ( trigger = trigger , payload = payload ) <EOL> def _handle_part ( self , connection , event ) : <EOL> trigger = '<STR_LIT>' <EOL> payload = { <EOL> '<STR_LIT:source>' : { <EOL> '<STR_LIT>' : event . source . nick , <EOL> '<STR_LIT:host>' : event . source . host <EOL> } , <EOL> '<STR_LIT>' : event . timestamp , <EOL> '<STR_LIT>' : event . target <EOL> } <EOL> self . _sensor_service . dispatch ( trigger = trigger , payload = payload ) </s>
