[Freeswitch-svn] [commit] r6351 - freeswitch/trunk/src/mod/endpoints/mod_sofia

Freeswitch SVN mikej at freeswitch.org
Mon Nov 19 22:26:40 EST 2007


Author: mikej
Date: Mon Nov 19 22:26:40 2007
New Revision: 6351

Modified:
   freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c

Log:
re-factor, reduce indentation in some function, whitespace and comment cleanup.

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c	Mon Nov 19 22:26:40 2007
@@ -48,60 +48,63 @@
 	nua_handle_t *msg_nh;
 	char *contact;
 
-	if (to && (user = strdup(to))) {
-		if ((host = strchr(user, '@'))) {
-			*host++ = '\0';
-		}
+	if (!to) {
+		return SWITCH_STATUS_SUCCESS;
+	}
 
-		if (!host || !(profile = sofia_glue_find_profile(host))) {
-			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
-							  "Chat proto [%s]\nfrom [%s]\nto [%s]\n%s\nInvalid Profile %s\n", proto, from, to,
-							  body ? body : "[no body]", host ? host : "NULL");
-			return SWITCH_STATUS_FALSE;
-		}
+	user = strdup(to);
+	assert(user);
 
-		if (!sofia_reg_find_reg_url(profile, user, host, buf, sizeof(buf))) {
-			return SWITCH_STATUS_FALSE;
-		}
+	if ((host = strchr(user, '@'))) {
+		*host++ = '\0';
+	}
 
-		if (!strcmp(proto, SOFIA_CHAT_PROTO)) {
-			from = hint;
-		} else {
-			char *fp, *p, *fu = NULL;
+	if (!host || !(profile = sofia_glue_find_profile(host))) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
+						  "Chat proto [%s]\nfrom [%s]\nto [%s]\n%s\nInvalid Profile %s\n", proto, from, to,
+						  body ? body : "[no body]", host ? host : "NULL");
+		return SWITCH_STATUS_FALSE;
+	}
 
-			if (!(fp = strdup(from))) {
-				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
-				return SWITCH_STATUS_FALSE;
-			}
+	if (!sofia_reg_find_reg_url(profile, user, host, buf, sizeof(buf))) {
+		return SWITCH_STATUS_FALSE;
+	}
 
-			if ((p = strchr(fp, '@'))) {
-				*p = '\0';
-				fu = strdup(fp);
-				*p = '+';
-			}
+	if (!strcmp(proto, SOFIA_CHAT_PROTO)) {
+		from = hint;
+	} else {
+		char *fp, *p, *fu = NULL;
+		fp = strdup(from);
+		if (!fp) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
+			return SWITCH_STATUS_FALSE;
+		}
 
-			ffrom = switch_mprintf("\"%s\" <sip:%s+%s@%s>", fu, proto, fp, profile->name);
-			from = ffrom;
-			switch_safe_free(fu);
-			switch_safe_free(fp);
+		if ((p = strchr(fp, '@'))) {
+			*p = '\0';
+			fu = strdup(fp);
+			*p = '+';
 		}
 
-		contact = sofia_glue_get_url_from_contact(buf, 1);
-		msg_nh = nua_handle(profile->nua, NULL, SIPTAG_FROM_STR(from), NUTAG_URL(contact), SIPTAG_TO_STR(buf),	// if this cries, add contact here too, change the 1 to 0 and omit the safe_free
-							SIPTAG_CONTACT_STR(profile->url), TAG_END());
+		ffrom = switch_mprintf("\"%s\" <sip:%s+%s@%s>", fu, proto, fp, profile->name);
+		from = ffrom;
+		switch_safe_free(fu);
+		switch_safe_free(fp);
+	}
 
-		switch_safe_free(contact);
+	contact = sofia_glue_get_url_from_contact(buf, 1);
+	msg_nh = nua_handle(profile->nua, NULL, SIPTAG_FROM_STR(from), NUTAG_URL(contact), SIPTAG_TO_STR(buf),	// if this cries, add contact here too, change the 1 to 0 and omit the safe_free
+						SIPTAG_CONTACT_STR(profile->url), TAG_END());
 
+	switch_safe_free(contact);
 
-		nua_message(msg_nh, SIPTAG_CONTENT_TYPE_STR("text/html"), SIPTAG_PAYLOAD_STR(body), TAG_END());
-		
+	nua_message(msg_nh, SIPTAG_CONTENT_TYPE_STR("text/html"), SIPTAG_PAYLOAD_STR(body), TAG_END());
 
-		switch_safe_free(ffrom);
-		free(user);
+	switch_safe_free(ffrom);
+	free(user);
 
-		if (profile) {
-			switch_thread_rwlock_unlock(profile->rwlock);
-		}
+	if (profile) {
+		switch_thread_rwlock_unlock(profile->rwlock);
 	}
 
 	return SWITCH_STATUS_SUCCESS;
@@ -149,8 +152,6 @@
 	}
 }
 
-
-
 char *sofia_presence_translate_rpid(char *in, char *ext)
 {
 	char *r = in;
@@ -233,7 +234,6 @@
 									sofia_presence_mwi_callback,
 									profile);
 
-
 	switch_safe_free(sql);
 	switch_safe_free(dup_account);
 	if (profile) {
@@ -241,7 +241,6 @@
 	}
 }
 
-
 void sofia_presence_event_handler(switch_event_t *event)
 {
 	sofia_profile_t *profile = NULL;
@@ -252,11 +251,9 @@
 	char *rpid = switch_event_get_header(event, "rpid");
 	char *status = switch_event_get_header(event, "status");
 	char *event_type = switch_event_get_header(event, "event_type");
-	//char *event_subtype = switch_event_get_header(event, "event_subtype");
 	char *sql = NULL;
 	char *euser = NULL, *user = NULL, *host = NULL;
 
-
 	if (rpid && !strcasecmp(rpid, "n/a")) {
 		rpid = NULL;
 	}
@@ -304,15 +301,12 @@
 				continue;
 			}
 
-		
 			sofia_glue_execute_sql_callback(profile,
 											SWITCH_FALSE,
 											profile->ireg_mutex,
 											sql,
 											sofia_presence_sub_callback,
 											profile);
-		
-
 		}
 		switch_mutex_unlock(mod_sofia_globals.hash_mutex);
 		free(sql);
@@ -339,12 +333,10 @@
 		} else {
 			euser = user;
 		}
-
 	} else {
 		return;
 	}
 
-
 	switch (event->event_id) {
 	case SWITCH_EVENT_PRESENCE_PROBE:
 		if (proto) {
@@ -374,9 +366,7 @@
 												sofia_presence_resub_callback,
 												profile);
 
-				
 				sofia_glue_release_profile(profile);
-					
 				switch_safe_free(sql);
 			}
 			switch_safe_free(user);
@@ -413,7 +403,6 @@
 											sql,
 											sofia_presence_sub_callback,
 											profile);
-
 		}
 	}
 	switch_mutex_unlock(mod_sofia_globals.hash_mutex);
@@ -425,7 +414,6 @@
 static int sofia_presence_sub_reg_callback(void *pArg, int argc, char **argv, char **columnNames)
 {
 	sofia_profile_t *profile = (sofia_profile_t *) pArg;
-	//char *proto = argv[0];
 	char *user = argv[1];
 	char *host = argv[2];
 	switch_event_t *event;
@@ -603,8 +591,6 @@
 			   SIPTAG_SUBSCRIPTION_STATE_STR("active;expires=3600"),
 			   SIPTAG_EVENT_STR(event), SIPTAG_CONTENT_TYPE_STR("application/pidf+xml"), SIPTAG_PAYLOAD_STR(pl), TAG_END());
 
-
-
 	if (done) {
 		switch_core_hash_delete(profile->sub_hash, call_id);
 	}
@@ -621,7 +607,7 @@
 {
 	//char *proto = argv[0];
 	//char *user = argv[1];
-	char *host = argv[2];
+	//char *host = argv[2];
 	char *sub_to_user = argv[3];
 	char *sub_to_host = argv[4];
 	char *event = argv[5];
@@ -638,7 +624,7 @@
 	int expire_sec = atoi(expires);
 
 	if (!(profile = sofia_glue_find_profile(sub_to_host))) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot find profile for host %s\n", host);
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot find profile for host %s\n", sub_to_host);
 		return 0;
 	}
 	
@@ -658,17 +644,14 @@
 			   SIPTAG_SUBSCRIPTION_STATE_STR(exp),
 			   SIPTAG_EVENT_STR(event), SIPTAG_CONTENT_TYPE_STR("application/simple-message-summary"), SIPTAG_PAYLOAD_STR(body), TAG_END());
 
-
 	switch_safe_free(id);
 	switch_safe_free(exp);
 
 	sofia_glue_release_profile(profile);
 
-
 	return 0;
 }
 
-
 void sofia_presence_handle_sip_i_subscribe(int status,
 							char const *phrase,
 							nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip, tagi_t tags[])
@@ -678,10 +661,9 @@
 		sip_to_t const *to = sip->sip_to;
 		sip_from_t const *from = sip->sip_from;
 		sip_contact_t const *contact = sip->sip_contact;
-		char *from_user = NULL;
-		char *from_host = NULL;
-		char *to_user = NULL;
-		char *to_host = NULL;
+		const char *from_user = NULL, *from_host = NULL;
+		const char *to_user = NULL, *to_host = NULL;
+		char *my_to_user = NULL;
 		char *sql, *event = NULL;
 		char *proto = "sip";
 		char *d_user = NULL;
@@ -698,7 +680,6 @@
 		tl_gets(tags,
 				NUTAG_SUBSTATE_REF(sub_state), TAG_END());
 
-
 		if (contact) {
 			char *port = (char *) contact->m_url->url_port;
 
@@ -732,13 +713,13 @@
 		}
 
 		if (to) {
-			to_user = (char *) to->a_url->url_user;
-			to_host = (char *) to->a_url->url_host;
+			to_user = to->a_url->url_user;
+			to_host = to->a_url->url_host;
 		}
 
 		if (sip && sip->sip_from) {
-			from_user = (char *) sip->sip_from->a_url->url_user;
-			from_host = (char *) sip->sip_from->a_url->url_host;
+			from_user = sip->sip_from->a_url->url_user;
+			from_host = sip->sip_from->a_url->url_host;
 		} else {
 			from_user = "n/a";
 			from_host = "n/a";
@@ -776,8 +757,9 @@
 		if (strchr(to_user, '+')) {
 			char *h;
 			if ((proto = (d_user = strdup(to_user)))) {
-				if ((to_user = strchr(d_user, '+'))) {
-					*to_user++ = '\0';
+				if ((my_to_user = strchr(d_user, '+'))) {
+					*my_to_user++ = '\0';
+					to_user = my_to_user;
 					if ((h = strchr(to_user, '+')) || (h = strchr(to_user, '@'))) {
 						*h++ = '\0';
 						to_host = h;
@@ -791,7 +773,7 @@
 			}
 		}
 
-		call_id = sip->sip_call_id->i_id; //sip_header_as_string(profile->home, (void *) sip->sip_call_id);
+		call_id = sip->sip_call_id->i_id;
 		event = sip_header_as_string(profile->home, (void *) sip->sip_event);
 		full_from = sip_header_as_string(profile->home, (void *) sip->sip_from);
 		full_via = sip_header_as_string(profile->home, (void *) sip->sip_via);
@@ -799,8 +781,6 @@
 		exp_raw = (sip->sip_expires ? sip->sip_expires->ex_delta : 3600);
 		exp = (long) time(NULL) + exp_raw;
 
-		
-
 		switch_mutex_lock(profile->ireg_mutex);
 
 		sql = switch_mprintf("delete from sip_subscriptions where "
@@ -827,11 +807,9 @@
 			sofia_glue_execute_sql(profile, SWITCH_FALSE, sql, NULL);
 			free(sql);
 
-
 			switch_mutex_unlock(profile->ireg_mutex);
 			sstr = switch_mprintf("active;expires=%ld", exp_raw);
 			switch_core_hash_insert(profile->sub_hash, call_id, nh);
-			
 		}
 
 		nua_respond(nh, SIP_202_ACCEPTED,
@@ -862,12 +840,6 @@
 			su_free(profile->home, event);
 		}
 
-		/*
-		  if (call_id) {
-		  su_free(profile->home, call_id);
-		  }
-		*/
-
 		if (full_from) {
 			su_free(profile->home, full_from);
 		}
@@ -975,70 +947,52 @@
 				if (event_type) {
 					su_free(profile->home, event_type);
 				}
-
 				switch_xml_free(xml);
 			}
-
 		}
-
 	}
-
 	nua_respond(nh, SIP_200_OK, NUTAG_WITH_THIS(nua), TAG_END());
-
 }
 
 void sofia_presence_set_hash_key(char *hash_key, int32_t len, sip_t const *sip)
 {
-
-	snprintf(hash_key, len, "%s%s%s", (char *) sip->sip_from->a_url->url_user, (char *) sip->sip_from->a_url->url_host,
-			 (char *) sip->sip_to->a_url->url_user);
-
-
-#if 0
-	/* nicer one we cant use in both directions >=0 */
-	snprintf(hash_key, len, "%s%s%s%s%s%s",
-			 (char *) sip->sip_to->a_url->url_user,
-			 (char *) sip->sip_to->a_url->url_host,
-			 (char *) sip->sip_to->a_url->url_params,
-			 (char *) sip->sip_from->a_url->url_user, (char *) sip->sip_from->a_url->url_host, (char *) sip->sip_from->a_url->url_params);
-#endif
-
+	url_t *to = sip->sip_to->a_url;
+	url_t *from = sip->sip_from->a_url;
+	snprintf(hash_key, len, "%s%s%s", from->url_user, from->url_host, to->url_user);
 }
 
-
 void sofia_presence_handle_sip_i_message(int status,
 						  char const *phrase,
 						  nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip, tagi_t tags[])
 {
 	if (sip) {
 		sip_from_t const *from = sip->sip_from;
-		char *from_user = NULL;
-		char *from_host = NULL;
+		const char *from_user = NULL;
+		const char *from_host = NULL;
 		sip_to_t const *to = sip->sip_to;
-		char *to_user = NULL;
-		char *to_host = NULL;
+		const char *to_user = NULL;
+		const char *to_host = NULL;
 		sip_subject_t const *sip_subject = sip->sip_subject;
 		sip_payload_t *payload = sip->sip_payload;
 		const char *subject = "n/a";
 		char *msg = NULL;
 
 		if (sip->sip_content_type) {
-			if (strstr((char *) sip->sip_content_type->c_subtype, "composing")) {
+			if (strstr(sip->sip_content_type->c_subtype, "composing")) {
 				return;
 			}
 		}
 
 		if (from) {
-			from_user = (char *) from->a_url->url_user;
-			from_host = (char *) from->a_url->url_host;
+			from_user = from->a_url->url_user;
+			from_host = from->a_url->url_host;
 		}
 
 		if (to) {
-			to_user = (char *) to->a_url->url_user;
-			to_host = (char *) to->a_url->url_host;
+			to_user = to->a_url->url_user;
+			to_host = to->a_url->url_host;
 		}
 
-
 		if (!to_user) {
 			return;
 		}
@@ -1074,14 +1028,12 @@
 						*p = '@';
 					}
 				}
-
 			} else {
 				to_addr = switch_mprintf("%s@%s", to_user, to_host);
 			}
 
 			from_addr = switch_mprintf("%s@%s", from_user, from_host);
 
-
 			sofia_presence_set_hash_key(hash_key, sizeof(hash_key), sip);
 			if ((tech_pvt = (private_object_t *) switch_core_hash_find(profile->chat_hash, hash_key))) {
 				channel = switch_core_session_get_channel(tech_pvt->session);
@@ -1107,7 +1059,6 @@
 				} else {
 					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Chat Interface [%s]!\n", proto);
 				}
-
 			}
 			switch_safe_free(to_addr);
 			switch_safe_free(from_addr);
@@ -1115,7 +1066,6 @@
 				su_free(profile->home, full_from);
 			}
 		}
-
 	}
 }
 



More information about the Freeswitch-svn mailing list