From 36542c262531933a5e6888256b6a901bbae42b35 Mon Sep 17 00:00:00 2001
From: Mathieu Rene <mrene@avgs.ca>
Date: Tue, 16 Feb 2010 00:49:42 +0000
Subject: [PATCH] MODSOFIA-57 actually do the hangup handler, skipping sofiasip
 calls

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@16655 d0543943-73ff-0310-b7d9-9358b9ac24b2
---
 src/mod/endpoints/mod_sofia/mod_sofia.c  | 12 +++++-------
 src/mod/endpoints/mod_sofia/sofia_glue.c |  3 +--
 2 files changed, 6 insertions(+), 9 deletions(-)

diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.c b/src/mod/endpoints/mod_sofia/mod_sofia.c
index f076bdf2cf..1b1770ed23 100644
--- a/src/mod/endpoints/mod_sofia/mod_sofia.c
+++ b/src/mod/endpoints/mod_sofia/mod_sofia.c
@@ -380,14 +380,11 @@ switch_status_t sofia_on_hangup(switch_core_session_t *session)
 	switch_call_cause_t cause = switch_channel_get_cause(channel);
 	int sip_cause = hangup_cause_to_sip(cause);
 	const char *ps_cause = NULL, *use_my_cause;
-	
-	if (sofia_test_pflag(tech_pvt->profile, PFLAG_DESTROY)) {
-		return SWITCH_STATUS_SUCCESS;
-	}
 
 	switch_mutex_lock(tech_pvt->sofia_mutex);
 
 	sofia_clear_flag(tech_pvt, TFLAG_RECOVERING);
+
 	sofia_glue_tech_untrack(tech_pvt->profile, session, SWITCH_TRUE);
 
 	if (!switch_channel_test_flag(channel, CF_ANSWERED)) {
@@ -414,7 +411,7 @@ switch_status_t sofia_on_hangup(switch_core_session_t *session)
 	switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Channel %s hanging up, cause: %s\n",
 					  switch_channel_get_name(channel), switch_channel_cause2str(cause));
 
-	if (tech_pvt->hash_key) {
+	if (tech_pvt->hash_key && !sofia_test_pflag(tech_pvt->profile, PFLAG_DESTROY)) {
 		switch_core_hash_delete(tech_pvt->profile->chat_hash, tech_pvt->hash_key);
 	}
 
@@ -430,7 +427,9 @@ switch_status_t sofia_on_hangup(switch_core_session_t *session)
 		switch_core_session_rwunlock(a_session);
 	}
 
-	if (tech_pvt->nh && !sofia_test_flag(tech_pvt, TFLAG_BYE)) {
+	if (sofia_test_pflag(tech_pvt->profile, PFLAG_DESTROY)) {
+		sofia_set_flag(tech_pvt, TFLAG_BYE);		
+	} else if (tech_pvt->nh && !sofia_test_flag(tech_pvt, TFLAG_BYE)) {
 		char reason[128] = "";
 		char *bye_headers = sofia_glue_get_extra_headers(channel, SOFIA_SIP_BYE_HEADER_PREFIX);
 		const char *val = NULL;
@@ -482,7 +481,6 @@ switch_status_t sofia_on_hangup(switch_core_session_t *session)
 				}
 			}
 		}
-
 		sofia_set_flag_locked(tech_pvt, TFLAG_BYE);
 		switch_safe_free(bye_headers);
 	}
diff --git a/src/mod/endpoints/mod_sofia/sofia_glue.c b/src/mod/endpoints/mod_sofia/sofia_glue.c
index 987e25cbf6..7929d8e02b 100644
--- a/src/mod/endpoints/mod_sofia/sofia_glue.c
+++ b/src/mod/endpoints/mod_sofia/sofia_glue.c
@@ -4701,8 +4701,7 @@ void sofia_glue_execute_sql_now(sofia_profile_t *profile, char **sqlp, switch_bo
 
 switch_cache_db_handle_t *sofia_glue_get_db_handle(sofia_profile_t *profile)
 {
-	switch_cache_db_connection_options_t options = { {0}
-	};
+	switch_cache_db_connection_options_t options = { {0} };
 	switch_cache_db_handle_t *dbh = NULL;
 
 	if (!zstr(profile->odbc_dsn)) {