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

Generated on Sun Jun 12 16:37:47 2011 for Asterisk - the Open Source PBX by  doxygen 1.5.6