Tue Aug 20 16:34:34 2013

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

Generated on 20 Aug 2013 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1