From a873b3841a04e0a2e0da535b8af648f017fadb29 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Mon, 23 Aug 2010 18:18:56 -0500
Subject: [PATCH] more presence stuff

---
 src/mod/endpoints/mod_sofia/sofia_presence.c | 15 ++++++---------
 src/mod/endpoints/mod_sofia/sofia_reg.c      | 11 ++++++++++-
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/src/mod/endpoints/mod_sofia/sofia_presence.c b/src/mod/endpoints/mod_sofia/sofia_presence.c
index de5560d875..de5e8d49d9 100644
--- a/src/mod/endpoints/mod_sofia/sofia_presence.c
+++ b/src/mod/endpoints/mod_sofia/sofia_presence.c
@@ -1069,11 +1069,6 @@ static char *gen_pidf(char *user_agent, char *id, char *url, char *open, char *r
 			prpid = "online";
 		}
 
-		/* FS currently send prpid closed on register, this force it to online */
-		if (!strncasecmp(status, "Registered", 10) && !strcasecmp(prpid, "closed")) {
-			prpid = "online";
-		}
-
 		/* of course!, lets make a big deal over dashes. Now the stupidity is complete. */
 		if (!strcmp(prpid, "on-the-phone")) {
 			prpid = "onthephone";
@@ -1430,9 +1425,11 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char *
 				if (direction && !strcasecmp(direction, "outbound")) {
 					op = switch_event_get_header(helper->event, "Other-Leg-Caller-ID-Number");
 				} else {
-					if (!(op = switch_event_get_header(helper->event, "Caller-Callee-ID-Number"))) {
-						op = switch_event_get_header(helper->event, "Caller-Destination-Number");
-					}
+					op = switch_event_get_header(helper->event, "Caller-Callee-ID-Number");
+				}
+
+				if (!op) {
+					op = switch_event_get_header(helper->event, "Caller-Destination-Number");
 				}
 
 				if (direction) {
@@ -1451,7 +1448,7 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char *
 
 				} else if (!strcmp(astate, "confirmed")) {
 					if (zstr(op)) {
-						switch_snprintf(status_line, sizeof(status_line), "On The Phone %s", status);
+						switch_snprintf(status_line, sizeof(status_line), "On The Phone");
 					} else {
 						switch_snprintf(status_line, sizeof(status_line), "Talk %s", op);
 					}
diff --git a/src/mod/endpoints/mod_sofia/sofia_reg.c b/src/mod/endpoints/mod_sofia/sofia_reg.c
index 33f26e98dc..8ac765aa42 100644
--- a/src/mod/endpoints/mod_sofia/sofia_reg.c
+++ b/src/mod/endpoints/mod_sofia/sofia_reg.c
@@ -1146,6 +1146,13 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
 		}
 
 		if (multi_reg) {
+			if (reg_count == 1) {
+				sql = switch_mprintf("delete from sip_subscriptions where sip_user='%q' and sip_host='%q' and contact='%q'", 
+									 to_user, reg_host, contact_str);
+				sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE);
+			}
+				
+
 			if (multi_reg_contact) {
 				sql =
 					switch_mprintf("delete from sip_registrations where sip_user='%q' and sip_host='%q' and contact='%q'", to_user, reg_host, contact_str);
@@ -1153,6 +1160,8 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand
 				sql = switch_mprintf("delete from sip_registrations where call_id='%q'", call_id);
 			}
 		} else {
+			sql = switch_mprintf("delete from sip_subscriptions where sip_user='%q' and sip_host='%q'");
+			sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE);
 			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);
@@ -1327,7 +1336,7 @@ 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, "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, "rpid", "online");
 				switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "status", "Registered");
 				switch_event_fire(&s_event);
 			}