From 957361cf790d7ead58209de8f77830ddc7e0de43 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Wed, 19 Oct 2011 16:50:46 -0500
Subject: [PATCH] presence tweaks

---
 .../mod_conference/mod_conference.c           | 42 ++++++++++++++-----
 .../mod_valet_parking/mod_valet_parking.c     | 41 ++++++++++++++----
 src/mod/endpoints/mod_sofia/sofia_presence.c  |  8 +++-
 3 files changed, 71 insertions(+), 20 deletions(-)

diff --git a/src/mod/applications/mod_conference/mod_conference.c b/src/mod/applications/mod_conference/mod_conference.c
index 31e234ab4c..70002a8314 100644
--- a/src/mod/applications/mod_conference/mod_conference.c
+++ b/src/mod/applications/mod_conference/mod_conference.c
@@ -886,7 +886,12 @@ static switch_status_t conference_add_member(conference_obj_t *conference, confe
 		if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) {
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", CONF_CHAT_PROTO);
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", conference->name);
-			switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", conference->name, conference->domain);
+            if (strchr(conference->name, '@')) {
+                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "from", conference->name);
+            } else {
+                switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", conference->name, conference->domain);
+            }
+
 			switch_event_add_header(event, SWITCH_STACK_BOTTOM, "force-status", "Active (%d caller%s)", conference->count, conference->count == 1 ? "" : "s");
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "alt_event_type", "dialog");
@@ -1107,7 +1112,12 @@ static switch_status_t conference_del_member(conference_obj_t *conference, confe
 		if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) {
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", CONF_CHAT_PROTO);
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", conference->name);
-			switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", conference->name, conference->domain);
+            if (strchr(conference->name, '@')) {
+                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "from", conference->name);
+            } else {
+                switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", conference->name, conference->domain);
+            }
+
 			switch_event_add_header(event, SWITCH_STACK_BOTTOM, "force-status", "Active (%d caller%s)", conference->count, conference->count == 1 ? "" : "s");
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "alt_event_type", "dialog");
@@ -1701,7 +1711,12 @@ static void *SWITCH_THREAD_FUNC conference_thread_run(switch_thread_t *thread, v
 	if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) {
 		switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", CONF_CHAT_PROTO);
 		switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", conference->name);
-		switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", conference->name, conference->domain);
+		if (strchr(conference->name, '@')) {
+			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "from", conference->name);
+		} else {
+			switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", conference->name, conference->domain);
+		}
+
 		switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "force-status", "Inactive");
 		switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "rpid", "unknown");
 		switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
@@ -6057,7 +6072,7 @@ SWITCH_STANDARD_APP(conference_function)
 	}
 
 	/* is there profile specification ? */
-	if ((profile_name = strchr(conf_name, '@'))) {
+	if ((profile_name = strrchr(conf_name, '@'))) {
 		*profile_name++ = '\0';
 	} else {
 		profile_name = "default";
@@ -6665,6 +6680,7 @@ static conference_obj_t *conference_new(char *name, conf_xml_cfg_t cfg, switch_c
 	switch_xml_t xml_kvp;
 	char *timer_name = NULL;
 	char *domain = NULL;
+	char *name_domain = NULL;
 	char *tts_engine = NULL;
 	char *tts_voice = NULL;
 	char *enter_sound = NULL;
@@ -7116,7 +7132,10 @@ static conference_obj_t *conference_new(char *name, conf_xml_cfg_t cfg, switch_c
 	conference->announce_count = announce_count;
 
 	conference->name = switch_core_strdup(conference->pool, name);
-	if (domain) {
+
+	if ((name_domain = strchr(conference->name, '@'))) {
+		conference->domain = switch_core_strdup(conference->pool, name_domain);
+	} else if (domain) {
 		conference->domain = switch_core_strdup(conference->pool, domain);
 	} else {
 		conference->domain = "cluecon.com";
@@ -7173,7 +7192,7 @@ static conference_obj_t *conference_new(char *name, conf_xml_cfg_t cfg, switch_c
 static void pres_event_handler(switch_event_t *event)
 {
 	char *to = switch_event_get_header(event, "to");
-	char *dup_to = NULL, *conf_name, *e;
+	char *dup_to = NULL, *conf_name;
 	conference_obj_t *conference;
 
 	if (!to || strncasecmp(to, "conf+", 5)) {
@@ -7186,15 +7205,16 @@ static void pres_event_handler(switch_event_t *event)
 
 	conf_name = dup_to + 5;
 
-	if ((e = strchr(conf_name, '@'))) {
-		*e = '\0';
-	}
-
 	if ((conference = conference_find(conf_name))) {
 		if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) {
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", CONF_CHAT_PROTO);
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", conference->name);
-			switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", conference->name, conference->domain);
+			if (strchr(conference->name, '@')) {
+				switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "from", conference->name);
+			} else {
+				switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", conference->name, conference->domain);
+			}
+
 			switch_event_add_header(event, SWITCH_STACK_BOTTOM, "force-status", "Active (%d caller%s)", conference->count, conference->count == 1 ? "" : "s");
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "alt_event_type", "dialog");
diff --git a/src/mod/applications/mod_valet_parking/mod_valet_parking.c b/src/mod/applications/mod_valet_parking/mod_valet_parking.c
index f158c6c240..5b7c41f22b 100644
--- a/src/mod/applications/mod_valet_parking/mod_valet_parking.c
+++ b/src/mod/applications/mod_valet_parking/mod_valet_parking.c
@@ -244,7 +244,12 @@ static void valet_send_presence(const char *lot_name, valet_lot_t *lot, valet_to
 		if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) {
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", VALET_PROTO);
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", lot_name);
-			switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", lot_name, domain_name);
+			if (strchr(lot_name, '@')) {
+				switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "from", lot_name);
+			} else {
+				switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", lot_name, domain_name);
+			}
+			
 			switch_event_add_header(event, SWITCH_STACK_BOTTOM, "force-status", "Active (%d caller%s)", count, count == 1 ? "" : "s");
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "rpid", "active");
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
@@ -260,7 +265,12 @@ static void valet_send_presence(const char *lot_name, valet_lot_t *lot, valet_to
 		if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) {
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", VALET_PROTO);
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", lot_name);
-			switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", lot_name, domain_name);
+			if (strchr(lot_name, '@')) {
+				switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "from", lot_name);
+			} else {
+				switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", lot_name, domain_name);
+			}
+			
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "force-status", "Empty");
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "rpid", "unknown");
 			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
@@ -660,7 +670,11 @@ static void pres_event_handler(switch_event_t *event)
 				if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) {
 					switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", VALET_PROTO);
 					switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", lot_name);
-					switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", lot_name, domain_name);
+					if (strchr(lot_name, '@')) {
+						switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "from", lot_name);
+					} else {
+						switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", lot_name, domain_name);
+					}
 					switch_event_add_header(event, SWITCH_STACK_BOTTOM, "force-status", "Active (%d caller%s)", count, count == 1 ? "" : "s");
 					switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "rpid", "active");
 					switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
@@ -676,8 +690,12 @@ static void pres_event_handler(switch_event_t *event)
 		} else {
 			if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) {
 				switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", VALET_PROTO);
-				switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", lot_name);
-				switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", lot_name, domain_name);
+				if (strchr(lot_name, '@')) {
+					switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "from", lot_name);
+				} else {
+					switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", lot_name, domain_name);
+				}
+
 				switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "force-status", "Empty");
 				switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "rpid", "unknown");
 				switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
@@ -715,7 +733,12 @@ static void pres_event_handler(switch_event_t *event)
 					if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) {
 						switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", VALET_PROTO);
 						switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", lot_name);
-						switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", lot_name, domain_name);
+						if (strchr(lot_name, '@')) {
+							switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "from", lot_name);
+						} else {
+							switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", lot_name, domain_name);
+						}
+
 						switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "force-status", "Active");
 						switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
 						switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "alt_event_type", "dialog");
@@ -738,7 +761,11 @@ static void pres_event_handler(switch_event_t *event)
 	if (switch_event_create(&event, SWITCH_EVENT_PRESENCE_IN) == SWITCH_STATUS_SUCCESS) {
 		switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", VALET_PROTO);
 		switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", lot_name);
-		switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "from", to);
+		if (strchr(lot_name, '@')) {
+			switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "from", lot_name);
+		} else {
+			switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", lot_name, domain_name);
+		}
 		switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "force-status", "Empty");
 		switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "rpid", "unknown");
 		switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", "presence");
diff --git a/src/mod/endpoints/mod_sofia/sofia_presence.c b/src/mod/endpoints/mod_sofia/sofia_presence.c
index 582e8f9eb2..0f4f9768ad 100644
--- a/src/mod/endpoints/mod_sofia/sofia_presence.c
+++ b/src/mod/endpoints/mod_sofia/sofia_presence.c
@@ -912,6 +912,10 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
 				sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_dialog_callback, &dh);
 				switch_safe_free(sql);
 				
+				if (zstr(proto)) {
+					proto = SOFIA_CHAT_PROTO;
+				}
+
 				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,"
 										  "sip_subscriptions.contact,sip_subscriptions.call_id,sip_subscriptions.full_from,"
@@ -925,12 +929,13 @@ static void actual_sofia_presence_event_handler(switch_event_t *event)
 										  "sip_subscriptions.profile_name=sip_presence.profile_name) "
 										  
 										  "where sip_subscriptions.version > -1 and sip_subscriptions.expires > -1 and "
+										  "sip_subscriptions.proto='%q' and "
 										  "(event='%q' or event='%q') and sub_to_user='%q' "
 										  "and (sub_to_host='%q' or presence_hosts like '%%%q%%') "
 										  "and (sip_subscriptions.profile_name = '%q' or sip_subscriptions.presence_hosts != sip_subscriptions.sub_to_host) ",
 										  
 										  switch_str_nil(status), switch_str_nil(rpid), host,
-										  dh.status,dh.rpid,dh.presence_id,
+										  dh.status,dh.rpid,dh.presence_id, proto,
 										  event_type, alt_event_type, euser, host, host, profile->name))) {
 					
 					struct presence_helper helper = { 0 };			
@@ -1476,7 +1481,6 @@ static int sofia_presence_sub_callback(void *pArg, int argc, char **argv, char *
 		user = free_me;
 	}
 	
-
 	in = helper->event && helper->event->event_id == SWITCH_EVENT_PRESENCE_IN;
 
 	if (zstr(rpid)) {