Merge pull request #112 from lazedo/kazoo-cfw

[mod_kazoo] add kz-endpoint-runtime-context
This commit is contained in:
Andrey Volk 2019-11-08 18:10:32 +04:00 committed by GitHub
commit 00cbf0ea37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 281 additions and 175 deletions

View File

@ -73,9 +73,9 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
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;
switch_xml_t x_user = NULL, x_param, x_params, x_callfwd;
char *user = NULL, *domain = NULL, *dup_domain = NULL, *dialed_user = NULL;
const char *dest = NULL;
char *dest = NULL;
switch_call_cause_t cause = SWITCH_CAUSE_NONE;
unsigned int timelimit = SWITCH_DEFAULT_TIMEOUT;
switch_channel_t *new_channel = NULL;
@ -83,6 +83,21 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
char stupid[128] = "";
const char *skip = NULL, *var = NULL;
switch_core_session_t *a_session = NULL, *e_session = NULL;
cJSON * ctx = NULL;
const char *endpoint_dial = NULL;
const char *callforward_dial = NULL;
const char *failover_dial = NULL;
char *b_failover_dial = NULL;
const char *endpoint_separator = NULL;
const char *varval = NULL;
char *d_dest = NULL;
switch_channel_t *channel = NULL;
switch_originate_flag_t myflags = SOF_NONE;
char *cid_name_override = NULL;
char *cid_num_override = NULL;
switch_event_t *event = NULL;
switch_status_t status = SWITCH_STATUS_SUCCESS;
if (zstr(outbound_profile->destination_number)) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "NO DESTINATION NUMBER\n");
@ -131,6 +146,16 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
goto done;
}
if (var_event) {
const char * str_ctx = switch_event_get_header(var_event, "kz-endpoint-runtime-context");
if ( str_ctx ) {
ctx = cJSON_Parse(str_ctx);
if (ctx) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "call context parsed => %s\n", str_ctx);
}
}
}
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");
@ -150,8 +175,12 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
const char *pvar = switch_xml_attr_soft(x_param, "name");
const char *val = switch_xml_attr(x_param, "value");
if (!strcasecmp(pvar, "dial-string")) {
dest = val;
if (!strcasecmp(pvar, "endpoint-dial-string")) {
endpoint_dial = val;
} else if (!strcasecmp(pvar, "callforward-dial-string")) {
callforward_dial = val;
} else if (!strcasecmp(pvar, "endpoint-separator")) {
endpoint_separator = val;
} else if (!strncasecmp(pvar, "dial-var-", 9)) {
if (!var_event) {
switch_event_create(&var_event, SWITCH_EVENT_GENERAL);
@ -164,6 +193,61 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
}
}
x_callfwd = switch_xml_child(x_user, "call-forward");
if (x_callfwd) {
switch_bool_t call_fwd_is_substitute = SWITCH_FALSE,
call_fwd_is_failover = SWITCH_FALSE,
call_fwd_direct_calls_only = SWITCH_FALSE,
call_fwd_is_valid = SWITCH_TRUE;
for (x_param = switch_xml_child(x_callfwd, "variable"); x_param; x_param = x_param->next) {
const char *var = 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, "cfw %s => %s\n", var, val);
if (!strcasecmp(var, "Is-Substitute")) {
call_fwd_is_substitute = switch_true(val);
} else if (!strcasecmp(var, "Is-Failover")) {
call_fwd_is_failover = switch_true(val);
} else if (!strcasecmp(var, "Direct-Calls-Only")) {
call_fwd_direct_calls_only = switch_true(val);
}
}
if (call_fwd_direct_calls_only) {
call_fwd_is_valid = SWITCH_FALSE;
if (ctx ) {
cJSON *json_flags = cJSON_GetObjectItem(ctx, "Flags");
if (json_flags && json_flags->type == cJSON_Array) {
cJSON *item;
cJSON_ArrayForEach(item, json_flags) {
if (!strcmp(item->valuestring, "direct_call")) {
call_fwd_is_valid = SWITCH_TRUE;
break;
}
}
if (!call_fwd_is_valid) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "call fwd requires direct_call and it was not found\n");
}
}
}
}
if (!call_fwd_is_valid) {
dest = strdup(endpoint_dial);
} else if (call_fwd_is_failover) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "setting failover => %s\n", callforward_dial);
dest = strdup(endpoint_dial);
failover_dial = callforward_dial;
} else if (call_fwd_is_substitute) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "setting call fwd substitute => %s\n", callforward_dial);
dest = strdup(callforward_dial);
} else {
dest = switch_mprintf("%s%s%s", endpoint_dial ? endpoint_dial : "", (endpoint_dial ? endpoint_separator ? endpoint_separator : "," : ""), callforward_dial);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "setting call fwd append => %s => %s\n", callforward_dial, dest);
}
} else {
dest = strdup(endpoint_dial);
}
dialed_user = (char *)switch_xml_attr(x_user, "id");
if (var_event) {
@ -174,14 +258,8 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
if (!dest) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "No dial-string available, please check your user directory.\n");
cause = SWITCH_CAUSE_MANDATORY_IE_MISSING;
} else {
const char *varval;
char *d_dest = NULL;
switch_channel_t *channel;
switch_originate_flag_t myflags = SOF_NONE;
char *cid_name_override = NULL;
char *cid_num_override = NULL;
switch_event_t *event = NULL;
goto done;
}
if (var_event) {
cid_name_override = switch_event_get_header(var_event, "origination_caller_id_name");
@ -207,9 +285,6 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
timelimit = atoi(varval);
}
switch_channel_event_set_data(channel, event);
if(var_event) {
switch_event_merge(event, var_event);
}
switch_channel_set_variable(channel, "dialed_user", dialed_user);
switch_channel_set_variable(channel, "dialed_domain", domain);
@ -263,7 +338,27 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
}
}
// add runtime vars to event for expand
if (ctx) {
cJSON *item = NULL;
char *response = NULL;
cJSON_ArrayForEach(item, ctx) {
if (item->type == cJSON_String) {
response = strdup(item->valuestring);
} else {
response = cJSON_PrintUnformatted(item);
}
kz_switch_event_add_variable_name_printf(event, SWITCH_STACK_BOTTOM, response, "kz_ctx_%s", item->string);
switch_safe_free(response);
}
}
d_dest = kz_event_expand_headers(event, dest);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "dialing %s => %s\n", dest, d_dest);
if(failover_dial) {
b_failover_dial = kz_event_expand_headers(event, failover_dial);
}
if (var_event) {
kz_expand_headers(event, var_event);
@ -294,9 +389,21 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
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);
cause = SWITCH_CAUSE_INVALID_IE_CONTENTS;
} else if (switch_ivr_originate(session, new_session, &cause, d_dest, timelimit, NULL,
goto done;
}
status = switch_ivr_originate(session, new_session, &cause, d_dest, timelimit, NULL,
cid_name_override, cid_num_override, outbound_profile, var_event, myflags,
cancel_cause, NULL) == SWITCH_STATUS_SUCCESS) {
cancel_cause, NULL);
if (status != SWITCH_STATUS_SUCCESS && b_failover_dial) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "trying failover => %s\n", failover_dial);
status = switch_ivr_originate(session, new_session, &cause, b_failover_dial, timelimit, NULL,
cid_name_override, cid_num_override, outbound_profile, var_event, myflags,
cancel_cause, NULL);
}
if (status == SWITCH_STATUS_SUCCESS) {
const char *context;
switch_caller_profile_t *cp;
@ -312,16 +419,6 @@ 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");
const char *val = switch_xml_attr(x_param, "value");
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"))) {
switch_caller_profile_t *cp = NULL;
@ -345,12 +442,21 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s
switch_core_session_rwunlock(*new_session);
}
if (d_dest != dest) {
done:
if (d_dest && d_dest != dest) {
switch_safe_free(d_dest);
}
if(b_failover_dial && b_failover_dial != failover_dial) {
switch_safe_free(b_failover_dial);
}
done:
switch_safe_free(dest);
if (ctx) {
cJSON_Delete(ctx);
}
if (x_user) {
switch_xml_free(x_user);