Revision
14987
Author
mochouinard
Date
2009-09-25 15:07:40 -0500 (Fri, 25 Sep 2009)

Log Message

mod_sofia:  Override the MWI User@Domain used per user/group/domain (MODENDP-241)
To use this, set a PARAM named MWI-Account with the user@domain you want to monitor the MWI in the user directory.

Modified Paths

Diff

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c (14986 => 14987)


--- freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c	2009-09-25 19:53:30 UTC (rev 14986)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c	2009-09-25 20:07:40 UTC (rev 14987)
@@ -1634,10 +1634,12 @@
 							   "IP:         \t%s\n"
 							   "Port:       \t%s\n"
 							   "Auth-User:  \t%s\n"
-							   "Auth-Realm: \t%s\n\n",
+							   "Auth-Realm: \t%s\n"
+							   "MWI-Account:\t%s@%s\n\n",
 							   switch_str_nil(argv[0]), switch_str_nil(argv[1]), switch_str_nil(argv[2]), switch_str_nil(argv[3]),
 							   switch_str_nil(argv[7]), switch_str_nil(argv[4]), switch_str_nil(argv[5]), exp_buf, switch_str_nil(argv[11]),
-							   switch_str_nil(argv[12]), switch_str_nil(argv[13]), switch_str_nil(argv[14]), switch_str_nil(argv[15]));
+							   switch_str_nil(argv[12]), switch_str_nil(argv[13]), switch_str_nil(argv[14]), switch_str_nil(argv[15]),
+							   switch_str_nil(argv[16]), switch_str_nil(argv[17]));
 	return 0;
 }
 
@@ -1669,11 +1671,13 @@
 							   "        <network-port>%s</network-port>\n"
 							   "        <sip-auth-user>%s</sip-auth-user>\n"
 							   "        <sip-auth-realm>%s</sip-auth-realm>\n"
+							   "        <mwi-account>%s@%s</mwi-account>\n"
 							   "    </registration>\n", 
 							   switch_str_nil(argv[0]), switch_str_nil(argv[1]), switch_str_nil(argv[2]), 
 							   switch_amp_encode(switch_str_nil(argv[3]),xmlbuf,buflen),
 							   switch_str_nil(argv[7]), switch_str_nil(argv[4]), switch_str_nil(argv[5]), exp_buf, switch_str_nil(argv[11]),
-							   switch_str_nil(argv[12]), switch_str_nil(argv[13]), switch_str_nil(argv[14]), switch_str_nil(argv[15]));
+							   switch_str_nil(argv[12]), switch_str_nil(argv[13]), switch_str_nil(argv[14]), switch_str_nil(argv[15]),
+							   switch_str_nil(argv[16]), switch_str_nil(argv[17]));
 	return 0;
 }
 
@@ -1791,14 +1795,14 @@
 				if (!sql && argv[2] && !strcasecmp(argv[2], "pres") && argv[3]) {
 					sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,"
 							"rpid,expires,user_agent,server_user,server_host,profile_name,hostname,"
-							"network_ip,network_port,sip_username,sip_realm"
+							"network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host"
 							" from sip_registrations where profile_name='%q' and presence_hosts like '%%%q%%'", 
 							profile->name, argv[3]);
 				}
 				if (!sql && argv[2] && !strcasecmp(argv[2], "reg") && argv[3]) {
 					sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,"
 							"rpid,expires,user_agent,server_user,server_host,profile_name,hostname,"
-							"network_ip,network_port,sip_username,sip_realm"
+							"network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host"
 							" from sip_registrations where profile_name='%q' and contact like '%%%q%%'", 
 							profile->name, argv[3]);
 				}
@@ -1826,7 +1830,7 @@
 
 					sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,"
 							"rpid,expires,user_agent,server_user,server_host,profile_name,hostname,"
-							"network_ip,network_port,sip_username,sip_realm"
+							"network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host"
 							" from sip_registrations where profile_name='%q' and %s",
 							profile->name, sqlextra);
 					switch_safe_free(dup);
@@ -1836,7 +1840,7 @@
 				if (!sql) {
 					sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,"
 										 "rpid,expires,user_agent,server_user,server_host,profile_name,hostname,"
-										 "network_ip,network_port,sip_username,sip_realm"
+										 "network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host"
 										 " from sip_registrations where profile_name='%q'", 
 										 profile->name);
 				}
@@ -2011,7 +2015,7 @@
 
 					sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,"
 							"rpid,expires,user_agent,server_user,server_host,profile_name,hostname,"
-							"network_ip,network_port,sip_username,sip_realm"
+							"network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host"
 							" from sip_registrations where profile_name='%q' and presence_hosts like '%%%q%%'", 
 							profile->name, argv[3]);
 				}
@@ -2019,7 +2023,7 @@
 
 					sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,"
 							"rpid,expires,user_agent,server_user,server_host,profile_name,hostname,"
-							"network_ip,network_port,sip_username,sip_realm"
+							"network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host"
 							" from sip_registrations where profile_name='%q' and contact like '%%%q%%'", 
 							profile->name, argv[3]);
 				}
@@ -2047,7 +2051,7 @@
 
 					sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,"
 							"rpid,expires,user_agent,server_user,server_host,profile_name,hostname,"
-							"network_ip,network_port,sip_username,sip_realm"
+							"network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host"
 							" from sip_registrations where profile_name='%q' and %s",
 							profile->name, sqlextra);
 					switch_safe_free(dup);
@@ -2057,7 +2061,7 @@
 				if (!sql) {
 					sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,"
 										 "rpid,expires,user_agent,server_user,server_host,profile_name,hostname,"
-										 "network_ip,network_port,sip_username,sip_realm"
+										 "network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host"
 										 " from sip_registrations where profile_name='%q'", 
 										 profile->name);
 				}
@@ -3108,10 +3112,18 @@
 										 "from sip_registrations where call_id='%q'", ct, es, switch_str_nil(body), call_id
 										 );
 				} else {
-					sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,'%q','%q','%q' "
-										 "from sip_registrations where sip_user='%s' and sip_host='%q'",
-										 ct, es, switch_str_nil(body), switch_str_nil(user), switch_str_nil(host)
-										 );
+					if (!strcasecmp(es, "message-summary")) {
+						sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,'%q','%q','%q' "
+								"from sip_registrations where mwi_user='%s' and mwi_host='%q'",
+								ct, es, switch_str_nil(body), switch_str_nil(user), switch_str_nil(host)
+								);
+					} else {
+						sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,'%q','%q','%q' "
+								"from sip_registrations where sip_user='%s' and sip_host='%q'",
+								ct, es, switch_str_nil(body), switch_str_nil(user), switch_str_nil(host)
+								);
+
+					}
 				}
 				
 

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c (14986 => 14987)


--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c	2009-09-25 19:53:30 UTC (rev 14986)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c	2009-09-25 20:07:40 UTC (rev 14987)
@@ -651,6 +651,24 @@
 
 		sofia_profile_t *profile = NULL;
 
+		char *mwi_account = NULL;
+		char *dup_mwi_account = NULL;
+		char *mwi_user = NULL;
+		char *mwi_host = NULL;
+
+		if ((mwi_account = switch_event_get_header(event, "orig-mwi-account"))) {
+			dup_mwi_account = strdup(mwi_account);
+			switch_assert(dup_mwi_account != NULL);
+			sofia_glue_get_user_host(dup_mwi_account, &mwi_user, &mwi_host);
+		}
+
+		if (!mwi_user) {
+			mwi_user = (char *) from_user;
+		}
+		if (!mwi_host) {
+			mwi_host = (char *) from_host;
+		}
+
 		char guess_ip4[256];
 
 		if (exp_str) {
@@ -663,7 +681,7 @@
 
 		if (!profile_name || !(profile = sofia_glue_find_profile(profile_name))) {
 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid Profile\n");
-			return;
+			goto end;
 		}
 		if (sofia_test_pflag(profile, PFLAG_MULTIREG)) {
 			sql = switch_mprintf("delete from sip_registrations where call_id='%q'", call_id);
@@ -699,10 +717,10 @@
 		switch_find_local_ip(guess_ip4, sizeof(guess_ip4), NULL, AF_INET);
 		sql = switch_mprintf("insert into sip_registrations "
 							 "(call_id, sip_user, sip_host, presence_hosts, contact, status, rpid, expires,"
-							 "user_agent, server_user, server_host, profile_name, hostname, network_ip, network_port, sip_username, sip_realm) "
-							 "values ('%q','%q','%q','%q','%q','Registered','%q',%ld, '%q','%q','%q','%q','%q','%q','%q','%q','%q')",
+							 "user_agent, server_user, server_host, profile_name, hostname, network_ip, network_port, sip_username, sip_realm, mwi_user, mwi_host) "
+							 "values ('%q','%q','%q','%q','%q','Registered','%q',%ld, '%q','%q','%q','%q','%q','%q','%q','%q','%q','%q','%q')",
 							 call_id, from_user, from_host, presence_hosts, contact_str, rpid, expires, user_agent, to_user, guess_ip4, 
-							 profile_name, mod_sofia_globals.hostname, network_ip, network_port, username, realm);
+							 profile_name, mod_sofia_globals.hostname, network_ip, network_port, username, realm, mwi_user, mwi_host);
 
 		if (sql) {
 			sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
@@ -713,8 +731,9 @@
 		if (profile) {
 			sofia_glue_release_profile(profile);
 		}
-
+end:
 		switch_safe_free(fixed_contact_str);
+		switch_safe_free(dup_mwi_account);
 	}
 }
 

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c (14986 => 14987)


--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c	2009-09-25 19:53:30 UTC (rev 14986)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c	2009-09-25 20:07:40 UTC (rev 14987)
@@ -3597,7 +3597,9 @@
 		"   network_ip      VARCHAR(255),\n" 
 		"   network_port    VARCHAR(6),\n" 
 		"   sip_username    VARCHAR(255),\n" 
-		"   sip_realm       VARCHAR(255)\n" 
+		"   sip_realm       VARCHAR(255),\n"
+		"   mwi_user        VARCHAR(255),\n"
+		"   mwi_host        VARCHAR(255)\n"
 		");\n";
 
 
@@ -3740,7 +3742,7 @@
 		
 		test_sql = switch_mprintf("delete from sip_registrations where (contact like '%%TCP%%' "
 								  "or status like '%%TCP%%' or status like '%%TLS%%') and hostname='%q' "
-								  "and network_ip!='-1' and network_port!='-1' and sip_username != '-1'", 
+								  "and network_ip!='-1' and network_port!='-1' and sip_username != '-1' and mwi_user != '-1' and mwi_host != '-1'", 
 								  mod_sofia_globals.hostname);
 
 		if (switch_odbc_handle_exec(profile->master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
@@ -3813,7 +3815,7 @@
 
 		test_sql = switch_mprintf("delete from sip_registrations where (contact like '%%TCP%%' "
 								  "or status like '%%TCP%%' or status like '%%TLS%%') and hostname='%q' "
-								  "and network_ip!='-1' and network_port!='-1' and sip_username != '-1'",
+								  "and network_ip!='-1' and network_port!='-1' and sip_username != '-1' and mwi_user != '-1' and mwi_host != '-1'",
 								  mod_sofia_globals.hostname);
 		
 		switch_core_db_test_reactive(profile->master_db, test_sql, "DROP TABLE sip_registrations", reg_sql);
@@ -4120,46 +4122,44 @@
 
 int sofia_glue_get_user_host(char *in, char **user, char **host)
 {
-	char *p, *h, *u = in;
+	char *p = NULL, *h = NULL, *u = in;
 
-	if (user) {
-		*user = NULL;
+	if (!in) {
+		return 0;
 	}
 
-	*host = NULL;
-
 	/* First isolate the host part from the user part */
 	if ((h = strchr(u, '@'))) {
 		*h++ = '\0';
-	} else {
-		return 0;
 	}
 
 	/* Clean out the user part of its protocol prefix (if any) */
-	if (user && (p = strchr(u, ':'))) {
+	if ((p = strchr(u, ':'))) {
 		*p++ = '\0';
 		u = p;
 	}
 
 	/* Clean out the host part of any suffix */
-	if ((p = strchr(h, ':'))) {
-		*p = '\0';
+	if (h) {
+		if ((p = strchr(h, ':'))) {
+			*p = '\0';
+		}
+
+		if ((p = strchr(h, ';'))) {
+			*p = '\0';
+		}
+
+		if ((p = strchr(h, ' '))) {
+			*p = '\0';
+		}
 	}
-	
-	if ((p = strchr(h, ';'))) {
-		*p = '\0';
+	if (user) {
+		*user = u;
 	}
-	
-	if ((p = strchr(h, ' '))) {
-		*p = '\0';
+	if (host) {
+		*host = h;
 	}
 
-	if (user) {
-	*user = u;
-	}
-
-	*host = h;
-
 	return 1;
 }
 

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c (14986 => 14987)


--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c	2009-09-25 19:53:30 UTC (rev 14986)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c	2009-09-25 20:07:40 UTC (rev 14987)
@@ -345,11 +345,11 @@
 
 	if (for_everyone) {
 		sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,network_ip,'%q' "
-							 "from sip_registrations where sip_user='%q' and sip_host='%q'", 
+							 "from sip_registrations where mwi_user='%q' and mwi_host='%q'", 
 							 stream.data, user, host);
 	} else if (call_id) {
 		sql = switch_mprintf("select sip_user,sip_host,contact,profile_name,network_ip,'%q' "
-							 "from sip_registrations where sip_user='%q' and sip_host='%q' and call_id='%q'", 
+							 "from sip_registrations where mwi_user='%q' and mwi_host='%q' and call_id='%q'", 
 							 stream.data, user, host, call_id);
 	}
 

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c (14986 => 14987)


--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c	2009-09-25 19:53:30 UTC (rev 14986)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c	2009-09-25 20:07:40 UTC (rev 14987)
@@ -710,6 +710,10 @@
 	switch_event_t *s_event;
 	const char *to_user = NULL;
 	const char *to_host = NULL;
+	char *mwi_account = NULL;
+	char *dup_mwi_account = NULL;
+	char *mwi_user = NULL;
+	char *mwi_host = NULL;
 	const char *from_user = NULL;
 	const char *from_host = NULL;
 	const char *reg_host = profile->reg_db_domain;
@@ -1004,6 +1008,19 @@
 	}
   reg:
 
+	if ((mwi_account = switch_event_get_header(*v_event, "mwi-account"))) {
+		dup_mwi_account = strdup(mwi_account);
+		switch_assert(dup_mwi_account != NULL);
+		sofia_glue_get_user_host(dup_mwi_account, &mwi_user, &mwi_host);
+	}
+
+	if (!mwi_user) {
+		mwi_user = (char *) to_user;
+	}
+	if (!mwi_host) {
+		mwi_host = (char *) reg_host;
+	}
+
 	if (regtype != REG_REGISTER) {
 		switch_goto_int(r, 0, end);
 	}
@@ -1049,21 +1066,20 @@
 		}
 		switch_mutex_lock(profile->ireg_mutex);
 		sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
-		
+
 		switch_find_local_ip(guess_ip4, sizeof(guess_ip4), NULL, AF_INET);
 		sql = switch_mprintf("insert into sip_registrations "
 							 "(call_id,sip_user,sip_host,presence_hosts,contact,status,rpid,expires,"
-							 "user_agent,server_user,server_host,profile_name,hostname,network_ip,network_port,sip_username,sip_realm) "
-							 "values ('%q','%q', '%q','%q','%q','%q', '%q', %ld, '%q', '%q', '%q', '%q', '%q', '%q', '%q','%q','%q')", 
+							 "user_agent,server_user,server_host,profile_name,hostname,network_ip,network_port,sip_username,sip_realm,mwi_user,mwi_host) "
+							 "values ('%q','%q', '%q','%q','%q','%q', '%q', %ld, '%q', '%q', '%q', '%q', '%q', '%q', '%q','%q','%q','%q','%q')", 
 							 call_id, to_user, reg_host, profile->presence_hosts ? profile->presence_hosts : reg_host, 
 							 contact_str, reg_desc, rpid, (long) switch_epoch_time_now(NULL) + (long) exptime * 2, 
-							 agent, from_user, guess_ip4, profile->name, mod_sofia_globals.hostname, network_ip, network_port_c, username, realm);
+							 agent, from_user, guess_ip4, profile->name, mod_sofia_globals.hostname, network_ip, network_port_c, username, realm, mwi_user, mwi_host);
 							 
 		if (sql) {
 			sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
 		}
 
-
 		switch_mutex_unlock(profile->ireg_mutex);
 
 		if (switch_event_create_subclass(&s_event, SWITCH_EVENT_CUSTOM, MY_EVENT_REGISTER) == SWITCH_STATUS_SUCCESS) {
@@ -1198,7 +1214,7 @@
 
 			if (sofia_test_pflag(profile, PFLAG_MESSAGE_QUERY_ON_REGISTER)) {
 				if (switch_event_create(&s_event, SWITCH_EVENT_MESSAGE_QUERY) == SWITCH_STATUS_SUCCESS) {
-					switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "Message-Account", "sip:%s@%s", to_user, reg_host);
+					switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "Message-Account", "sip:%s@%s", mwi_user, mwi_host);
 					switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "VM-Sofia-Profile", profile->name);
 					switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "VM-Call-ID", call_id);
 				}
@@ -1239,6 +1255,7 @@
 
 
  end:
+	switch_safe_free(dup_mwi_account);
 
 	if (auth_params) {
 		switch_event_destroy(&auth_params);
@@ -1568,6 +1585,7 @@
 	int first = 0;
 	const char *passwd = NULL;
 	const char *a1_hash = NULL;
+	const char *mwi_account = NULL;
 	char *sql;
 	char *number_alias = NULL;
 	switch_xml_t domain, xml = NULL, user, param, uparams, dparams, group = NULL, gparams = NULL;
@@ -1762,6 +1780,9 @@
 			if (!strcasecmp(var, "a1-hash")) {
 				a1_hash = val;
 			}
+			if (!strcasecmp(var, "mwi-account")) {
+				mwi_account = val;
+			}
 		}
 	}
 
@@ -1786,6 +1807,9 @@
 			if (!strcasecmp(var, "a1-hash")) {
 				a1_hash = val;
 			}
+			if (!strcasecmp(var, "mwi-account")) {
+				mwi_account = val;
+			}
 		}
 	}
 
@@ -1810,6 +1834,9 @@
 			if (!strcasecmp(var, "a1-hash")) {
 				a1_hash = val;
 			}
+			if (!strcasecmp(var, "mwi-account")) {
+				mwi_account = val;
+			}
 		}
 	}
 
@@ -1897,6 +1924,10 @@
 			switch_event_add_header_string(*v_event, SWITCH_STACK_BOTTOM, "user_name", username);
 			switch_event_add_header_string(*v_event, SWITCH_STACK_BOTTOM, "domain_name", domain_name);
 
+			if (mwi_account) {
+				switch_event_add_header_string(*v_event, SWITCH_STACK_BOTTOM, "mwi-account", mwi_account);
+			}
+
 			if ((uparams = switch_xml_child(user, "params"))) {
 				xparams_type[i] = 0;
 				xparams[i++] = uparams;