Wed Aug 18 22:33:52 2010

Asterisk developer's documentation


func_groupcount.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, 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  * \ingroup functions
00022  */
00023 
00024 #include "asterisk.h"
00025 
00026 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 232271 $")
00027 
00028 #include "asterisk/module.h"
00029 #include "asterisk/channel.h"
00030 #include "asterisk/pbx.h"
00031 #include "asterisk/utils.h"
00032 #include "asterisk/app.h"
00033 
00034 static int group_count_function_read(struct ast_channel *chan, const char *cmd,
00035                  char *data, char *buf, size_t len)
00036 {
00037    int ret = -1;
00038    int count = -1;
00039    char group[80] = "", category[80] = "";
00040 
00041    ast_app_group_split_group(data, group, sizeof(group), category,
00042               sizeof(category));
00043 
00044    /* If no group has been provided let's find one */
00045    if (ast_strlen_zero(group)) {
00046       struct ast_group_info *gi = NULL;
00047 
00048       ast_app_group_list_rdlock();
00049       for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, group_list)) {
00050          if (gi->chan != chan)
00051             continue;
00052          if (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category)))
00053             break;
00054       }
00055       if (gi) {
00056          ast_copy_string(group, gi->group, sizeof(group));
00057          if (!ast_strlen_zero(gi->category))
00058             ast_copy_string(category, gi->category, sizeof(category));
00059       }
00060       ast_app_group_list_unlock();
00061    }
00062 
00063    if ((count = ast_app_group_get_count(group, category)) == -1) {
00064       ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n", chan->name);
00065    } else {
00066       snprintf(buf, len, "%d", count);
00067       ret = 0;
00068    }
00069 
00070    return ret;
00071 }
00072 
00073 static struct ast_custom_function group_count_function = {
00074    .name = "GROUP_COUNT",
00075    .syntax = "GROUP_COUNT([groupname][@category])",
00076    .synopsis = "Counts the number of channels in the specified group",
00077    .desc =
00078       "Calculates the group count for the specified group, or uses the\n"
00079       "channel's current group if not specifed (and non-empty).\n",
00080    .read = group_count_function_read,
00081 };
00082 
00083 static int group_match_count_function_read(struct ast_channel *chan,
00084                   const char *cmd, char *data, char *buf,
00085                   size_t len)
00086 {
00087    int count;
00088    char group[80] = "";
00089    char category[80] = "";
00090 
00091    ast_app_group_split_group(data, group, sizeof(group), category,
00092               sizeof(category));
00093 
00094    if (!ast_strlen_zero(group)) {
00095       count = ast_app_group_match_get_count(group, category);
00096       snprintf(buf, len, "%d", count);
00097       return 0;
00098    }
00099 
00100    return -1;
00101 }
00102 
00103 static struct ast_custom_function group_match_count_function = {
00104    .name = "GROUP_MATCH_COUNT",
00105    .syntax = "GROUP_MATCH_COUNT(groupmatch[@category])",
00106    .synopsis =
00107       "Counts the number of channels in the groups matching the specified pattern",
00108    .desc =
00109       "Calculates the group count for all groups that match the specified pattern.\n"
00110       "Uses standard regular expression matching (see regex(7)).\n",
00111    .read = group_match_count_function_read,
00112    .write = NULL,
00113 };
00114 
00115 static int group_function_read(struct ast_channel *chan, const char *cmd,
00116                 char *data, char *buf, size_t len)
00117 {
00118    int ret = -1;
00119    struct ast_group_info *gi = NULL;
00120    
00121    ast_app_group_list_rdlock();
00122    
00123    for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, group_list)) {
00124       if (gi->chan != chan)
00125          continue;
00126       if (ast_strlen_zero(data))
00127          break;
00128       if (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, data))
00129          break;
00130    }
00131    
00132    if (gi) {
00133       ast_copy_string(buf, gi->group, len);
00134       ret = 0;
00135    }
00136    
00137    ast_app_group_list_unlock();
00138    
00139    return ret;
00140 }
00141 
00142 static int group_function_write(struct ast_channel *chan, const char *cmd,
00143             char *data, const char *value)
00144 {
00145    char grpcat[256];
00146 
00147    if (!value) {
00148       return -1;
00149    }
00150 
00151    if (!ast_strlen_zero(data)) {
00152       snprintf(grpcat, sizeof(grpcat), "%s@%s", value, data);
00153    } else {
00154       ast_copy_string(grpcat, value, sizeof(grpcat));
00155    }
00156 
00157    if (ast_app_group_set_channel(chan, grpcat))
00158       ast_log(LOG_WARNING,
00159             "Setting a group requires an argument (group name)\n");
00160 
00161    return 0;
00162 }
00163 
00164 static struct ast_custom_function group_function = {
00165    .name = "GROUP",
00166    .syntax = "GROUP([category])",
00167    .synopsis = "Gets or sets the channel group.",
00168    .desc = "Gets or sets the channel group.\n",
00169    .read = group_function_read,
00170    .write = group_function_write,
00171 };
00172 
00173 static int group_list_function_read(struct ast_channel *chan, const char *cmd,
00174                 char *data, char *buf, size_t len)
00175 {
00176    struct ast_group_info *gi = NULL;
00177    char tmp1[1024] = "";
00178    char tmp2[1024] = "";
00179 
00180    if (!chan)
00181       return -1;
00182 
00183    ast_app_group_list_rdlock();
00184 
00185    for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, group_list)) {
00186       if (gi->chan != chan)
00187          continue;
00188       if (!ast_strlen_zero(tmp1)) {
00189          ast_copy_string(tmp2, tmp1, sizeof(tmp2));
00190          if (!ast_strlen_zero(gi->category))
00191             snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, gi->group, gi->category);
00192          else
00193             snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, gi->group);
00194       } else {
00195          if (!ast_strlen_zero(gi->category))
00196             snprintf(tmp1, sizeof(tmp1), "%s@%s", gi->group, gi->category);
00197          else
00198             snprintf(tmp1, sizeof(tmp1), "%s", gi->group);
00199       }
00200    }
00201    
00202    ast_app_group_list_unlock();
00203 
00204    ast_copy_string(buf, tmp1, len);
00205 
00206    return 0;
00207 }
00208 
00209 static struct ast_custom_function group_list_function = {
00210    .name = "GROUP_LIST",
00211    .syntax = "GROUP_LIST()",
00212    .synopsis = "Gets a list of the groups set on a channel.",
00213    .desc = "Gets a list of the groups set on a channel.\n",
00214    .read = group_list_function_read,
00215    .write = NULL,
00216 };
00217 
00218 static int unload_module(void)
00219 {
00220    int res = 0;
00221 
00222    res |= ast_custom_function_unregister(&group_count_function);
00223    res |= ast_custom_function_unregister(&group_match_count_function);
00224    res |= ast_custom_function_unregister(&group_list_function);
00225    res |= ast_custom_function_unregister(&group_function);
00226 
00227    return res;
00228 }
00229 
00230 static int load_module(void)
00231 {
00232    int res = 0;
00233 
00234    res |= ast_custom_function_register(&group_count_function);
00235    res |= ast_custom_function_register(&group_match_count_function);
00236    res |= ast_custom_function_register(&group_list_function);
00237    res |= ast_custom_function_register(&group_function);
00238 
00239    return res;
00240 }
00241 
00242 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel group dialplan functions");

Generated on Wed Aug 18 22:33:52 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7