]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
[mod_kazoo] several fixes & code cleanup 86/head
authorlazedo <luis.azedo@factorlusitano.com>
Thu, 31 Oct 2019 10:34:40 +0000 (10:34 +0000)
committerlazedo <luis.azedo@factorlusitano.com>
Thu, 31 Oct 2019 11:06:16 +0000 (11:06 +0000)
16 files changed:
src/mod/event_handlers/mod_kazoo/kazoo_api.c
src/mod/event_handlers/mod_kazoo/kazoo_commands.c
src/mod/event_handlers/mod_kazoo/kazoo_defs.h [new file with mode: 0644]
src/mod/event_handlers/mod_kazoo/kazoo_dptools.c
src/mod/event_handlers/mod_kazoo/kazoo_ei.h
src/mod/event_handlers/mod_kazoo/kazoo_ei_config.c
src/mod/event_handlers/mod_kazoo/kazoo_ei_utils.c
src/mod/event_handlers/mod_kazoo/kazoo_endpoints.c
src/mod/event_handlers/mod_kazoo/kazoo_message.c
src/mod/event_handlers/mod_kazoo/kazoo_tweaks.c
src/mod/event_handlers/mod_kazoo/kazoo_tweaks.h [new file with mode: 0644]
src/mod/event_handlers/mod_kazoo/kazoo_utils.c
src/mod/event_handlers/mod_kazoo/kazoo_utils.h [new file with mode: 0644]
src/mod/event_handlers/mod_kazoo/kz_node.c
src/mod/event_handlers/mod_kazoo/mod_kazoo.c
src/mod/event_handlers/mod_kazoo/mod_kazoo.h

index b070f8633633ab60bfb70b97af0df46dbf1337ad..955ff5c64eff1d807e2f4608ddb6e1cb54153a5d 100644 (file)
@@ -516,8 +516,9 @@ SWITCH_STANDARD_API(exec_api_cmd)
        return SWITCH_STATUS_SUCCESS;
 }
 
-void add_cli_api(switch_loadable_module_interface_t **module_interface, switch_api_interface_t *api_interface)
+void add_cli_api(switch_loadable_module_interface_t **module_interface)
 {
+       switch_api_interface_t *api_interface = NULL;
        SWITCH_ADD_API(api_interface, "erlang", KAZOO_DESC, exec_api_cmd, KAZOO_SYNTAX);
        switch_console_set_complete("add erlang status");
        switch_console_set_complete("add erlang event_filter");
index f798d027b7e010ef87b7b226166da7b88504f3dc..2e9a8f2459a4e15f02753a5860fa33d413a8b342 100644 (file)
@@ -104,7 +104,8 @@ static void process_history_item(char* value, cJSON *json)
        switch_safe_free(item);
 }
 
-SWITCH_STANDARD_API(kz_json_history) {
+SWITCH_STANDARD_API(kz_json_history)
+{
        char *mycmd = NULL, *argv[MAX_HISTORY] = { 0 };
        int n, argc = 0;
        cJSON *json = cJSON_CreateObject();
@@ -145,7 +146,8 @@ SWITCH_STANDARD_API(kz_json_history) {
        return SWITCH_STATUS_SUCCESS;
 }
 
-SWITCH_STANDARD_API(kz_first_of) {
+SWITCH_STANDARD_API(kz_first_of)
+{
        char delim = '|';
        char *mycmd = NULL, *argv[MAX_FIRST_OF] = { 0 };
        int n, argc = 0;
@@ -498,7 +500,20 @@ done:
        return status;
 }
 
-void add_kz_commands(switch_loadable_module_interface_t **module_interface, switch_api_interface_t *api_interface) {
+SWITCH_STANDARD_API(kz_expand_api)
+{
+       if (!zstr(cmd)) {
+               char * val = kz_expand(cmd);
+               stream->write_function(stream, "+OK %s", val);
+               switch_safe_free(val);
+       } else {
+               stream->write_function(stream, "ERR invalid input");
+       }
+       return SWITCH_STATUS_SUCCESS;
+}
+
+void add_kz_commands(switch_loadable_module_interface_t **module_interface) {
+       switch_api_interface_t *api_interface = NULL;
        SWITCH_ADD_API(api_interface, "kz_uuid_setvar_multi", UUID_SET_DESC, uuid_setvar_multi_function, UUID_MULTISET_SYNTAX);
        SWITCH_ADD_API(api_interface, "kz_uuid_setvar_multi_encoded", UUID_SET_DESC, uuid_setvar_multi_encoded_function, UUID_MULTISET_SYNTAX);
        switch_console_set_complete("add kz_uuid_setvar_multi ::console::list_uuid");
@@ -510,5 +525,6 @@ void add_kz_commands(switch_loadable_module_interface_t **module_interface, swit
        SWITCH_ADD_API(api_interface, "kz_http_put", KZ_HTTP_PUT_DESC, kz_http_put, KZ_HTTP_PUT_SYNTAX);
        SWITCH_ADD_API(api_interface, "first-of", KZ_FIRST_OF_DESC, kz_first_of, KZ_FIRST_OF_SYNTAX);
        SWITCH_ADD_API(api_interface, "kz_json_history", KZ_FIRST_OF_DESC, kz_json_history, KZ_FIRST_OF_SYNTAX);
+       SWITCH_ADD_API(api_interface, "kz_expand", KZ_FIRST_OF_DESC, kz_expand_api, KZ_FIRST_OF_SYNTAX);
 }
 
diff --git a/src/mod/event_handlers/mod_kazoo/kazoo_defs.h b/src/mod/event_handlers/mod_kazoo/kazoo_defs.h
new file mode 100644 (file)
index 0000000..7fe86dd
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef KAZOO_DEFS_H_
+#define KAZOO_DEFS_H_
+
+#define INTERACTION_VARIABLE "Call-Interaction-ID"
+
+#define UNIX_EPOCH_IN_GREGORIAN 62167219200
+#define UNIX_EPOCH_IN_GREGORIAN_STR "62167219200"
+
+#endif /* KAZOO_DEFS_H_ */
index 72e2afa18f9aaa58f674dc8cbc36b3cabb54a93e..da5354f3ec96395316b6b1568f5352adf6fde379 100644 (file)
@@ -891,7 +891,8 @@ SWITCH_STANDARD_APP(kz_att_xfer_function)
        }
 }
 
-void add_kz_dptools(switch_loadable_module_interface_t **module_interface, switch_application_interface_t *app_interface) {
+void add_kz_dptools(switch_loadable_module_interface_t **module_interface) {
+       switch_application_interface_t *app_interface = NULL;
        SWITCH_ADD_APP(app_interface, "kz_set", SET_SHORT_DESC, SET_LONG_DESC, set_function, SET_SYNTAX, SAF_SUPPORT_NOMEDIA | SAF_ROUTING_EXEC | SAF_ZOMBIE_EXEC);
        SWITCH_ADD_APP(app_interface, "kz_set_encoded", SET_SHORT_DESC, SET_LONG_DESC, set_encoded_function, SET_SYNTAX, SAF_SUPPORT_NOMEDIA | SAF_ROUTING_EXEC | SAF_ZOMBIE_EXEC);
        SWITCH_ADD_APP(app_interface, "kz_multiset", MULTISET_SHORT_DESC, MULTISET_LONG_DESC, multiset_function, MULTISET_SYNTAX, SAF_SUPPORT_NOMEDIA | SAF_ROUTING_EXEC | SAF_ZOMBIE_EXEC);
index b28a888978b1be0d2822d4ee3b8de86b600de558..4d247629e3882f49c51fa8b912281be8ee2e1af6 100644 (file)
@@ -10,6 +10,8 @@
 #define VERSION "mod_kazoo v1.5.0-1 community"
 
 #define KZ_MAX_SEPARATE_STRINGS 10
+#define HOSTNAME_MAX 1024
+#define NODENAME_MAX 1024
 
 typedef enum {KAZOO_FETCH_PROFILE, KAZOO_EVENT_PROFILE} kazoo_profile_type;
 
@@ -139,25 +141,7 @@ struct ei_xml_agent_s {
 
 };
 
-typedef enum {
-       KZ_TWEAK_INTERACTION_ID,
-       KZ_TWEAK_EXPORT_VARS,
-       KZ_TWEAK_SWITCH_URI,
-       KZ_TWEAK_REPLACES_CALL_ID,
-       KZ_TWEAK_LOOPBACK_VARS,
-       KZ_TWEAK_CALLER_ID,
-       KZ_TWEAK_TRANSFERS,
-       KZ_TWEAK_BRIDGE,
-       KZ_TWEAK_BRIDGE_REPLACES_ALEG,
-       KZ_TWEAK_BRIDGE_REPLACES_CALL_ID,
-       KZ_TWEAK_BRIDGE_VARIABLES,
-       KZ_TWEAK_RESTORE_CALLER_ID_ON_BLIND_XFER,
-
-       /* No new flags below this line */
-       KZ_TWEAK_MAX
-} kz_tweak_t;
-
-struct globals_s {
+struct kz_globals_s {
        switch_memory_pool_t *pool;
        switch_atomic_t threads;
        switch_socket_t *acceptor;
@@ -180,6 +164,7 @@ struct globals_s {
        int ei_compat_rel;
        char *ip;
        char *hostname;
+       struct hostent* hostname_ent;
        char *ei_cookie;
        char *ei_nodename;
        uint32_t flags;
@@ -209,8 +194,8 @@ struct globals_s {
 
 
 };
-typedef struct globals_s globals_t;
-extern globals_t kazoo_globals;
+typedef struct kz_globals_s kz_globals_t;
+extern kz_globals_t kazoo_globals;
 
 /* kazoo_event_stream.c */
 ei_event_stream_t *find_event_stream(ei_event_stream_t *event_streams, const erlang_pid *from);
index 0d2b95a8c23e8a5fbd6f15df6bea2d2fa56f3fa7..df3eec0bf6843c2e6b3f6377f0a8a2f3b3c67672 100644 (file)
@@ -95,6 +95,19 @@ static int read_cookie_from_file(char *filename) {
        }
 }
 
+void kz_set_hostname()
+{
+       if (kazoo_globals.hostname == NULL) {
+               char hostname[NODENAME_MAX];
+               memcpy(hostname, switch_core_get_hostname(), NODENAME_MAX);
+               kazoo_globals.hostname_ent = gethostbyname(hostname);
+               if(kazoo_globals.hostname_ent != NULL) {
+                       kazoo_globals.hostname = switch_core_strdup(kazoo_globals.pool, kazoo_globals.hostname_ent->h_name);
+               } else {
+                       kazoo_globals.hostname = switch_core_strdup(kazoo_globals.pool, hostname);
+               }
+       }
+}
 
 switch_status_t kazoo_ei_config(switch_xml_t cfg) {
        switch_xml_t child, param;
@@ -109,7 +122,7 @@ switch_status_t kazoo_ei_config(switch_xml_t cfg) {
        kazoo_globals.receive_msg_preallocate = 2000;
        kazoo_globals.event_stream_preallocate = KZ_DEFAULT_STREAM_PRE_ALLOCATE;
        kazoo_globals.send_msg_batch = 10;
-       kazoo_globals.event_stream_framing = 2;
+       kazoo_globals.event_stream_framing = 4;
        kazoo_globals.port = 0;
        kazoo_globals.io_fault_tolerance = 10;
        kazoo_globals.json_encoding = ERLANG_TUPLE;
@@ -211,11 +224,18 @@ switch_status_t kazoo_ei_config(switch_xml_t cfg) {
        }
 
        if ((child = switch_xml_child(cfg, "tweaks"))) {
+               char *default_tweaks = (char *) switch_xml_attr_soft(param, "default");
+               if (default_tweaks) {
+                       int i, v = switch_true(default_tweaks) ? 1 : 0;
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Set tweak default : %s\n", default_tweaks);
+                       for (i = 0; i < KZ_TWEAK_MAX; i++) kazoo_globals.tweaks[i] = v;
+               }
                for (param = switch_xml_child(child, "tweak"); param; param = param->next) {
                        kz_tweak_t tweak = KZ_TWEAK_MAX;
                        char *var = (char *) switch_xml_attr_soft(param, "name");
                        char *val = (char *) switch_xml_attr_soft(param, "value");
                        if(var && val && kz_name_tweak(var, &tweak) == SWITCH_STATUS_SUCCESS) {
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Set tweak %s : %s\n", var, val);
                                if(switch_true(val)) {
                                        kz_set_tweak(tweak);
                                } else {
@@ -230,6 +250,7 @@ switch_status_t kazoo_ei_config(switch_xml_t cfg) {
                        char *var = (char *) switch_xml_attr_soft(param, "name");
                        char *val = (char *) switch_xml_attr_soft(param, "value");
                        if(var && val) {
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Set core variable %s : %s\n", var, val);
                                switch_core_set_variable(var, val);
                        }
                }
index 514980b4268fb120f2f57d76e546ec47a11e1951..7def04974be353c2e042bdf64060cd9fae879a2f 100644 (file)
@@ -332,8 +332,6 @@ switch_socket_t *create_socket_with_port(switch_memory_pool_t *pool, switch_port
                return NULL;
        }
 
-       switch_getnameinfo(&kazoo_globals.hostname, sa, 0);
-
        if (kazoo_globals.nat_map && switch_nat_get_type()) {
                switch_nat_add_mapping(port, SWITCH_NAT_TCP, NULL, SWITCH_FALSE);
        }
index fba1adddb1cb5caaad295a0b5764b11f29fea621..111edfdb9043b25d6890f40f5c25997c4d34883c 100644 (file)
  */
 #include "mod_kazoo.h"
 
+#define INTERACTION_VARIABLE "Call-Interaction-ID"
+
+static const char *x_bridge_variables[] = {
+               "Call-Control-Queue",
+               "Call-Control-PID",
+               "Call-Control-Node",
+               INTERACTION_VARIABLE,
+               "ecallmgr_Ecallmgr-Node",
+               "sip_h_k-cid",
+               "Switch-URI",
+               "Switch-URL",
+               NULL
+};
+
+static void kz_tweaks_variables_to_event(switch_core_session_t *session, switch_event_t *event)
+{
+       int i;
+       switch_channel_t *channel = switch_core_session_get_channel(session);
+       for(i = 0; x_bridge_variables[i] != NULL; i++) {
+               const char *val = switch_channel_get_variable_dup(channel, x_bridge_variables[i], SWITCH_FALSE, -1);
+               switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, x_bridge_variables[i], val);
+       }
+}
+
 /* kazoo endpoint */
 switch_endpoint_interface_t *kz_endpoint_interface;
 static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *session,
-                                                                                                switch_event_t *var_event,
-                                                                                                switch_caller_profile_t *outbound_profile,
-                                                                                                switch_core_session_t **new_session, switch_memory_pool_t **pool, switch_originate_flag_t flags,
-                                                                                                switch_call_cause_t *cancel_cause);
+                                                       switch_event_t *var_event,
+                                                       switch_caller_profile_t *outbound_profile,
+                                                       switch_core_session_t **new_session, switch_memory_pool_t **pool, switch_originate_flag_t flags,
+                                                       switch_call_cause_t *cancel_cause);
 switch_io_routines_t kz_endpoint_io_routines = {
        /*.outgoing_channel */ kz_endpoint_outgoing_channel
 };
 
 static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *session,
-                                                                                                switch_event_t *var_event,
-                                                                                                switch_caller_profile_t *outbound_profile,
-                                                                                                switch_core_session_t **new_session, switch_memory_pool_t **pool, switch_originate_flag_t flags,
-                                                                                                switch_call_cause_t *cancel_cause)
+                                                       switch_event_t *var_event,
+                                                       switch_caller_profile_t *outbound_profile,
+                                                       switch_core_session_t **new_session, switch_memory_pool_t **pool, switch_originate_flag_t flags,
+                                                       switch_call_cause_t *cancel_cause)
 {
        switch_xml_t x_user = NULL, x_param, x_params;
        char *user = NULL, *domain = NULL, *dup_domain = NULL, *dialed_user = NULL;
@@ -58,8 +82,10 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
        switch_event_t *params = NULL, *var_event_orig = var_event;
        char stupid[128] = "";
        const char *skip = NULL, *var = NULL;
+       switch_core_session_t *a_session = NULL, *e_session = NULL;
 
        if (zstr(outbound_profile->destination_number)) {
+               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "NO DESTINATION NUMBER\n");
                goto done;
        }
 
@@ -84,6 +110,9 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
        switch_assert(params);
        switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "as_channel", "true");
        switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "action", "user_call");
+       if (session) {
+               switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "Unique-ID", switch_core_session_get_uuid(session));
+       }
 
        if (var_event) {
                switch_event_merge(params, var_event);
@@ -102,6 +131,20 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
                goto done;
        }
 
+       if ((x_params = switch_xml_child(x_user, "variables"))) {
+               for (x_param = switch_xml_child(x_params, "variable"); x_param; x_param = x_param->next) {
+                       const char *pvar = switch_xml_attr_soft(x_param, "name");
+                       const char *val = switch_xml_attr(x_param, "value");
+                       switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "adding variable to var_event => %s = %s\n", pvar, val);
+                       if (!var_event) {
+                               switch_event_create(&var_event, SWITCH_EVENT_GENERAL);
+                       } else {
+                               switch_event_del_header(var_event, pvar);
+                       }
+                       switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, pvar, val);
+               }
+       }
+
        if ((x_params = switch_xml_child(x_user, "params"))) {
                for (x_param = switch_xml_child(x_params, "param"); x_param; x_param = x_param->next) {
                        const char *pvar = switch_xml_attr_soft(x_param, "name");
@@ -115,7 +158,7 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
                                } else {
                                        switch_event_del_header(var_event, pvar + 9);
                                }
-                               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG1, "adding variable to var_event => %s = %s\n", pvar + 9, val);
+                               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "adding dialog var to var_event => %s = %s\n", pvar + 9, val);
                                switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, pvar + 9, val);
                        }
                }
@@ -126,9 +169,6 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
        if (var_event) {
                switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "dialed_user", dialed_user);
                switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "dialed_domain", domain);
-               if (!zstr(dest) && !strstr(dest, "presence_id=")) {
-                       switch_event_add_header(var_event, SWITCH_STACK_BOTTOM, "presence_id", "%s@%s", dialed_user, domain);
-               }
        }
 
        if (!dest) {
@@ -141,16 +181,27 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
                switch_originate_flag_t myflags = SOF_NONE;
                char *cid_name_override = NULL;
                char *cid_num_override = NULL;
+               switch_event_t *event = NULL;
 
                if (var_event) {
                        cid_name_override = switch_event_get_header(var_event, "origination_caller_id_name");
                        cid_num_override = switch_event_get_header(var_event, "origination_caller_id_number");
                }
 
-               if (session) {
-                       switch_event_t *event = NULL;
+               if(session) {
+                       a_session = session;
+               } else if(var_event) {
+                       const char* uuid_e_session = switch_event_get_header(var_event, "ent_originate_aleg_uuid");
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "CHECKING ORIGINATE-UUID : %s\n", uuid_e_session);
+                       if (uuid_e_session && (e_session = switch_core_session_force_locate(uuid_e_session)) != NULL) {
+                               a_session = e_session;
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "FOUND ORIGINATE-UUID : %s\n", uuid_e_session);
+                       }
+               }
+
+               if (a_session) {
                        switch_event_create(&event, SWITCH_EVENT_GENERAL);
-                       channel = switch_core_session_get_channel(session);
+                       channel = switch_core_session_get_channel(a_session);
                        if ((varval = switch_channel_get_variable(channel, SWITCH_CALL_TIMEOUT_VARIABLE))
                                || (var_event && (varval = switch_event_get_header(var_event, "leg_timeout")))) {
                                timelimit = atoi(varval);
@@ -163,13 +214,7 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
                        switch_channel_set_variable(channel, "dialed_user", dialed_user);
                        switch_channel_set_variable(channel, "dialed_domain", domain);
 
-                       d_dest = switch_event_expand_headers(event, dest);
-
-                       switch_event_destroy(&event);
-
                } else {
-                       switch_event_t *event = NULL;
-
                        if (var_event) {
                                switch_event_dup(&event, var_event);
                                switch_event_del_header(event, "dialed_user");
@@ -185,10 +230,48 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
 
                        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "dialed_user", dialed_user);
                        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "dialed_domain", domain);
-                       d_dest = switch_event_expand_headers(event, dest);
-                       switch_event_destroy(&event);
                }
 
+               if ((x_params = switch_xml_child(x_user, "profile-variables"))) {
+                       for (x_param = switch_xml_child(x_params, "variable"); x_param; x_param = x_param->next) {
+                               const char *pvar = switch_xml_attr_soft(x_param, "name");
+                               const char *val = switch_xml_attr(x_param, "value");
+                               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "adding profile variable to event => %s = %s\n", pvar, val);
+                               switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, pvar, val);
+                       }
+               }
+
+               if ((x_params = switch_xml_child(x_user, "variables"))) {
+                       for (x_param = switch_xml_child(x_params, "variable"); x_param; x_param = x_param->next) {
+                               const char *pvar = switch_xml_attr_soft(x_param, "name");
+                               const char *val = switch_xml_attr(x_param, "value");
+                               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "adding variable to event => %s = %s\n", pvar, val);
+                               switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, pvar, val);
+                       }
+               }
+
+               if ((x_params = switch_xml_child(x_user, "params"))) {
+                       for (x_param = switch_xml_child(x_params, "param"); x_param; x_param = x_param->next) {
+                               const char *pvar = switch_xml_attr_soft(x_param, "name");
+                               const char *val = switch_xml_attr(x_param, "value");
+
+                               if (!strncasecmp(pvar, "dial-var-", 9)) {
+                                       switch_event_del_header(event, pvar + 9);
+                                       switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "adding dialog var to event => %s = %s\n", pvar + 9, val);
+                                       switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, pvar + 9, val);
+                               }
+                       }
+               }
+
+               d_dest = kz_event_expand_headers(event, dest);
+
+               if (var_event) {
+                       kz_expand_headers(event, var_event);
+               }
+
+               switch_event_destroy(&event);
+
+
                if ((flags & SOF_NO_LIMITS)) {
                        myflags |= SOF_NO_LIMITS;
                }
@@ -197,10 +280,19 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
                        myflags |= SOF_NOBLOCK;
                }
 
+               if ( a_session ) {
+                       if(var_event) {
+                               kz_tweaks_variables_to_event(a_session, var_event);
+                       }
+               }
+
+               if(e_session) {
+                       switch_core_session_rwunlock(e_session);
+               }
+
                switch_snprintf(stupid, sizeof(stupid), "kz/%s", dialed_user);
                if (switch_stristr(stupid, d_dest)) {
-                       switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Waddya Daft? You almost called '%s' in an infinate loop!\n",
-                                                         stupid);
+                       switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Waddya Daft? You almost called '%s' in an infinate loop!\n", stupid);
                        cause = SWITCH_CAUSE_INVALID_IE_CONTENTS;
                } else if (switch_ivr_originate(session, new_session, &cause, d_dest, timelimit, NULL,
                                                                                cid_name_override, cid_num_override, outbound_profile, var_event, myflags,
@@ -220,6 +312,7 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
                                }
                        }
 
+                       /*
                        if ((x_params = switch_xml_child(x_user, "variables"))) {
                                for (x_param = switch_xml_child(x_params, "variable"); x_param; x_param = x_param->next) {
                                        const char *pvar = switch_xml_attr(x_param, "name");
@@ -227,6 +320,7 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
                                        switch_channel_set_variable(new_channel, pvar, val);
                                }
                        }
+                       */
 
                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(*new_session), SWITCH_LOG_DEBUG1, "CHECKING CALLER-ID\n");
                        if ((x_params = switch_xml_child(x_user, "profile-variables"))) {
@@ -272,7 +366,6 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
 
        switch_safe_free(user);
        switch_safe_free(dup_domain);
-
        return cause;
 }
 
index 74ddff66473b0c49f48bb038358b9c95cf93b664..a35dfab080f641bcd153f8c1dc00f588ec175bfa 100644 (file)
@@ -232,7 +232,9 @@ cJSON * kazoo_event_add_field_to_json(cJSON *dst, switch_event_t *src, kazoo_fie
                        expanded = kz_event_expand_headers(src, field->value);
                        if(expanded != NULL && !zstr(expanded)) {
                                item = kazoo_event_add_json_value(dst, field, field->as ? field->as : field->name, expanded);
-                               free(expanded);
+                               if(expanded != field->value) {
+                                       free(expanded);
+                               }
                        }
                        break;
 
@@ -325,6 +327,21 @@ static switch_status_t kazoo_event_add_fields_to_json(kazoo_logging_ptr logging,
     return SWITCH_STATUS_SUCCESS;
 }
 
+#define EVENT_TIMESTAMP_FIELD "Event-Date-Timestamp"
+#define JSON_TIMESTAMP_FIELD "Event-Timestamp"
+
+static switch_status_t kazoo_event_add_timestamp(switch_event_t* evt, cJSON* JObj)
+{
+       switch_event_header_t *header;
+       cJSON *item = NULL;
+       if((header = switch_event_get_header_ptr(evt, EVENT_TIMESTAMP_FIELD)) != NULL) {
+               if ((item = kazoo_event_json_value(JSON_NUMBER, header->value)) != NULL) {
+                       kazoo_cJSON_AddItemToObject(JObj, JSON_TIMESTAMP_FIELD, item);
+                       return SWITCH_STATUS_SUCCESS;
+               }
+       }
+       return SWITCH_STATUS_NOTFOUND;
+}
 
 kazoo_message_ptr kazoo_message_create_event(switch_event_t* evt, kazoo_event_ptr event, kazoo_event_profile_ptr profile)
 {
@@ -366,6 +383,8 @@ kazoo_message_ptr kazoo_message_create_event(switch_event_t* evt, kazoo_event_pt
 
        kazoo_event_init_json(profile->fields, event ? event->fields : NULL, evt, &JObj);
 
+       kazoo_event_add_timestamp(evt, JObj);
+
        if(profile->fields)
                kazoo_event_add_fields_to_json(&logging, JObj, evt, profile->fields->head);
 
@@ -402,6 +421,8 @@ kazoo_message_ptr kazoo_message_create_fetch(switch_event_t* evt, kazoo_fetch_pr
 
        kazoo_event_init_json(profile->fields, NULL, evt, &JObj);
 
+       kazoo_event_add_timestamp(evt, JObj);
+
        if(profile->fields)
                kazoo_event_add_fields_to_json(&logging, JObj, evt, profile->fields->head);
 
index 67f4d37a9904a8f059a26e376a7b3444869d9d3d..2afcdf1a6a2a47888f7996f427da7e9c023f8b34 100644 (file)
@@ -30,9 +30,6 @@
  */
 #include "mod_kazoo.h"
 
-#define INTERACTION_VARIABLE "Call-Interaction-ID"
-#define BRIDGE_INTERCEPT_VARIABLE "Bridge-Intercepted"
-
 static char *TWEAK_NAMES[] = {
        "interaction-id",
        "export-vars",
@@ -69,7 +66,6 @@ static switch_status_t kz_tweaks_signal_bridge_on_hangup(switch_core_session_t *
 
        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "tweak signal bridge on hangup: %s , %s\n", switch_core_session_get_uuid(session), peer_uuid);
 
-
        if (switch_event_create(&my_event, SWITCH_EVENT_CHANNEL_UNBRIDGE) == SWITCH_STATUS_SUCCESS) {
                switch_event_add_header_string(my_event, SWITCH_STACK_BOTTOM, "Bridge-A-Unique-ID", switch_core_session_get_uuid(session));
                switch_event_add_header_string(my_event, SWITCH_STACK_BOTTOM, "Bridge-B-Unique-ID", peer_uuid);
@@ -106,7 +102,6 @@ static void kz_tweaks_handle_bridge_variables(switch_event_t *event)
 
        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "tweak bridge event handler: variables : %s , %s\n", a_leg, b_leg);
 
-
        if (a_leg && (a_session = switch_core_session_force_locate(a_leg)) != NULL) {
                switch_channel_t *a_channel = switch_core_session_get_channel(a_session);
                if(switch_channel_get_variable_dup(a_channel, bridge_variables[0], SWITCH_FALSE, -1) == NULL) {
@@ -137,56 +132,91 @@ static void kz_tweaks_handle_bridge_variables(switch_event_t *event)
 
 }
 
-static void kz_tweaks_handle_bridge_intercepted(switch_event_t *event)
+static void kz_tweaks_handle_bridge_replaces_aleg(switch_event_t *event)
 {
        switch_event_t *my_event;
-       switch_core_session_t *a_session = NULL;
-       switch_core_session_t *b_session = NULL;
 
-       const char *uuid = switch_event_get_header(event, "Unique-ID");
-       const char *a_leg = switch_event_get_header(event, "Bridge-A-Unique-ID");
-       const char *b_leg = switch_event_get_header(event, "Bridge-B-Unique-ID");
-       const char *bridge_intercepted = NULL;
+       const char *replaced_call_id =  switch_event_get_header(event, "variable_sip_replaces_call_id");
+       const char *a_leg_call_id =     switch_event_get_header(event, "variable_sip_replaces_a-leg");
+       const char *peer_uuid = switch_event_get_header(event, "Unique-ID");
+       int processed = 0;
 
        if (!kz_test_tweak(KZ_TWEAK_BRIDGE_REPLACES_ALEG)) return;
 
-       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "tweak bridge event handler: intercepted : %s , %s, %s\n", uuid, a_leg, b_leg);
 
+       if(a_leg_call_id && replaced_call_id) {
+               const char *call_id = switch_event_get_header(event, "Bridge-B-Unique-ID");
+               switch_core_session_t *session = NULL;
+               if ((session = switch_core_session_locate(peer_uuid)) != NULL) {
+                       switch_channel_t *channel = switch_core_session_get_channel(session);
+                       processed = switch_true(switch_channel_get_variable_dup(channel, "Bridge-Event-Processed", SWITCH_FALSE, -1));
+                       switch_channel_set_variable(channel, "Bridge-Event-Processed", "true");
+                       switch_core_session_rwunlock(session);
+               }
 
-       if ((a_session = switch_core_session_locate(a_leg)) != NULL) {
-               switch_channel_t *a_channel = switch_core_session_get_channel(a_session);
-               bridge_intercepted = switch_channel_get_variable_dup(a_channel, BRIDGE_INTERCEPT_VARIABLE, SWITCH_TRUE, -1);
-               switch_channel_set_variable(a_channel, BRIDGE_INTERCEPT_VARIABLE, NULL);
-               if (bridge_intercepted && switch_true(bridge_intercepted)) {
-                       switch_channel_set_variable(a_channel, "Bridge-B-Unique-ID", b_leg);
-                       switch_channel_add_state_handler(a_channel, &kz_tweaks_signal_bridge_state_handlers);
-
-                       if ((b_session = switch_core_session_locate(b_leg)) != NULL) {
-                               switch_channel_t *b_channel = switch_core_session_get_channel(b_session);
-                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "creating channel_bridge event A - %s , B - %s\n", switch_core_session_get_uuid(b_session), uuid);
-                               if (switch_event_create(&my_event, SWITCH_EVENT_CHANNEL_BRIDGE) == SWITCH_STATUS_SUCCESS) {
-                                       switch_event_add_header_string(my_event, SWITCH_STACK_BOTTOM, "Bridge-A-Unique-ID", switch_core_session_get_uuid(b_session));
-                                       switch_event_add_header_string(my_event, SWITCH_STACK_BOTTOM, "Bridge-B-Unique-ID", uuid);
-                                       switch_channel_event_set_data(b_channel, my_event);
-                                       switch_event_fire(&my_event);
+               if(processed) {
+                       if(call_id) {
+                               if((session = switch_core_session_locate(call_id)) != NULL) {
+                                       switch_channel_t *channel = switch_core_session_get_channel(session);
+                                       switch_channel_set_variable(channel, "Bridge-Event-Processed", "true");
+                                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "creating channel_bridge event A - %s , B - %s\n", switch_core_session_get_uuid(session), peer_uuid);
+                                       if (switch_event_create(&my_event, SWITCH_EVENT_CHANNEL_BRIDGE) == SWITCH_STATUS_SUCCESS) {
+                                               switch_event_add_header_string(my_event, SWITCH_STACK_BOTTOM, "Bridge-A-Unique-ID", switch_core_session_get_uuid(session));
+                                               switch_event_add_header_string(my_event, SWITCH_STACK_BOTTOM, "Bridge-B-Unique-ID", peer_uuid);
+                                               switch_channel_event_set_data(channel, my_event);
+                                               switch_event_fire(&my_event);
+                                       }
+                                       switch_channel_set_variable(channel, "Bridge-B-Unique-ID", peer_uuid);
+                                       switch_channel_add_state_handler(channel, &kz_tweaks_signal_bridge_state_handlers);
+                                       switch_core_session_rwunlock(session);
+                               } else {
+                                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "invalid session : %s\n", call_id);
+                                       DUMP_EVENT(event);
                                }
-                               switch_core_session_rwunlock(b_session);
                        }
                }
-               switch_core_session_rwunlock(a_session);
+
        }
-       switch_safe_strdup(bridge_intercepted);
+
 }
 
-static void kz_tweaks_channel_bridge_event_handler(switch_event_t *event)
+static void kz_tweaks_handle_bridge_replaces_call_id(switch_event_t *event)
 {
-       const char *uuid = switch_event_get_header(event, "Unique-ID");
+       switch_event_t *my_event;
 
-       if (!kz_test_tweak(KZ_TWEAK_BRIDGE)) return;
+       const char *replaced_call_id =  switch_event_get_header(event, "variable_sip_replaces_call_id");
+       const char *a_leg_call_id =     switch_event_get_header(event, "variable_sip_replaces_a-leg");
+       const char *peer_uuid = switch_event_get_header(event, "Unique-ID");
+
+       if (!kz_test_tweak(KZ_TWEAK_BRIDGE_REPLACES_CALL_ID)) return;
+
+       if(a_leg_call_id && replaced_call_id) {
+               switch_core_session_t *call_session = NULL;
+               const char *call_id = switch_event_get_header(event, "Bridge-B-Unique-ID");
+               if (call_id && (call_session = switch_core_session_force_locate(call_id)) != NULL) {
+                       switch_channel_t *call_channel = switch_core_session_get_channel(call_session);
+                       if (switch_event_create(&my_event, SWITCH_EVENT_CHANNEL_BRIDGE) == SWITCH_STATUS_SUCCESS) {
+                               switch_event_add_header_string(my_event, SWITCH_STACK_BOTTOM, "Bridge-A-Unique-ID", switch_core_session_get_uuid(call_session));
+                               switch_event_add_header_string(my_event, SWITCH_STACK_BOTTOM, "Bridge-B-Unique-ID", peer_uuid);
+                               switch_channel_event_set_data(call_channel, my_event);
+                               switch_event_fire(&my_event);
+                       }
+                       switch_channel_set_variable(call_channel, "Bridge-B-Unique-ID", peer_uuid);
+                       switch_channel_add_state_handler(call_channel, &kz_tweaks_signal_bridge_state_handlers);
+                       switch_core_session_rwunlock(call_session);
+               } else {
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "NOT FOUND : %s\n", call_id);
+               }
+       }
 
-       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "tweak bridge event handler: %s\n", uuid);
+}
+
+static void kz_tweaks_channel_bridge_event_handler(switch_event_t *event)
+{
+       if (!kz_test_tweak(KZ_TWEAK_BRIDGE)) return;
 
-       kz_tweaks_handle_bridge_intercepted(event);
+       kz_tweaks_handle_bridge_replaces_call_id(event);
+       kz_tweaks_handle_bridge_replaces_aleg(event);
        kz_tweaks_handle_bridge_variables(event);
 }
 
@@ -204,21 +234,12 @@ static void kz_tweaks_channel_replaced_event_handler(switch_event_t *event)
 
 static void kz_tweaks_channel_intercepted_event_handler(switch_event_t *event)
 {
-       switch_core_session_t *uuid_session = NULL;
        const char *uuid = switch_event_get_header(event, "Unique-ID");
        const char *peer_uuid = switch_event_get_header(event, "intercepted_by");
 
        if (!kz_test_tweak(KZ_TWEAK_TRANSFERS)) return;
 
-       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "tweak intercepted handler : %s was intercepted by %s\n", uuid, peer_uuid);
-
-       if ((uuid_session = switch_core_session_force_locate(peer_uuid)) != NULL) {
-               switch_channel_t *uuid_channel = switch_core_session_get_channel(uuid_session);
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "marking %s for channel_bridge handling\n", peer_uuid);
-               switch_channel_set_variable(uuid_channel, BRIDGE_INTERCEPT_VARIABLE, "true");
-               switch_core_session_rwunlock(uuid_session);
-       }
-
+       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "INTERCEPTED : %s => %s\n", uuid, peer_uuid);
 }
 
 static void kz_tweaks_channel_transferor_event_handler(switch_event_t *event)
@@ -423,30 +444,6 @@ static void kz_tweaks_handle_caller_id(switch_core_session_t *session)
        }
 }
 
-/*
-static switch_status_t kz_tweaks_handle_auth_token(switch_core_session_t *session)
-{
-       switch_channel_t *channel = switch_core_session_get_channel(session);
-       switch_event_t *event;
-       const char *token = switch_channel_get_variable(channel, "sip_h_X-FS-Auth-Token");
-       if(token) {
-               switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Authenticating user for nightmare xfer %s\n", token);
-               if (switch_ivr_set_user(session, token) == SWITCH_STATUS_SUCCESS) {
-                       if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_DATA) == SWITCH_STATUS_SUCCESS) {
-                               switch_channel_event_set_data(channel, event);
-                               switch_event_fire(&event);
-                       }
-                       switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Authenticated user from nightmare xfer %s\n", token);
-               } else {
-                       switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Error Authenticating user for nightmare xfer %s\n", token);
-               }
-       }
-
-       return SWITCH_STATUS_SUCCESS;
-
-}
-*/
-
 static switch_status_t kz_tweaks_handle_nightmare_xfer_interaction_id(switch_core_session_t *session)
 {
        if (kz_test_tweak(KZ_TWEAK_INTERACTION_ID)) {
@@ -525,16 +522,26 @@ static switch_status_t kz_tweaks_handle_switch_uri(switch_core_session_t *sessio
 
 }
 
+static char * kz_tweaks_new_interaction_id()
+{
+       long int first = (switch_micro_time_now() / 1000000) + UNIX_EPOCH_IN_GREGORIAN;
+       char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
+       switch_uuid_t uuid;
+       switch_uuid_get(&uuid);
+       switch_uuid_format(uuid_str, &uuid);
+       uuid_str[8] = '\0';
+       return switch_mprintf("%ld-%s", first, uuid_str);
+}
+
 static void kz_tweaks_handle_interaction_id(switch_core_session_t *session)
 {
-       const char *expr = "${expr(ceil((${Event-Date-Timestamp} / 1000000) + $${UNIX_EPOCH_IN_GREGORIAN}))}-${regex(${create_uuid()}|^([^-]*)|%1)}";
        switch_channel_t *channel = switch_core_session_get_channel(session);
        char * val = NULL;
        switch_core_session_t *peer_session = NULL;
        const char* peer_interaction_id = NULL;
 
        if (kz_test_tweak(KZ_TWEAK_INTERACTION_ID)) {
-               val = kz_expand(expr);
+               val = kz_tweaks_new_interaction_id();
                if (val) {
                        switch_channel_set_variable(channel, "Original-"INTERACTION_VARIABLE, val);
                        if(switch_core_session_get_partner(session, &peer_session) == SWITCH_STATUS_SUCCESS) {
@@ -583,32 +590,6 @@ static switch_status_t kz_tweaks_register_handle_blind_xfer(switch_core_session_
        return SWITCH_STATUS_SUCCESS;
 }
 
-static switch_status_t kz_tweaks_set_export_vars(switch_core_session_t *session)
-{
-       switch_channel_t *channel = switch_core_session_get_channel(session);
-       const char *exports;
-       char *var, *new_exports, *new_exports_d = NULL;
-
-       if (kz_test_tweak(KZ_TWEAK_EXPORT_VARS)) {
-               exports = switch_channel_get_variable(channel, SWITCH_EXPORT_VARS_VARIABLE);
-               var = switch_core_session_strdup(session, "Switch-URI,Switch-URL");
-
-               if (exports) {
-                       new_exports_d = switch_mprintf("%s,%s", exports, var);
-                       new_exports = new_exports_d;
-               } else {
-                       new_exports = var;
-               }
-
-               switch_channel_set_variable(channel, SWITCH_EXPORT_VARS_VARIABLE, new_exports);
-
-               switch_safe_free(new_exports_d);
-       }
-
-
-       return SWITCH_STATUS_SUCCESS;
-}
-
 static switch_status_t kz_tweaks_on_init(switch_core_session_t *session)
 {
        switch_channel_t *channel = switch_core_session_get_channel(session);
@@ -618,12 +599,10 @@ static switch_status_t kz_tweaks_on_init(switch_core_session_t *session)
        kz_tweaks_handle_interaction_id(session);
        kz_tweaks_handle_switch_uri(session);
        kz_tweaks_handle_caller_id(session);
-//     kz_tweaks_handle_auth_token(session);
        kz_tweaks_handle_nightmare_xfer_interaction_id(session);
        kz_tweaks_handle_replaces_call_id(session);
        kz_tweaks_handle_loopback(session);
        kz_tweaks_register_handle_blind_xfer(session);
-       kz_tweaks_set_export_vars(session);
 
        return SWITCH_STATUS_SUCCESS;
 }
@@ -693,7 +672,7 @@ static void kz_tweaks_unbind_events()
 
 void kz_tweaks_add_core_variables()
 {
-       switch_core_set_variable("UNIX_EPOCH_IN_GREGORIAN", "62167219200");
+       switch_core_set_variable("UNIX_EPOCH_IN_GREGORIAN", UNIX_EPOCH_IN_GREGORIAN_STR);
 }
 
 void kz_tweaks_start()
diff --git a/src/mod/event_handlers/mod_kazoo/kazoo_tweaks.h b/src/mod/event_handlers/mod_kazoo/kazoo_tweaks.h
new file mode 100644 (file)
index 0000000..11cf5f6
--- /dev/null
@@ -0,0 +1,32 @@
+#pragma once
+
+#include <switch.h>
+
+typedef enum {
+       KZ_TWEAK_INTERACTION_ID,
+       KZ_TWEAK_EXPORT_VARS,
+       KZ_TWEAK_SWITCH_URI,
+       KZ_TWEAK_REPLACES_CALL_ID,
+       KZ_TWEAK_LOOPBACK_VARS,
+       KZ_TWEAK_CALLER_ID,
+       KZ_TWEAK_TRANSFERS,
+       KZ_TWEAK_BRIDGE,
+       KZ_TWEAK_BRIDGE_REPLACES_ALEG,
+       KZ_TWEAK_BRIDGE_REPLACES_CALL_ID,
+       KZ_TWEAK_BRIDGE_VARIABLES,
+       KZ_TWEAK_RESTORE_CALLER_ID_ON_BLIND_XFER,
+
+       /* No new flags below this line */
+       KZ_TWEAK_MAX
+} kz_tweak_t;
+
+void kz_tweaks_start();
+void kz_tweaks_stop();
+SWITCH_DECLARE(const char *) kz_tweak_name(kz_tweak_t tweak);
+SWITCH_DECLARE(switch_status_t) kz_name_tweak(const char *name, kz_tweak_t *type);
+
+
+#define kz_test_tweak(flag) (kazoo_globals.tweaks[flag] ? 1 : 0)
+#define kz_set_tweak(flag) kazoo_globals.tweaks[flag] = 1
+#define kz_clear_tweak(flag) kazoo_globals.tweaks[flag] = 0
+
index f4050bb34a77d7c092abafa4e012f04d86017761..afc685434fb48870a2949ec7976089978195eb78 100644 (file)
@@ -90,15 +90,11 @@ SWITCH_DECLARE(switch_status_t) kz_expand_api_execute(const char *cmd, const cha
        switch_assert(stream->data != NULL);
        switch_assert(stream->write_function != NULL);
 
-       if (strcasecmp(cmd, "console_complete")) {
-               cmd_used = switch_strip_whitespace(cmd);
-               arg_used = switch_strip_whitespace(arg);
-       } else {
-               cmd_used = (char *) cmd;
-               arg_used = (char *) arg;
-       }
+       cmd_used = switch_strip_whitespace(cmd);
+       arg_used = switch_strip_whitespace(arg);
 
        if (cmd_used && (api = switch_loadable_module_get_api_interface(cmd_used)) != 0) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "executing [%s] => [%s]\n", cmd_used, arg_used);
                if ((status = api->function(arg_used, session, stream)) != SWITCH_STATUS_SUCCESS) {
                        stream->write_function(stream, "COMMAND RETURNED ERROR!\n");
                }
@@ -148,6 +144,7 @@ SWITCH_DECLARE(char *) kz_event_expand_headers_check(switch_event_t *event, cons
        nv = 0;
        olen = strlen(in) + 1;
        indup = strdup(in);
+       switch_assert(indup);
        endof_indup = end_of_p(indup) + 1;
 
        if ((data = malloc(olen))) {
@@ -342,46 +339,37 @@ SWITCH_DECLARE(char *) kz_event_expand_headers_check(switch_event_t *event, cons
                                } else {
                                        switch_stream_handle_t stream = { 0 };
                                        char *expanded = NULL;
+                                       char *expanded_vname = NULL;
 
                                        SWITCH_STANDARD_STREAM(stream);
 
-                                       if (stream.data) {
-                                               char *expanded_vname = NULL;
-
-                                               if ((expanded_vname = kz_event_expand_headers_check(event, (char *) vname, var_list, api_list, recur+1)) == vname) {
-                                                       expanded_vname = NULL;
-                                               } else {
-                                                       vname = expanded_vname;
-                                               }
+                                       if ((expanded_vname = kz_event_expand_headers_check(event, (char *) vname, var_list, api_list, recur+1)) == vname) {
+                                               expanded_vname = NULL;
+                                       } else {
+                                               vname = expanded_vname;
+                                       }
 
-                                               if ((expanded = kz_event_expand_headers_check(event, vval, var_list, api_list, recur+1)) == vval) {
-                                                       expanded = NULL;
-                                               } else {
-                                                       vval = expanded;
-                                               }
+                                       if ((expanded = kz_event_expand_headers_check(event, vval, var_list, api_list, recur+1)) == vval) {
+                                               expanded = NULL;
+                                       } else {
+                                               vval = expanded;
+                                       }
 
-                                               if (!switch_core_test_flag(SCF_API_EXPANSION) || (api_list && !switch_event_check_permission_list(api_list, vname))) {
-                                                       func_val = NULL;
-                                                       sub_val = "<API execute Permission Denied>";
+                                       if (!switch_core_test_flag(SCF_API_EXPANSION) || (api_list && !switch_event_check_permission_list(api_list, vname))) {
+                                               func_val = NULL;
+                                               sub_val = "<API execute Permission Denied>";
+                                       } else {
+                                               stream.param_event = event;
+                                               if (kz_expand_api_execute(vname, vval, NULL, &stream) == SWITCH_STATUS_SUCCESS) {
+                                                       func_val = stream.data;
+                                                       sub_val = func_val;
                                                } else {
-                                                       stream.param_event = event;
-                                                       if (kz_expand_api_execute(vname, vval, NULL, &stream) == SWITCH_STATUS_SUCCESS) {
-                                                               func_val = stream.data;
-                                                               sub_val = func_val;
-                                                       } else {
-                                                               free(stream.data);
-                                                       }
+                                                       free(stream.data);
                                                }
-
-                                               switch_safe_free(expanded);
-                                               switch_safe_free(expanded_vname);
-
-                                       } else {
-                                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
-                                               free(data);
-                                               free(indup);
-                                               return (char *) in;
                                        }
+
+                                       switch_safe_free(expanded);
+                                       switch_safe_free(expanded_vname);
                                }
                                if ((nlen = sub_val ? strlen(sub_val) : 0)) {
                                        if (len + nlen >= olen) {
@@ -435,6 +423,21 @@ SWITCH_DECLARE(char *) kz_event_expand_headers(switch_event_t *event, const char
        return kz_event_expand_headers_check(event, in, NULL, NULL, 0);
 }
 
+SWITCH_DECLARE(char *) kz_event_expand_headers_pool(switch_memory_pool_t *pool, switch_event_t *event, char *val)
+{
+       char *expanded;
+       char *dup = NULL;
+
+       expanded = kz_event_expand_headers(event, val);
+       dup = switch_core_strdup(pool, expanded);
+
+       if (expanded != val) {
+               free(expanded);
+       }
+
+       return dup;
+}
+
 SWITCH_DECLARE(char *) kz_event_expand(const char *in)
 {
        switch_event_t *event = NULL;
@@ -565,6 +568,35 @@ void kz_event_decode(switch_event_t *event)
        }
 }
 
+void kz_expand_headers(switch_event_t *resolver, switch_event_t *event) {
+       switch_event_t *clone = NULL;
+       switch_event_header_t *header = NULL;
+       switch_event_create_plain(&clone, event->event_id);
+
+       for(header = event->headers; header; header = header->next) {
+               char *expanded = kz_event_expand_headers(resolver, header->value);
+               if (expanded != header->value) {
+                       switch_event_add_header_string(clone, SWITCH_STACK_BOTTOM, header->name, expanded);
+                       switch_safe_free(expanded);
+               }
+       }
+
+       /* we don't want to force unique headers
+        * so we delete and then merge
+        */
+       for(header = clone->headers; header; header = header->next) {
+               switch_event_del_header(event, header->name);
+       }
+
+       switch_event_merge(event, clone);
+
+       switch_event_destroy(&clone);
+}
+
+void kz_expand_headers_self(switch_event_t *event) {
+       kz_expand_headers(event, event);
+}
+
 char * kz_expand_vars(char *xml_str) {
        return kz_expand_vars_pool(xml_str, NULL);
 }
diff --git a/src/mod/event_handlers/mod_kazoo/kazoo_utils.h b/src/mod/event_handlers/mod_kazoo/kazoo_utils.h
new file mode 100644 (file)
index 0000000..397d94a
--- /dev/null
@@ -0,0 +1,51 @@
+#pragma once
+
+#include <switch.h>
+
+#ifdef __cplusplus
+#define KZ_BEGIN_EXTERN_C       extern "C" {
+#define KZ_END_EXTERN_C         }
+#else
+#define KZ_BEGIN_EXTERN_C
+#define KZ_END_EXTERN_C
+#endif
+
+KZ_BEGIN_EXTERN_C
+
+void kz_check_set_profile_var(switch_channel_t *channel, char* var, char *val);
+
+SWITCH_DECLARE(switch_status_t) kz_switch_core_merge_variables(switch_event_t *event);
+
+SWITCH_DECLARE(switch_status_t) kz_switch_core_base_headers_for_expand(switch_event_t **event);
+
+SWITCH_DECLARE(switch_status_t) kz_expand_api_execute(const char *cmd, const char *arg, switch_core_session_t *session, switch_stream_handle_t *stream);
+
+SWITCH_DECLARE(char *) kz_event_expand_headers_check(switch_event_t *event, const char *in, switch_event_t *var_list, switch_event_t *api_list, uint32_t recur);
+
+SWITCH_DECLARE(char *) kz_event_expand_headers(switch_event_t *event, const char *in);
+
+SWITCH_DECLARE(char *) kz_event_expand_headers_pool(switch_memory_pool_t *pool, switch_event_t *event, char *val);
+
+SWITCH_DECLARE(char *) kz_event_expand(const char *in);
+
+SWITCH_DECLARE(char *) kz_expand(const char *in);
+
+SWITCH_DECLARE(char *) kz_expand_pool(switch_memory_pool_t *pool, const char *in);
+
+char* kz_switch_event_get_first_of(switch_event_t *event, const char *list[]);
+
+SWITCH_DECLARE(switch_status_t) kz_switch_event_add_variable_name_printf(switch_event_t *event, switch_stack_t stack, const char *val, const char *fmt, ...);
+
+SWITCH_DECLARE(switch_xml_t) kz_xml_child(switch_xml_t xml, const char *name);
+
+void kz_xml_process(switch_xml_t cfg);
+void kz_event_decode(switch_event_t *event);
+
+char * kz_expand_vars(char *xml_str);
+void kz_expand_headers(switch_event_t *resolver, switch_event_t *event);
+void kz_expand_headers_self(switch_event_t *event);
+
+char * kz_expand_vars_pool(char *xml_str, switch_memory_pool_t *pool);
+switch_status_t kz_json_api(const char * command, cJSON *args, cJSON **res);
+
+KZ_END_EXTERN_C
index 26c362980be6f32203f9cee434c7a2d80608f2be..1c50d0586e9930fb6ce5165f625191a43e6f3895 100644 (file)
@@ -86,6 +86,6 @@ SWITCH_STANDARD_JSON_API(kz_node_info_json_function)
 
 void add_kz_node(switch_loadable_module_interface_t **module_interface)
 {
-       switch_json_api_interface_t *json_api_interface  = NULL;
+       switch_json_api_interface_t *json_api_interface = NULL;
        SWITCH_ADD_JSON_API(json_api_interface, "node.info", "JSON node API", kz_node_info_json_function, "");
 }
index ca0538039bc1c411ba18cd9e63508bad64e4f137..6a4a9a4ce668de913f63310fa17847fcc6dd3565 100644 (file)
  */
 #include "mod_kazoo.h"
 
-globals_t kazoo_globals = {0};
+kz_globals_t kazoo_globals = {0};
 
 
 
 SWITCH_MODULE_DEFINITION(mod_kazoo, mod_kazoo_load, mod_kazoo_shutdown, mod_kazoo_runtime);
 
 SWITCH_MODULE_LOAD_FUNCTION(mod_kazoo_load) {
-       switch_api_interface_t *api_interface = NULL;
-       switch_application_interface_t *app_interface = NULL;
 
        memset(&kazoo_globals, 0, sizeof(kazoo_globals));
-
        kazoo_globals.pool = pool;
-       kazoo_globals.ei_nodes = NULL;
-
-       // ensure epmd is running
-
        if(kazoo_load_config() != SWITCH_STATUS_SUCCESS) {
                // TODO: what would we need to clean up here?
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Improper configuration!\n");
@@ -66,13 +59,13 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_kazoo_load) {
        bind_fetch_agents();
 
        /* create an api for cli debug commands */
-       add_cli_api(module_interface, api_interface);
+       add_cli_api(module_interface);
 
        /* add our modified commands */
-       add_kz_commands(module_interface, api_interface);
+       add_kz_commands(module_interface);
 
        /* add our modified dptools */
-       add_kz_dptools(module_interface, app_interface);
+       add_kz_dptools(module_interface);
 
        /* add our endpoints */
        add_kz_endpoints(module_interface);
index 1fc12ba2aa2503bc3570846bc59d1b14604727fa..c9f16f4826700fe4fe1d498bf9be43ec949e7e13 100644 (file)
 extern const char kz_default_config[];
 extern const int kz_default_config_size;
 
+#include "kazoo_defs.h"
+#include "kazoo_tweaks.h"
 #include "kazoo_ei.h"
 #include "kazoo_message.h"
+#include "kazoo_utils.h"
 
 typedef enum {
        LFLAG_RUNNING = (1 << 0)
@@ -26,16 +29,17 @@ typedef enum {
 
 
 /* kazoo_commands.c */
-void add_kz_commands(switch_loadable_module_interface_t **module_interface, switch_api_interface_t *api_interface);
+void add_kz_commands(switch_loadable_module_interface_t **module_interface);
 
 /* kazoo_dptools.c */
-void add_kz_dptools(switch_loadable_module_interface_t **module_interface, switch_application_interface_t *app_interface);
+void add_kz_dptools(switch_loadable_module_interface_t **module_interface);
 
 /* kazoo_api.c */
-void add_cli_api(switch_loadable_module_interface_t **module_interface, switch_api_interface_t *api_interface);
+void add_cli_api(switch_loadable_module_interface_t **module_interface);
 void remove_cli_api();
 
 /* kazoo_utils.c */
+/*
 SWITCH_DECLARE(switch_status_t) kz_switch_core_merge_variables(switch_event_t *event);
 SWITCH_DECLARE(switch_status_t) kz_switch_core_base_headers_for_expand(switch_event_t **event);
 void kz_check_set_profile_var(switch_channel_t *channel, char* var, char *val);
@@ -49,6 +53,7 @@ SWITCH_DECLARE(char *) kz_event_expand_headers(switch_event_t *event, const char
 SWITCH_DECLARE(char *) kz_expand(const char *in);
 SWITCH_DECLARE(char *) kz_expand_pool(switch_memory_pool_t *pool, const char *in);
 switch_status_t kz_json_api(const char * command, cJSON *args, cJSON **res);
+*/
 
 /* kazoo_endpoints.c */
 void add_kz_endpoints(switch_loadable_module_interface_t **module_interface);