def main(): global target global plugins global broadcaster broadcaster = Broadcaster.Broadcaster() broadcaster.on_chat += on_chat args = get_args() target = args.target client = IRCLib.client.IRC() try: c = client.server().connect(args.server, args.port, args.nickname) except IRCLib.client.ServerConnectionError: print(sys.exc_info()[1]) raise SystemExit(1) index = 0 plugins = [] for i in PluginLoader.get_plugins(): print("Loading plugin " + i["name"]) plugin = PluginLoader.load_plugin(i) plugins.append(plugin) plugin.run(broadcaster) c.add_global_handler("welcome", on_connect) c.add_global_handler("disconnect", on_disconnect) c.add_global_handler('pubmsg', on_message) threading.Thread(target = main_loop, args = (c,)).start() client.process_forever()
def _load_response_plugin(self): """ Read RESPONSE plugin. """ response_plugin = None GlobalModule.EM_LOGGER.info('102012 Start Loading Plugin for Response') plg_dir_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "NcsPluginCreateResponse") try: result, plugin_name = GlobalModule.EM_CONFIG.read_if_process_conf( "Response_plugin") if not result: raise IOError("Load Error IfProcess Conf %s", "Response_plugin") GlobalModule.EM_LOGGER.debug('target plugin = %s', plugin_name) plugins = PluginLoader.load_plugins(plg_dir_path, "NcsPlugin") if not plugins: raise IOError("Response plugin files is not found.") GlobalModule.EM_LOGGER.debug('load plugins = %s', [tmp.plugin_name for tmp in plugins]) for plugin in plugins: if plugin.plugin_name.startswith(plugin_name): response_plugin = plugin break if not response_plugin: raise IOError("Designated Response Plugin is not found.") except Exception as ex: GlobalModule.EM_LOGGER.error( '302013 Error Loading Plugin for Response') GlobalModule.EM_LOGGER.debug("Error : %s", ex) raise else: GlobalModule.EM_LOGGER.debug('load plugin = %s', response_plugin) GlobalModule.EM_LOGGER.debug('response plugin load') return response_plugin
def _load_message_plugins(self): ''' Load Plugin. Explanation about parameter: None Explanation about return value: None ''' GlobalModule.EM_LOGGER.info( '103008 Start Loading Plugin for Analysis Message') plugin_dir_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "OfcPluginMessage") try: plugins = PluginLoader.load_plugins(plugin_dir_path, "OfcPlugin") except Exception: GlobalModule.EM_LOGGER.error( '303009 Error Loading Plugin for Analysis Message') raise GlobalModule.EM_LOGGER.debug('load plugins = %s', plugins) def key_weight(mod): return mod.plugin_weight self.plugin_analysis_message = sorted(plugins, key=key_weight, reverse=True) GlobalModule.EM_LOGGER.debug('plugin list = %s', self.plugin_analysis_message)
def __init__(self, configuration): global connection """ initialise connection object. """ self.configuration = configuration self.__conn_hooks__ = configuration.get_value('main', 'onconnect').split(',') self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._setupdone, self._connected, self._registered, self._passrequired, self.debug = (False, False, False, False, False) self.log = logging.getLogger('ashiema') self.basic = Basic(self) self._queue = Queue() self._evh = EventHandler() self.pluginloader = PluginLoader((self, self._evh)) self.scheduler = Scheduler() self.tasks = {}
def run_start_plugins(): GlobalModule.EM_LOGGER.info('101016 Start Loading Plugin for Start') plugin_dir_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "EmStartPlugin") try: plugins = PluginLoader.load_plugins(plugin_dir_path, "EmPlugin") except Exception: GlobalModule.EM_LOGGER.error('301017 Error Loading Plugin for Start') raise GlobalModule.EM_LOGGER.debug('load plugins = %s', plugins) try: for plugin in plugins: plugin.run() except Exception as ex: GlobalModule.EM_LOGGER.error('301018 Error Execute Plugin for Start') GlobalModule.EM_LOGGER.debug('Plugin Run Error = %s', ex) raise GlobalModule.EM_LOGGER.debug('plugins all load and run') return True
def __init__(self, input_data=''): self.intelligence = [] self.intellingece_q = Queue.Queue() self.loader = PluginLoader.PluginLoader() if input_data != '': self.intellingece_q.put(input_data)
class Connection(object): """ Connection object to manage the connection to the uplink """ def __init__(self, configuration): global connection """ initialise connection object. """ self.configuration = configuration self.__conn_hooks__ = configuration.get_value('main', 'onconnect').split(',') self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._setupdone, self._connected, self._registered, self._passrequired, self.debug = (False, False, False, False, False) self.log = logging.getLogger('ashiema') self.basic = Basic(self) self._queue = Queue() self._evh = EventHandler() self.pluginloader = PluginLoader((self, self._evh)) self.scheduler = Scheduler() self.tasks = {} """ information setup """ def setup_info(self, nick = '', ident = '', real = ''): """ set up post connection info. """ self.nick = nick self.ident = ident self.real = real self._setupdone = True return self def set_debug(self, debug = True): """ set connection debug logging. """ self.debug = debug return self """ flag management """ def shutdown(self): """ change the self._connection flag to shut down the bot """ # unload all plugins self.pluginloader.unload() # shut down all leftover apscheduler tasks for task in self.tasks: self.scheduler.unschedule_job(task) # shut down the scheduler self.scheduler.shutdown() # change the value that controls the connection loop self._connected = False """ socket manipulation and management """ def connect(self, address = '', port = '', _ssl = None, password = None): """ complete the connection process. """ assert self._setupdone is True, 'Information setup has not been completed.' assert address and port, 'Parameters for connection have not been provided.' _ssl = True if _ssl == True or _ssl == "True" or _ssl == "true" else False if _ssl is True: self.connection = ssl.wrap_socket(self._socket) self.log.info("connection type: SSL") elif _ssl is False: self.connection = self._socket self.log.info("connection type: plain") else: self.connection = self._socket self.log.warning("connection type not specified, assuming plain.") if password is not None: self._passrequired, self._password = (True, password) self._evh.load_default_events() self.connection.connect((address, int(port))) # we're connected! self._connected = True return self def send(self, data): """ function to add a line to the sendqueue to be sent. """ assert self._setupdone is True, 'Information setup has not been completed.' assert self._connected is True, 'Connection to the uplink has not yet been established.' self._queue.append(data.encode("UTF-8") + '\r\n') def _raw_send(self, data): """ allows sending of raw data directly to the uplink assumes +data+ has received all necessary formatting to actually be read and processed by the uplink """ assert self._setupdone is True, 'Information setup has not been completed.' assert self._connected is True, 'Connection to the uplink has not yet been established.' data = data.decode('UTF-8', 'ignore') self.connection.send(data.encode('UTF-8')) def run(self): """ runs the polling loop. this should run off of two assertions: self._connected is true, and self._setupdone is true.""" assert self._setupdone is True, 'Information setup has not been completed.' assert self._connected is True, 'Connection to the uplink has not yet been established.' # start the scheduler self.scheduler.start() # cycle counter _cc = 1 while self._connected is True: try: # first, sleep so we don't slurp up CPU time like no tomorrow time.sleep(0.005) # send user registration if we're not already registered and about 35 cycles have passed if not self._registered and _cc is 20: if self._passrequired: self.send("PASS :%s" % (self._password)) self._password = None self.send("NICK %s" % (self.nick)) self.send("USER %s +iw %s :%s" % (self.nick, self.ident, self.real)) self._registered = True # now, select. r, w, e = select.select([self.connection], [], [self.connection], .025) # now GO! if self.connection in r: self.parse(self.connection.recv(35000)) # check if im in the errors if self.connection in e: self._connected = False self.log.severe("error during poll; aborting") break # process the data thats in the queue try: [self._raw_send(data) for data in [self._queue.pop() for count in xrange(0, 1)]] except (AssertionError, QueueError) as e: pass except (KeyboardInterrupt, SystemExit) as e: self.shutdown() _cc += 1 except (KeyboardInterrupt, SystemExit) as e: self.shutdown() # what are we going to do after the loop closes? self.log.info("shutting down.") self.connection.close() self._socket.close() exit() """ data parsing and event dispatch """ def parse(self, data): assert self._connected is True, 'Connection to the uplink has not yet been established.' data = data.split('\r\n') for line in data: # serialisation. line = Serialise(line, (self, self._evh)) # fire off all events that match the data. self._evh.map_events(line)