From 021ab87f2be5736c57d3c14bb8f28e7cee4ee102 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthony.minessale@gmail.com>
Date: Thu, 3 Jan 2008 23:42:15 +0000
Subject: [PATCH] fix MODENDP-66

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@7078 d0543943-73ff-0310-b7d9-9358b9ac24b2
---
 conf/dialplan/default.xml                     | 12 +++++++++-
 conf/vars.xml                                 |  9 ++++---
 src/include/switch_types.h                    |  4 +++-
 .../applications/mod_dptools/mod_dptools.c    | 18 ++++++++++++--
 src/mod/endpoints/mod_sofia/mod_sofia.c       | 15 ++++++++++++
 src/mod/endpoints/mod_sofia/sofia.c           | 24 +++++++++++++------
 6 files changed, 66 insertions(+), 16 deletions(-)

diff --git a/conf/dialplan/default.xml b/conf/dialplan/default.xml
index a5ba393a52..ca4769a86b 100644
--- a/conf/dialplan/default.xml
+++ b/conf/dialplan/default.xml
@@ -2,6 +2,14 @@
 <!-- http://wiki.freeswitch.org/wiki/Dialplan_XML -->
 <include>
   <context name="default">
+
+    <extension name="unloop">
+      <condition field="${unroll_loops}" expression="^true$" continue="on-true"/>
+      <condition field="${sip_looped_call}" expression="^true$">
+	<action application="deflect" data="${destination_number}"/>
+      </condition>
+    </extension>
+
     <extension name="intercept">
       <condition field="destination_number" expression="^886$">
 	<action application="answer"/>
@@ -35,8 +43,10 @@
       <condition field="${numbering_plan}" expression="^$" break="never">
 	<action application="set_user" data="default@${domain}"/>
       </condition>
-      <condition>
+      <condition field="${call_debug}" expression="^true$" break="never">
 	<action application="info"/>
+      </condition>
+      <condition>
 	<action application="db" data="insert/spymap/${caller_id_number}/${uuid}"/>
 	<action application="db" data="insert/last_dial/${caller_id_number}/${destination_number}"/>
 	<action application="db" data="insert/last_dial/global/${uuid}"/>
diff --git a/conf/vars.xml b/conf/vars.xml
index c8b229f671..54dd2bbcf0 100644
--- a/conf/vars.xml
+++ b/conf/vars.xml
@@ -41,15 +41,14 @@
        Used by: sofia.conf.xml dingaling.conf.xml
   -->
   <X-PRE-PROCESS cmd="set" data="external_sip_ip=stun:stun.fwdnet.net"/>
-  <!-- server_name
-       A public ip address or DNS name that is used when advertising conference
-       presence or registering sip.
-       Used by: conference.conf.xml
+  <!-- unroll-loops
+       Used to turn on sip loopback unrolling.
   --> 
+  <X-PRE-PROCESS cmd="set" data="unroll_loops=true"/>
   <!-- outbound_caller_id and outbound_caller_name
        The caller ID telephone number we should use when calling out.
        Used by: conference.conf.xml
   -->
   <X-PRE-PROCESS cmd="set" data="outbound_caller_name=FreeSWITCH"/>
   <X-PRE-PROCESS cmd="set" data="outbound_caller_id=8777423583"/>
-
+  <X-PRE-PROCESS cmd="set" data="call_debug=false"/>
diff --git a/src/include/switch_types.h b/src/include/switch_types.h
index 9278c95759..c80271b84e 100644
--- a/src/include/switch_types.h
+++ b/src/include/switch_types.h
@@ -431,6 +431,7 @@ typedef enum {
 	SWITCH_MESSAGE_INDICATE_RESPOND    - indicate reject
 	SWITCH_MESSAGE_INDICATE_BROADCAST - indicate media broadcast
 	SWITCH_MESSAGE_INDICATE_MEDIA_REDIRECT - indicate media broadcast
+	SWITCH_MESSAGE_INDICATE_DEFLECT - indicate deflect
 </pre>
  */
 typedef enum {
@@ -449,7 +450,8 @@ typedef enum {
 	SWITCH_MESSAGE_INDICATE_REDIRECT,
 	SWITCH_MESSAGE_INDICATE_RESPOND,
 	SWITCH_MESSAGE_INDICATE_BROADCAST,
-	SWITCH_MESSAGE_INDICATE_MEDIA_REDIRECT
+	SWITCH_MESSAGE_INDICATE_MEDIA_REDIRECT,
+	SWITCH_MESSAGE_INDICATE_DEFLECT
 } switch_core_session_message_types_t;
 
 
diff --git a/src/mod/applications/mod_dptools/mod_dptools.c b/src/mod/applications/mod_dptools/mod_dptools.c
index 030f0d289c..8408a625ae 100644
--- a/src/mod/applications/mod_dptools/mod_dptools.c
+++ b/src/mod/applications/mod_dptools/mod_dptools.c
@@ -428,6 +428,18 @@ SWITCH_STANDARD_APP(respond_function)
 
 }
 
+SWITCH_STANDARD_APP(deflect_function)
+{
+	switch_core_session_message_t msg = { 0 };
+
+	/* Tell the channel to respond the call */
+	msg.from = __FILE__;
+	msg.string_arg = data;
+	msg.message_id = SWITCH_MESSAGE_INDICATE_DEFLECT;
+	switch_core_session_receive_message(session, &msg);
+
+}
+
 
 SWITCH_STANDARD_APP(set_function)
 {
@@ -1398,8 +1410,9 @@ SWITCH_STANDARD_APP(audio_bridge_function)
 			}
 
 		}
-
-		switch_channel_hangup(caller_channel, cause);
+		if (!switch_channel_test_flag(caller_channel, CF_TRANSFER) && switch_channel_get_state(caller_channel) != CS_RING) {
+			switch_channel_hangup(caller_channel, cause);
+		}
 		return;
 	} else {
 		if (no_media_bridge) {
@@ -1632,6 +1645,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_dptools_load)
 	SWITCH_ADD_APP(app_interface, "ivr", "Run an ivr menu", "Run an ivr menu.", ivr_application_function, "<menu_name>", SAF_NONE);
 	SWITCH_ADD_APP(app_interface, "redirect", "Send session redirect", "Send a redirect message to a session.", redirect_function, "<redirect_data>", SAF_SUPPORT_NOMEDIA);
 	SWITCH_ADD_APP(app_interface, "respond", "Send session respond", "Send a respond message to a session.", respond_function, "<respond_data>", SAF_SUPPORT_NOMEDIA);
+	SWITCH_ADD_APP(app_interface, "deflect", "Send call deflect", "Send a call deflect.", deflect_function, "<deflect_data>", SAF_SUPPORT_NOMEDIA);
 	SWITCH_ADD_APP(app_interface, "reject", "Send session reject (depricated)", "Send a respond message to a session.", respond_function, "<respond_data>", SAF_SUPPORT_NOMEDIA);
 	SWITCH_ADD_APP(app_interface, "queue_dtmf", "Queue dtmf to be sent", "Queue dtmf to be sent from a session", queue_dtmf_function, "<dtmf_data>", SAF_SUPPORT_NOMEDIA);
 	SWITCH_ADD_APP(app_interface, "sched_hangup", SCHED_HANGUP_DESCR, SCHED_HANGUP_DESCR, sched_hangup_function, "[+]<time> [<cause>]", SAF_SUPPORT_NOMEDIA);
diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.c b/src/mod/endpoints/mod_sofia/mod_sofia.c
index c562884e83..eaf4a6e2e7 100644
--- a/src/mod/endpoints/mod_sofia/mod_sofia.c
+++ b/src/mod/endpoints/mod_sofia/mod_sofia.c
@@ -914,6 +914,21 @@ static switch_status_t sofia_receive_message(switch_core_session_t *session, swi
 			nua_respond(tech_pvt->nh, SIP_302_MOVED_TEMPORARILY, SIPTAG_CONTACT_STR(msg->string_arg), TAG_END());
 		}
 		break;
+
+	case SWITCH_MESSAGE_INDICATE_DEFLECT:
+		{
+			char ref_to[128] = "";
+
+			if (!strstr(msg->string_arg, "sip:")) {
+				switch_snprintf(ref_to, sizeof(ref_to), "sip:%s@%s", msg->string_arg, tech_pvt->profile->sipip);
+			} else {
+				switch_set_string(ref_to, msg->string_arg);
+			}
+
+			nua_refer(tech_pvt->nh, SIPTAG_REFER_TO_STR(ref_to), SIPTAG_REFERRED_BY_STR(tech_pvt->contact_url), TAG_END());
+		}
+		break;
+
 	case SWITCH_MESSAGE_INDICATE_RESPOND:
 		if (msg->numeric_arg || msg->string_arg) {
 			int code = msg->numeric_arg;
diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c
index f675ee6f86..ebcdd1bc61 100644
--- a/src/mod/endpoints/mod_sofia/sofia.c
+++ b/src/mod/endpoints/mod_sofia/sofia.c
@@ -2241,7 +2241,7 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_
 	switch_event_t *v_event = NULL;
 	uint32_t sess_count = switch_core_session_count();
 	uint32_t sess_max = switch_core_session_limit(0);
-	int is_auth = 0;
+	int is_auth = 0, calling_myself = 0;
 	su_addrinfo_t *my_addrinfo = msg_addrinfo(nua_current_request(nua));
 	
 	if (sess_count >= sess_max) {
@@ -2261,12 +2261,19 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_
 		return;
 	}
 	
+
+	get_addr(network_ip, sizeof(network_ip), &((struct sockaddr_in *) my_addrinfo->ai_addr)->sin_addr);
+
 	if ((profile->pflags & PFLAG_AUTH_CALLS) || sip->sip_proxy_authorization || sip->sip_authorization) {
-		if (sofia_reg_handle_register(nua, profile, nh, sip, REG_INVITE, key, sizeof(key), &v_event)) {
-			if (v_event) {
-				switch_event_destroy(&v_event);
+		if (strcmp(network_ip, profile->sipip)) {
+			if (sofia_reg_handle_register(nua, profile, nh, sip, REG_INVITE, key, sizeof(key), &v_event)) {
+				if (v_event) {
+					switch_event_destroy(&v_event);
+				}
+				return;
 			}
-			return;
+		} else {
+			calling_myself++;
 		}
 		is_auth++;
 	}
@@ -2288,13 +2295,16 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_
 		tech_pvt->key = switch_core_session_strdup(session, key);
 	}
 
-	get_addr(network_ip, sizeof(network_ip), &((struct sockaddr_in *) my_addrinfo->ai_addr)->sin_addr);
-
 	channel = switch_core_session_get_channel(session);
+
 	if (is_auth) {
 		switch_channel_set_variable(channel, "sip_authorized", "true");
 	}
 
+	if (calling_myself) {
+		switch_channel_set_variable(channel, "sip_looped_call", "true");
+	}
+
 	if (v_event) {
 		switch_event_header_t *hp;