FS-3469 here is some more, this may never be perfect

This commit is contained in:
Anthony Minessale 2011-10-24 18:54:22 -05:00
parent 86fef38b28
commit 3764a60df9
5 changed files with 111 additions and 70 deletions

View File

@ -4468,6 +4468,8 @@ static switch_call_cause_t sofia_outgoing_channel(switch_core_session_t *session
if (profile->pres_type) {
char *sql;
time_t now;
const char *presence_id = switch_channel_get_variable(nchannel, "presence_id");
const char *presence_data = switch_channel_get_variable(nchannel, "presence_data");
@ -4479,9 +4481,10 @@ static switch_call_cause_t sofia_outgoing_channel(switch_core_session_t *session
presence_data = switch_event_get_header(var_event, "presence_data");
}
sql = switch_mprintf("insert into sip_dialogs (uuid,presence_id,presence_data,profile_name,hostname) "
"values ('%q', '%q', '%q', '%q', '%q')", switch_core_session_get_uuid(nsession),
switch_str_nil(presence_id), switch_str_nil(presence_data), profile->name, mod_sofia_globals.hostname);
now = switch_epoch_time_now(NULL);
sql = switch_mprintf("insert into sip_dialogs (uuid,presence_id,presence_data,profile_name,hostname,rcd) "
"values ('%q', '%q', '%q', '%q', '%q', %ld)", switch_core_session_get_uuid(nsession),
switch_str_nil(presence_id), switch_str_nil(presence_data), profile->name, mod_sofia_globals.hostname, (long) now);
sofia_glue_actually_execute_sql(profile, sql, profile->ireg_mutex);
switch_safe_free(sql);
}

View File

@ -3544,8 +3544,9 @@ switch_status_t config_sofia(int reload, char *profile_name)
profile->contact_user = SOFIA_DEFAULT_CONTACT_USER;
sofia_set_pflag(profile, PFLAG_PASS_CALLEE_ID);
sofia_set_pflag(profile, PFLAG_MESSAGE_QUERY_ON_FIRST_REGISTER);
sofia_set_pflag(profile, PFLAG_SQL_IN_TRANS);
sofia_set_pflag(profile, PFLAG_PRESENCE_ON_FIRST_REGISTER);
sofia_set_pflag(profile, PFLAG_SQL_IN_TRANS);
profile->shutdown_type = "false";
profile->local_network = "localnet.auto";
sofia_set_flag(profile, TFLAG_ENABLE_SOA);
@ -5010,7 +5011,8 @@ static void sofia_handle_sip_r_invite(switch_core_session_t *session, int status
const char *presence_id = switch_channel_get_variable(channel, "presence_id");
char *full_contact = "";
char *p = NULL;
time_t now;
if (sip->sip_contact) {
full_contact = sip_header_as_string(nua_handle_home(tech_pvt->nh), (void *) sip->sip_contact);
}
@ -5018,16 +5020,19 @@ static void sofia_handle_sip_r_invite(switch_core_session_t *session, int status
if (call_info && (p = strchr(call_info, ';'))) {
p++;
}
now = switch_epoch_time_now(NULL);
sql = switch_mprintf("insert into sip_dialogs "
"(call_id,uuid,sip_to_user,sip_to_host,sip_to_tag,sip_from_user,sip_from_host,sip_from_tag,contact_user,"
"contact_host,state,direction,user_agent,profile_name,hostname,contact,presence_id,presence_data,call_info) "
"values('%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q')",
"contact_host,state,direction,user_agent,profile_name,hostname,contact,presence_id,presence_data,call_info,rcd) "
"values('%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q',%ld)",
call_id,
switch_core_session_get_uuid(session),
to_user, to_host, to_tag, from_user, from_host, from_tag, contact_user,
contact_host, astate, "outbound", user_agent,
profile->name, mod_sofia_globals.hostname, switch_str_nil(full_contact),
switch_str_nil(presence_id), switch_str_nil(presence_data), switch_str_nil(p));
switch_str_nil(presence_id), switch_str_nil(presence_data), switch_str_nil(p), (long) now);
switch_assert(sql);
sofia_glue_actually_execute_sql(profile, sql, profile->ireg_mutex);
@ -8138,7 +8143,7 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_
const char *presence_id = switch_channel_get_variable(channel, "presence_id");
char *full_contact = "";
char *p = NULL;
time_t now;
if (sip->sip_contact) {
full_contact = sip_header_as_string(nua_handle_home(tech_pvt->nh), (void *) sip->sip_contact);
@ -8152,17 +8157,18 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_
}
}
now = switch_epoch_time_now(NULL);
sql = switch_mprintf("insert into sip_dialogs "
"(call_id,uuid,sip_to_user,sip_to_host,sip_to_tag,sip_from_user,sip_from_host,sip_from_tag,contact_user,"
"contact_host,state,direction,user_agent,profile_name,hostname,contact,presence_id,presence_data,call_info) "
"values('%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q')",
"contact_host,state,direction,user_agent,profile_name,hostname,contact,presence_id,presence_data,call_info,rcd) "
"values('%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q',%ld)",
call_id,
tech_pvt->sofia_private->uuid,
to_user, to_host, to_tag, dialog_from_user, dialog_from_host, from_tag,
contact_user, contact_host, "confirmed", "inbound", user_agent,
profile->name, mod_sofia_globals.hostname, switch_str_nil(full_contact),
switch_str_nil(presence_id), switch_str_nil(presence_data), switch_str_nil(p));
switch_str_nil(presence_id), switch_str_nil(presence_data), switch_str_nil(p), now);
switch_assert(sql);

View File

@ -5826,7 +5826,8 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
" status VARCHAR(255),\n"
" rpid VARCHAR(255),\n"
" sip_to_tag VARCHAR(255),\n"
" sip_from_tag VARCHAR(255)\n"
" sip_from_tag VARCHAR(255),\n"
" rcd INTEGER not null default 0\n"
");\n";
char sub_sql[] =
@ -5970,12 +5971,14 @@ int sofia_glue_init_sql(sofia_profile_t *profile)
free(test_sql);
test_sql = switch_mprintf("delete from sip_subscriptions where hostname='%q' and version < 0 and orig_proto like '%%' and network_ip like '%%' and network_port like '%%'",
test_sql = switch_mprintf("delete from sip_subscriptions where hostname='%q' "
"and (version < 0 or orig_proto like '%%' or network_ip like '%%' or network_port like '%%')",
mod_sofia_globals.hostname);
switch_cache_db_test_reactive(dbh, test_sql, "DROP TABLE sip_subscriptions", sub_sql);
free(test_sql);
test_sql = switch_mprintf("delete from sip_dialogs where hostname='%q' and expires <> -9999 or rpid='' or sip_from_tag=''", mod_sofia_globals.hostname);
test_sql = switch_mprintf("delete from sip_dialogs where hostname='%q' and (expires <> -9999 or rpid='' or sip_from_tag='' || rcd > 0)",
mod_sofia_globals.hostname);
switch_cache_db_test_reactive(dbh, test_sql, "DROP TABLE sip_dialogs", dialog_sql);

View File

@ -58,6 +58,7 @@ struct dialog_helper {
char status[512];
char rpid[512];
char presence_id[1024];
int hits;
};
struct resub_helper {
@ -585,6 +586,7 @@ static int sofia_presence_dialog_callback(void *pArg, int argc, char **argv, cha
switch_set_string(helper->status, argv[0]);
switch_set_string(helper->rpid, argv[1]);
switch_set_string(helper->presence_id, argv[2]);
helper->hits++;
}
return -1;
@ -600,6 +602,8 @@ static void do_normal_probe(sofia_profile_t *profile, switch_event_t *event)
char *probe_user = NULL, *probe_euser, *probe_host, *p;
struct dialog_helper dh = { { 0 } };
//DUMP_EVENT(event);
if (!proto || strcasecmp(proto, SOFIA_CHAT_PROTO) != 0) {
return;
}
@ -618,7 +622,7 @@ static void do_normal_probe(sofia_profile_t *profile, switch_event_t *event)
if (probe_euser && probe_host && (profile = sofia_glue_find_profile(probe_host))) {
sql = switch_mprintf("select status,rpid,presence_id from sip_dialogs "
"where ((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q')",
"where ((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q') order by rcd desc",
probe_euser, probe_host, probe_euser, probe_host);
sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_dialog_callback, &dh);
@ -842,6 +846,7 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
char *sql = NULL;
char *euser = NULL, *user = NULL, *host = NULL;
char *call_info = switch_event_get_header(event, "presence-call-info");
char *presence_source = switch_event_get_header(event, "presence-source");
char *call_info_state = switch_event_get_header(event, "presence-call-info-state");
switch_console_callback_match_t *matches;
@ -1023,6 +1028,7 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
call_info, call_info_state, mod_sofia_globals.hostname, euser, host, euser, host, call_info);
}
//printf("WTF %s\n", sql);
if (mod_sofia_globals.debug_sla > 1) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "STATE SQL %s\n", sql);
@ -1044,10 +1050,17 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE);
}
sql = switch_mprintf("select status,rpid,presence_id from sip_dialogs where ((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q')",
sql = switch_mprintf("select status,rpid,presence_id from sip_dialogs "
"where ((sip_from_user='%q' and sip_from_host='%q') or presence_id='%q@%q') order by rcd desc",
euser, host, euser, host);
sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_dialog_callback, &dh);
switch_safe_free(sql);
if (dh.hits && presence_source && (!strcasecmp(presence_source, "register") || switch_stristr("register", status))) {
goto done;
}
if ((sql = switch_mprintf("select distinct sip_subscriptions.proto,sip_subscriptions.sip_user,sip_subscriptions.sip_host,"
"sip_subscriptions.sub_to_user,sip_subscriptions.sub_to_host,sip_subscriptions.event,"
@ -1111,7 +1124,7 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
event->event_id == SWITCH_EVENT_PRESENCE_IN ? "IN" : "OUT", profile->name);
}
#if 0
#if 1
if (event) {
const char *refresh = switch_event_get_header(event, "refresh");
if (switch_true(refresh)) {
@ -1386,6 +1399,7 @@ static int sofia_presence_resub_callback(void *pArg, int argc, char **argv, char
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Force-Direction", "inbound");
}
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "resub", "true");
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "status", status);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "rpid", rpid);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
@ -2009,6 +2023,7 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char *
//const char *force_direction = switch_str_nil(switch_event_get_header(helper->event, "force-direction"));
const char *uuid = switch_str_nil(switch_event_get_header(helper->event, "unique-id"));
const char *event_status = switch_str_nil(switch_event_get_header(helper->event, "status"));
const char *resub = switch_str_nil(switch_event_get_header(helper->event, "resub"));
const char *force_event_status = switch_str_nil(switch_event_get_header(helper->event, "force-status"));
const char *astate = switch_str_nil(switch_event_get_header(helper->event, "astate"));
const char *answer_state = switch_str_nil(switch_event_get_header(helper->event, "answer-state"));
@ -2202,9 +2217,10 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char *
ct = "application/dialog-info+xml";
}
if (!zstr(astate) && !zstr(uuid) && helper && helper->stream.data && strcmp(helper->last_uuid, uuid)) {
if (!zstr(astate) && !zstr(uuid) &&
helper && helper->stream.data && strcmp(helper->last_uuid, uuid) && strcasecmp(astate, "terminated") && strchr(uuid, '-')) {
helper->stream.write_function(&helper->stream, "update sip_dialogs set state='%s' where uuid='%s';", astate, uuid);
//printf("WTF update sip_dialogs set state='%s' where uuid='%s';\n", astate, uuid);
switch_copy_string(helper->last_uuid, uuid, sizeof(helper->last_uuid));
}
@ -2213,52 +2229,58 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char *
switch_set_string(status_line, status);
if (in) {
const char *direction = switch_event_get_header(helper->event, "Caller-Direction");
const char *op, *what = "Ring";
if (direction && !strcasecmp(direction, "outbound")) {
op = switch_event_get_header(helper->event, "Other-Leg-Caller-ID-Number");
} else {
op = switch_event_get_header(helper->event, "Caller-Callee-ID-Number");
}
if (zstr(op)) {
op = switch_event_get_header(helper->event, "Caller-Destination-Number");
}
if (direction) {
what = strcasecmp(direction, "outbound") ? "Call" : "Ring";
}
if (!strcmp(astate, "early")) {
if (zstr(op)) {
switch_snprintf(status_line, sizeof(status_line), "%sing", what);
} else {
switch_snprintf(status_line, sizeof(status_line), "%s %s", what, op);
}
rpid = "on-the-phone";
force_status = 1;
} else if (!strcmp(astate, "confirmed")) {
if (zstr(op)) {
switch_snprintf(status_line, sizeof(status_line), "On The Phone");
} else {
switch_snprintf(status_line, sizeof(status_line), "Talk %s", op);
}
rpid = "on-the-phone";
force_status = 1;
}
open = "open";
if (!strcmp(status, "hold")) {
rpid = "on-the-phone";
switch_snprintf(status_line, sizeof(status_line), "Hold %s", op);
force_status = 1;
}
if (switch_false(resub)) {
const char *direction = switch_event_get_header(helper->event, "Caller-Direction");
const char *op, *what = "Ring";
if (direction && !strcasecmp(direction, "outbound")) {
op = switch_event_get_header(helper->event, "Other-Leg-Caller-ID-Number");
} else {
op = switch_event_get_header(helper->event, "Caller-Callee-ID-Number");
}
if (zstr(op)) {
op = switch_event_get_header(helper->event, "Caller-Destination-Number");
}
if (direction) {
what = strcasecmp(direction, "outbound") ? "Call" : "Ring";
}
if (!strcmp(astate, "early")) {
if (!zstr(op)) {
//switch_snprintf(status_line, sizeof(status_line), "%sing", what);
//} else {
switch_snprintf(status_line, sizeof(status_line), "%s %s", what, op);
}
rpid = "on-the-phone";
force_status = 1;
} else if (!strcmp(astate, "confirmed")) {
if (!zstr(op)) {
//switch_snprintf(status_line, sizeof(status_line), "On The Phone");
//} else {
switch_snprintf(status_line, sizeof(status_line), "Talk %s", op);
}
rpid = "on-the-phone";
force_status = 1;
}
if (!strcmp(status, "hold")) {
rpid = "on-the-phone";
if (!zstr(op)) {
switch_snprintf(status_line, sizeof(status_line), "Hold %s", op);
force_status = 1;
}
}
}
} else {
open = "closed";
}
@ -2315,11 +2337,13 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char *
}
if (helper->event){
if (!is_dialog && helper->event && !switch_stristr("registered", status_line)){
const char *uuid = switch_event_get_header_nil(helper->event, "unique-id");
if (!zstr(uuid) && strchr(uuid, '-')) {
const char *register_source = switch_event_get_header_nil(helper->event, "register-source");
if (!zstr(uuid) && strchr(uuid, '-') && !zstr(status_line) && !zstr(rpid) && (zstr(register_source) || strcasecmp(register_source, "register"))) {
char *sql = switch_mprintf("update sip_dialogs set rpid='%q',status='%q' where uuid='%q'", rpid, status_line, uuid);
//printf("WTF %s\n", sql);
sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
}
}
@ -2985,6 +3009,7 @@ void sofia_presence_handle_sip_i_subscribe(int status,
if (!strcasecmp(event, "line-seize")) {
char *full_call_info = NULL;
char *p;
switch_time_t now;
if (sip->sip_call_info) {
full_call_info = sip_header_as_string(nh->nh_home, (void *) sip->sip_call_info);
@ -3009,9 +3034,10 @@ void sofia_presence_handle_sip_i_subscribe(int status,
}
sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE);
sql = switch_mprintf("insert into sip_dialogs (sip_from_user,sip_from_host,call_info,call_info_state,hostname,expires) "
"values ('%q','%q','%q','seized','%q',%ld)",
to_user, to_host, switch_str_nil(p), mod_sofia_globals.hostname, switch_epoch_time_now(NULL) + exp_delta);
now = switch_epoch_time_now(NULL);
sql = switch_mprintf("insert into sip_dialogs (sip_from_user,sip_from_host,call_info,call_info_state,hostname,expires,rcd) "
"values ('%q','%q','%q','seized','%q',%ld,%ld)",
to_user, to_host, switch_str_nil(p), mod_sofia_globals.hostname, switch_epoch_time_now(NULL) + exp_delta, (long)now);
if (mod_sofia_globals.debug_sla > 1) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SEIZE SQL %s\n", sql);

View File

@ -1562,7 +1562,7 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
switch_event_fire(&event);
}
#else
if (sofia_test_pflag(profile, PFLAG_MESSAGE_QUERY_ON_REGISTER) ||
(reg_count == 1 && sofia_test_pflag(profile, PFLAG_MESSAGE_QUERY_ON_FIRST_REGISTER))) {
@ -1599,6 +1599,7 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
(sip && sip->sip_user_agent) ? sip->sip_user_agent->g_string : "unknown");
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", to_user, sub_host);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "status", "Unregistered");
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "presence-source", "register");
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
switch_event_fire(&event);
}
@ -1696,6 +1697,7 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "from", "%s@%s", to_user, sub_host);
switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "to", "%s@%s", to_user, sub_host);
switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "event_type", "presence");
switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "presence-source", "register");
switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "alt_event_type", "dialog");
switch_event_fire(&s_event);
}
@ -1703,6 +1705,7 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
if (switch_event_create(&s_event, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) {
switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "proto", SOFIA_CHAT_PROTO);
switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "login", profile->name);
switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "presence-source", "register");
switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "from", "%s@%s", to_user, sub_host);
switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "rpid", "unknown");
switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "status", "Registered");