blocking 80167
[asterisk/asterisk.git] / funcs / func_groupcount.c
index d3bf627..8839201 100644 (file)
  *
  * \brief Channel group related dialplan functions
  * 
+ * \ingroup functions
  */
 
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
 
-#include "asterisk.h"
-
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include "asterisk/module.h"
 #include "asterisk/channel.h"
 #include "asterisk/pbx.h"
@@ -36,27 +37,19 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/utils.h"
 #include "asterisk/app.h"
 
-static int group_count_function_read(struct ast_channel *chan, char *cmd,
+static int group_count_function_read(struct ast_channel *chan, const char *cmd,
                                     char *data, char *buf, size_t len)
 {
-       int count;
-       char group[80] = "";
-       char category[80] = "";
-       const char *grp;
+       int count = -1;
+       char group[80] = "", category[80] = "";
 
        ast_app_group_split_group(data, group, sizeof(group), category,
                                  sizeof(category));
 
-       if (ast_strlen_zero(group)) {
-               if ((grp = pbx_builtin_getvar_helper(chan, category)))
-                       ast_copy_string(group, grp, sizeof(group));
-               else
-                       ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n",
-                               chan->name);
-       }
-
-       count = ast_app_group_get_count(group, category);
-       snprintf(buf, len, "%d", count);
+       if ((count = ast_app_group_get_count(group, category)) == -1)
+               ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n", chan->name);
+       else
+               snprintf(buf, len, "%d", count);
 
        return 0;
 }
@@ -72,7 +65,7 @@ static struct ast_custom_function group_count_function = {
 };
 
 static int group_match_count_function_read(struct ast_channel *chan,
-                                          char *cmd, char *data, char *buf,
+                                          const char *cmd, char *data, char *buf,
                                           size_t len)
 {
        int count;
@@ -102,27 +95,31 @@ static struct ast_custom_function group_match_count_function = {
        .write = NULL,
 };
 
-static int group_function_read(struct ast_channel *chan, char *cmd,
+static int group_function_read(struct ast_channel *chan, const char *cmd,
                               char *data, char *buf, size_t len)
 {
-       char varname[256];
-       const char *group;
-
-       if (!ast_strlen_zero(data)) {
-               snprintf(varname, sizeof(varname), "%s_%s", GROUP_CATEGORY_PREFIX,
-                        data);
-       } else {
-               ast_copy_string(varname, GROUP_CATEGORY_PREFIX, sizeof(varname));
+       struct ast_group_info *gi = NULL;
+       
+       ast_app_group_list_rdlock();
+       
+       for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, list)) {
+               if (gi->chan != chan)
+                       continue;
+               if (ast_strlen_zero(data))
+                       break;
+               if (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, data))
+                       break;
        }
-
-       group = pbx_builtin_getvar_helper(chan, varname);
-       if (group)
-               ast_copy_string(buf, group, len);
-
+       
+       if (gi)
+               ast_copy_string(buf, gi->group, len);
+       
+       ast_app_group_list_unlock();
+       
        return 0;
 }
 
-static int group_function_write(struct ast_channel *chan, char *cmd,
+static int group_function_write(struct ast_channel *chan, const char *cmd,
                                char *data, const char *value)
 {
        char grpcat[256];
@@ -149,38 +146,37 @@ static struct ast_custom_function group_function = {
        .write = group_function_write,
 };
 
-static int group_list_function_read(struct ast_channel *chan, char *cmd,
+static int group_list_function_read(struct ast_channel *chan, const char *cmd,
                                    char *data, char *buf, size_t len)
 {
-       struct ast_var_t *current;
-       struct varshead *headp;
+       struct ast_group_info *gi = NULL;
        char tmp1[1024] = "";
        char tmp2[1024] = "";
 
-       headp = &chan->varshead;
-       AST_LIST_TRAVERSE(headp, current, entries) {
-               if (!strncmp(ast_var_name(current), GROUP_CATEGORY_PREFIX "_", strlen(GROUP_CATEGORY_PREFIX) + 1)) {
-                       if (!ast_strlen_zero(tmp1)) {
-                               ast_copy_string(tmp2, tmp1, sizeof(tmp2));
-                               snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2,
-                                        ast_var_value(current),
-                                        (ast_var_name(current) +
-                                         strlen(GROUP_CATEGORY_PREFIX) + 1));
-                       } else {
-                               snprintf(tmp1, sizeof(tmp1), "%s@%s", ast_var_value(current),
-                                        (ast_var_name(current) +
-                                         strlen(GROUP_CATEGORY_PREFIX) + 1));
-                       }
-               } else if (!strcmp(ast_var_name(current), GROUP_CATEGORY_PREFIX)) {
-                       if (!ast_strlen_zero(tmp1)) {
-                               ast_copy_string(tmp2, tmp1, sizeof(tmp2));
-                               snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2,
-                                        ast_var_value(current));
-                       } else {
-                               snprintf(tmp1, sizeof(tmp1), "%s", ast_var_value(current));
-                       }
+       if (!chan)
+               return -1;
+
+       ast_app_group_list_rdlock();
+
+       for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, list)) {
+               if (gi->chan != chan)
+                       continue;
+               if (!ast_strlen_zero(tmp1)) {
+                       ast_copy_string(tmp2, tmp1, sizeof(tmp2));
+                       if (!ast_strlen_zero(gi->category))
+                               snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, gi->group, gi->category);
+                       else
+                               snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, gi->group);
+               } else {
+                       if (!ast_strlen_zero(gi->category))
+                               snprintf(tmp1, sizeof(tmp1), "%s@%s", gi->group, gi->category);
+                       else
+                               snprintf(tmp1, sizeof(tmp1), "%s", gi->group);
                }
        }
+       
+       ast_app_group_list_unlock();
+
        ast_copy_string(buf, tmp1, len);
 
        return 0;
@@ -195,9 +191,7 @@ static struct ast_custom_function group_list_function = {
        .write = NULL,
 };
 
-static char *tdesc = "Channel group dialplan functions";
-
-static int unload_module(void *mod)
+static int unload_module(void)
 {
        int res = 0;
 
@@ -209,7 +203,7 @@ static int unload_module(void *mod)
        return res;
 }
 
-static int load_module(void *mod)
+static int load_module(void)
 {
        int res = 0;
 
@@ -221,14 +215,4 @@ static int load_module(void *mod)
        return res;
 }
 
-static const char *description(void)
-{
-       return tdesc;
-}
-
-static const char *key(void)
-{
-       return ASTERISK_GPL_KEY;
-}
-
-STD_MOD(MOD_1 | NO_USECOUNT, NULL, NULL, NULL);
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel group dialplan functions");