Wed Aug 15 01:24:21 2007

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

Generated on Wed Aug 15 01:24:21 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.3