[Freeswitch-svn] [commit] r3775 - freeswitch/branches/knhor/trunk/src/mod/applications/mod_conference

Freeswitch SVN knhor at freeswitch.org
Thu Dec 21 02:29:02 EST 2006


Author: knhor
Date: Thu Dec 21 02:29:01 2006
New Revision: 3775

Modified:
   freeswitch/branches/knhor/trunk/src/mod/applications/mod_conference/mod_conference.c

Log:
fix list.
restore commands cmd.
add synonym help.
move conf_api_main lookup sub-code into conf_api_dispatch.
use conf_api_dispatch for the chat api. (i got busy coding....)
add 'pretty' mode to list cmd.


Modified: freeswitch/branches/knhor/trunk/src/mod/applications/mod_conference/mod_conference.c
==============================================================================
--- freeswitch/branches/knhor/trunk/src/mod/applications/mod_conference/mod_conference.c	(original)
+++ freeswitch/branches/knhor/trunk/src/mod/applications/mod_conference/mod_conference.c	Thu Dec 21 02:29:01 2006
@@ -2088,6 +2088,36 @@
 	}
 }
 
+static void conference_list_pretty(conference_obj_t *conference, switch_stream_handle_t *stream)
+{
+	if (conference != NULL && stream != NULL) {
+		conference_member_t *member = NULL;
+
+		switch_mutex_lock(conference->member_mutex);
+//		stream->write_function(stream, "<pre>Current Callers:\n");
+
+		for (member = conference->members; member; member = member->next) {
+			switch_channel_t *channel;
+			switch_caller_profile_t *profile;
+
+			if (switch_test_flag(member, MFLAG_NOCHANNEL)) {
+				continue;
+			}
+			channel = switch_core_session_get_channel(member->session);
+			profile = switch_channel_get_caller_profile(channel);
+
+
+			stream->write_function(stream, "%u) %s (%s)\n", 
+								member->id,
+								profile->caller_id_name,
+								profile->caller_id_number
+								);
+
+		}
+		switch_mutex_unlock(conference->member_mutex);
+	}
+}
+
 static void conference_list(conference_obj_t *conference, switch_stream_handle_t *stream, char *delim)
 {
 	if (conference != NULL && stream != NULL && delim != NULL) {
@@ -2103,6 +2133,7 @@
 			uint32_t count = 0;
 
 			if (switch_test_flag(member, MFLAG_NOCHANNEL)) {
+				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "continue\n");
 				continue;
 			}
 
@@ -2412,29 +2443,32 @@
 {
 	int syntax_err = 1;
 
-//	if (conference != NULL && stream != NULL) {
-	{
-		switch_hash_index_t *hi;
-		void *val;
-		char *d = ";";
+	switch_hash_index_t *hi;
+	void *val;
+	char *d = ";";
+	int pretty = 0;
+	int argofs = (strcasecmp(argv[1],"list") == 0);	// detect being called from chat vs. api
 
-		if (argv[1]) {
-			if (argv[2] && !strcasecmp(argv[1], "delim")) {
-				d = argv[2];
+	if (argv[1+argofs]) {
+		if (argv[2+argofs] && !strcasecmp(argv[1+argofs], "delim")) {
+			d = argv[2+argofs];
 
-				if (*d == '"') {
-					if (++d) {
-						char *p;
-						if ((p = strchr(d, '"'))) {
-							*p = '\0';
-						}
-					} else {
-						d = ";";
+			if (*d == '"') {
+				if (++d) {
+					char *p;
+					if ((p = strchr(d, '"'))) {
+						*p = '\0';
 					}
+				} else {
+					d = ";";
 				}
 			}
+		} else if (strcasecmp(argv[1+argofs], "pretty") == 0) {
+			pretty = 1;
 		}
+	}
 
+	if (conference == NULL) {
 		for (hi = switch_hash_first(globals.conference_pool, globals.conference_hash); hi; hi = switch_hash_next(hi)) {
 			switch_hash_this(hi, NULL, NULL, &val);
 			conference = (conference_obj_t *) val;
@@ -2443,12 +2477,22 @@
 											conference->name,
 											conference->count,
 											conference->count == 1 ? "" : "");
+			if (pretty) {
+				conference_list_pretty(conference, stream);
+			} else {
+				conference_list(conference, stream, d);
+			}
+		}
+	} else {
+		if (pretty) {
+			conference_list_pretty(conference, stream);
+		} else {
 			conference_list(conference, stream, d);
 		}
-
-		syntax_err = 0;
 	}
 
+	syntax_err = 0;
+
 	return syntax_err;
 }
 
@@ -2902,6 +2946,108 @@
 
 #define CONFFUNCAPISIZE (sizeof(conf_api_sub_commands)/sizeof(conf_api_sub_commands[0]))
 
+switch_status_t conf_api_dispatch(conference_obj_t *conference, switch_stream_handle_t *stream, int argc, char **argv, char *cmdline, int argn)
+{	switch_status_t status = SWITCH_STATUS_FALSE;
+
+	if (conference != NULL) {
+		int i,found=0;
+
+		/* loop through the command table to find a match */
+		for (i=0; i<CONFFUNCAPISIZE && !found; i++) {
+			if (strcasecmp(argv[argn],conf_api_sub_commands[i].pname) == 0) {
+				found = 1;
+				switch(conf_api_sub_commands[i].fntype) {
+
+					/* commands that we've broken the command line into arguments for */
+					case CONF_API_SUB_ARGS_SPLIT:
+						{	conf_api_args_cmd_t pfn = (conf_api_args_cmd_t)conf_api_sub_commands[i].pfnapicmd;
+
+							if (pfn(conference, stream, argc, argv) != 0) {
+								/* command returned error, so show syntax usage */
+								stream->write_function(stream,conf_api_sub_commands[i].psyntax);
+								}
+						}
+						break;
+
+					/* member specific command that can be itteratted */
+					case CONF_API_SUB_MEMBER_TARGET:
+						{
+							uint32_t id = atoi(argv[argn+1]);
+							int all = ( id == 0 && strcasecmp(argv[argn+1], "all") == 0 );
+							int last = ( id == 0 && strcasecmp(argv[argn+1], "last") == 0 );
+
+							if(all) {
+								 conference_member_itterator(conference, stream, conf_api_sub_commands[i].pfnapicmd, argv[argn+2]);
+							} else if (last) {
+								conference_member_t *member = NULL;
+								conference_member_t *last_member = NULL;
+
+								switch_mutex_lock(conference->member_mutex);
+
+								/* find last (oldest) member */
+								member = conference->members;
+								while (member != NULL) {
+									if (last_member == NULL || member->id > last_member->id) {
+										last_member = member;
+									}
+									member = member->next;
+								}
+
+								/* exec functio on last (oldest) member */
+								if (last_member != NULL) {
+									conf_api_member_cmd_t pfn = (conf_api_member_cmd_t)conf_api_sub_commands[i].pfnapicmd;
+									pfn(last_member, stream, argv[argn+2]);
+								}
+
+								switch_mutex_unlock(conference->member_mutex);
+							} else {
+								conf_api_member_cmd_t pfn = (conf_api_member_cmd_t)conf_api_sub_commands[i].pfnapicmd;
+								conference_member_t *member = conference_member_get(conference, id);
+
+								if (member != NULL) {
+									pfn(conference_member_get(conference, id), stream, argv[argn+2]);
+								} else {
+									if (id == 0) {
+										stream->write_function(stream,conf_api_sub_commands[i].psyntax);
+									} else {
+										stream->write_function(stream, "Non-Existant ID %u\n", id);
+									}
+								}
+							}
+						}
+						break;
+
+					/* commands that deals with all text after command */
+					case CONF_API_SUB_ARGS_AS_ONE:
+						{	conf_api_text_cmd_t pfn = (conf_api_text_cmd_t)conf_api_sub_commands[i].pfnapicmd;
+							char *pstr = cmdline+strlen(conf_api_sub_commands[i].pname)+1;
+
+							/* advance past all leading white space after command */
+							while(*pstr == ' ' || *pstr == '\t') {
+								pstr++;
+							}
+
+							/* call the command handler */
+							if (pfn(conference, stream, pstr) != 0) {
+								/* command returned error, so show syntax usage */
+								stream->write_function(stream,conf_api_sub_commands[i].psyntax);
+								}
+						}
+						break;
+				}
+			}
+		}
+
+		if(!found) {
+			stream->write_function(stream, "Confernece command '%s' not found.\n", argv[argn]);
+		} else {
+			status = SWITCH_STATUS_SUCCESS;
+		}
+	}
+
+	return status;
+}
+
 /* API Interface Function */
 static switch_status_t conf_api_main(char *buf, switch_core_session_t *session, switch_stream_handle_t *stream)
 {
@@ -2931,116 +3077,28 @@
 
 		/* try to find a command to execute */
 		if (argc) {
-			int i,found=0;
-			uint32_t cnum = atoi(argv[0]);
-			conference_obj_t *conference = (cnum != 0 ? (conference_obj_t *) switch_core_hash_find(globals.conference_hash, argv[0]) : NULL);
+			conference_obj_t *conference = (conference_obj_t *) switch_core_hash_find(globals.conference_hash, argv[0]);
 
-			if(cnum == 0 || (cnum != 0 && conference != NULL && argc >= 3)) {
-				int argn = (cnum != 0 ? 1 : 0);
-
-				/* loop through the command table to find a match */
-				for (i=0; i<CONFFUNCAPISIZE && !found; i++) {
-					if (strcasecmp(argv[argn],conf_api_sub_commands[i].pname) == 0) {
-						found = 1;
-						switch(conf_api_sub_commands[i].fntype) {
-
-							/* commands that we've broken the command line into arguments for */
-							case CONF_API_SUB_ARGS_SPLIT:
-								{	conf_api_args_cmd_t pfn = (conf_api_args_cmd_t)conf_api_sub_commands[i].pfnapicmd;
-
-									if (pfn(conference, stream, argc, &argv[0]) != 0) {
-										/* command returned error, so show syntax usage */
-										stream->write_function(stream,conf_api_sub_commands[i].psyntax);
-										}
-								}
-								break;
-
-							/* member specific command that can be itteratted */
-							case CONF_API_SUB_MEMBER_TARGET:
-								if (conference != NULL) {
-									uint32_t id = atoi(argv[argn+1]);
-									int all = ( id == 0 && strcasecmp(argv[argn+1], "all") == 0 );
-									int last = ( id == 0 && strcasecmp(argv[argn+1], "last") == 0 );
-
-									if(all) {
-										 conference_member_itterator(conference, stream, conf_api_sub_commands[i].pfnapicmd, argv[argn+2]);
-									} else if (last) {
-										conference_member_t *member = NULL;
-										conference_member_t *last_member = NULL;
-
-										switch_mutex_lock(conference->member_mutex);
-
-										/* find last (oldest) member */
-										member = conference->members;
-										while (member != NULL) {
-											if (last_member == NULL || member->id > last_member->id) {
-												last_member = member;
-											}
-											member = member->next;
-										}
-
-										/* exec functio on last (oldest) member */
-										if (last_member != NULL) {
-											conf_api_member_cmd_t pfn = (conf_api_member_cmd_t)conf_api_sub_commands[i].pfnapicmd;
-											pfn(last_member, stream, argv[argn+2]);
-										}
-
-										switch_mutex_unlock(conference->member_mutex);
-									} else {
-										conf_api_member_cmd_t pfn = (conf_api_member_cmd_t)conf_api_sub_commands[i].pfnapicmd;
-										conference_member_t *member = conference_member_get(conference, id);
-
-										if (member != NULL) {
-											pfn(conference_member_get(conference, id), stream, argv[argn+2]);
-										} else {
-											if (id == 0) {
-												stream->write_function(stream,conf_api_sub_commands[i].psyntax);
-											} else {
-												stream->write_function(stream, "Non-Existant ID %u\n", id);
-											}
-										}
-									}
-								} else {
-									stream->write_function(stream, "Conference %s not found\n", argv[0]);
-									switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Conference %s not found\n", argv[0]);
-								}
-								break;
-
-							/* commands that deals with all text after command */
-							case CONF_API_SUB_ARGS_AS_ONE:
-								{	conf_api_text_cmd_t pfn = (conf_api_text_cmd_t)conf_api_sub_commands[i].pfnapicmd;
-									char *pstr = lbuf+strlen(conf_api_sub_commands[i].pname)+1;
-
-									/* advance past all leading white space after command */
-									while(*pstr == ' ' || *pstr == '\t') {
-										pstr++;
-									}
-
-									/* call the command handler */
-									if (pfn(conference, stream, pstr) != 0) {
-										/* command returned error, so show syntax usage */
-										stream->write_function(stream,conf_api_sub_commands[i].psyntax);
-										}
-								}
-								break;
-						}
-					}
+			if(conference != NULL) {
+				if (argc >= 2) {
+					status = conf_api_dispatch(conference, stream, argc, argv, lbuf, 1);
+				} else {
+					stream->write_function(stream,"Conference command, not specified.\nTry 'help'\n");
 				}
-
-				/* no command found */
-				if(!found) {
-					stream->write_function(stream, "Confernece command '%s' not found.\nTry 'help conference'\n", argv[argn]);
-				}
 			} else {
-				if (argc < 3) {
-					stream->write_function(stream,"Conference argument required\n");
+				/* special case the list command, because it doesn't require a conference argument */
+				if (strcasecmp(argv[0],"list") == 0) {
+					conf_api_sub_list(NULL, stream, argc, argv);
+				} else if (strcasecmp(argv[0],"help") == 0 || strcasecmp(argv[0],"commands") == 0) {
+					stream->write_function(stream, "%s\n", conf_api_interface.syntax);
 				} else {
-					stream->write_function(stream,"No Conference called %s found.\n",argv[0]);
+					stream->write_function(stream, "Conference %s not found\n", argv[0]);
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Conference %s not found\n", argv[0]);
 				}
 			}
 
 		} else {
-			stream->write_function(stream, "Command not specified.\nTry 'help conference'\n");
+			stream->write_function(stream, "No parameters specified.\nTry 'help conference'\n");
 		}
 	}
 
@@ -3640,36 +3698,9 @@
 	/*.next */ 
 };
 
-static void chat_list_pretty(conference_obj_t *conference, switch_stream_handle_t *stream)
-{
-	conference_member_t *member = NULL;
-
-	switch_mutex_lock(conference->member_mutex);
-	stream->write_function(stream, "<pre>Current Callers:\n");
-
-	for (member = conference->members; member; member = member->next) {
-		switch_channel_t *channel;
-		switch_caller_profile_t *profile;
-
-		if (switch_test_flag(member, MFLAG_NOCHANNEL)) {
-			continue;
-		}
-		channel = switch_core_session_get_channel(member->session);
-		profile = switch_channel_get_caller_profile(channel);
-
-
-		stream->write_function(stream, "*) %s (%s)\n", 
-							   profile->caller_id_name,
-							   profile->caller_id_number
-							   );
-
-	}
-	switch_mutex_unlock(conference->member_mutex);
-}
-
 static switch_status_t chat_send(char *proto, char *from, char *to, char *subject, char *body, char *hint)
 {
-	char name[512] = "", *p;
+	char name[512] = "", *p, *lbuf;
 	switch_chat_interface_t *ci;
 	conference_obj_t *conference = NULL;
 	switch_stream_handle_t stream = {0};
@@ -3694,22 +3725,39 @@
 	}
 
 	if (!(conference = (conference_obj_t *) switch_core_hash_find(globals.conference_hash, name))) {
-		ci->chat_send(CONF_CHAT_PROTO, to, hint && strchr(hint, '/') ? hint : from, "", "Sorry, We're Closed", NULL);
+		ci->chat_send(CONF_CHAT_PROTO, to, hint && strchr(hint, '/') ? hint : from, "", "Conference not active.", NULL);
 		return SWITCH_STATUS_FALSE;
 	}
 
 	SWITCH_STANDARD_STREAM(stream);
 
-	if (strstr(body, "list")) {
-		chat_list_pretty(conference, &stream);
-	} else {
-		stream.write_function(&stream, "The only command we have so far is 'list'.\nGet coding or go press PayPal!!\n");
+	if (body != NULL && (lbuf = strdup(body))) {
+		int argc;
+		char *argv[25];
+
+		memset(argv,0,sizeof(argv));
+		argc = switch_separate_string(lbuf, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
+
+		/* try to find a command to execute */
+		if (argc) {
+			/* special case list */
+			if (strcasecmp(argv[0],"list") == 0) {
+				conference_list_pretty(conference, &stream);
+			/* provide help */
+			} else if (strcasecmp(argv[0],"help") == 0 || strcasecmp(argv[0],"commands") == 0) {
+				stream.write_function(&stream, "%s\n", conf_api_interface.syntax);
+			/* find a normal command */
+			} else {
+				conf_api_dispatch(conference, &stream, argc, argv, lbuf, 0);
+			}
+		} else {
+			stream.write_function(&stream, "No parameters specified.\nTry 'help'\n");
+		}
 	}
+	switch_safe_free(lbuf);
 
 	ci->chat_send(CONF_CHAT_PROTO, to, from, "", stream.data, NULL);
 	switch_safe_free(stream.data);
-
-
 
 	return SWITCH_STATUS_SUCCESS;
 }



More information about the Freeswitch-svn mailing list