[svn-commits] tilghman: trunk r120230 - /trunk/funcs/func_channel.c

SVN commits to the Digium repositories svn-commits at lists.digium.com
Tue Jun 3 18:17:34 CDT 2008


Author: tilghman
Date: Tue Jun  3 18:17:33 2008
New Revision: 120230

URL: http://svn.digium.com/view/asterisk?view=rev&rev=120230
Log:
Add a function, CHANNELS(), which retrieves a list of all active channels.
(closes issue #11330)
 Reported by: rain
 Patches: 
       func_channel-channel_list_function.diff uploaded by rain (license 327)
       (with some additional changes by me, mostly to meet coding guidelines)

Modified:
    trunk/funcs/func_channel.c

Modified: trunk/funcs/func_channel.c
URL: http://svn.digium.com/view/asterisk/trunk/funcs/func_channel.c?view=diff&rev=120230&r1=120229&r2=120230
==============================================================================
--- trunk/funcs/func_channel.c (original)
+++ trunk/funcs/func_channel.c Tue Jun  3 18:17:33 2008
@@ -16,9 +16,10 @@
 
 /*! \file
  *
- * \brief Channel info dialplan function
+ * \brief Channel info dialplan functions
  *
  * \author Kevin P. Fleming <kpfleming at digium.com>
+ * \author Ben Winslow
  * 
  * \ingroup functions
  */
@@ -26,6 +27,8 @@
 #include "asterisk.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <regex.h>
 
 #include "asterisk/module.h"
 #include "asterisk/channel.h"
@@ -222,14 +225,76 @@
 	.write = func_channel_write,
 };
 
+static int func_channels_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
+{
+	struct ast_channel *c = NULL;
+	regex_t re;
+	int res;
+	size_t buflen = 0;
+	
+	buf[0] = '\0';
+
+	if (!ast_strlen_zero(data)) {
+		if ((res = regcomp(&re, data, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
+			regerror(res, &re, buf, maxlen);
+			ast_log(LOG_WARNING, "Error compiling regular expression for %s(%s): %s\n", function, data, buf);
+			return -1;
+		}
+	}
+
+	for (c = ast_channel_walk_locked(NULL); c; ast_channel_unlock(c), c = ast_channel_walk_locked(c)) {
+		if (ast_strlen_zero(data) || regexec(&re, c->name, 0, NULL, 0) == 0) {
+			size_t namelen = strlen(c->name);
+			if (buflen + namelen + (ast_strlen_zero(buf) ? 0 : 1) + 1 < maxlen) {
+				if (!ast_strlen_zero(buf)) {
+					strcat(buf, " ");
+					buflen++;
+				}
+				strcat(buf, c->name);
+				buflen += namelen;
+			} else {
+				ast_log(LOG_WARNING, "Number of channels exceeds the available buffer space.  Output will be truncated!\n");
+			}
+		}
+	}
+
+	if (!ast_strlen_zero(data)) {
+		regfree(&re);
+	}
+
+	return 0;
+}
+
+static struct ast_custom_function channels_function = {
+	.name = "CHANNELS",
+	.synopsis = "Gets the list of channels, optionally filtering by a regular expression.",
+	.syntax = "CHANNEL([regular expression])",
+	.desc =
+"Gets the list of channels, optionally filtering by a regular expression.  If\n"
+"no argument is provided, all known channels are returned.  The regular\n"
+"expression must correspond to the POSIX.2 specification, as shown in\n"
+"regex(7).  The list returned will be space-delimited.\n",
+	.read = func_channels_read,
+};
+
 static int unload_module(void)
 {
-	return ast_custom_function_unregister(&channel_function);
+	int res = 0;
+	
+	res |= ast_custom_function_unregister(&channel_function);
+	res |= ast_custom_function_unregister(&channels_function);
+	
+	return res;
 }
 
 static int load_module(void)
 {
-	return ast_custom_function_register(&channel_function);
-}
-
-AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan function");
+	int res = 0;
+	
+	res |= ast_custom_function_register(&channel_function);
+	res |= ast_custom_function_register(&channels_function);
+	
+	return res;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan functions");




More information about the svn-commits mailing list