aboutsummaryrefslogtreecommitdiffstats
path: root/funcs/func_channel.c
diff options
context:
space:
mode:
authortilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2008-06-03 23:17:33 +0000
committertilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2008-06-03 23:17:33 +0000
commite8d263f34437e0d0fbf56b3d056f43c4b4d04338 (patch)
treea8c2515fd1658d221eb08b60bc5cc3cb95ced780 /funcs/func_channel.c
parent7abff700d413ba3153c76b50bc31b256eb0c50db (diff)
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) git-svn-id: http://svn.digium.com/svn/asterisk/trunk@120230 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'funcs/func_channel.c')
-rw-r--r--funcs/func_channel.c73
1 files changed, 69 insertions, 4 deletions
diff --git a/funcs/func_channel.c b/funcs/func_channel.c
index 8388d7fa6..6a036139f 100644
--- a/funcs/func_channel.c
+++ b/funcs/func_channel.c
@@ -16,9 +16,10 @@
/*! \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");