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

Generated on Mon Jun 27 16:50:54 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7