Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:54 2009

## 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 - 2005, 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 <stdlib.h>
    00024 #include <stdio.h>
    00025 #include <string.h>
    00026 #include <sys/types.h>
    00027
    00028 #include "asterisk.h"
    00029
    00030 /* ASTERISK_FILE_VERSION(__FILE__, "$Revision: 69258 $") */
    00031
    00032 #include "asterisk/channel.h"
    00033 #include "asterisk/pbx.h"
    00034 #include "asterisk/logger.h"
    00035 #include "asterisk/utils.h"
    00036 #include "asterisk/app.h"
    00037
    00038 static char *group_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
    00039 {
    00040    int count = -1;
    00041    char group[80] = "", category[80] = "";
    00042
    00043    ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
    00044
    00045    if ((count = ast_app_group_get_count(group, category)) == -1)
    00046       ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n", chan->name);
    00047    else
    00048       snprintf(buf, len, "%d", count);
    00049
    00050    return buf;
    00051 }
    00052
    00053 #ifndef BUILTIN_FUNC
    00054 static
    00055 #endif
    00056 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 = "Calculates the group count for the specified group, or uses the\n"
    00061    "channel's current group if not specifed (and non-empty).\n",
    00062    .read = group_count_function_read,
    00063 };
    00064
    00065 static char *group_match_count_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
    00066 {
    00067    int count;
    00068    char group[80] = "";
    00069    char category[80] = "";
    00070
    00071    ast_app_group_split_group(data, group, sizeof(group), category, sizeof(category));
    00072
    00073    if (!ast_strlen_zero(group)) {
    00074       count = ast_app_group_match_get_count(group, category);
    00075       snprintf(buf, len, "%d", count);
    00076    }
    00077
    00078    return buf;
    00079 }
    00080
    00081 #ifndef BUILTIN_FUNC
    00082 static
    00083 #endif
    00084 struct ast_custom_function group_match_count_function = {
    00085    .name = "GROUP_MATCH_COUNT",
    00086    .syntax = "GROUP_MATCH_COUNT(groupmatch[@category])",
    00087    .synopsis = "Counts the number of channels in the groups matching the specified pattern",
    00088    .desc = "Calculates the group count for all groups that match the specified pattern.\n"
    00089    "Uses standard regular expression matching (see regex(7)).\n",
    00090    .read = group_match_count_function_read,
    00091    .write = NULL,
    00092 };
    00093
    00094 static char *group_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
    00095 {
    00096    struct ast_group_info *gi = NULL;
    00097
    00098    ast_app_group_list_lock();
    00099
    00100    for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, list)) {
    00101       if (gi->chan != chan)
    00102          continue;
    00103       if (ast_strlen_zero(data))
    00104          break;
    00105       if (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, data))
    00106          break;
    00107    }
    00108
    00109    if (gi)
    00110       ast_copy_string(buf, gi->group, len);
    00111
    00112    ast_app_group_list_unlock();
    00113
    00114    return buf;
    00115 }
    00116
    00117 static void group_function_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
    00118 {
    00119    char grpcat[256];
    00120
    00121    if (!ast_strlen_zero(data)) {
    00122       snprintf(grpcat, sizeof(grpcat), "%s@%s", value, data);
    00123    } else {
    00124       ast_copy_string(grpcat, value, sizeof(grpcat));
    00125    }
    00126
    00127    if (ast_app_group_set_channel(chan, grpcat))
    00128       ast_log(LOG_WARNING, "Setting a group requires an argument (group name)\n");
    00129 }
    00130
    00131 #ifndef BUILTIN_FUNC
    00132 static
    00133 #endif
    00134 struct ast_custom_function group_function = {
    00135    .name = "GROUP",
    00136    .syntax = "GROUP([category])",
    00137    .synopsis = "Gets or sets the channel group.",
    00138    .desc = "Gets or sets the channel group.\n",
    00139    .read = group_function_read,
    00140    .write = group_function_write,
    00141 };
    00142
    00143 static char *group_list_function_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
    00144 {
    00145    struct ast_group_info *gi = NULL;
    00146    char tmp1[1024] = "";
    00147    char tmp2[1024] = "";
    00148
    00149    ast_app_group_list_lock();
    00150
    00151    for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, list)) {
    00152       if (gi->chan != chan)
    00153          continue;
    00154       if (!ast_strlen_zero(tmp1)) {
    00155          ast_copy_string(tmp2, tmp1, sizeof(tmp2));
    00156          if (!ast_strlen_zero(gi->category))
    00157             snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, gi->group, gi->category);
    00158          else
    00159             snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, gi->group);
    00160       } else {
    00161          if (!ast_strlen_zero(gi->category))
    00162             snprintf(tmp1, sizeof(tmp1), "%s@%s", gi->group, gi->category);
    00163          else
    00164             snprintf(tmp1, sizeof(tmp1), "%s", gi->group);
    00165       }
    00166    }
    00167
    00168    ast_app_group_list_unlock();
    00169
    00170    ast_copy_string(buf, tmp1, len);
    00171
    00172    return buf;
    00173 }
    00174
    00175 #ifndef BUILTIN_FUNC
    00176 static
    00177 #endif
    00178 struct ast_custom_function group_list_function = {
    00179    .name = "GROUP_LIST",
    00180    .syntax = "GROUP_LIST()",
    00181    .synopsis = "Gets a list of the groups set on a channel.",
    00182    .desc = "Gets a list of the groups set on a channel.\n",
    00183    .read = group_list_function_read,
    00184    .write = NULL,
    00185 };
    00186
    00187 /*
    00188 Local Variables:
    00189 mode: C
    00190 c-file-style: "linux"
    00191 indent-tabs-mode: nil
    00192 End:
    00193 */