[asterisk-commits] junky: branch junky/cli-tls r210768 - in /team/junky/cli-tls: include/asteris...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Aug 5 20:37:16 CDT 2009


Author: junky
Date: Wed Aug  5 20:37:12 2009
New Revision: 210768

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=210768
Log:
Add AMI commands: FilterAdd, FilterRemove, FilterReset, FilterShow
also, instead of duplicate code for CLI commands and these commands, i wrote internal functions that both functions can use.


Modified:
    team/junky/cli-tls/include/asterisk/logger.h
    team/junky/cli-tls/main/logger.c
    team/junky/cli-tls/main/manager.c

Modified: team/junky/cli-tls/include/asterisk/logger.h
URL: http://svn.asterisk.org/svn-view/asterisk/team/junky/cli-tls/include/asterisk/logger.h?view=diff&rev=210768&r1=210767&r2=210768
==============================================================================
--- team/junky/cli-tls/include/asterisk/logger.h (original)
+++ team/junky/cli-tls/include/asterisk/logger.h Wed Aug  5 20:37:12 2009
@@ -219,7 +219,6 @@
 
 int ast_is_string_match_cli_filters(void);
 struct ast_cli_filter *ast_filter_get_by_name(const char *name);
-
 
 /*!
  * \brief Send a log message to a dynamically registered log level

Modified: team/junky/cli-tls/main/logger.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/junky/cli-tls/main/logger.c?view=diff&rev=210768&r1=210767&r2=210768
==============================================================================
--- team/junky/cli-tls/main/logger.c (original)
+++ team/junky/cli-tls/main/logger.c Wed Aug  5 20:37:12 2009
@@ -214,6 +214,9 @@
 static void filter_channel_cleanup(void *data);
 void junky_printf(const char *msg);
 static void __attribute__((format(printf, 1,2))) my_printf(const char *str, ...);
+static int ast_filter_reset(void);
+static int ast_filter_remove(const char *name); 
+static int ast_filter_add(const char *type, const char *name);
 //#define PRINTF_DEBUG 1 /* TODO just for debugging now */
 
 
@@ -810,9 +813,7 @@
 
 static char *handle_add_filter(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-	struct ast_cli_filter *filter = NULL;
-	struct ast_cli_filter *tmp;
-	char *keyword = NULL;
+	int count = 0;
 
 	switch (cmd) {
 	case CLI_INIT:
@@ -830,43 +831,12 @@
 		return CLI_SHOWUSAGE;
 	}
 
-	/*
-	TODO should deal with type here
-	*/
-	tmp = ast_filter_get_by_name(a->argv[4]);
-	if (tmp) {
-		ast_cli(a->fd, "%s already exists\n", a->argv[4]);		
-		return CLI_SUCCESS;
-	}
-	
-
-	//TODO use ast_filter_alloc(name,type) instead
-	filter = ast_malloc(sizeof(*filter));
-	if ( !filter ) {
-		ast_log(LOG_WARNING, "cant allocate enough memory\n");
+	count = ast_filter_add(a->argv[3], a->argv[4]);
+	if (count <= 0 ) {
 		return CLI_FAILURE;
-	}
-
-	keyword = ast_strdup(a->argv[3]);
-	filter->name = ast_strdup(a->argv[4]);
-
-	/* Keywords are matched with filter type here */
-	if (!strcmp(keyword,"name")) {
-		filter->type = 1;
-	} else if (!strcmp(keyword,"cid_num")) {
-		filter->type = 2;
-	} else if (!strcmp(keyword, "accountcode")) {
-		filter->type = 3;
 	} else {
-		filter->type = 1;
-	}
-
-	ast_free(keyword);
-
-	/* TODO search it that filter already exists */
-	AST_LIST_INSERT_HEAD(&ast_cli_filters, filter, filter_list);
-
-	ast_cli(a->fd, "%s sucessfully added to CLI filters.\n", filter->name);
+		ast_cli(a->fd, "%s sucessfully added to CLI filters.\n", a->argv[4]);
+	}
 
 	return CLI_SUCCESS;
 }
@@ -878,6 +848,7 @@
 	char *ret = NULL;
         int which = 0;
         int wordlen;
+	int count = 0;
 
 	switch (cmd) {
 	case CLI_INIT:
@@ -908,19 +879,10 @@
 	if (!tmp) {
 		ast_cli(a->fd, "%s is not an active CLI filter\n", a->argv[3]);
 		return CLI_SUCCESS;
-	} else {
-		ast_cli(a->fd, "ive to remove %s from the list\n", a->argv[3]);
-
-		AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_cli_filters, filter, filter_list) {
-			if (!strcmp(filter->name, a->argv[3])) {
-				AST_LIST_REMOVE_CURRENT(filter_list);
-					ast_cli(a->fd, "%s has been sucessfully removed from CLI filter list.\n", a->argv[3]);
-					ast_free(filter->name); /* since I ast_strdup it */
-					ast_free(filter);
-					break;
-			}
-			AST_LIST_TRAVERSE_SAFE_END;
-		}
+	} 
+
+	if ( (count = ast_filter_remove(a->argv[3])) > 0) {
+		ast_cli(a->fd, "%s has been sucessfully removed from CLI filter list.\n", a->argv[3]);
 	}
 
 	return CLI_SUCCESS;
@@ -928,7 +890,6 @@
 
 static char *handle_reset_filters(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-        struct ast_cli_filter *filter;
 	int count = 0;
 
         switch (cmd) {
@@ -946,14 +907,8 @@
                 return CLI_SHOWUSAGE;
         }
 
-                AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_cli_filters, filter, filter_list) {
-                	AST_LIST_REMOVE_CURRENT(filter_list);
-                        ast_free(filter->name); /* since I ast_strdup it */
-                        ast_free(filter);
-			count++;
-                AST_LIST_TRAVERSE_SAFE_END;
-                }
-                ast_cli(a->fd, "%d CLI filters has been sucessfully removed.\n", count);
+	count = ast_filter_reset();
+	ast_cli(a->fd, "%d CLI filters has been sucessfully removed.\n", count);
 		
         return CLI_SUCCESS;
 }
@@ -975,7 +930,6 @@
         AST_CLI_DEFINE(handle_add_filter, "Add CLI filter"),
         AST_CLI_DEFINE(handle_remove_filter, "Remove CLI filter"),
         AST_CLI_DEFINE(handle_reset_filters, "Remove all CLI filters"),
-
 };
 
 static int handle_SIGXFSZ(int sig) 
@@ -1144,6 +1098,175 @@
 	return NULL;
 }
 
+static int action_filter_add(struct mansession *s, const struct message *m)
+{
+        const char *id = astman_get_header(m, "ActionID");
+        char idText[256];
+	const char *name = astman_get_header(m, "Name");
+	const char *type = astman_get_header(m, "Type");
+	int count  = 0;
+        if (!ast_strlen_zero(id)) {
+                snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
+        } else {
+                idText[0] = '\0';
+        }
+
+	if (ast_strlen_zero(name)) {
+		astman_send_error(s, m, "FilterAdd must provides a name.");
+		return -1;
+	}
+
+	if (ast_strlen_zero(type)) {
+		astman_send_error(s, m, "FilterAdd must provides a type.");
+		return -1;
+	}
+
+	count = ast_filter_add(type,name);
+
+	
+        astman_append(s, "Response: Success\r\n%s", idText);
+        return 0;
+}
+
+static int action_filter_remove(struct mansession *s, const struct message *m)
+{
+        const char *id = astman_get_header(m, "ActionID");
+	const char *name = astman_get_header(m, "Name");
+        char idText[256];
+	int count = 0;
+
+        if (!ast_strlen_zero(id)) {
+                snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
+        } else {
+                idText[0] = '\0';
+        }
+
+	count = ast_filter_remove(name);
+	if ( count > 0 ) {
+	        astman_append(s, "Response: Success\r\n%s", idText);
+	} else {
+		astman_send_error(s, m, "That filter doesnt exists.");	
+	}
+        return 0;
+}
+
+static int action_filter_show(struct mansession *s, const struct message *m)
+{
+        const char *id = astman_get_header(m, "ActionID");
+        char idText[256];
+        struct ast_cli_filter *filter;
+
+        if (!ast_strlen_zero(id)) {
+                snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
+        } else {
+                idText[0] = '\0';
+        }
+
+        AST_LIST_TRAVERSE(&ast_cli_filters, filter, filter_list) {
+                astman_append(s,"%s\n", filter->name);
+        }
+
+        astman_append(s, "Response: Success\r\n%s", idText);
+        return 0;
+}
+
+static int action_filter_reset(struct mansession *s, const struct message *m)
+{
+        const char *id = astman_get_header(m, "ActionID");
+        char idText[256];
+	int count = ast_filter_reset();
+
+        if (!ast_strlen_zero(id)) {
+                snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
+        } else {
+                idText[0] = '\0';
+        }
+
+	if (count > 0) {	
+	        astman_append(s, "Response: Success\r\n%s", idText);
+	} else {
+		astman_send_error(s, m, "No filters are currently enabled.");  
+
+	}
+        return 0;
+}
+
+static int ast_filter_add(const char *type, const char *name) {
+        struct ast_cli_filter *filter = NULL;
+        struct ast_cli_filter *tmp;
+        char *keyword = NULL;
+
+        /*
+        TODO should deal with type here
+        */
+        tmp = ast_filter_get_by_name(name);
+        if (tmp) {
+                return 0;
+        }
+
+        //TODO use ast_filter_alloc(name,type) instead
+        filter = ast_malloc(sizeof(*filter));
+        if ( !filter ) {
+                return -1;
+        }
+
+        keyword = ast_strdup(type);
+        filter->name = ast_strdup(name);
+
+        /* Keywords are matched with filter type here */
+        if (!strcmp(keyword,"name")) {
+                filter->type = 1;
+        } else if (!strcmp(keyword,"cid_num")) {
+                filter->type = 2;
+        } else if (!strcmp(keyword, "accountcode")) {
+                filter->type = 3;
+        } else {
+                filter->type = 1;
+        }
+
+        ast_free(keyword);
+
+        /* TODO search it that filter already exists */
+        AST_LIST_INSERT_HEAD(&ast_cli_filters, filter, filter_list);
+
+	return 1;
+}
+
+
+
+static int ast_filter_reset() {
+	int count = 0;
+	struct ast_cli_filter *filter;	
+
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_cli_filters, filter, filter_list) {
+		AST_LIST_REMOVE_CURRENT(filter_list);
+		ast_free(filter->name); /* since I ast_strdup it */
+		ast_free(filter);
+		count++;
+		AST_LIST_TRAVERSE_SAFE_END;
+	}
+
+	return count;
+}
+
+static int ast_filter_remove(const char *name) {
+	int count = 0 ;
+	struct ast_cli_filter *filter;
+
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_cli_filters, filter, filter_list) {
+		if (!strcmp(filter->name, name)) {
+			AST_LIST_REMOVE_CURRENT(filter_list);
+				count++;
+				ast_free(filter->name); /* since I ast_strdup it */
+				ast_free(filter);
+				break;
+			}
+			AST_LIST_TRAVERSE_SAFE_END;
+	}
+
+	return count;
+}
+
 int init_logger(void)
 {
 	char tmp[256];
@@ -1172,6 +1295,12 @@
 		qlog = fopen(tmp, "a");
 		ast_queue_log("NONE", "NONE", "NONE", "QUEUESTART", "%s", "");
 	}
+
+	res |= ast_manager_register_xml("FilterAdd", EVENT_FLAG_SYSTEM, action_filter_add);
+	res |= ast_manager_register_xml("FilterRemove", EVENT_FLAG_SYSTEM, action_filter_remove);
+	res |= ast_manager_register_xml("FilterShow", EVENT_FLAG_SYSTEM, action_filter_show);
+	res |= ast_manager_register_xml("FilterReset", EVENT_FLAG_SYSTEM, action_filter_reset);
+
 	return res;
 }
 

Modified: team/junky/cli-tls/main/manager.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/junky/cli-tls/main/manager.c?view=diff&rev=210768&r1=210767&r2=210768
==============================================================================
--- team/junky/cli-tls/main/manager.c (original)
+++ team/junky/cli-tls/main/manager.c Wed Aug  5 20:37:12 2009
@@ -684,6 +684,58 @@
 		<description>
 			<para>Checks if Asterisk module is loaded. Will return Success/Failure.
 			For success returns, the module revision number is included.</para>
+		</description>
+	</manager>
+	<manager name="FilterAdd" language="en_US">
+		<synopsis>
+			Add a CLI Filter to the current list of active filters.
+		</synopsis>
+		<syntax>
+			<parameter name="Module" required="true">
+				<para>Asterisk module name (not including extension).</para>
+			</parameter>
+		</syntax>
+		<description>
+			<para></para>
+		</description>
+	</manager>
+	<manager name="FilterRemove" language="en_US">
+		<synopsis>
+			Check if module is loaded.
+		</synopsis>
+		<syntax>
+			<parameter name="Module" required="true">
+				<para>Asterisk module name (not including extension).</para>
+			</parameter>
+		</syntax>
+		<description>
+			<para></para>
+		</description>
+	</manager>
+	<manager name="FilterShow" language="en_US">
+		<synopsis>
+			List all active filters
+		</synopsis>
+		<syntax>
+			<parameter name="Module" required="true">
+				<para>Asterisk module name (not including extension).</para>
+			</parameter>
+		</syntax>
+		<description>
+			<para></para>
+		</description>
+	</manager>
+	<manager name="FilterReset" language="en_US">
+		<synopsis>
+			Delete all active filters and return to normal mode.
+		</synopsis>
+		<syntax>
+			<parameter name="Module" required="true">
+				<para>Asterisk module name (not including extension).</para>
+			</parameter>
+		</syntax>
+		<description>
+			<para></para>
 		</description>
 	</manager>
  ***/




More information about the asterisk-commits mailing list