| 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 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#include "asterisk.h" |
| 56 |
|
| 57 |
#include "asterisk/app.h" |
| 58 |
#include "asterisk/mwi.h" |
| 59 |
#include "asterisk/module.h" |
| 60 |
#include "asterisk/res_mwi_external.h" |
| 61 |
#include "asterisk/sorcery.h" |
| 62 |
#include "asterisk/cli.h" |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
#define MWI_ASTDB_PREFIX "mwi_external" |
| 73 |
#define MWI_MAILBOX_TYPE "mailboxes" |
| 74 |
|
| 75 |
struct ast_mwi_mailbox_object { |
| 76 |
SORCERY_OBJECT(details); |
| 77 |
|
| 78 |
unsigned int msgs_new; |
| 79 |
|
| 80 |
unsigned int msgs_old; |
| 81 |
}; |
| 82 |
|
| 83 |
static struct ast_sorcery *mwi_sorcery; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
static void mwi_post_event(const struct ast_mwi_mailbox_object *mailbox) |
| 91 |
{ |
| 92 |
ast_publish_mwi_state(ast_sorcery_object_get_id(mailbox), NULL, |
| 93 |
mailbox->msgs_new, mailbox->msgs_old); |
| 94 |
} |
| 95 |
|
| 96 |
static void mwi_observe_update(const void *obj) |
| 97 |
{ |
| 98 |
mwi_post_event(obj); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
static void mwi_observe_delete(const void *obj) |
| 107 |
{ |
| 108 |
const struct ast_mwi_mailbox_object *mailbox = obj; |
| 109 |
|
| 110 |
if (mailbox->msgs_new || mailbox->msgs_old) { |
| 111 |
|
| 112 |
ast_publish_mwi_state(ast_sorcery_object_get_id(mailbox), NULL, 0, 0); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
ast_delete_mwi_state(ast_sorcery_object_get_id(mailbox), NULL); |
| 117 |
} |
| 118 |
|
| 119 |
static const struct ast_sorcery_observer mwi_observers = { |
| 120 |
.created = mwi_observe_update, |
| 121 |
.updated = mwi_observe_update, |
| 122 |
.deleted = mwi_observe_delete, |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
static void *mwi_sorcery_object_alloc(const char *id) |
| 127 |
{ |
| 128 |
return ast_sorcery_generic_alloc(sizeof(struct ast_mwi_mailbox_object), NULL); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
static int mwi_sorcery_init(void) |
| 140 |
{ |
| 141 |
int res; |
| 142 |
|
| 143 |
mwi_sorcery = ast_sorcery_open(); |
| 144 |
if (!mwi_sorcery) { |
| 145 |
ast_log(LOG_ERROR, "MWI external: Sorcery failed to open.\n"); |
| 146 |
return -1; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
if (ast_sorcery_apply_default(mwi_sorcery, MWI_MAILBOX_TYPE, "astdb", |
| 151 |
MWI_ASTDB_PREFIX) == AST_SORCERY_APPLY_FAIL) { |
| 152 |
ast_log(LOG_ERROR, "MWI external: Sorcery could not setup wizards.\n"); |
| 153 |
return -1; |
| 154 |
} |
| 155 |
|
| 156 |
res = ast_sorcery_object_register(mwi_sorcery, MWI_MAILBOX_TYPE, |
| 157 |
mwi_sorcery_object_alloc, NULL, NULL); |
| 158 |
if (res) { |
| 159 |
ast_log(LOG_ERROR, "MWI external: Sorcery could not register object type '%s'.\n", |
| 160 |
MWI_MAILBOX_TYPE); |
| 161 |
return -1; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
res |= ast_sorcery_object_field_register_nodoc(mwi_sorcery, MWI_MAILBOX_TYPE, |
| 166 |
"msgs_new", "0", OPT_UINT_T, 0, FLDSET(struct ast_mwi_mailbox_object, msgs_new)); |
| 167 |
res |= ast_sorcery_object_field_register_nodoc(mwi_sorcery, MWI_MAILBOX_TYPE, |
| 168 |
"msgs_old", "0", OPT_UINT_T, 0, FLDSET(struct ast_mwi_mailbox_object, msgs_old)); |
| 169 |
return res ? -1 : 0; |
| 170 |
} |
| 171 |
|
| 172 |
struct ao2_container *ast_mwi_mailbox_get_all(void) |
| 173 |
{ |
| 174 |
return ast_sorcery_retrieve_by_fields(mwi_sorcery, MWI_MAILBOX_TYPE, |
| 175 |
AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); |
| 176 |
} |
| 177 |
|
| 178 |
struct ao2_container *ast_mwi_mailbox_get_by_regex(const char *regex) |
| 179 |
{ |
| 180 |
return ast_sorcery_retrieve_by_regex(mwi_sorcery, MWI_MAILBOX_TYPE, regex ?: ""); |
| 181 |
} |
| 182 |
|
| 183 |
const struct ast_mwi_mailbox_object *ast_mwi_mailbox_get(const char *mailbox_id) |
| 184 |
{ |
| 185 |
if (ast_strlen_zero(mailbox_id)) { |
| 186 |
return NULL; |
| 187 |
} |
| 188 |
|
| 189 |
return ast_sorcery_retrieve_by_id(mwi_sorcery, MWI_MAILBOX_TYPE, mailbox_id); |
| 190 |
} |
| 191 |
|
| 192 |
struct ast_mwi_mailbox_object *ast_mwi_mailbox_alloc(const char *mailbox_id) |
| 193 |
{ |
| 194 |
if (ast_strlen_zero(mailbox_id)) { |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
|
| 198 |
return ast_sorcery_alloc(mwi_sorcery, MWI_MAILBOX_TYPE, mailbox_id); |
| 199 |
} |
| 200 |
|
| 201 |
struct ast_mwi_mailbox_object *ast_mwi_mailbox_copy(const struct ast_mwi_mailbox_object *mailbox) |
| 202 |
{ |
| 203 |
return ast_sorcery_copy(mwi_sorcery, mailbox); |
| 204 |
} |
| 205 |
|
| 206 |
const char *ast_mwi_mailbox_get_id(const struct ast_mwi_mailbox_object *mailbox) |
| 207 |
{ |
| 208 |
return ast_sorcery_object_get_id(mailbox); |
| 209 |
} |
| 210 |
|
| 211 |
unsigned int ast_mwi_mailbox_get_msgs_new(const struct ast_mwi_mailbox_object *mailbox) |
| 212 |
{ |
| 213 |
return mailbox->msgs_new; |
| 214 |
} |
| 215 |
|
| 216 |
unsigned int ast_mwi_mailbox_get_msgs_old(const struct ast_mwi_mailbox_object *mailbox) |
| 217 |
{ |
| 218 |
return mailbox->msgs_old; |
| 219 |
} |
| 220 |
|
| 221 |
void ast_mwi_mailbox_set_msgs_new(struct ast_mwi_mailbox_object *mailbox, unsigned int num_msgs) |
| 222 |
{ |
| 223 |
mailbox->msgs_new = num_msgs; |
| 224 |
} |
| 225 |
|
| 226 |
void ast_mwi_mailbox_set_msgs_old(struct ast_mwi_mailbox_object *mailbox, unsigned int num_msgs) |
| 227 |
{ |
| 228 |
mailbox->msgs_old = num_msgs; |
| 229 |
} |
| 230 |
|
| 231 |
int ast_mwi_mailbox_update(struct ast_mwi_mailbox_object *mailbox) |
| 232 |
{ |
| 233 |
const struct ast_mwi_mailbox_object *exists; |
| 234 |
int res; |
| 235 |
|
| 236 |
exists = ast_sorcery_retrieve_by_id(mwi_sorcery, MWI_MAILBOX_TYPE, |
| 237 |
ast_sorcery_object_get_id(mailbox)); |
| 238 |
if (exists) { |
| 239 |
res = ast_sorcery_update(mwi_sorcery, mailbox); |
| 240 |
ast_mwi_mailbox_unref(exists); |
| 241 |
} else { |
| 242 |
res = ast_sorcery_create(mwi_sorcery, mailbox); |
| 243 |
} |
| 244 |
return res; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
static void mwi_mailbox_delete(struct ast_mwi_mailbox_object *mailbox) |
| 255 |
{ |
| 256 |
ast_sorcery_delete(mwi_sorcery, mailbox); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
static void mwi_mailbox_delete_all(struct ao2_container *mailboxes) |
| 267 |
{ |
| 268 |
struct ast_mwi_mailbox_object *mailbox; |
| 269 |
struct ao2_iterator iter; |
| 270 |
|
| 271 |
iter = ao2_iterator_init(mailboxes, AO2_ITERATOR_UNLINK); |
| 272 |
for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) { |
| 273 |
mwi_mailbox_delete(mailbox); |
| 274 |
} |
| 275 |
ao2_iterator_destroy(&iter); |
| 276 |
} |
| 277 |
|
| 278 |
int ast_mwi_mailbox_delete_all(void) |
| 279 |
{ |
| 280 |
struct ao2_container *mailboxes; |
| 281 |
|
| 282 |
mailboxes = ast_mwi_mailbox_get_all(); |
| 283 |
if (mailboxes) { |
| 284 |
mwi_mailbox_delete_all(mailboxes); |
| 285 |
ao2_ref(mailboxes, -1); |
| 286 |
} |
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
int ast_mwi_mailbox_delete_by_regex(const char *regex) |
| 291 |
{ |
| 292 |
struct ao2_container *mailboxes; |
| 293 |
|
| 294 |
mailboxes = ast_mwi_mailbox_get_by_regex(regex); |
| 295 |
if (mailboxes) { |
| 296 |
mwi_mailbox_delete_all(mailboxes); |
| 297 |
ao2_ref(mailboxes, -1); |
| 298 |
} |
| 299 |
return 0; |
| 300 |
} |
| 301 |
|
| 302 |
int ast_mwi_mailbox_delete(const char *mailbox_id) |
| 303 |
{ |
| 304 |
const struct ast_mwi_mailbox_object *mailbox; |
| 305 |
|
| 306 |
if (ast_strlen_zero(mailbox_id)) { |
| 307 |
return -1; |
| 308 |
} |
| 309 |
|
| 310 |
mailbox = ast_mwi_mailbox_get(mailbox_id); |
| 311 |
if (mailbox) { |
| 312 |
mwi_mailbox_delete((struct ast_mwi_mailbox_object *) mailbox); |
| 313 |
ast_mwi_mailbox_unref(mailbox); |
| 314 |
} |
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
enum folder_map { |
| 319 |
FOLDER_INVALID = 0, |
| 320 |
FOLDER_INBOX = 1, |
| 321 |
FOLDER_OLD = 2, |
| 322 |
}; |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
static enum folder_map mwi_folder_map(const char *folder) |
| 334 |
{ |
| 335 |
enum folder_map which_folder; |
| 336 |
|
| 337 |
if (ast_strlen_zero(folder) || !strcasecmp(folder, "INBOX")) { |
| 338 |
which_folder = FOLDER_INBOX; |
| 339 |
} else if (!strcasecmp(folder, "Old")) { |
| 340 |
which_folder = FOLDER_OLD; |
| 341 |
} else { |
| 342 |
which_folder = FOLDER_INVALID; |
| 343 |
} |
| 344 |
return which_folder; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
static int mwi_messagecount(const char *mailbox_id, const char *folder) |
| 358 |
{ |
| 359 |
const struct ast_mwi_mailbox_object *mailbox; |
| 360 |
int num_msgs; |
| 361 |
enum folder_map which_folder; |
| 362 |
|
| 363 |
which_folder = mwi_folder_map(folder); |
| 364 |
if (which_folder == FOLDER_INVALID) { |
| 365 |
return 0; |
| 366 |
} |
| 367 |
|
| 368 |
mailbox = ast_mwi_mailbox_get(mailbox_id); |
| 369 |
if (!mailbox) { |
| 370 |
return 0; |
| 371 |
} |
| 372 |
num_msgs = 0; |
| 373 |
switch (which_folder) { |
| 374 |
case FOLDER_INVALID: |
| 375 |
break; |
| 376 |
case FOLDER_INBOX: |
| 377 |
num_msgs = mailbox->msgs_new; |
| 378 |
break; |
| 379 |
case FOLDER_OLD: |
| 380 |
num_msgs = mailbox->msgs_old; |
| 381 |
break; |
| 382 |
} |
| 383 |
ast_mwi_mailbox_unref(mailbox); |
| 384 |
|
| 385 |
return num_msgs; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
static int mwi_has_voicemail(const char *mailboxes, const char *folder) |
| 400 |
{ |
| 401 |
char *parse; |
| 402 |
char *mailbox_id; |
| 403 |
enum folder_map which_folder; |
| 404 |
|
| 405 |
which_folder = mwi_folder_map(folder); |
| 406 |
if (which_folder == FOLDER_INVALID) { |
| 407 |
return 0; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
parse = ast_strdupa(mailboxes); |
| 412 |
while ((mailbox_id = strsep(&parse, ",&"))) { |
| 413 |
const struct ast_mwi_mailbox_object *mailbox; |
| 414 |
int num_msgs; |
| 415 |
|
| 416 |
|
| 417 |
mailbox = ast_mwi_mailbox_get(mailbox_id); |
| 418 |
if (!mailbox) { |
| 419 |
continue; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
num_msgs = 0; |
| 424 |
switch (which_folder) { |
| 425 |
case FOLDER_INVALID: |
| 426 |
break; |
| 427 |
case FOLDER_INBOX: |
| 428 |
num_msgs = mailbox->msgs_new; |
| 429 |
break; |
| 430 |
case FOLDER_OLD: |
| 431 |
num_msgs = mailbox->msgs_old; |
| 432 |
break; |
| 433 |
} |
| 434 |
ast_mwi_mailbox_unref(mailbox); |
| 435 |
if (num_msgs) { |
| 436 |
return 1; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
return 0; |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
static int mwi_inboxcount(const char *mailboxes, int *newmsgs, int *oldmsgs) |
| 460 |
{ |
| 461 |
char *parse; |
| 462 |
char *mailbox_id; |
| 463 |
|
| 464 |
if (!newmsgs && !oldmsgs) { |
| 465 |
|
| 466 |
return 0; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
parse = ast_strdupa(mailboxes); |
| 471 |
while ((mailbox_id = strsep(&parse, ", "))) { |
| 472 |
const struct ast_mwi_mailbox_object *mailbox; |
| 473 |
|
| 474 |
|
| 475 |
mailbox = ast_mwi_mailbox_get(mailbox_id); |
| 476 |
if (!mailbox) { |
| 477 |
continue; |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
if (newmsgs) { |
| 482 |
*newmsgs += mailbox->msgs_new; |
| 483 |
} |
| 484 |
if (oldmsgs) { |
| 485 |
*oldmsgs += mailbox->msgs_old; |
| 486 |
} |
| 487 |
|
| 488 |
ast_mwi_mailbox_unref(mailbox); |
| 489 |
} |
| 490 |
|
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
static int mwi_inboxcount2(const char *mailboxes, int *urgentmsgs, int *newmsgs, int *oldmsgs) |
| 513 |
{ |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
return mwi_inboxcount(mailboxes, newmsgs, oldmsgs); |
| 519 |
} |
| 520 |
|
| 521 |
static const struct ast_vm_functions vm_table = { |
| 522 |
.module_version = VM_MODULE_VERSION, |
| 523 |
.module_name = AST_MODULE, |
| 524 |
|
| 525 |
.has_voicemail = mwi_has_voicemail, |
| 526 |
.inboxcount = mwi_inboxcount, |
| 527 |
.inboxcount2 = mwi_inboxcount2, |
| 528 |
.messagecount = mwi_messagecount, |
| 529 |
}; |
| 530 |
|
| 531 |
#if defined(MWI_DEBUG_CLI) |
| 532 |
static char *complete_mailbox(const char *word, int state) |
| 533 |
{ |
| 534 |
struct ao2_iterator iter; |
| 535 |
int wordlen = strlen(word); |
| 536 |
int which = 0; |
| 537 |
char *ret = NULL; |
| 538 |
char *regex; |
| 539 |
const struct ast_mwi_mailbox_object *mailbox; |
| 540 |
RAII_VAR(struct ao2_container *, mailboxes, NULL, ao2_cleanup); |
| 541 |
|
| 542 |
regex = ast_alloca(2 + wordlen); |
| 543 |
sprintf(regex, "^%s", word); |
| 544 |
|
| 545 |
mailboxes = ast_mwi_mailbox_get_by_regex(regex); |
| 546 |
if (!mailboxes) { |
| 547 |
return NULL; |
| 548 |
} |
| 549 |
|
| 550 |
iter = ao2_iterator_init(mailboxes, 0); |
| 551 |
for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) { |
| 552 |
if (++which > state) { |
| 553 |
ret = ast_strdup(ast_sorcery_object_get_id(mailbox)); |
| 554 |
ast_mwi_mailbox_unref(mailbox); |
| 555 |
break; |
| 556 |
} |
| 557 |
} |
| 558 |
ao2_iterator_destroy(&iter); |
| 559 |
|
| 560 |
return ret; |
| 561 |
} |
| 562 |
#endif |
| 563 |
|
| 564 |
#if defined(MWI_DEBUG_CLI) |
| 565 |
static char *handle_mwi_delete_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 566 |
{ |
| 567 |
switch (cmd) { |
| 568 |
case CLI_INIT: |
| 569 |
e->command = "mwi delete all"; |
| 570 |
e->usage = |
| 571 |
"Usage: mwi delete all\n" |
| 572 |
" Delete all external MWI mailboxes.\n"; |
| 573 |
return NULL; |
| 574 |
case CLI_GENERATE: |
| 575 |
return NULL; |
| 576 |
} |
| 577 |
|
| 578 |
ast_mwi_mailbox_delete_all(); |
| 579 |
ast_cli(a->fd, "Deleted all external MWI mailboxes.\n"); |
| 580 |
return CLI_SUCCESS; |
| 581 |
} |
| 582 |
#endif |
| 583 |
|
| 584 |
#if defined(MWI_DEBUG_CLI) |
| 585 |
static char *handle_mwi_delete_like(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 586 |
{ |
| 587 |
const char *regex; |
| 588 |
|
| 589 |
switch (cmd) { |
| 590 |
case CLI_INIT: |
| 591 |
e->command = "mwi delete like"; |
| 592 |
e->usage = |
| 593 |
"Usage: mwi delete like <pattern>\n" |
| 594 |
" Delete external MWI mailboxes matching a regular expression.\n"; |
| 595 |
return NULL; |
| 596 |
case CLI_GENERATE: |
| 597 |
return NULL; |
| 598 |
} |
| 599 |
|
| 600 |
if (a->argc != 4) { |
| 601 |
return CLI_SHOWUSAGE; |
| 602 |
} |
| 603 |
regex = a->argv[3]; |
| 604 |
|
| 605 |
ast_mwi_mailbox_delete_by_regex(regex); |
| 606 |
ast_cli(a->fd, "Deleted external MWI mailboxes matching '%s'.\n", regex); |
| 607 |
return CLI_SUCCESS; |
| 608 |
} |
| 609 |
#endif |
| 610 |
|
| 611 |
#if defined(MWI_DEBUG_CLI) |
| 612 |
static char *handle_mwi_delete_mailbox(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 613 |
{ |
| 614 |
const char *mailbox_id; |
| 615 |
|
| 616 |
switch (cmd) { |
| 617 |
case CLI_INIT: |
| 618 |
e->command = "mwi delete mailbox"; |
| 619 |
e->usage = |
| 620 |
"Usage: mwi delete mailbox <mailbox_id>\n" |
| 621 |
" Delete a specific external MWI mailbox.\n"; |
| 622 |
return NULL; |
| 623 |
case CLI_GENERATE: |
| 624 |
if (a->pos == 3) { |
| 625 |
return complete_mailbox(a->word, a->n); |
| 626 |
} |
| 627 |
return NULL; |
| 628 |
} |
| 629 |
|
| 630 |
if (a->argc != 4) { |
| 631 |
return CLI_SHOWUSAGE; |
| 632 |
} |
| 633 |
mailbox_id = a->argv[3]; |
| 634 |
|
| 635 |
ast_mwi_mailbox_delete(mailbox_id); |
| 636 |
ast_cli(a->fd, "Deleted external MWI mailbox '%s'.\n", mailbox_id); |
| 637 |
|
| 638 |
return CLI_SUCCESS; |
| 639 |
} |
| 640 |
#endif |
| 641 |
|
| 642 |
#define FORMAT_MAILBOX_HDR "%6s %6s %s\n" |
| 643 |
#define FORMAT_MAILBOX_ROW "%6u %6u %s\n" |
| 644 |
|
| 645 |
#if defined(MWI_DEBUG_CLI) |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
static void mwi_cli_print_mailbox(int cli_fd, const struct ast_mwi_mailbox_object *mailbox) |
| 655 |
{ |
| 656 |
ast_cli(cli_fd, FORMAT_MAILBOX_ROW, mailbox->msgs_new, mailbox->msgs_old, |
| 657 |
ast_sorcery_object_get_id(mailbox)); |
| 658 |
} |
| 659 |
#endif |
| 660 |
|
| 661 |
#if defined(MWI_DEBUG_CLI) |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
static void mwi_cli_list_mailboxes(int cli_fd, struct ao2_container *mailboxes) |
| 671 |
{ |
| 672 |
struct ao2_iterator iter; |
| 673 |
const struct ast_mwi_mailbox_object *mailbox; |
| 674 |
|
| 675 |
ast_cli(cli_fd, FORMAT_MAILBOX_HDR, "New", "Old", "Mailbox"); |
| 676 |
|
| 677 |
iter = ao2_iterator_init(mailboxes, 0); |
| 678 |
for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) { |
| 679 |
mwi_cli_print_mailbox(cli_fd, mailbox); |
| 680 |
} |
| 681 |
ao2_iterator_destroy(&iter); |
| 682 |
} |
| 683 |
#endif |
| 684 |
|
| 685 |
#undef FORMAT_MAILBOX_HDR |
| 686 |
#undef FORMAT_MAILBOX_ROW |
| 687 |
|
| 688 |
#if defined(MWI_DEBUG_CLI) |
| 689 |
static char *handle_mwi_list_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 690 |
{ |
| 691 |
struct ao2_container *mailboxes; |
| 692 |
|
| 693 |
switch (cmd) { |
| 694 |
case CLI_INIT: |
| 695 |
e->command = "mwi list all"; |
| 696 |
e->usage = |
| 697 |
"Usage: mwi list all\n" |
| 698 |
" List all external MWI mailboxes.\n"; |
| 699 |
return NULL; |
| 700 |
case CLI_GENERATE: |
| 701 |
return NULL; |
| 702 |
} |
| 703 |
|
| 704 |
mailboxes = ast_mwi_mailbox_get_all(); |
| 705 |
if (!mailboxes) { |
| 706 |
ast_cli(a->fd, "Failed to retrieve external MWI mailboxes.\n"); |
| 707 |
return CLI_SUCCESS; |
| 708 |
} |
| 709 |
mwi_cli_list_mailboxes(a->fd, mailboxes); |
| 710 |
ao2_ref(mailboxes, -1); |
| 711 |
return CLI_SUCCESS; |
| 712 |
} |
| 713 |
#endif |
| 714 |
|
| 715 |
#if defined(MWI_DEBUG_CLI) |
| 716 |
static char *handle_mwi_list_like(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 717 |
{ |
| 718 |
struct ao2_container *mailboxes; |
| 719 |
const char *regex; |
| 720 |
|
| 721 |
switch (cmd) { |
| 722 |
case CLI_INIT: |
| 723 |
e->command = "mwi list like"; |
| 724 |
e->usage = |
| 725 |
"Usage: mwi list like <pattern>\n" |
| 726 |
" List external MWI mailboxes matching a regular expression.\n"; |
| 727 |
return NULL; |
| 728 |
case CLI_GENERATE: |
| 729 |
return NULL; |
| 730 |
} |
| 731 |
|
| 732 |
if (a->argc != 4) { |
| 733 |
return CLI_SHOWUSAGE; |
| 734 |
} |
| 735 |
regex = a->argv[3]; |
| 736 |
|
| 737 |
mailboxes = ast_mwi_mailbox_get_by_regex(regex); |
| 738 |
if (!mailboxes) { |
| 739 |
ast_cli(a->fd, "Failed to retrieve external MWI mailboxes.\n"); |
| 740 |
return CLI_SUCCESS; |
| 741 |
} |
| 742 |
mwi_cli_list_mailboxes(a->fd, mailboxes); |
| 743 |
ao2_ref(mailboxes, -1); |
| 744 |
return CLI_SUCCESS; |
| 745 |
} |
| 746 |
#endif |
| 747 |
|
| 748 |
#if defined(MWI_DEBUG_CLI) |
| 749 |
static char *handle_mwi_show_mailbox(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 750 |
{ |
| 751 |
const struct ast_mwi_mailbox_object *mailbox; |
| 752 |
const char *mailbox_id; |
| 753 |
|
| 754 |
switch (cmd) { |
| 755 |
case CLI_INIT: |
| 756 |
e->command = "mwi show mailbox"; |
| 757 |
e->usage = |
| 758 |
"Usage: mwi show mailbox <mailbox_id>\n" |
| 759 |
" Show a specific external MWI mailbox.\n"; |
| 760 |
return NULL; |
| 761 |
case CLI_GENERATE: |
| 762 |
if (a->pos == 3) { |
| 763 |
return complete_mailbox(a->word, a->n); |
| 764 |
} |
| 765 |
return NULL; |
| 766 |
} |
| 767 |
|
| 768 |
if (a->argc != 4) { |
| 769 |
return CLI_SHOWUSAGE; |
| 770 |
} |
| 771 |
mailbox_id = a->argv[3]; |
| 772 |
|
| 773 |
mailbox = ast_mwi_mailbox_get(mailbox_id); |
| 774 |
if (mailbox) { |
| 775 |
ast_cli(a->fd, |
| 776 |
"Mailbox: %s\n" |
| 777 |
"NewMessages: %u\n" |
| 778 |
"OldMessages: %u\n", |
| 779 |
ast_sorcery_object_get_id(mailbox), |
| 780 |
mailbox->msgs_new, |
| 781 |
mailbox->msgs_old); |
| 782 |
|
| 783 |
ast_mwi_mailbox_unref(mailbox); |
| 784 |
} else { |
| 785 |
ast_cli(a->fd, "External MWI mailbox '%s' not found.\n", mailbox_id); |
| 786 |
} |
| 787 |
|
| 788 |
return CLI_SUCCESS; |
| 789 |
} |
| 790 |
#endif |
| 791 |
|
| 792 |
#if defined(MWI_DEBUG_CLI) |
| 793 |
static char *handle_mwi_update_mailbox(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 794 |
{ |
| 795 |
struct ast_mwi_mailbox_object *mailbox; |
| 796 |
const char *mailbox_id; |
| 797 |
unsigned int num_new; |
| 798 |
unsigned int num_old; |
| 799 |
|
| 800 |
switch (cmd) { |
| 801 |
case CLI_INIT: |
| 802 |
e->command = "mwi update mailbox"; |
| 803 |
e->usage = |
| 804 |
"Usage: mwi update mailbox <mailbox_id> [<new> [<old>]]\n" |
| 805 |
" Update a specific external MWI mailbox.\n"; |
| 806 |
return NULL; |
| 807 |
case CLI_GENERATE: |
| 808 |
if (a->pos == 3) { |
| 809 |
return complete_mailbox(a->word, a->n); |
| 810 |
} |
| 811 |
return NULL; |
| 812 |
} |
| 813 |
|
| 814 |
if (a->argc < 4 || 6 < a->argc) { |
| 815 |
return CLI_SHOWUSAGE; |
| 816 |
} |
| 817 |
mailbox_id = a->argv[3]; |
| 818 |
|
| 819 |
num_new = 0; |
| 820 |
if (4 < a->argc) { |
| 821 |
const char *count_new = a->argv[4]; |
| 822 |
|
| 823 |
if (sscanf(count_new, "%u", &num_new) != 1) { |
| 824 |
ast_cli(a->fd, "Invalid NewMessages: '%s'.\n", count_new); |
| 825 |
return CLI_SHOWUSAGE; |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
num_old = 0; |
| 830 |
if (5 < a->argc) { |
| 831 |
const char *count_old = a->argv[5]; |
| 832 |
|
| 833 |
if (sscanf(count_old, "%u", &num_old) != 1) { |
| 834 |
ast_cli(a->fd, "Invalid OldMessages: '%s'.\n", count_old); |
| 835 |
return CLI_SHOWUSAGE; |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
mailbox = ast_mwi_mailbox_alloc(mailbox_id); |
| 840 |
if (mailbox) { |
| 841 |
ast_mwi_mailbox_set_msgs_new(mailbox, num_new); |
| 842 |
ast_mwi_mailbox_set_msgs_old(mailbox, num_old); |
| 843 |
if (ast_mwi_mailbox_update(mailbox)) { |
| 844 |
ast_cli(a->fd, "Could not update mailbox %s.\n", |
| 845 |
ast_sorcery_object_get_id(mailbox)); |
| 846 |
} else { |
| 847 |
ast_cli(a->fd, "Updated mailbox %s.\n", ast_sorcery_object_get_id(mailbox)); |
| 848 |
} |
| 849 |
|
| 850 |
ast_mwi_mailbox_unref(mailbox); |
| 851 |
} |
| 852 |
|
| 853 |
return CLI_SUCCESS; |
| 854 |
} |
| 855 |
#endif |
| 856 |
|
| 857 |
#if defined(MWI_DEBUG_CLI) |
| 858 |
static struct ast_cli_entry mwi_cli[] = { |
| 859 |
AST_CLI_DEFINE(handle_mwi_delete_all, "Delete all external MWI mailboxes"), |
| 860 |
AST_CLI_DEFINE(handle_mwi_delete_like, "Delete external MWI mailboxes matching regex"), |
| 861 |
AST_CLI_DEFINE(handle_mwi_delete_mailbox, "Delete a specific external MWI mailbox"), |
| 862 |
AST_CLI_DEFINE(handle_mwi_list_all, "List all external MWI mailboxes"), |
| 863 |
AST_CLI_DEFINE(handle_mwi_list_like, "List external MWI mailboxes matching regex"), |
| 864 |
AST_CLI_DEFINE(handle_mwi_show_mailbox, "Show a specific external MWI mailbox"), |
| 865 |
AST_CLI_DEFINE(handle_mwi_update_mailbox, "Update a specific external MWI mailbox"), |
| 866 |
}; |
| 867 |
#endif |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
static void mwi_initial_events(void) |
| 875 |
{ |
| 876 |
struct ao2_container *mailboxes; |
| 877 |
const struct ast_mwi_mailbox_object *mailbox; |
| 878 |
struct ao2_iterator iter; |
| 879 |
|
| 880 |
|
| 881 |
mailboxes = ast_mwi_mailbox_get_all(); |
| 882 |
if (!mailboxes) { |
| 883 |
return; |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
iter = ao2_iterator_init(mailboxes, AO2_ITERATOR_UNLINK); |
| 888 |
for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) { |
| 889 |
mwi_post_event(mailbox); |
| 890 |
} |
| 891 |
ao2_iterator_destroy(&iter); |
| 892 |
|
| 893 |
ao2_ref(mailboxes, -1); |
| 894 |
} |
| 895 |
|
| 896 |
static int unload_module(void) |
| 897 |
{ |
| 898 |
ast_vm_unregister(vm_table.module_name); |
| 899 |
#if defined(MWI_DEBUG_CLI) |
| 900 |
ast_cli_unregister_multiple(mwi_cli, ARRAY_LEN(mwi_cli)); |
| 901 |
#endif |
| 902 |
ast_sorcery_observer_remove(mwi_sorcery, MWI_MAILBOX_TYPE, &mwi_observers); |
| 903 |
|
| 904 |
ast_sorcery_unref(mwi_sorcery); |
| 905 |
mwi_sorcery = NULL; |
| 906 |
|
| 907 |
return 0; |
| 908 |
} |
| 909 |
|
| 910 |
static int load_module(void) |
| 911 |
{ |
| 912 |
int res; |
| 913 |
|
| 914 |
if (mwi_sorcery_init() |
| 915 |
|| ast_sorcery_observer_add(mwi_sorcery, MWI_MAILBOX_TYPE, &mwi_observers) |
| 916 |
#if defined(MWI_DEBUG_CLI) |
| 917 |
|| ast_cli_register_multiple(mwi_cli, ARRAY_LEN(mwi_cli)) |
| 918 |
#endif |
| 919 |
) { |
| 920 |
unload_module(); |
| 921 |
return AST_MODULE_LOAD_DECLINE; |
| 922 |
} |
| 923 |
|
| 924 |
|
| 925 |
res = ast_vm_register(&vm_table); |
| 926 |
if (res) { |
| 927 |
ast_log(LOG_ERROR, "Failure registering as a voicemail provider\n"); |
| 928 |
unload_module(); |
| 929 |
return AST_MODULE_LOAD_DECLINE; |
| 930 |
} |
| 931 |
|
| 932 |
|
| 933 |
mwi_initial_events(); |
| 934 |
|
| 935 |
return AST_MODULE_LOAD_SUCCESS; |
| 936 |
} |
| 937 |
|
| 938 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Core external MWI resource", |
| 939 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 940 |
.load = load_module, |
| 941 |
.unload = unload_module, |
| 942 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND - 5, |
| 943 |
); |