def process_command(): '''Take command and add it to the processing queue''' log.info(":API:/api/command") response = {"type": None, "data": {}, "text": None} try: command = request.form["command"] session_id = request.form["session_id"] log.debug(":{1}:Processing command {0}".format(command, session_id)) if session_id in core.sessions.keys(): # Add the command to the core.sessions command queue session_data = core.sessions[session_id] log.info(":{1}:Adding command {0} to the command queue".format( command, session_id)) command_id = tools.get_command_id(session_id) command_data = {"id": command_id, "command": command} command_response = core.sessions_monitor.command( command_data, core.sessions[session_id], db, add_to_updates_queue=False) session_data["commands"].put(command_data) log.info(":{0}:Returning command response {1}".format( session_id, tools.fold(str(command_response)))) response = command_response else: log.info( ":{0}:Couldn't find session id in sessions".format(session_id)) response["type"] = "error" response["text"] = "Invalid session id" except KeyError: log.debug("Couldn't find session id and command in request data") response["type"] = "error" response[ "text"] = "Couldn't find session id and command in request data" return tools.return_json(response)
def process_command(): """ Api path for processing a command :param command: :param session_id: :return response object: """ log.info(":API:/api/command") response = {"type": None, "data": {}, "text": None} if request.is_json: request_data = request.get_json() else: request_data = request.form try: command = request_data["command"] session_id = request_data["session_id"] log.debug(":{1}:Processing command {0}".format(command, session_id)) if session_id in core.sessions.keys(): # Add the command to the core.sessions command queue session_data = core.sessions[session_id] log.info(":{1}:Adding command {0} to the command queue".format( command, session_id)) command_data = tools.create_command_obj(session_id, command) command_response = core.sessions_monitor.command( command_data, core.sessions[session_id], db, add_to_updates_queue=False) if session_id in core.commands.keys(): core.commands[session_id].append(command_data) else: core.commands.update({session_id: [command_data]}) session_data["commands"].append(command_data) core.commands.update(command_data) log.info(":{0}:Returning command response {1}".format( session_id, tools.fold(str(command_response)))) response = command_response else: log.info( ":{0}:Couldn't find session id in sessions".format(session_id)) response["type"] = "error" response["text"] = "Invalid session id" except KeyError: log.debug("Couldn't find session id and command in request data") response["type"] = "error" response[ "text"] = "Couldn't find session id and command in request data" return tools.return_json(response)
def command_response(): """ Api path for responding to a command question :param session_id: :param command_id: :return: """ log.info(":API:/api/respond") response = {"type": None, "text": None, "data": {}} if request.is_json: request_data = request.get_json() try: log.debug(request_data.keys()) command_id = request_data["command_id"] session_id = request_data["session_id"] response_value = request_data["value"] #Validate the JSON response object if tools.check_string([command_id, session_id]): if session_id in core.sessions.keys(): session_data = core.sessions[session_id] session_commands = session_data["commands"] response_command = None for command_obj in session_commands: if command_obj["id"] == command_id: response_command = command_obj if response_command: if "function" in response_command.keys( ) and "event" in response_command.keys(): response_function = response_command["function"] log.info( ":{0}: Executing response function {1} with response {2}" .format(command_id, response_function, response_value)) #Execute the response try: response_result = response_function( response_value, response_command["event"]) log.info( ":{0}:Successfully executed response, returning {1}" .format(session_id, tools.fold(response_result))) response = response_result except Exception: exc_type, exc_value, exc_traceback = sys.exc_info( ) error_string = repr( traceback.format_exception( exc_type, exc_value, exc_traceback)) log.error(error_string) username = session_data["username"] user_table = db["users"].find_one( username=username) if user_table: response["type"] = "error" if user_table["admin"]: response["text"] = error_string else: response["text"] = "An error has occurred while trying to fetch a response." \ "Please contact [email protected] to report the error and " \ "get more information" else: log.error( "USER {0} NOT FOUND IN DATABASE. WARNING." .format(username)) response["type"] = "error" response["text"] = "A database error has occurred. Please contact [email protected]" \ "to report the error, along with the circumstances under which it" \ "occurred." else: response["type"] = "error" response["text"] = "Command {0} didn't register for a response or didn't" \ " register the required data for a response.".format(command_id) else: response["type"] = "error" response[ "text"] = "Couldn't find a command object in session {0} with command id {1}".format( session_id, command_id) else: response["type"] = "error" response["text"] = "Invalid session id {0}".format( session_id) else: response["type"] = "error" response[ "text"] = "Submitted response data {0} failed string validation. Valid characters are {0}".format( tools.valid_chars) except KeyError: response["type"] = "error" response[ "text"] = "command_id, session_id, and JSON response object required" else: response["type"] = "error" response["text"] = "/api/respond requires a JSON request" return tools.return_json(response)