| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#include "asterisk.h" |
| 41 |
|
| 42 |
#include "asterisk/module.h" |
| 43 |
#include "asterisk/channel.h" |
| 44 |
#include "asterisk/pbx.h" |
| 45 |
#include "asterisk/utils.h" |
| 46 |
#include "asterisk/lock.h" |
| 47 |
#include "asterisk/file.h" |
| 48 |
#include "asterisk/enum.h" |
| 49 |
#include "asterisk/app.h" |
| 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 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
static char *synopsis = "Syntax: ENUMLOOKUP(number[,Method-type[,options[,record#[,zone-suffix]]]])\n"; |
| 167 |
|
| 168 |
static int function_enum(struct ast_channel *chan, const char *cmd, char *data, |
| 169 |
char *buf, size_t len) |
| 170 |
{ |
| 171 |
AST_DECLARE_APP_ARGS(args, |
| 172 |
AST_APP_ARG(number); |
| 173 |
AST_APP_ARG(tech); |
| 174 |
AST_APP_ARG(options); |
| 175 |
AST_APP_ARG(record); |
| 176 |
AST_APP_ARG(zone); |
| 177 |
); |
| 178 |
char tech[80]; |
| 179 |
char dest[256] = "", tmp[2] = "", num[AST_MAX_EXTENSION] = ""; |
| 180 |
char *s, *p; |
| 181 |
unsigned int record = 1; |
| 182 |
|
| 183 |
buf[0] = '\0'; |
| 184 |
|
| 185 |
if (ast_strlen_zero(data)) { |
| 186 |
ast_log(LOG_WARNING, "%s", synopsis); |
| 187 |
return -1; |
| 188 |
} |
| 189 |
|
| 190 |
AST_STANDARD_APP_ARGS(args, data); |
| 191 |
|
| 192 |
if (args.argc < 1) { |
| 193 |
ast_log(LOG_WARNING, "%s", synopsis); |
| 194 |
return -1; |
| 195 |
} |
| 196 |
|
| 197 |
if (args.tech && !ast_strlen_zero(args.tech)) { |
| 198 |
ast_copy_string(tech,args.tech, sizeof(tech)); |
| 199 |
} else { |
| 200 |
ast_copy_string(tech,"pjsip",sizeof(tech)); |
| 201 |
} |
| 202 |
|
| 203 |
if (!args.zone) { |
| 204 |
args.zone = "e164.arpa"; |
| 205 |
} |
| 206 |
if (!args.options) { |
| 207 |
args.options = ""; |
| 208 |
} |
| 209 |
if (args.record) { |
| 210 |
record = atoi(args.record) ? atoi(args.record) : record; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
for (s = p = args.number; *s; s++) { |
| 215 |
if (*s != '-') { |
| 216 |
snprintf(tmp, sizeof(tmp), "%c", *s); |
| 217 |
strncat(num, tmp, sizeof(num) - strlen(num) - 1); |
| 218 |
} |
| 219 |
|
| 220 |
} |
| 221 |
ast_get_enum(chan, num, dest, sizeof(dest), tech, sizeof(tech), args.zone, args.options, record, NULL); |
| 222 |
|
| 223 |
p = strchr(dest, ':'); |
| 224 |
if (p && strcasecmp(tech, "ALL") && !strchr(args.options, 'u')) { |
| 225 |
ast_copy_string(buf, p + 1, len); |
| 226 |
} else { |
| 227 |
ast_copy_string(buf, dest, len); |
| 228 |
} |
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
static unsigned int enum_datastore_id; |
| 233 |
|
| 234 |
struct enum_result_datastore { |
| 235 |
struct enum_context *context; |
| 236 |
unsigned int id; |
| 237 |
}; |
| 238 |
|
| 239 |
static void erds_destroy(struct enum_result_datastore *data) |
| 240 |
{ |
| 241 |
int k; |
| 242 |
|
| 243 |
for (k = 0; k < data->context->naptr_rrs_count; k++) { |
| 244 |
ast_free(data->context->naptr_rrs[k].result); |
| 245 |
ast_free(data->context->naptr_rrs[k].tech); |
| 246 |
} |
| 247 |
|
| 248 |
ast_free(data->context->naptr_rrs); |
| 249 |
ast_free(data->context); |
| 250 |
ast_free(data); |
| 251 |
} |
| 252 |
|
| 253 |
static void erds_destroy_cb(void *data) |
| 254 |
{ |
| 255 |
struct enum_result_datastore *erds = data; |
| 256 |
erds_destroy(erds); |
| 257 |
} |
| 258 |
|
| 259 |
static const struct ast_datastore_info enum_result_datastore_info = { |
| 260 |
.type = "ENUMQUERY", |
| 261 |
.destroy = erds_destroy_cb, |
| 262 |
}; |
| 263 |
|
| 264 |
static int enum_query_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 265 |
{ |
| 266 |
struct enum_result_datastore *erds; |
| 267 |
struct ast_datastore *datastore; |
| 268 |
char *parse, tech[128], dest[128]; |
| 269 |
int res = -1; |
| 270 |
|
| 271 |
AST_DECLARE_APP_ARGS(args, |
| 272 |
AST_APP_ARG(number); |
| 273 |
AST_APP_ARG(tech); |
| 274 |
AST_APP_ARG(zone); |
| 275 |
); |
| 276 |
|
| 277 |
if (ast_strlen_zero(data)) { |
| 278 |
ast_log(LOG_WARNING, "ENUMQUERY requires at least a number as an argument...\n"); |
| 279 |
goto finish; |
| 280 |
} |
| 281 |
|
| 282 |
parse = ast_strdupa(data); |
| 283 |
|
| 284 |
AST_STANDARD_APP_ARGS(args, parse); |
| 285 |
|
| 286 |
if (!chan) { |
| 287 |
ast_log(LOG_ERROR, "ENUMQUERY cannot be used without a channel!\n"); |
| 288 |
goto finish; |
| 289 |
} |
| 290 |
|
| 291 |
if (!args.zone) |
| 292 |
args.zone = "e164.zone"; |
| 293 |
|
| 294 |
ast_copy_string(tech, args.tech ? args.tech : "pjsip", sizeof(tech)); |
| 295 |
|
| 296 |
if (!(erds = ast_calloc(1, sizeof(*erds)))) |
| 297 |
goto finish; |
| 298 |
|
| 299 |
if (!(erds->context = ast_calloc(1, sizeof(*erds->context)))) { |
| 300 |
ast_free(erds); |
| 301 |
goto finish; |
| 302 |
} |
| 303 |
|
| 304 |
erds->id = ast_atomic_fetchadd_int((int *) &enum_datastore_id, 1); |
| 305 |
|
| 306 |
snprintf(buf, len, "%u", erds->id); |
| 307 |
|
| 308 |
if (!(datastore = ast_datastore_alloc(&enum_result_datastore_info, buf))) { |
| 309 |
ast_free(erds->context); |
| 310 |
ast_free(erds); |
| 311 |
goto finish; |
| 312 |
} |
| 313 |
|
| 314 |
ast_get_enum(chan, args.number, dest, sizeof(dest), tech, sizeof(tech), args.zone, "", 1, &erds->context); |
| 315 |
|
| 316 |
datastore->data = erds; |
| 317 |
|
| 318 |
ast_channel_lock(chan); |
| 319 |
ast_channel_datastore_add(chan, datastore); |
| 320 |
ast_channel_unlock(chan); |
| 321 |
|
| 322 |
res = 0; |
| 323 |
|
| 324 |
finish: |
| 325 |
|
| 326 |
return res; |
| 327 |
} |
| 328 |
|
| 329 |
static int enum_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 330 |
{ |
| 331 |
struct enum_result_datastore *erds; |
| 332 |
struct ast_datastore *datastore; |
| 333 |
char *parse, *p; |
| 334 |
unsigned int num; |
| 335 |
int res = -1, k; |
| 336 |
AST_DECLARE_APP_ARGS(args, |
| 337 |
AST_APP_ARG(id); |
| 338 |
AST_APP_ARG(resultnum); |
| 339 |
); |
| 340 |
|
| 341 |
if (ast_strlen_zero(data)) { |
| 342 |
ast_log(LOG_WARNING, "ENUMRESULT requires two arguments (id and resultnum)\n"); |
| 343 |
goto finish; |
| 344 |
} |
| 345 |
|
| 346 |
if (!chan) { |
| 347 |
ast_log(LOG_ERROR, "ENUMRESULT can not be used without a channel!\n"); |
| 348 |
goto finish; |
| 349 |
} |
| 350 |
|
| 351 |
parse = ast_strdupa(data); |
| 352 |
|
| 353 |
AST_STANDARD_APP_ARGS(args, parse); |
| 354 |
|
| 355 |
if (ast_strlen_zero(args.id)) { |
| 356 |
ast_log(LOG_ERROR, "A result ID must be provided to ENUMRESULT\n"); |
| 357 |
goto finish; |
| 358 |
} |
| 359 |
|
| 360 |
if (ast_strlen_zero(args.resultnum)) { |
| 361 |
ast_log(LOG_ERROR, "A result number must be given to ENUMRESULT!\n"); |
| 362 |
goto finish; |
| 363 |
} |
| 364 |
|
| 365 |
ast_channel_lock(chan); |
| 366 |
datastore = ast_channel_datastore_find(chan, &enum_result_datastore_info, args.id); |
| 367 |
ast_channel_unlock(chan); |
| 368 |
if (!datastore) { |
| 369 |
ast_log(LOG_WARNING, "No ENUM results found for query id!\n"); |
| 370 |
goto finish; |
| 371 |
} |
| 372 |
|
| 373 |
erds = datastore->data; |
| 374 |
|
| 375 |
if (!strcasecmp(args.resultnum, "getnum")) { |
| 376 |
snprintf(buf, len, "%d", erds->context->naptr_rrs_count); |
| 377 |
res = 0; |
| 378 |
goto finish; |
| 379 |
} |
| 380 |
|
| 381 |
if (sscanf(args.resultnum, "%30u", &num) != 1) { |
| 382 |
ast_log(LOG_ERROR, "Invalid value '%s' for resultnum to ENUMRESULT!\n", args.resultnum); |
| 383 |
goto finish; |
| 384 |
} |
| 385 |
|
| 386 |
if (!num || num > erds->context->naptr_rrs_count) { |
| 387 |
ast_log(LOG_WARNING, "Result number %u is not valid for ENUM query results for ID %s!\n", num, args.id); |
| 388 |
goto finish; |
| 389 |
} |
| 390 |
|
| 391 |
for (k = 0; k < erds->context->naptr_rrs_count; k++) { |
| 392 |
if (num - 1 != erds->context->naptr_rrs[k].sort_pos) |
| 393 |
continue; |
| 394 |
|
| 395 |
p = strchr(erds->context->naptr_rrs[k].result, ':'); |
| 396 |
|
| 397 |
if (p && strcasecmp(erds->context->naptr_rrs[k].tech, "ALL")) |
| 398 |
ast_copy_string(buf, p + 1, len); |
| 399 |
else |
| 400 |
ast_copy_string(buf, erds->context->naptr_rrs[k].result, len); |
| 401 |
|
| 402 |
break; |
| 403 |
} |
| 404 |
|
| 405 |
res = 0; |
| 406 |
|
| 407 |
finish: |
| 408 |
|
| 409 |
return res; |
| 410 |
} |
| 411 |
|
| 412 |
static struct ast_custom_function enum_query_function = { |
| 413 |
.name = "ENUMQUERY", |
| 414 |
.read = enum_query_read, |
| 415 |
}; |
| 416 |
|
| 417 |
static struct ast_custom_function enum_result_function = { |
| 418 |
.name = "ENUMRESULT", |
| 419 |
.read = enum_result_read, |
| 420 |
}; |
| 421 |
|
| 422 |
static struct ast_custom_function enum_function = { |
| 423 |
.name = "ENUMLOOKUP", |
| 424 |
.read = function_enum, |
| 425 |
}; |
| 426 |
|
| 427 |
static int function_txtcidname(struct ast_channel *chan, const char *cmd, |
| 428 |
char *data, char *buf, size_t len) |
| 429 |
{ |
| 430 |
AST_DECLARE_APP_ARGS(args, |
| 431 |
AST_APP_ARG(number); |
| 432 |
AST_APP_ARG(zone); |
| 433 |
); |
| 434 |
|
| 435 |
buf[0] = '\0'; |
| 436 |
|
| 437 |
if (ast_strlen_zero(data)) { |
| 438 |
ast_log(LOG_WARNING, "Syntax: TXTCIDNAME(number[,zone-suffix])\n"); |
| 439 |
return -1; |
| 440 |
} |
| 441 |
|
| 442 |
AST_STANDARD_APP_ARGS(args, data); |
| 443 |
|
| 444 |
if (args.argc < 1) { |
| 445 |
ast_log(LOG_WARNING, "Syntax: TXTCIDNAME(number[,zone-suffix])\n"); |
| 446 |
return -1; |
| 447 |
} |
| 448 |
|
| 449 |
if (!args.zone) { |
| 450 |
args.zone = "e164.arpa"; |
| 451 |
} |
| 452 |
|
| 453 |
ast_get_txt(chan, args.number, buf, len, args.zone); |
| 454 |
|
| 455 |
return 0; |
| 456 |
} |
| 457 |
|
| 458 |
static struct ast_custom_function txtcidname_function = { |
| 459 |
.name = "TXTCIDNAME", |
| 460 |
.read = function_txtcidname, |
| 461 |
}; |
| 462 |
|
| 463 |
static int unload_module(void) |
| 464 |
{ |
| 465 |
int res = 0; |
| 466 |
|
| 467 |
res |= ast_custom_function_unregister(&enum_result_function); |
| 468 |
res |= ast_custom_function_unregister(&enum_query_function); |
| 469 |
res |= ast_custom_function_unregister(&enum_function); |
| 470 |
res |= ast_custom_function_unregister(&txtcidname_function); |
| 471 |
|
| 472 |
return res; |
| 473 |
} |
| 474 |
|
| 475 |
static int load_module(void) |
| 476 |
{ |
| 477 |
int res = 0; |
| 478 |
|
| 479 |
res |= ast_custom_function_register(&enum_result_function); |
| 480 |
res |= ast_custom_function_register(&enum_query_function); |
| 481 |
res |= ast_custom_function_register(&enum_function); |
| 482 |
res |= ast_custom_function_register(&txtcidname_function); |
| 483 |
|
| 484 |
return res; |
| 485 |
} |
| 486 |
|
| 487 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ENUM related dialplan functions", |
| 488 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 489 |
.load = load_module, |
| 490 |
.unload = unload_module, |
| 491 |
.requires = "enum", |
| 492 |
); |