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

Generated on Thu Jul 9 13:40:35 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7