Mon Mar 31 07:38:02 2008

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

Generated on Mon Mar 31 07:38:02 2008 for Asterisk - the Open Source PBX by  doxygen 1.5.1