From 0ac9a16321ee93364acf1e96bc07bc441ab05ae4 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthony.minessale@gmail.com>
Date: Tue, 13 Nov 2007 19:58:44 +0000
Subject: [PATCH] fix some stuff and add set_user func

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@6239 d0543943-73ff-0310-b7d9-9358b9ac24b2
---
 .../applications/mod_dptools/mod_dptools.c    | 62 +++++++++++++++++++
 src/mod/applications/mod_limit/mod_limit.c    | 26 ++++----
 src/mod/endpoints/mod_sofia/sofia.c           |  2 -
 src/mod/endpoints/mod_sofia/sofia_reg.c       |  3 +
 4 files changed, 78 insertions(+), 15 deletions(-)

diff --git a/src/mod/applications/mod_dptools/mod_dptools.c b/src/mod/applications/mod_dptools/mod_dptools.c
index e495943e54..ef2c488c0d 100644
--- a/src/mod/applications/mod_dptools/mod_dptools.c
+++ b/src/mod/applications/mod_dptools/mod_dptools.c
@@ -86,6 +86,67 @@ SWITCH_STANDARD_APP(exe_function)
 	}
 }
 
+
+#define SET_USER_SYNTAX "<user>@<domain>"
+SWITCH_STANDARD_APP(set_user_function)
+{
+	switch_xml_t x_domain, xml = NULL, x_user, x_param, x_params;	
+	char *user, *mailbox, *domain;
+	switch_channel_t *channel;
+
+	channel = switch_core_session_get_channel(session);
+	assert(channel != NULL);
+
+	if (switch_strlen_zero(data)) {
+		goto error;
+	}
+
+	user = switch_core_session_strdup(session, data);
+
+	if (!(domain = strchr(user, '@'))) {
+		goto error;
+	}
+
+	*domain++ = '\0';
+	
+	if (switch_xml_locate_user(user, domain, NULL, &xml, &x_domain, &x_user, NULL) != SWITCH_STATUS_SUCCESS) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "can't find user [%s@%s]\n", user, domain);
+		goto done;
+	}
+
+	if ((mailbox = (char *)switch_xml_attr(x_user, "mailbox"))) {
+		switch_channel_set_variable(channel, "mailbox", mailbox);
+	}
+	
+	if ((x_params = switch_xml_child(x_user, "variables"))) {
+		for (x_param = switch_xml_child(x_params, "variable"); x_param; x_param = x_param->next) {
+			const char *var = switch_xml_attr(x_param, "name");
+			const char *val = switch_xml_attr(x_param, "value");
+			
+			if (var && val) {
+				switch_channel_set_variable(channel, var, val);
+			}
+		}
+	}
+
+	switch_channel_set_variable(channel, "user_name", user);
+	switch_channel_set_variable(channel, "domain_name", domain);
+
+	goto done;
+
+ error:
+	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No user@domain specified.\n");
+
+ done:
+	
+	if (xml) {
+		switch_xml_free(xml);
+	}
+
+
+}
+
+
 SWITCH_STANDARD_APP(ring_ready_function)
 {
 	switch_channel_t *channel;
@@ -1390,6 +1451,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_dptools_load)
 	SWITCH_ADD_APP(app_interface, "sched_broadcast", SCHED_BROADCAST_DESCR, SCHED_BROADCAST_DESCR, sched_broadcast_function, "[+]<time> <path> [aleg|bleg|both]", SAF_SUPPORT_NOMEDIA);
 	SWITCH_ADD_APP(app_interface, "sched_transfer", SCHED_TRANSF_DESCR, SCHED_TRANSF_DESCR, sched_transfer_function, "[+]<time> <extension> <dialplan> <context>", SAF_SUPPORT_NOMEDIA);
 	SWITCH_ADD_APP(app_interface, "execute_extension", "Execute an extension", "Execute an extension", exe_function, EXE_SYNTAX, SAF_SUPPORT_NOMEDIA);
+	SWITCH_ADD_APP(app_interface, "set_user", "Set a User", "Set a User", set_user_function, SET_USER_SYNTAX, SAF_SUPPORT_NOMEDIA);
 	SWITCH_ADD_APP(app_interface, "stop_dtmf", "stop inband dtmf", "Stop detecting inband dtmf.", stop_dtmf_session_function, "", SAF_NONE);
 	SWITCH_ADD_APP(app_interface, "start_dtmf", "Detect dtmf", "Detect inband dtmf on the session", dtmf_session_function, "", SAF_NONE);
 	SWITCH_ADD_APP(app_interface, "stop_dtmf_generate", "stop inband dtmf generation", "Stop generating inband dtmf.", 
diff --git a/src/mod/applications/mod_limit/mod_limit.c b/src/mod/applications/mod_limit/mod_limit.c
index 4ef4eb03f1..c65a6dd2a2 100644
--- a/src/mod/applications/mod_limit/mod_limit.c
+++ b/src/mod/applications/mod_limit/mod_limit.c
@@ -390,14 +390,14 @@ SWITCH_STANDARD_API(db_api_function)
 }
 
 
-#define DB_USAGE "[add|del]/<realm>/<key>/<val>"
+#define DB_USAGE "[insert|delete]/<realm>/<key>/<val>"
 #define DB_DESC "save data"
 
 SWITCH_STANDARD_APP(db_function)
 {
     switch_channel_t *channel;
     int argc = 0;
-    char *argv[3] = { 0 };
+    char *argv[4] = { 0 };
     char *mydata = NULL;
     char *sql = NULL;
 
@@ -408,15 +408,15 @@ SWITCH_STANDARD_APP(db_function)
         mydata = switch_core_session_strdup(session, data);
         argc = switch_separate_string(mydata, '/', argv, (sizeof(argv) / sizeof(argv[0])));
     }
-
+    printf("WTF [%s] %d\n", argv[0], argc);
     if (argc < 4) {
         goto error;
     }
-
     
-    if (!strcasecmp(argv[0], "add")) {
+    
+    if (!strcasecmp(argv[0], "insert")) {
         sql = switch_mprintf("insert into db_data values('%q','%q','%q','%q');", globals.hostname, argv[1], argv[2], argv[3]);
-    } else if (!strcasecmp(argv[0], "del")) {
+    } else if (!strcasecmp(argv[0], "delete")) {
         sql = switch_mprintf("delete from db_data where realm='%q' and data_key='%q'", argv[1], argv[2]);
     }
 
@@ -454,7 +454,7 @@ SWITCH_STANDARD_API(group_api_function)
         goto error;
     }
 
-    if (!strcasecmp(argv[0], "add")) {
+    if (!strcasecmp(argv[0], "insert")) {
         if (argc < 3) {
             goto error;
         }
@@ -468,7 +468,7 @@ SWITCH_STANDARD_API(group_api_function)
         switch_safe_free(sql);
         stream->write_function(stream, "+OK");
         goto done;
-    } else if (!strcasecmp(argv[0], "del")) {
+    } else if (!strcasecmp(argv[0], "delete")) {
         if (argc < 3) {
             goto error;
         }
@@ -515,7 +515,7 @@ SWITCH_STANDARD_API(group_api_function)
     
 }
 
-#define GROUP_USAGE "[add|del]:<group name>:<val>"
+#define GROUP_USAGE "[insert|delete]:<group name>:<val>"
 #define GROUP_DESC "save data"
 
 SWITCH_STANDARD_APP(group_function)
@@ -539,12 +539,12 @@ SWITCH_STANDARD_APP(group_function)
         return;
     }
 
-    if (!strcasecmp(argv[0], "add")) {
+    if (!strcasecmp(argv[0], "insert")) {
         sql = switch_mprintf("insert into group_data values('%q','%q','%q');", globals.hostname, argv[1], argv[2]);
         assert(sql);
         limit_execute_sql(sql, globals.mutex);
         switch_safe_free(sql);
-    } else if (!strcasecmp(argv[0], "del")) {
+    } else if (!strcasecmp(argv[0], "delete")) {
         sql = switch_mprintf("delete from group_data where groupname='%q' and url='%q';", argv[1], argv[2]);
         assert(sql);
         limit_execute_sql(sql, globals.mutex);
@@ -643,11 +643,11 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_limit_load)
     *module_interface = switch_loadable_module_create_module_interface(pool, modname);
 
 	SWITCH_ADD_APP(app_interface, "limit", "Limit", LIMIT_DESC, limit_function, LIMIT_USAGE, SAF_NONE);
-	SWITCH_ADD_APP(app_interface, "db_insert", "Insert to the db", DB_DESC, db_function, DB_USAGE, SAF_NONE);
+	SWITCH_ADD_APP(app_interface, "db", "Insert to the db", DB_DESC, db_function, DB_USAGE, SAF_NONE);
 	SWITCH_ADD_APP(app_interface, "group", "Manage a group", GROUP_DESC, group_function, GROUP_USAGE, SAF_NONE);
 
 	SWITCH_ADD_API(commands_api_interface, "db", "db get/set", db_api_function, "[get|set]/<realm>/<key>/<value>");
-	SWITCH_ADD_API(commands_api_interface, "group", "group [add|del|call]", group_api_function, "[add|del|call] <group name> <url>");
+	SWITCH_ADD_API(commands_api_interface, "group", "group [insert|delete|call]", group_api_function, "[insert|delete|call] <group name> <url>");
 
     /* indicate that the module should continue to be loaded */
     return SWITCH_STATUS_SUCCESS;
diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c
index 030e4fc98e..ac3f0b29d3 100644
--- a/src/mod/endpoints/mod_sofia/sofia.c
+++ b/src/mod/endpoints/mod_sofia/sofia.c
@@ -1994,8 +1994,6 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_
 		
 		
 		if (!switch_strlen_zero(from_user)) {
-			switch_channel_set_variable(channel, "sip_mailbox", from_user);
-
 			if (*from_user == '+') {
 				switch_channel_set_variable(channel, "sip_from_user_stripped", (const char *) (from_user + 1));
 			} else {
diff --git a/src/mod/endpoints/mod_sofia/sofia_reg.c b/src/mod/endpoints/mod_sofia/sofia_reg.c
index a1d83dbfd4..92ed4b3341 100644
--- a/src/mod/endpoints/mod_sofia/sofia_reg.c
+++ b/src/mod/endpoints/mod_sofia/sofia_reg.c
@@ -981,6 +981,9 @@ auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile, sip_authorization_t co
 			switch_event_add_header(*v_event, SWITCH_STACK_BOTTOM, "sip_mailbox", "%s", mailbox);
 			switch_event_add_header(*v_event, SWITCH_STACK_BOTTOM, "sip_auth_username", "%s", username);
 			switch_event_add_header(*v_event, SWITCH_STACK_BOTTOM, "sip_auth_realm", "%s", realm);
+			switch_event_add_header(*v_event, SWITCH_STACK_BOTTOM, "mailbox", "%s", mailbox);
+			switch_event_add_header(*v_event, SWITCH_STACK_BOTTOM, "user_name", "%s", username);
+			switch_event_add_header(*v_event, SWITCH_STACK_BOTTOM, "domain_name", "%s", realm);
 			
 			if ((xparams = switch_xml_child(user, "variables"))) {
 				for (param = switch_xml_child(xparams, "variable"); param; param = param->next) {