Merge pull request #1567 in FS/freeswitch from rayo-exec-dialplan-app to master

* commit '01bac4cc5f02f34f2fb3af87aa0bd055c8657adb':
  FS-11316 [mod_rayo] Add FS API support
  FS-11316 [mod_rayo] Add dialplan app execution component
This commit is contained in:
Mike Jerris 2018-08-08 16:40:02 +00:00
commit efc6515e43
7 changed files with 278 additions and 7 deletions

View File

@ -7,7 +7,7 @@ IKS_LA=$(IKS_BUILDDIR)/src/libiksemel.la
mod_LTLIBRARIES = mod_rayo.la
mod_rayo_la_SOURCES = mod_rayo.c iks_helpers.c nlsml.c rayo_components.c rayo_cpa_component.c rayo_cpa_detector.c rayo_elements.c rayo_fax_components.c
mod_rayo_la_SOURCES += rayo_input_component.c rayo_output_component.c rayo_prompt_component.c rayo_record_component.c sasl.c srgs.c xmpp_streams.c
mod_rayo_la_SOURCES += rayo_input_component.c rayo_output_component.c rayo_prompt_component.c rayo_record_component.c sasl.c srgs.c xmpp_streams.c rayo_exec_component.c
mod_rayo_la_CFLAGS = $(AM_CFLAGS) -I$(IKS_DIR)/include $(PCRE_CFLAGS)
mod_rayo_la_LIBADD = $(switch_builddir)/libfreeswitch.la $(IKS_LA) $(PCRE_LIBS)
mod_rayo_la_LDFLAGS = -avoid-version -module -no-undefined -shared

View File

@ -1,6 +1,6 @@
/*
* mod_rayo for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2013-2015, Grasshopper
* Copyright (C) 2013-2018, Grasshopper
*
* Version: MPL 1.1
*
@ -2921,12 +2921,10 @@ done:
return NULL;
}
/**
* Dial a new call
* @param rclient requesting the call
* @param server handling the call
* @param node the request
* @param msg the request
*/
static iks *on_rayo_dial(struct rayo_actor *server, struct rayo_message *msg, void *data)
{
@ -2961,6 +2959,82 @@ static iks *on_rayo_dial(struct rayo_actor *server, struct rayo_message *msg, vo
return response;
}
struct exec_thread_data {
switch_memory_pool_t *pool;
iks *node;
};
/**
* Thread that handles executing server APIs
* @param thread this thread
* @param user the API request
* @return NULL
*/
static void *SWITCH_THREAD_FUNC rayo_exec_thread(switch_thread_t *thread, void *user)
{
struct exec_thread_data *etdata = (struct exec_thread_data *)user;
iks *response = NULL;
iks *exec = iks_find(etdata->node, "exec");
const char *api = iks_find_attrib(exec, "api");
const char *args = iks_find_attrib_soft(exec, "args");
switch_stream_handle_t stream = { 0 };
SWITCH_STANDARD_STREAM(stream);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "BGAPI EXEC: %s %s\n", api, args);
if (switch_api_execute(api, args, NULL, &stream) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "BGAPI EXEC FAILURE\n");
response = iks_new_error_detailed(etdata->node, STANZA_ERROR_BAD_REQUEST, "Failed to execute API");
} else {
iks *api_result = NULL;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "BGAPI EXEC RESULT: %s\n", (char *)stream.data);
response = iks_new_iq_result(etdata->node);
api_result = iks_insert(response, "response");
iks_insert_attrib(api_result, "response", zstr((char *)stream.data) ? "" : (char *)stream.data);
}
RAYO_SEND_REPLY(globals.server, iks_find_attrib(response, "to"), response);
switch_safe_free(stream.data);
{
switch_memory_pool_t *pool = etdata->pool;
switch_core_destroy_memory_pool(&pool);
}
return NULL;
}
/**
* Execute an API on the server
* @param server handling the call
* @param msg the request
*/
static iks *on_rayo_exec(struct rayo_actor *server, struct rayo_message *msg, void *data)
{
iks *node = msg->payload;
switch_thread_t *thread;
switch_threadattr_t *thd_attr = NULL;
iks *exec = iks_find(node, "exec");
iks *response = NULL;
const char *api = iks_find_attrib_soft(exec, "api");
if (zstr(api)) {
response = iks_new_error_detailed(node, STANZA_ERROR_BAD_REQUEST, "missing <exec> api attribute");
} else {
struct exec_thread_data *etdata = NULL;
switch_memory_pool_t *pool = NULL;
switch_core_new_memory_pool(&pool);
etdata = switch_core_alloc(pool, sizeof(*etdata));
etdata->pool = pool;
etdata->node = iks_copy(node);
/* start exec thread */
switch_threadattr_create(&thd_attr, pool);
switch_threadattr_detach_set(thd_attr, 1);
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
switch_thread_create(&thread, thd_attr, rayo_exec_thread, etdata, pool);
}
return response;
}
/**
* Handle <iq><ping> request
* @param rclient the Rayo client
@ -5206,6 +5280,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_rayo_load)
rayo_actor_command_handler_add(RAT_SERVER, "", "get:"IKS_NS_XMPP_PING":ping", on_iq_xmpp_ping);
rayo_actor_command_handler_add(RAT_SERVER, "", "get:"IKS_NS_XMPP_DISCO":query", on_iq_get_xmpp_disco);
rayo_actor_command_handler_add(RAT_SERVER, "", "set:"RAYO_NS":dial", on_rayo_dial);
rayo_actor_command_handler_add(RAT_SERVER, "", "set:"RAYO_NS":exec", on_rayo_exec);
/* Rayo call commands */
rayo_actor_command_handler_add(RAT_CALL, "", "set:"RAYO_NS":accept", on_rayo_accept);

View File

@ -228,7 +228,8 @@ switch_status_t rayo_components_load(switch_loadable_module_interface_t **module
rayo_output_component_load(module_interface, pool, config_file) != SWITCH_STATUS_SUCCESS ||
rayo_prompt_component_load(module_interface, pool, config_file) != SWITCH_STATUS_SUCCESS ||
rayo_record_component_load(module_interface, pool, config_file) != SWITCH_STATUS_SUCCESS ||
rayo_fax_components_load(module_interface, pool, config_file) != SWITCH_STATUS_SUCCESS) {
rayo_fax_components_load(module_interface, pool, config_file) != SWITCH_STATUS_SUCCESS ||
rayo_exec_component_load(module_interface, pool, config_file) != SWITCH_STATUS_SUCCESS) {
return SWITCH_STATUS_TERM;
}
return SWITCH_STATUS_SUCCESS;
@ -244,6 +245,7 @@ switch_status_t rayo_components_shutdown(void)
rayo_prompt_component_shutdown();
rayo_record_component_shutdown();
rayo_fax_components_shutdown();
rayo_exec_component_shutdown();
return SWITCH_STATUS_SUCCESS;
}

View File

@ -1,6 +1,6 @@
/*
* mod_rayo for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2013, Grasshopper
* Copyright (C) 2013-2018, Grasshopper
*
* Version: MPL 1.1
*
@ -52,9 +52,13 @@
#define RAYO_FAX_NS RAYO_BASE "fax:" RAYO_VERSION
#define RAYO_FAX_COMPLETE_NS RAYO_BASE "fax:complete:" RAYO_VERSION
#define RAYO_EXEC_NS RAYO_BASE "exec:" RAYO_VERSION
#define RAYO_EXEC_COMPLETE_NS RAYO_BASE "exec:complete:" RAYO_VERSION
#define COMPONENT_COMPLETE_STOP "stop", RAYO_EXT_COMPLETE_NS
#define COMPONENT_COMPLETE_ERROR "error", RAYO_EXT_COMPLETE_NS
#define COMPONENT_COMPLETE_HANGUP "hangup", RAYO_EXT_COMPLETE_NS
#define COMPONENT_COMPLETE_DONE "done", RAYO_EXT_COMPLETE_NS
extern switch_status_t rayo_components_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool, const char *config_file);
extern switch_status_t rayo_input_component_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool, const char *config_file);
@ -62,6 +66,7 @@ extern switch_status_t rayo_output_component_load(switch_loadable_module_interfa
extern switch_status_t rayo_prompt_component_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool, const char *config_file);
extern switch_status_t rayo_record_component_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool, const char *config_file);
extern switch_status_t rayo_fax_components_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool, const char *config_file);
extern switch_status_t rayo_exec_component_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool, const char *config_file);
extern switch_status_t rayo_components_shutdown(void);
extern switch_status_t rayo_input_component_shutdown(void);
@ -69,6 +74,7 @@ extern switch_status_t rayo_output_component_shutdown(void);
extern switch_status_t rayo_prompt_component_shutdown(void);
extern switch_status_t rayo_record_component_shutdown(void);
extern switch_status_t rayo_fax_components_shutdown(void);
extern switch_status_t rayo_exec_component_shutdown(void);
extern void rayo_component_send_start(struct rayo_component *component, iks *iq);
extern void rayo_component_send_iq_error(struct rayo_component *component, iks *iq, const char *error_name, const char *error_type);

View File

@ -121,6 +121,17 @@ ELEMENT(RAYO_SENDFAX)
ATTRIB(xmlns,, any)
ELEMENT_END
/**
* <app> command validation
*/
ELEMENT(RAYO_APP)
ATTRIB(xmlns,, any)
ATTRIB(app,, any)
OPTIONAL_ATTRIB(args,, any)
ELEMENT_END
/* For Emacs:
* Local Variables:

View File

@ -39,6 +39,7 @@ ELEMENT_DECL(RAYO_PROMPT)
ELEMENT_DECL(RAYO_RECEIVEFAX)
ELEMENT_DECL(RAYO_RECORD)
ELEMENT_DECL(RAYO_SENDFAX)
ELEMENT_DECL(RAYO_APP)
#endif

View File

@ -0,0 +1,176 @@
/*
* mod_rayo for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2013-2018, Grasshopper
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mod_rayo for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is Grasshopper
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Chris Rienzo <chris.rienzo@grasshopper.com>
*
* rayo_exec_component.c -- Rayo call application execution component
*
*/
#include "rayo_components.h"
#include "rayo_elements.h"
/**
* An exec component
*/
struct exec_component {
/** component base class */
struct rayo_component base;
/** Dialplan app */
const char *app;
/** Dialplan app args */
char *args;
};
#define EXEC_COMPONENT(x) ((struct exec_component *)x)
/**
* Wrapper for executing dialplan app
*/
SWITCH_STANDARD_APP(rayo_app_exec)
{
if (!zstr(data)) {
struct rayo_component *component = RAYO_COMPONENT_LOCATE(data);
if (component) {
switch_status_t status;
switch_channel_set_variable(switch_core_session_get_channel(session), SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "");
status = switch_core_session_execute_application(session, EXEC_COMPONENT(component)->app, EXEC_COMPONENT(component)->args);
if (status != SWITCH_STATUS_SUCCESS) {
rayo_component_send_complete(component, COMPONENT_COMPLETE_ERROR);
} else {
const char *resp = switch_channel_get_variable(switch_core_session_get_channel(session), SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE);
if (zstr(resp)) {
rayo_component_send_complete(component, COMPONENT_COMPLETE_DONE);
} else {
/* send complete event to client */
iks *response = iks_new("app");
iks_insert_attrib(response, "xmlns", RAYO_EXEC_COMPLETE_NS);
iks_insert_attrib(response, "response", resp);
rayo_component_send_complete_with_metadata(component, COMPONENT_COMPLETE_DONE, response, 1);
iks_delete(response);
}
}
RAYO_RELEASE(component);
}
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Missing rayo exec component JID\n");
}
switch_channel_set_variable(switch_core_session_get_channel(session), SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "");
}
/**
* Create a record component
*/
static struct rayo_component *exec_component_create(struct rayo_actor *call, const char *client_jid, iks *exec)
{
switch_memory_pool_t *pool;
struct exec_component *exec_component = NULL;
switch_core_new_memory_pool(&pool);
exec_component = switch_core_alloc(pool, sizeof(*exec_component));
exec_component = EXEC_COMPONENT(rayo_component_init(RAYO_COMPONENT(exec_component), pool, RAT_CALL_COMPONENT, "exec", NULL, call, client_jid));
if (exec_component) {
exec_component->app = switch_core_strdup(pool, iks_find_attrib_soft(exec, "app"));
exec_component->args = switch_core_strdup(pool, iks_find_attrib_soft(exec, "args"));
} else {
switch_core_destroy_memory_pool(&pool);
return NULL;
}
return RAYO_COMPONENT(exec_component);
}
/**
* Execute dialplan APP on rayo call
*/
static iks *start_exec_app_component(struct rayo_actor *call, struct rayo_message *msg, void *data)
{
iks *iq = msg->payload;
iks *exec = iks_find(iq, "app");
struct rayo_component *exec_component = NULL;
switch_core_session_t *session = NULL;
/* validate record attributes */
if (!VALIDATE_RAYO_APP(exec)) {
return iks_new_error(iq, STANZA_ERROR_BAD_REQUEST);
}
exec_component = exec_component_create(call, iks_find_attrib(iq, "from"), exec);
if (!exec_component) {
return iks_new_error_detailed(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR, "Failed to create exec entity");
}
session = switch_core_session_locate(call->id);
if (session) {
if (switch_core_session_execute_application_async(session, switch_core_session_strdup(session, "rayo-app-exec"), switch_core_session_strdup(session, RAYO_JID(exec_component))) != SWITCH_STATUS_SUCCESS) {
switch_core_session_rwunlock(session);
RAYO_RELEASE(exec_component);
RAYO_DESTROY(exec_component);
return iks_new_error_detailed(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR, "failed to execute app");
}
switch_core_session_rwunlock(session);
} else {
RAYO_RELEASE(exec_component);
RAYO_DESTROY(exec_component);
return iks_new_error_detailed(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR, "Call is gone");
}
rayo_component_send_start(exec_component, iq);
return NULL;
}
/**
* Initialize exec component
* @param module_interface
* @param pool memory pool to allocate from
* @param config_file to use
* @return SWITCH_STATUS_SUCCESS if successful
*/
switch_status_t rayo_exec_component_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool, const char *config_file)
{
switch_application_interface_t *app_interface;
SWITCH_ADD_APP(app_interface, "rayo-app-exec", "Wrapper dialplan app for internal use only", "", rayo_app_exec, "", SAF_SUPPORT_NOMEDIA | SAF_ZOMBIE_EXEC);
rayo_actor_command_handler_add(RAT_CALL, "", "set:"RAYO_EXEC_NS":app", start_exec_app_component);
return SWITCH_STATUS_SUCCESS;
}
/**
* Shutdown exec component
* @return SWITCH_STATUS_SUCCESS if successful
*/
switch_status_t rayo_exec_component_shutdown(void)
{
return SWITCH_STATUS_SUCCESS;
}
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet
*/