diff --git a/src/mod/endpoints/mod_sofia/sofia_presence.c b/src/mod/endpoints/mod_sofia/sofia_presence.c index 347bd2a535..1ea4bebdd3 100644 --- a/src/mod/endpoints/mod_sofia/sofia_presence.c +++ b/src/mod/endpoints/mod_sofia/sofia_presence.c @@ -55,6 +55,7 @@ static void sync_sla(sofia_profile_t *profile, const char *to_user, const char * struct resub_helper { sofia_profile_t *profile; switch_event_t *event; + int rowcount; }; struct presence_helper { @@ -549,7 +550,7 @@ static void actual_sofia_presence_event_handler(switch_event_t *event) "(sip_registrations.sip_host='%q' or sip_registrations.presence_hosts like '%%%q%%')", probe_host, probe_euser, probe_host, probe_host); switch_assert(sql); - + if (mod_sofia_globals.debug_presence > 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s START_PRESENCE_PROBE_SQL\n", profile->name); @@ -565,6 +566,18 @@ static void actual_sofia_presence_event_handler(switch_event_t *event) switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s END_PRESENCE_PROBE_SQL\n\n", profile->name); } + if (!h.rowcount) { + switch_event_t *sevent; + if (switch_event_create(&sevent, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) { + switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "proto", SOFIA_CHAT_PROTO); + switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "login", profile->name); + switch_event_add_header(sevent, SWITCH_STACK_BOTTOM, "from", "%s@%s", probe_euser, probe_host); + switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "status", "Unregistered"); + switch_event_fire(&sevent); + } + } + + sofia_glue_release_profile(profile); switch_safe_free(sql); } @@ -953,6 +966,7 @@ static int sofia_presence_resub_callback(void *pArg, int argc, char **argv, char switch_event_fire(&event); } + h->rowcount++; return 0; } @@ -1018,11 +1032,17 @@ static char *gen_pidf(char *user_agent, char *id, char *url, char *open, char *r } else { *ct = "application/pidf+xml"; - if (!strncasecmp(status, "Registered(", 11)) { + if (!strncasecmp(status, "Registered", 10)) { prpid = NULL; status = "Available"; } + + if (!strcasecmp(status, "Unregistered")) { + prpid = NULL; + open = "closed"; + } + if (prpid) { ret = switch_mprintf(" \n" "name); - switch_event_add_header(sevent, SWITCH_STACK_BOTTOM, "from", "%s@%s", to_user, to_host); - switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "rpid", "active"); - switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "status", "Click To Call"); - switch_event_fire(&sevent); - } - - } else { - if (switch_event_create(&sevent, SWITCH_EVENT_PRESENCE_PROBE) == SWITCH_STATUS_SUCCESS) { - switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "proto", SOFIA_CHAT_PROTO); - switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "login", profile->name); - switch_event_add_header(sevent, SWITCH_STACK_BOTTOM, "from", "%s@%s", from_user, from_host); - switch_event_add_header(sevent, SWITCH_STACK_BOTTOM, "to", "%s@%s", to_user, to_host); - switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "proto-specific-event-name", event); - switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "expires", exp_delta_str); - switch_event_fire(&sevent); - } - } - if (to_user && strchr(to_user, '+')) { char *h; if ((proto = (d_user = strdup(to_user)))) { @@ -2174,19 +2160,42 @@ void sofia_presence_handle_sip_i_subscribe(int status, switch_safe_free(sql); } - } else { - if ((sql = switch_mprintf("select proto,sip_user,'%q',sub_to_user,sub_to_host,event,contact,call_id,full_from," - "full_via,expires,user_agent,accept,profile_name,network_ip" - " from sip_subscriptions where expires > -1 and event='%s' and sip_user='%q' " - "and (sip_host='%q' or presence_hosts like '%%%q%%')", to_host, event, to_user, to_host, to_host))) { - sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_sub_callback, profile); - - switch_safe_free(sql); - } } end: + if (to_user && (strstr(to_user, "ext+") || strstr(to_user, "user+"))) { + char protocol[80]; + char *p; + + switch_copy_string(protocol, to_user, sizeof(protocol)); + if ((p = strchr(protocol, '+'))) { + *p = '\0'; + } + + if (switch_event_create(&sevent, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) { + switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "proto", protocol); + switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "login", profile->name); + switch_event_add_header(sevent, SWITCH_STACK_BOTTOM, "from", "%s@%s", to_user, to_host); + switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "rpid", "active"); + switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "status", "Click To Call"); + switch_event_fire(&sevent); + } + + } else { + if (switch_event_create(&sevent, SWITCH_EVENT_PRESENCE_PROBE) == SWITCH_STATUS_SUCCESS) { + switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "proto", SOFIA_CHAT_PROTO); + switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "login", profile->name); + switch_event_add_header(sevent, SWITCH_STACK_BOTTOM, "from", "%s@%s", from_user, from_host); + switch_event_add_header(sevent, SWITCH_STACK_BOTTOM, "to", "%s@%s", to_user, to_host); + switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "proto-specific-event-name", event); + switch_event_add_header_string(sevent, SWITCH_STACK_BOTTOM, "expires", exp_delta_str); + switch_event_fire(&sevent); + } + } + + + if (event) { su_free(profile->home, event); } diff --git a/src/mod/endpoints/mod_sofia/sofia_reg.c b/src/mod/endpoints/mod_sofia/sofia_reg.c index 32f22081b3..2e1b1273a0 100644 --- a/src/mod/endpoints/mod_sofia/sofia_reg.c +++ b/src/mod/endpoints/mod_sofia/sofia_reg.c @@ -1124,7 +1124,7 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand sql = switch_mprintf("delete from sip_registrations where sip_user='%q' and sip_host='%q'", to_user, reg_host); } switch_mutex_lock(profile->ireg_mutex); - sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); + sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE); switch_find_local_ip(guess_ip4, sizeof(guess_ip4), NULL, AF_INET); sql = switch_mprintf("insert into sip_registrations " @@ -1138,7 +1138,7 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand mwi_user, mwi_host, guess_ip4, mod_sofia_globals.hostname); if (sql) { - sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); + sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE); } switch_mutex_unlock(profile->ireg_mutex); @@ -1204,7 +1204,7 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand } else { - if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_OUT) == SWITCH_STATUS_SUCCESS) { + if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) { switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", SOFIA_CHAT_PROTO); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "rpid", rpid); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", profile->url); @@ -1287,6 +1287,17 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "VM-Call-ID", call_id); } } + + + 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(s_event, SWITCH_STACK_BOTTOM, "from", "%s@%s", to_user, reg_host); + switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "rpid", "closed"); + switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "status", "Registered"); + switch_event_fire(&s_event); + } + } else { if (switch_event_create_subclass(&s_event, SWITCH_EVENT_CUSTOM, MY_EVENT_UNREGISTER) == SWITCH_STATUS_SUCCESS) { switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "profile-name", profile->name);