Add a function, CHANNELS(), which retrieves a list of all active channels.
authorTilghman Lesher <tilghman@meg.abyt.es>
Tue, 3 Jun 2008 23:17:33 +0000 (23:17 +0000)
committerTilghman Lesher <tilghman@meg.abyt.es>
Tue, 3 Jun 2008 23:17:33 +0000 (23:17 +0000)
(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)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@120230 65c4cc65-6c06-0410-ace0-fbb531ad65f3

funcs/func_channel.c

index 8388d7f..6a03613 100644 (file)
 
 /*! \file
  *
- * \brief Channel info dialplan function
+ * \brief Channel info dialplan functions
  *
  * \author Kevin P. Fleming <kpfleming@digium.com>
+ * \author Ben Winslow
  * 
  * \ingroup functions
  */
@@ -27,6 +28,8 @@
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
+#include <regex.h>
+
 #include "asterisk/module.h"
 #include "asterisk/channel.h"
 #include "asterisk/pbx.h"
@@ -222,14 +225,76 @@ static struct ast_custom_function channel_function = {
        .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);
+       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 function");
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan functions");