| 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 |
#include "asterisk.h" |
| 35 |
|
| 36 |
#include <regex.h> |
| 37 |
|
| 38 |
#include "asterisk/module.h" |
| 39 |
#include "asterisk/channel.h" |
| 40 |
#include "asterisk/pbx.h" |
| 41 |
#include "asterisk/utils.h" |
| 42 |
#include "asterisk/app.h" |
| 43 |
#include "asterisk/astdb.h" |
| 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 |
|
| 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 |
static int function_db_read(struct ast_channel *chan, const char *cmd, |
| 163 |
char *parse, char *buf, size_t len) |
| 164 |
{ |
| 165 |
AST_DECLARE_APP_ARGS(args, |
| 166 |
AST_APP_ARG(family); |
| 167 |
AST_APP_ARG(key); |
| 168 |
); |
| 169 |
|
| 170 |
buf[0] = '\0'; |
| 171 |
|
| 172 |
if (ast_strlen_zero(parse)) { |
| 173 |
ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n"); |
| 174 |
return -1; |
| 175 |
} |
| 176 |
|
| 177 |
AST_NONSTANDARD_APP_ARGS(args, parse, '/'); |
| 178 |
|
| 179 |
if (args.argc < 2) { |
| 180 |
ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n"); |
| 181 |
return -1; |
| 182 |
} |
| 183 |
|
| 184 |
if (ast_db_get(args.family, args.key, buf, len - 1)) { |
| 185 |
ast_debug(1, "DB: %s/%s not found in database.\n", args.family, args.key); |
| 186 |
} else { |
| 187 |
pbx_builtin_setvar_helper(chan, "DB_RESULT", buf); |
| 188 |
} |
| 189 |
|
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
static int function_db_write(struct ast_channel *chan, const char *cmd, char *parse, |
| 194 |
const char *value) |
| 195 |
{ |
| 196 |
AST_DECLARE_APP_ARGS(args, |
| 197 |
AST_APP_ARG(family); |
| 198 |
AST_APP_ARG(key); |
| 199 |
); |
| 200 |
|
| 201 |
if (ast_strlen_zero(parse)) { |
| 202 |
ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n"); |
| 203 |
return -1; |
| 204 |
} |
| 205 |
|
| 206 |
AST_NONSTANDARD_APP_ARGS(args, parse, '/'); |
| 207 |
|
| 208 |
if (args.argc < 2) { |
| 209 |
ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n"); |
| 210 |
return -1; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
if (ast_strlen_zero(args.key) || args.key[0] == '/' || args.key[strlen(args.key) - 1] == '/' || strstr(args.key, "//")) { |
| 217 |
ast_log(LOG_WARNING, "DB: key '%s' seems malformed\n", args.key); |
| 218 |
} |
| 219 |
if (ast_db_put(args.family, args.key, value)) { |
| 220 |
ast_log(LOG_WARNING, "DB: Error writing value to database.\n"); |
| 221 |
} |
| 222 |
|
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
static struct ast_custom_function db_function = { |
| 227 |
.name = "DB", |
| 228 |
.read = function_db_read, |
| 229 |
.write = function_db_write, |
| 230 |
}; |
| 231 |
|
| 232 |
static int function_db_exists(struct ast_channel *chan, const char *cmd, |
| 233 |
char *parse, char *buf, size_t len) |
| 234 |
{ |
| 235 |
AST_DECLARE_APP_ARGS(args, |
| 236 |
AST_APP_ARG(family); |
| 237 |
AST_APP_ARG(key); |
| 238 |
); |
| 239 |
|
| 240 |
buf[0] = '\0'; |
| 241 |
|
| 242 |
if (ast_strlen_zero(parse)) { |
| 243 |
ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n"); |
| 244 |
return -1; |
| 245 |
} |
| 246 |
|
| 247 |
AST_NONSTANDARD_APP_ARGS(args, parse, '/'); |
| 248 |
|
| 249 |
if (args.argc < 2) { |
| 250 |
ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n"); |
| 251 |
return -1; |
| 252 |
} |
| 253 |
|
| 254 |
if (ast_db_get(args.family, args.key, buf, len - 1)) { |
| 255 |
strcpy(buf, "0"); |
| 256 |
} else { |
| 257 |
pbx_builtin_setvar_helper(chan, "DB_RESULT", buf); |
| 258 |
strcpy(buf, "1"); |
| 259 |
} |
| 260 |
|
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
static struct ast_custom_function db_exists_function = { |
| 265 |
.name = "DB_EXISTS", |
| 266 |
.read = function_db_exists, |
| 267 |
.read_max = 2, |
| 268 |
}; |
| 269 |
|
| 270 |
static int function_db_keys(struct ast_channel *chan, const char *cmd, char *parse, struct ast_str **result, ssize_t maxlen) |
| 271 |
{ |
| 272 |
size_t parselen = strlen(parse); |
| 273 |
struct ast_db_entry *dbe, *orig_dbe; |
| 274 |
struct ast_str *escape_buf = NULL; |
| 275 |
const char *last = ""; |
| 276 |
|
| 277 |
|
| 278 |
while (parse[0] == '/') { |
| 279 |
parse++; |
| 280 |
parselen--; |
| 281 |
} |
| 282 |
while (parse[parselen - 1] == '/') { |
| 283 |
parse[--parselen] = '\0'; |
| 284 |
} |
| 285 |
|
| 286 |
ast_str_reset(*result); |
| 287 |
|
| 288 |
|
| 289 |
if (!(orig_dbe = dbe = ast_db_gettree(parse, NULL))) { |
| 290 |
return 0; |
| 291 |
} |
| 292 |
|
| 293 |
for (; dbe; dbe = dbe->next) { |
| 294 |
|
| 295 |
char *curkey = &dbe->key[parselen + 1], *slash; |
| 296 |
if (*curkey == '/') { |
| 297 |
curkey++; |
| 298 |
} |
| 299 |
|
| 300 |
if ((slash = strchr(curkey, '/'))) { |
| 301 |
*slash = '\0'; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
if (!strcasecmp(last, curkey)) { |
| 306 |
continue; |
| 307 |
} |
| 308 |
last = curkey; |
| 309 |
|
| 310 |
if (orig_dbe != dbe) { |
| 311 |
ast_str_append(result, maxlen, ","); |
| 312 |
} |
| 313 |
ast_str_append_escapecommas(result, maxlen, curkey, strlen(curkey)); |
| 314 |
} |
| 315 |
ast_db_freetree(orig_dbe); |
| 316 |
ast_free(escape_buf); |
| 317 |
return 0; |
| 318 |
} |
| 319 |
|
| 320 |
static struct ast_custom_function db_keys_function = { |
| 321 |
.name = "DB_KEYS", |
| 322 |
.read2 = function_db_keys, |
| 323 |
}; |
| 324 |
|
| 325 |
static int function_db_keycount(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len) |
| 326 |
{ |
| 327 |
size_t parselen = strlen(parse); |
| 328 |
struct ast_db_entry *dbe, *orig_dbe; |
| 329 |
const char *last = ""; |
| 330 |
int keycount = 0; |
| 331 |
|
| 332 |
|
| 333 |
while (parse[0] == '/') { |
| 334 |
parse++; |
| 335 |
parselen--; |
| 336 |
} |
| 337 |
while (parse[parselen - 1] == '/') { |
| 338 |
parse[--parselen] = '\0'; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
if (!(orig_dbe = dbe = ast_db_gettree(parse, NULL))) { |
| 343 |
snprintf(buf, len, "%d", keycount); |
| 344 |
return 0; |
| 345 |
} |
| 346 |
|
| 347 |
for (; dbe; dbe = dbe->next) { |
| 348 |
|
| 349 |
char *curkey = &dbe->key[parselen + 1], *slash; |
| 350 |
if (*curkey == '/') { |
| 351 |
curkey++; |
| 352 |
} |
| 353 |
|
| 354 |
if ((slash = strchr(curkey, '/'))) { |
| 355 |
*slash = '\0'; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
if (!strcasecmp(last, curkey)) { |
| 360 |
continue; |
| 361 |
} |
| 362 |
last = curkey; |
| 363 |
|
| 364 |
keycount++; |
| 365 |
} |
| 366 |
ast_db_freetree(orig_dbe); |
| 367 |
snprintf(buf, len, "%d", keycount); |
| 368 |
return 0; |
| 369 |
} |
| 370 |
|
| 371 |
static struct ast_custom_function db_keycount_function = { |
| 372 |
.name = "DB_KEYCOUNT", |
| 373 |
.read = function_db_keycount, |
| 374 |
}; |
| 375 |
|
| 376 |
static int function_db_delete(struct ast_channel *chan, const char *cmd, |
| 377 |
char *parse, char *buf, size_t len) |
| 378 |
{ |
| 379 |
AST_DECLARE_APP_ARGS(args, |
| 380 |
AST_APP_ARG(family); |
| 381 |
AST_APP_ARG(key); |
| 382 |
); |
| 383 |
|
| 384 |
buf[0] = '\0'; |
| 385 |
|
| 386 |
if (ast_strlen_zero(parse)) { |
| 387 |
ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n"); |
| 388 |
return -1; |
| 389 |
} |
| 390 |
|
| 391 |
AST_NONSTANDARD_APP_ARGS(args, parse, '/'); |
| 392 |
|
| 393 |
if (args.argc < 2) { |
| 394 |
ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n"); |
| 395 |
return -1; |
| 396 |
} |
| 397 |
|
| 398 |
if (ast_db_get(args.family, args.key, buf, len - 1)) { |
| 399 |
ast_debug(1, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key); |
| 400 |
} else { |
| 401 |
if (ast_db_del(args.family, args.key)) { |
| 402 |
ast_debug(1, "DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
pbx_builtin_setvar_helper(chan, "DB_RESULT", buf); |
| 407 |
|
| 408 |
return 0; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
static int function_db_delete_write(struct ast_channel *chan, const char *cmd, char *parse, |
| 416 |
const char *value) |
| 417 |
{ |
| 418 |
|
| 419 |
char buf[128]; |
| 420 |
return function_db_delete(chan, cmd, parse, buf, sizeof(buf)); |
| 421 |
} |
| 422 |
|
| 423 |
static struct ast_custom_function db_delete_function = { |
| 424 |
.name = "DB_DELETE", |
| 425 |
.read = function_db_delete, |
| 426 |
.write = function_db_delete_write, |
| 427 |
}; |
| 428 |
|
| 429 |
static int unload_module(void) |
| 430 |
{ |
| 431 |
int res = 0; |
| 432 |
|
| 433 |
res |= ast_custom_function_unregister(&db_function); |
| 434 |
res |= ast_custom_function_unregister(&db_exists_function); |
| 435 |
res |= ast_custom_function_unregister(&db_delete_function); |
| 436 |
res |= ast_custom_function_unregister(&db_keys_function); |
| 437 |
res |= ast_custom_function_unregister(&db_keycount_function); |
| 438 |
|
| 439 |
return res; |
| 440 |
} |
| 441 |
|
| 442 |
static int load_module(void) |
| 443 |
{ |
| 444 |
int res = 0; |
| 445 |
|
| 446 |
res |= ast_custom_function_register_escalating(&db_function, AST_CFE_BOTH); |
| 447 |
res |= ast_custom_function_register(&db_exists_function); |
| 448 |
res |= ast_custom_function_register_escalating(&db_delete_function, AST_CFE_READ); |
| 449 |
res |= ast_custom_function_register(&db_keys_function); |
| 450 |
res |= ast_custom_function_register(&db_keycount_function); |
| 451 |
|
| 452 |
return res; |
| 453 |
} |
| 454 |
|
| 455 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Database (astdb) related dialplan functions"); |