Wed Oct 28 11:51:04 2009

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: 190063 $")
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 (!ast_strlen_zero(data)) {
00148       snprintf(grpcat, sizeof(grpcat), "%s@%s", value, data);
00149    } else {
00150       ast_copy_string(grpcat, value, sizeof(grpcat));
00151    }
00152 
00153    if (ast_app_group_set_channel(chan, grpcat))
00154       ast_log(LOG_WARNING,
00155             "Setting a group requires an argument (group name)\n");
00156 
00157    return 0;
00158 }
00159 
00160 static struct ast_custom_function group_function = {
00161    .name = "GROUP",
00162    .syntax = "GROUP([category])",
00163    .synopsis = "Gets or sets the channel group.",
00164    .desc = "Gets or sets the channel group.\n",
00165    .read = group_function_read,
00166    .write = group_function_write,
00167 };
00168 
00169 static int group_list_function_read(struct ast_channel *chan, const char *cmd,
00170                 char *data, char *buf, size_t len)
00171 {
00172    struct ast_group_info *gi = NULL;
00173    char tmp1[1024] = "";
00174    char tmp2[1024] = "";
00175 
00176    if (!chan)
00177       return -1;
00178 
00179    ast_app_group_list_rdlock();
00180 
00181    for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, group_list)) {
00182       if (gi->chan != chan)
00183          continue;
00184       if (!ast_strlen_zero(tmp1)) {
00185          ast_copy_string(tmp2, tmp1, sizeof(tmp2));
00186          if (!ast_strlen_zero(gi->category))
00187             snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, gi->group, gi->category);
00188          else
00189             snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, gi->group);
00190       } else {
00191          if (!ast_strlen_zero(gi->category))
00192             snprintf(tmp1, sizeof(tmp1), "%s@%s", gi->group, gi->category);
00193          else
00194             snprintf(tmp1, sizeof(tmp1), "%s", gi->group);
00195       }
00196    }
00197    
00198    ast_app_group_list_unlock();
00199 
00200    ast_copy_string(buf, tmp1, len);
00201 
00202    return 0;
00203 }
00204 
00205 static struct ast_custom_function group_list_function = {
00206    .name = "GROUP_LIST",
00207    .syntax = "GROUP_LIST()",
00208    .synopsis = "Gets a list of the groups set on a channel.",
00209    .desc = "Gets a list of the groups set on a channel.\n",
00210    .read = group_list_function_read,
00211    .write = NULL,
00212 };
00213 
00214 static int unload_module(void)
00215 {
00216    int res = 0;
00217 
00218    res |= ast_custom_function_unregister(&group_count_function);
00219    res |= ast_custom_function_unregister(&group_match_count_function);
00220    res |= ast_custom_function_unregister(&group_list_function);
00221    res |= ast_custom_function_unregister(&group_function);
00222 
00223    return res;
00224 }
00225 
00226 static int load_module(void)
00227 {
00228    int res = 0;
00229 
00230    res |= ast_custom_function_register(&group_count_function);
00231    res |= ast_custom_function_register(&group_match_count_function);
00232    res |= ast_custom_function_register(&group_list_function);
00233    res |= ast_custom_function_register(&group_function);
00234 
00235    return res;
00236 }
00237 
00238 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel group dialplan functions");

Generated on Wed Oct 28 11:51:04 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6