| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#include "asterisk.h" |
| 29 |
|
| 30 |
#include "asterisk/module.h" |
| 31 |
#include "asterisk/channel.h" |
| 32 |
#include "asterisk/pbx.h" |
| 33 |
#include "asterisk/utils.h" |
| 34 |
#include "asterisk/app.h" |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
static int group_count_function_read(struct ast_channel *chan, const char *cmd, |
| 111 |
char *data, char *buf, size_t len) |
| 112 |
{ |
| 113 |
int ret = -1; |
| 114 |
int count = -1; |
| 115 |
char group[80] = "", category[80] = ""; |
| 116 |
|
| 117 |
if (!chan) { |
| 118 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 119 |
return -1; |
| 120 |
} |
| 121 |
|
| 122 |
ast_app_group_split_group(data, group, sizeof(group), category, |
| 123 |
sizeof(category)); |
| 124 |
|
| 125 |
|
| 126 |
if (ast_strlen_zero(group)) { |
| 127 |
struct ast_group_info *gi = NULL; |
| 128 |
|
| 129 |
ast_app_group_list_rdlock(); |
| 130 |
for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, group_list)) { |
| 131 |
if (gi->chan != chan) |
| 132 |
continue; |
| 133 |
if (ast_strlen_zero(category) || (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, category))) |
| 134 |
break; |
| 135 |
} |
| 136 |
if (gi) { |
| 137 |
ast_copy_string(group, gi->group, sizeof(group)); |
| 138 |
if (!ast_strlen_zero(gi->category)) |
| 139 |
ast_copy_string(category, gi->category, sizeof(category)); |
| 140 |
} |
| 141 |
ast_app_group_list_unlock(); |
| 142 |
} |
| 143 |
|
| 144 |
if ((count = ast_app_group_get_count(group, category)) == -1) { |
| 145 |
ast_log(LOG_NOTICE, "No group could be found for channel '%s'\n", ast_channel_name(chan)); |
| 146 |
} else { |
| 147 |
snprintf(buf, len, "%d", count); |
| 148 |
ret = 0; |
| 149 |
} |
| 150 |
|
| 151 |
return ret; |
| 152 |
} |
| 153 |
|
| 154 |
static struct ast_custom_function group_count_function = { |
| 155 |
.name = "GROUP_COUNT", |
| 156 |
.read = group_count_function_read, |
| 157 |
.read_max = 12, |
| 158 |
}; |
| 159 |
|
| 160 |
static int group_match_count_function_read(struct ast_channel *chan, |
| 161 |
const char *cmd, char *data, char *buf, |
| 162 |
size_t len) |
| 163 |
{ |
| 164 |
char group[80] = ""; |
| 165 |
char category[80] = ""; |
| 166 |
|
| 167 |
ast_app_group_split_group(data, group, sizeof(group), category, |
| 168 |
sizeof(category)); |
| 169 |
|
| 170 |
if (!ast_strlen_zero(group)) { |
| 171 |
int count; |
| 172 |
count = ast_app_group_match_get_count(group, category); |
| 173 |
snprintf(buf, len, "%d", count); |
| 174 |
return 0; |
| 175 |
} |
| 176 |
|
| 177 |
return -1; |
| 178 |
} |
| 179 |
|
| 180 |
static struct ast_custom_function group_match_count_function = { |
| 181 |
.name = "GROUP_MATCH_COUNT", |
| 182 |
.read = group_match_count_function_read, |
| 183 |
.read_max = 12, |
| 184 |
.write = NULL, |
| 185 |
}; |
| 186 |
|
| 187 |
static int group_function_read(struct ast_channel *chan, const char *cmd, |
| 188 |
char *data, char *buf, size_t len) |
| 189 |
{ |
| 190 |
int ret = -1; |
| 191 |
struct ast_group_info *gi = NULL; |
| 192 |
|
| 193 |
if (!chan) { |
| 194 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 195 |
return -1; |
| 196 |
} |
| 197 |
|
| 198 |
ast_app_group_list_rdlock(); |
| 199 |
|
| 200 |
for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, group_list)) { |
| 201 |
if (gi->chan != chan) |
| 202 |
continue; |
| 203 |
if (ast_strlen_zero(data)) |
| 204 |
break; |
| 205 |
if (!ast_strlen_zero(gi->category) && !strcasecmp(gi->category, data)) |
| 206 |
break; |
| 207 |
} |
| 208 |
|
| 209 |
if (gi) { |
| 210 |
ast_copy_string(buf, gi->group, len); |
| 211 |
ret = 0; |
| 212 |
} |
| 213 |
|
| 214 |
ast_app_group_list_unlock(); |
| 215 |
|
| 216 |
return ret; |
| 217 |
} |
| 218 |
|
| 219 |
static int group_function_write(struct ast_channel *chan, const char *cmd, |
| 220 |
char *data, const char *value) |
| 221 |
{ |
| 222 |
char grpcat[256]; |
| 223 |
|
| 224 |
if (!chan) { |
| 225 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 226 |
return -1; |
| 227 |
} |
| 228 |
|
| 229 |
if (!value) { |
| 230 |
return -1; |
| 231 |
} |
| 232 |
|
| 233 |
if (!ast_strlen_zero(data)) { |
| 234 |
snprintf(grpcat, sizeof(grpcat), "%s@%s", value, data); |
| 235 |
} else { |
| 236 |
ast_copy_string(grpcat, value, sizeof(grpcat)); |
| 237 |
} |
| 238 |
|
| 239 |
if (ast_app_group_set_channel(chan, grpcat)) |
| 240 |
ast_log(LOG_WARNING, |
| 241 |
"Setting a group requires an argument (group name)\n"); |
| 242 |
|
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
static struct ast_custom_function group_function = { |
| 247 |
.name = "GROUP", |
| 248 |
.read = group_function_read, |
| 249 |
.write = group_function_write, |
| 250 |
}; |
| 251 |
|
| 252 |
static int group_list_function_read(struct ast_channel *chan, const char *cmd, |
| 253 |
char *data, char *buf, size_t len) |
| 254 |
{ |
| 255 |
struct ast_group_info *gi = NULL; |
| 256 |
char tmp1[1024] = ""; |
| 257 |
char tmp2[1024] = ""; |
| 258 |
|
| 259 |
if (!chan) |
| 260 |
return -1; |
| 261 |
|
| 262 |
ast_app_group_list_rdlock(); |
| 263 |
|
| 264 |
for (gi = ast_app_group_list_head(); gi; gi = AST_LIST_NEXT(gi, group_list)) { |
| 265 |
if (gi->chan != chan) |
| 266 |
continue; |
| 267 |
if (!ast_strlen_zero(tmp1)) { |
| 268 |
ast_copy_string(tmp2, tmp1, sizeof(tmp2)); |
| 269 |
if (!ast_strlen_zero(gi->category)) |
| 270 |
snprintf(tmp1, sizeof(tmp1), "%s %s@%s", tmp2, gi->group, gi->category); |
| 271 |
else |
| 272 |
snprintf(tmp1, sizeof(tmp1), "%s %s", tmp2, gi->group); |
| 273 |
} else { |
| 274 |
if (!ast_strlen_zero(gi->category)) |
| 275 |
snprintf(tmp1, sizeof(tmp1), "%s@%s", gi->group, gi->category); |
| 276 |
else |
| 277 |
snprintf(tmp1, sizeof(tmp1), "%s", gi->group); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
ast_app_group_list_unlock(); |
| 282 |
|
| 283 |
ast_copy_string(buf, tmp1, len); |
| 284 |
|
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
static struct ast_custom_function group_list_function = { |
| 289 |
.name = "GROUP_LIST", |
| 290 |
.read = group_list_function_read, |
| 291 |
.write = NULL, |
| 292 |
}; |
| 293 |
|
| 294 |
static int unload_module(void) |
| 295 |
{ |
| 296 |
int res = 0; |
| 297 |
|
| 298 |
res |= ast_custom_function_unregister(&group_count_function); |
| 299 |
res |= ast_custom_function_unregister(&group_match_count_function); |
| 300 |
res |= ast_custom_function_unregister(&group_list_function); |
| 301 |
res |= ast_custom_function_unregister(&group_function); |
| 302 |
|
| 303 |
return res; |
| 304 |
} |
| 305 |
|
| 306 |
static int load_module(void) |
| 307 |
{ |
| 308 |
int res = 0; |
| 309 |
|
| 310 |
res |= ast_custom_function_register(&group_count_function); |
| 311 |
res |= ast_custom_function_register(&group_match_count_function); |
| 312 |
res |= ast_custom_function_register(&group_list_function); |
| 313 |
res |= ast_custom_function_register(&group_function); |
| 314 |
|
| 315 |
return res; |
| 316 |
} |
| 317 |
|
| 318 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel group dialplan functions"); |