00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
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
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");