Sat Aug 6 00:39:29 2011

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

Generated on Sat Aug 6 00:39:29 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7