From b8b60e9f9937b2927551e3a5d45d2fe571fdb882 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthony.minessale@gmail.com>
Date: Fri, 19 Mar 2010 23:40:30 +0000
Subject: [PATCH] add display feature to SCA

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@17054 d0543943-73ff-0310-b7d9-9358b9ac24b2
---
 src/mod/endpoints/mod_sofia/sofia_presence.c | 54 ++++++++++++++++++--
 1 file changed, 50 insertions(+), 4 deletions(-)

diff --git a/src/mod/endpoints/mod_sofia/sofia_presence.c b/src/mod/endpoints/mod_sofia/sofia_presence.c
index 0f3dd43d81..c9cc932928 100644
--- a/src/mod/endpoints/mod_sofia/sofia_presence.c
+++ b/src/mod/endpoints/mod_sofia/sofia_presence.c
@@ -1515,11 +1515,14 @@ static int broadsoft_sla_gather_state_callback(void *pArg, int argc, char **argv
 {
 	struct state_helper *sh = (struct state_helper *) pArg;
 	char key[256] = "";
+	switch_core_session_t *session;
+	const char *callee_name = NULL, *callee_number = NULL;
 	char *data = NULL, *tmp;
 	char *user = argv[0];
 	char *host = argv[1];
 	char *info = argv[2];
 	char *state = argv[3];
+	char *uuid = argv[4];
 	int i;
 
 	if (mod_sofia_globals.debug_sla > 1) {
@@ -1540,10 +1543,53 @@ static int broadsoft_sla_gather_state_callback(void *pArg, int argc, char **argv
 
 	data = switch_core_hash_find(sh->hash, key);
 
-	if (data) {
-		tmp = switch_core_sprintf(sh->pool, "%s,<sip:%s>;%s;appearance-state=%s", data, host, info, state);
+	if (uuid && (session = switch_core_session_locate(uuid))) {
+		switch_channel_t *channel = switch_core_session_get_channel(session);
+
+		if (zstr((callee_name = switch_channel_get_variable(channel, "effective_callee_id_name"))) &&
+			zstr((callee_name = switch_channel_get_variable(channel, "sip_callee_id_name")))) {
+			callee_name = switch_channel_get_variable(channel, "callee_id_name");
+		}
+		
+		if (zstr((callee_number = switch_channel_get_variable(channel, "effective_callee_id_number"))) &&
+			zstr((callee_number = switch_channel_get_variable(channel, "sip_callee_id_number")))) {
+			callee_number = switch_channel_get_variable(channel, "destination_number");
+		}
+		
+		if (zstr(callee_name) && !zstr(callee_number)) {
+			callee_name = callee_number;
+		}
+
+		if (!zstr(callee_number)) {
+			callee_number = switch_sanitize_number(switch_core_session_strdup(session, callee_number));
+		}
+
+		if (!zstr(callee_name)) {
+			callee_name = switch_sanitize_number(switch_core_session_strdup(session, callee_name));
+		}
+		switch_core_session_rwunlock(session);
+	}
+
+	if (!zstr(callee_number)) {
+		if (zstr(callee_name)) {
+			callee_name = "unknown";
+		}
+		
+		if (data) {
+			tmp = switch_core_sprintf(sh->pool,
+									  "%s,<sip:%s>;%s;appearance-state=%s;appearance-uri=\"\\\"%s\\\" <sip:%s@%s>\"",
+									  data, host, info, state, callee_name, callee_number, host);
+		} else {
+			tmp = switch_core_sprintf(sh->pool,
+									  "<sip:%s>;%s;appearance-state=%s;appearance-uri=\"\\\"%s\\\" <sip:%s@%s>\"",
+									  host, info, state, callee_name, callee_number, host);
+		}
 	} else {
-		tmp = switch_core_sprintf(sh->pool, "<sip:%s>;%s;appearance-state=%s", host, info, state);
+		if (data) {
+			tmp = switch_core_sprintf(sh->pool, "%s,<sip:%s>;%s;appearance-state=%s", data, host, info, state);
+		} else {
+			tmp = switch_core_sprintf(sh->pool, "<sip:%s>;%s;appearance-state=%s", host, info, state);
+		}
 	}
 
 	switch_core_hash_insert(sh->hash, key, tmp);
@@ -1562,7 +1608,7 @@ static void sync_sla(sofia_profile_t *profile, const char *to_user, const char *
 	sh->pool = pool;
 	switch_core_hash_init(&sh->hash, sh->pool);
 
-	sql = switch_mprintf("select sip_from_user,sip_from_host,call_info,call_info_state from sip_dialogs "
+	sql = switch_mprintf("select sip_from_user,sip_from_host,call_info,call_info_state,uuid from sip_dialogs "
 						 "where hostname='%q' " "and sip_from_user='%q' and sip_from_host='%q' ", mod_sofia_globals.hostname, to_user, to_host);