Mon Mar 20 08:25:42 2006

Asterisk developer's documentation


Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

func_groupcount.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*! \file
00018  *
00019  * \brief Channel group related dialplan functions
00020  * 
00021  */
00022 
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <string.h>
00026 #include <sys/types.h>
00027 
00028 #include "asterisk.h"
00029 
00030 /* ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $") */
00031 
00032 #include "asterisk/channel.h"
00033 #include "asterisk/pbx.h"
00034 #include "asterisk/logger.h"
00035 #include "asterisk/utils.h"
00036 #include "asterisk/app.h"
00037 
00038 static char *group_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
00039 {
00040    int count;
00041    char group[80] = "";
00042    char category[80] = "";
00043    char *grp;
00044 
00045    ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
00046 
00047    if (ast_strlen_zero(group)) {
00048       if ((grp = pbx_builtin_getvar_helper(chan, category)))
00049          ast_copy_string(group, grp, sizeof(group));
00050       else
00051          ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n", chan->name);   
00052    }
00053 
00054    count = ast_app_group_get_count(group, category);
00055    snprintf(buf, len, "%d", count);
00056 
00057    return buf;
00058 }
00059 
00060 #ifndef BUILTIN_FUNC
00061 static
00062 #endif
00063 struct ast_custom_function group_count_function = {
00064    .name = "GROUP_COUNT",
00065    .syntax = "GROUP_COUNT([groupname][@category])",
00066    .synopsis = "Counts the number of channels in the specified group",
00067    .desc = "Calculates the group count for the specified group, or uses the\n"
00068    "channel's current group if not specifed (and non-empty).\n",
00069    .read = group_count_function_read,
00070 };
00071 
00072 static char *group_match_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
00073 {
00074    int count;
00075    char group[80] = "";
00076    char category[80] = "";
00077 
00078    ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
00079 
00080    if (!ast_strlen_zero(group)) {
00081       count = ast_app_group_match_get_count(group, category);
00082       snprintf(buf, len, "%d", count);
00083    }
00084 
00085    return buf;
00086 }
00087 
00088 #ifndef BUILTIN_FUNC
00089 static
00090 #endif
00091 struct ast_custom_function group_match_count_function = {
00092    .name = "GROUP_MATCH_COUNT",
00093    .syntax = "GROUP_MATCH_COUNT(groupmatch[@category])",
00094    .synopsis = "Counts the number of channels in the groups matching the specified pattern",
00095    .desc = "Calculates the group count for all groups that match the specified pattern.\n"
00096    "Uses standard regular expression matching (see regex(7)).\n",
00097    .read = group_match_count_function_read,
00098    .write = NULL,
00099 };
00100 
00101 static char *group_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00102 {
00103    char varname[256];
00104    char *group;
00105 
00106    if (!ast_strlen_zero(data)) {
00107       snprintf(varname, sizeof(varname), "%s_%s", GROUP_CATEGORY_PREFIX, data);
00108    } else {
00109       ast_copy_string(varname, GROUP_CATEGORY_PREFIX, sizeof(varname));
00110    }
00111 
00112    group = pbx_builtin_getvar_helper(chan, varname);
00113    if (group)
00114       ast_copy_string(buf, group, len);
00115 
00116    return buf;
00117 }
00118 
00119 static void group_function_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
00120 {
00121    char grpcat[256];
00122 
00123    if (!ast_strlen_zero(data)) {
00124       snprintf(grpcat, sizeof(grpcat), "%s@%s", value, data);
00125    } else {
00126       ast_copy_string(grpcat, value, sizeof(grpcat));
00127    }
00128 
00129         if (ast_app_group_set_channel(chan, grpcat))
00130                 ast_log(LOG_WARNING, "Setting a group requires an argument (group name)\n");
00131 }
00132 
00133 #ifndef BUILTIN_FUNC
00134 static
00135 #endif
00136 struct ast_custom_function group_function = {
00137    .name = "GROUP",
00138    .syntax = "GROUP([category])",
00139    .synopsis = "Gets or sets the channel group.",
00140    .desc = "Gets or sets the channel group.\n",
00141    .read = group_function_read,
00142    .write = group_function_write,
00143 };
00144 
00145 static char *group_list_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
00146 {
00147    struct ast_var_t *current;
00148    struct varshead *headp;
00149    char tmp1[1024] = "";
00150    char tmp2[1024] = "";
00151 
00152    headp=&chan->varshead;
00153    AST_LIST_TRAVERSE(headp,current,entries) {
00154       if (!strncmp(ast_var_name(current), GROUP_CATEGORY_PREFIX "_", strlen(GROUP_CATEGORY_PREFIX) + 1)) {
00155          if (!ast_strlen_zero(tmp1)) {
00156             ast_copy_string(tmp2, tmp1, sizeof(tmp2));
00157             snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, ast_var_value(current), (ast_var_name(current) + strlen(GROUP_CATEGORY_PREFIX) + 1));
00158          } else {
00159             snprintf(tmp1, sizeof(tmp1), "%s@%s", ast_var_value(current), (ast_var_name(current) + strlen(GROUP_CATEGORY_PREFIX) + 1));
00160          }
00161       } else if (!strcmp(ast_var_name(current), GROUP_CATEGORY_PREFIX)) {
00162          if (!ast_strlen_zero(tmp1)) {
00163             ast_copy_string(tmp2, tmp1, sizeof(tmp2));
00164             snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, ast_var_value(current));
00165          } else {
00166             snprintf(tmp1, sizeof(tmp1), "%s", ast_var_value(current));
00167          }
00168       }
00169    }
00170    ast_copy_string(buf, tmp1, len);
00171    return buf;
00172 }
00173 
00174 #ifndef BUILTIN_FUNC
00175 static
00176 #endif
00177 struct ast_custom_function group_list_function = {
00178    .name = "GROUP_LIST",
00179    .syntax = "GROUP_LIST()",
00180    .synopsis = "Gets a list of the groups set on a channel.",
00181    .desc = "Gets a list of the groups set on a channel.\n",
00182    .read = group_list_function_read,
00183    .write = NULL,
00184 };
00185 
00186 /*
00187 Local Variables:
00188 mode: C
00189 c-file-style: "linux"
00190 indent-tabs-mode: nil
00191 End:
00192 */

Generated on Mon Mar 20 08:25:43 2006 for Asterisk - the Open Source PBX by  doxygen 1.3.9.1