<s> import sys <EOL> import glob <EOL> import os <EOL> import time <EOL> import logging <EOL> from slackclient import SlackClient <EOL> sys . dont_write_bytecode = True <EOL> class RtmBot ( object ) : <EOL> def __init__ ( self , config ) : <EOL> self . config = config <EOL> self . token = config . get ( '<STR_LIT>' ) <EOL> working_directory = os . path . dirname ( sys . argv [ <NUM_LIT:0> ] ) <EOL> self . directory = self . config . get ( '<STR_LIT>' , working_directory ) <EOL> if not self . directory . startswith ( '<STR_LIT:/>' ) : <EOL> path = '<STR_LIT>' . format ( os . getcwd ( ) , self . directory ) <EOL> self . directory = os . path . abspath ( path ) <EOL> log_file = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> logging . basicConfig ( filename = log_file , <EOL> level = logging . INFO , <EOL> format = '<STR_LIT>' ) <EOL> logging . info ( self . directory ) <EOL> self . debug = self . config . get ( '<STR_LIT>' , False ) <EOL> self . last_ping = <NUM_LIT:0> <EOL> self . bot_plugins = [ ] <EOL> self . slack_client = None <EOL> def _dbg ( self , debug_string ) : <EOL> if self . debug : <EOL> logging . info ( debug_string ) <EOL> def connect ( self ) : <EOL> """<STR_LIT>""" <EOL> self . slack_client = SlackClient ( self . token ) <EOL> self . slack_client . rtm_connect ( ) <EOL> def _start ( self ) : <EOL> self . connect ( ) <EOL> self . load_plugins ( ) <EOL> while True : <EOL> for reply in self . slack_client . rtm_read ( ) : <EOL> self . input ( reply ) <EOL> self . crons ( ) <EOL> self . output ( ) <EOL> self . autoping ( ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> def start ( self ) : <EOL> if '<STR_LIT>' in self . config : <EOL> if self . config . get ( '<STR_LIT>' ) : <EOL> import daemon <EOL> with daemon . DaemonContext ( ) : <EOL> self . _start ( ) <EOL> self . _start ( ) <EOL> def autoping ( self ) : <EOL> now = int ( time . time ( ) ) <EOL> if now > self . last_ping + <NUM_LIT:3> : <EOL> self . slack_client . server . ping ( ) <EOL> self . last_ping = now <EOL> def input ( self , data ) : <EOL> if "<STR_LIT:type>" in data : <EOL> function_name = "<STR_LIT>" + data [ "<STR_LIT:type>" ] <EOL> self . _dbg ( "<STR_LIT>" . format ( function_name ) ) <EOL> for plugin in self . bot_plugins : <EOL> plugin . register_jobs ( ) <EOL> plugin . do ( function_name , data ) <EOL> def output ( self ) : <EOL> for plugin in self . bot_plugins : <EOL> limiter = False <EOL> for output in plugin . do_output ( ) : <EOL> channel = self . slack_client . server . channels . find ( output [ <NUM_LIT:0> ] ) <EOL> if channel is not None and output [ <NUM_LIT:1> ] is not None : <EOL> if limiter : <EOL> time . sleep ( <NUM_LIT> ) <EOL> limiter = False <EOL> message = output [ <NUM_LIT:1> ] . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) <EOL> channel . send_message ( "<STR_LIT:{}>" . format ( message ) ) <EOL> limiter = True <EOL> def crons ( self ) : <EOL> for plugin in self . bot_plugins : <EOL> plugin . do_jobs ( ) <EOL> def load_plugins ( self ) : <EOL> for plugin in glob . glob ( self . directory + '<STR_LIT>' ) : <EOL> sys . path . insert ( <NUM_LIT:0> , plugin ) <EOL> sys . path . insert ( <NUM_LIT:0> , self . directory + '<STR_LIT>' ) <EOL> for plugin in glob . glob ( self . directory + '<STR_LIT>' ) + glob . glob ( self . directory + '<STR_LIT>' ) : <EOL> logging . info ( plugin ) <EOL> name = plugin . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] [ : - <NUM_LIT:3> ] <EOL> if name in self . config : <EOL> logging . info ( "<STR_LIT>" + name ) <EOL> plugin_config = self . config . get ( name , { } ) <EOL> plugin_config [ '<STR_LIT>' ] = self . debug <EOL> self . bot_plugins . append ( Plugin ( name , plugin_config ) ) <EOL> class Plugin ( object ) : <EOL> def __init__ ( self , name , plugin_config = None ) : <EOL> '''<STR_LIT>''' <EOL> if plugin_config is None : <EOL> plugin_config = { } <EOL> self . name = name <EOL> self . jobs = [ ] <EOL> self . module = __import__ ( name ) <EOL> self . module . config = plugin_config <EOL> self . debug = self . module . config . get ( '<STR_LIT>' , False ) <EOL> self . register_jobs ( ) <EOL> self . outputs = [ ] <EOL> if '<STR_LIT>' in dir ( self . module ) : <EOL> self . module . setup ( ) <EOL> def register_jobs ( self ) : <EOL> if '<STR_LIT>' in dir ( self . module ) : <EOL> for interval , function in self . module . crontable : <EOL> self . jobs . append ( Job ( interval , eval ( "<STR_LIT>" + function ) , self . debug ) ) <EOL> logging . info ( self . module . crontable ) <EOL> self . module . crontable = [ ] <EOL> else : <EOL> self . module . crontable = [ ] <EOL> def do ( self , function_name , data ) : <EOL> if function_name in dir ( self . module ) : <EOL> if self . debug is True : <EOL> eval ( "<STR_LIT>" + function_name ) ( data ) <EOL> else : <EOL> try : <EOL> eval ( "<STR_LIT>" + function_name ) ( data ) <EOL> except Exception : <EOL> logging . exception ( "<STR_LIT>" . format ( function_name , data ) ) <EOL> if "<STR_LIT>" in dir ( self . module ) : <EOL> if self . debug is True : <EOL> self . module . catch_all ( data ) <EOL> else : <EOL> try : <EOL> self . module . catch_all ( data ) <EOL> except Exception : <EOL> logging . exception ( "<STR_LIT>" . format ( self . module , data ) ) <EOL> def do_jobs ( self ) : <EOL> for job in self . jobs : <EOL> job . check ( ) <EOL> def do_output ( self ) : <EOL> output = [ ] <EOL> while True : <EOL> if '<STR_LIT>' in dir ( self . module ) : <EOL> if len ( self . module . outputs ) > <NUM_LIT:0> : <EOL> logging . info ( "<STR_LIT>" . format ( self . module ) ) <EOL> output . append ( self . module . outputs . pop ( <NUM_LIT:0> ) ) <EOL> else : <EOL> break <EOL> else : <EOL> self . module . outputs = [ ] <EOL> return output <EOL> class Job ( object ) : <EOL> def __init__ ( self , interval , function , debug ) : <EOL> self . function = function <EOL> self . interval = interval <EOL> self . lastrun = <NUM_LIT:0> <EOL> self . debug = debug <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . function , self . interval , self . lastrun ) <EOL> def __repr__ ( self ) : <EOL> return self . __str__ ( ) <EOL> def check ( self ) : <EOL> if self . lastrun + self . interval < time . time ( ) : <EOL> if self . debug is True : <EOL> self . function ( ) <EOL> else : <EOL> try : <EOL> self . function ( ) <EOL> except Exception : <EOL> logging . exception ( "<STR_LIT>" . format ( self . function ) ) <EOL> self . lastrun = time . time ( ) <EOL> class UnknownChannel ( Exception ) : <EOL> pass </s>
