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

Generated on Mon May 14 04:42:56 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1