00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 165255 $")
00031
00032 #include <string.h>
00033 #include <ctype.h>
00034 #include <stdlib.h>
00035 #include <stdio.h>
00036
00037 #include "asterisk/lock.h"
00038 #include "asterisk/file.h"
00039 #include "asterisk/logger.h"
00040 #include "asterisk/channel.h"
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/module.h"
00043 #include "asterisk/config.h"
00044 #include "asterisk/say.h"
00045 #include "asterisk/utils.h"
00046 #include "asterisk/app.h"
00047
00048 #ifdef ODBC_STORAGE
00049 #include <errno.h>
00050 #include <sys/mman.h>
00051 #include "asterisk/res_odbc.h"
00052
00053 static char odbc_database[80] = "asterisk";
00054 static char odbc_table[80] = "voicemessages";
00055 static char vmfmts[80] = "wav";
00056 #endif
00057
00058 static char *app = "Directory";
00059
00060 static char *synopsis = "Provide directory of voicemail extensions";
00061 static char *descrip =
00062 " Directory(vm-context[|dial-context[|options]]): This application will present\n"
00063 "the calling channel with a directory of extensions from which they can search\n"
00064 "by name. The list of names and corresponding extensions is retrieved from the\n"
00065 "voicemail configuration file, voicemail.conf.\n"
00066 " This application will immediately exit if one of the following DTMF digits are\n"
00067 "received and the extension to jump to exists:\n"
00068 " 0 - Jump to the 'o' extension, if it exists.\n"
00069 " * - Jump to the 'a' extension, if it exists.\n\n"
00070 " Parameters:\n"
00071 " vm-context - This is the context within voicemail.conf to use for the\n"
00072 " Directory.\n"
00073 " dial-context - This is the dialplan context to use when looking for an\n"
00074 " extension that the user has selected, or when jumping to the\n"
00075 " 'o' or 'a' extension.\n\n"
00076 " Options:\n"
00077 " e - In addition to the name, also read the extension number to the\n"
00078 " caller before presenting dialing options.\n"
00079 " f - Allow the caller to enter the first name of a user in the directory\n"
00080 " instead of using the last name.\n";
00081
00082
00083
00084
00085 #define VOICEMAIL_CONFIG "voicemail.conf"
00086
00087
00088 #define NUMDIGITS 3
00089
00090
00091 #ifdef ODBC_STORAGE
00092 struct generic_prepare_struct {
00093 const char *sql;
00094 const char *param;
00095 };
00096
00097 static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
00098 {
00099 struct generic_prepare_struct *gps = data;
00100 SQLHSTMT stmt;
00101 int res;
00102
00103 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
00104 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
00105 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
00106 return NULL;
00107 }
00108
00109 res = SQLPrepare(stmt, (unsigned char *)gps->sql, SQL_NTS);
00110 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
00111 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", (char *)gps->sql);
00112 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
00113 return NULL;
00114 }
00115
00116 if (!ast_strlen_zero(gps->param))
00117 SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(gps->param), 0, (void *)gps->param, 0, NULL);
00118
00119 return stmt;
00120 }
00121
00122 static void retrieve_file(char *dir)
00123 {
00124 int x = 0;
00125 int res;
00126 int fd=-1;
00127 size_t fdlen = 0;
00128 void *fdm = MAP_FAILED;
00129 SQLHSTMT stmt;
00130 char sql[256];
00131 char fmt[80]="", empty[10] = "";
00132 char *c;
00133 SQLLEN colsize;
00134 char full_fn[256];
00135 struct odbc_obj *obj;
00136 struct generic_prepare_struct gps = { .sql = sql, .param = dir };
00137
00138 obj = ast_odbc_request_obj(odbc_database, 1);
00139 if (obj) {
00140 do {
00141 ast_copy_string(fmt, vmfmts, sizeof(fmt));
00142 c = strchr(fmt, '|');
00143 if (c)
00144 *c = '\0';
00145 if (!strcasecmp(fmt, "wav49"))
00146 strcpy(fmt, "WAV");
00147 snprintf(full_fn, sizeof(full_fn), "%s.%s", dir, fmt);
00148 snprintf(sql, sizeof(sql), "SELECT recording FROM %s WHERE dir=? AND msgnum=-1", odbc_table);
00149 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, &gps);
00150
00151 if (!stmt) {
00152 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
00153 break;
00154 }
00155 res = SQLFetch(stmt);
00156 if (res == SQL_NO_DATA) {
00157 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
00158 break;
00159 } else if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
00160 ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
00161 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
00162 break;
00163 }
00164 fd = open(full_fn, O_RDWR | O_CREAT | O_TRUNC, 0770);
00165 if (fd < 0) {
00166 ast_log(LOG_WARNING, "Failed to write '%s': %s\n", full_fn, strerror(errno));
00167 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
00168 break;
00169 }
00170
00171 res = SQLGetData(stmt, 1, SQL_BINARY, empty, 0, &colsize);
00172 fdlen = colsize;
00173 if (fd > -1) {
00174 char tmp[1]="";
00175 lseek(fd, fdlen - 1, SEEK_SET);
00176 if (write(fd, tmp, 1) != 1) {
00177 close(fd);
00178 fd = -1;
00179 break;
00180 }
00181 if (fd > -1)
00182 fdm = mmap(NULL, fdlen, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
00183 }
00184 if (fdm != MAP_FAILED) {
00185 memset(fdm, 0, fdlen);
00186 res = SQLGetData(stmt, x + 1, SQL_BINARY, fdm, fdlen, &colsize);
00187 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
00188 ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
00189 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
00190 break;
00191 }
00192 }
00193 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
00194 } while (0);
00195 ast_odbc_release_obj(obj);
00196 } else
00197 ast_log(LOG_WARNING, "Failed to obtain database object for '%s'!\n", odbc_database);
00198 if (fdm != MAP_FAILED)
00199 munmap(fdm, fdlen);
00200 if (fd > -1)
00201 close(fd);
00202 return;
00203 }
00204 #endif
00205
00206 static char *convert(const char *lastname)
00207 {
00208 char *tmp;
00209 int lcount = 0;
00210 tmp = ast_malloc(NUMDIGITS + 1);
00211 if (tmp) {
00212 while((*lastname > 32) && lcount < NUMDIGITS) {
00213 switch(toupper(*lastname)) {
00214 case '1':
00215 tmp[lcount++] = '1';
00216 break;
00217 case '2':
00218 case 'A':
00219 case 'B':
00220 case 'C':
00221 tmp[lcount++] = '2';
00222 break;
00223 case '3':
00224 case 'D':
00225 case 'E':
00226 case 'F':
00227 tmp[lcount++] = '3';
00228 break;
00229 case '4':
00230 case 'G':
00231 case 'H':
00232 case 'I':
00233 tmp[lcount++] = '4';
00234 break;
00235 case '5':
00236 case 'J':
00237 case 'K':
00238 case 'L':
00239 tmp[lcount++] = '5';
00240 break;
00241 case '6':
00242 case 'M':
00243 case 'N':
00244 case 'O':
00245 tmp[lcount++] = '6';
00246 break;
00247 case '7':
00248 case 'P':
00249 case 'Q':
00250 case 'R':
00251 case 'S':
00252 tmp[lcount++] = '7';
00253 break;
00254 case '8':
00255 case 'T':
00256 case 'U':
00257 case 'V':
00258 tmp[lcount++] = '8';
00259 break;
00260 case '9':
00261 case 'W':
00262 case 'X':
00263 case 'Y':
00264 case 'Z':
00265 tmp[lcount++] = '9';
00266 break;
00267 }
00268 lastname++;
00269 }
00270 tmp[lcount] = '\0';
00271 }
00272 return tmp;
00273 }
00274
00275
00276
00277
00278
00279
00280 static int play_mailbox_owner(struct ast_channel *chan, char *context,
00281 char *dialcontext, char *ext, char *name, int readext,
00282 int fromappvm)
00283 {
00284 int res = 0;
00285 int loop;
00286 char fn[256];
00287
00288
00289 snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/greet",
00290 ast_config_AST_SPOOL_DIR, context, ext);
00291 #ifdef ODBC_STORAGE
00292 retrieve_file(fn);
00293 #endif
00294
00295 if (ast_fileexists(fn, NULL, chan->language) <= 0) {
00296
00297 snprintf(fn, sizeof(fn), "%s/vm/%s/greet",
00298 ast_config_AST_SPOOL_DIR, ext);
00299 }
00300 #ifdef ODBC_STORAGE
00301 retrieve_file(fn);
00302 #endif
00303
00304 if (ast_fileexists(fn, NULL, chan->language) > 0) {
00305 res = ast_stream_and_wait(chan, fn, chan->language, AST_DIGIT_ANY);
00306 ast_stopstream(chan);
00307
00308 if (readext) {
00309 ast_stream_and_wait(chan, "vm-extension", chan->language, AST_DIGIT_ANY);
00310 res = ast_say_character_str(chan, ext, AST_DIGIT_ANY, chan->language);
00311 }
00312 } else {
00313 res = ast_say_character_str(chan, S_OR(name, ext), AST_DIGIT_ANY, chan->language);
00314 if (!ast_strlen_zero(name) && readext) {
00315 ast_stream_and_wait(chan, "vm-extension", chan->language, AST_DIGIT_ANY);
00316 res = ast_say_character_str(chan, ext, AST_DIGIT_ANY, chan->language);
00317 }
00318 }
00319 #ifdef ODBC_STORAGE
00320 ast_filedelete(fn, NULL);
00321 #endif
00322
00323 for (loop = 3 ; loop > 0; loop--) {
00324 if (!res)
00325 res = ast_stream_and_wait(chan, "dir-instr", chan->language, AST_DIGIT_ANY);
00326 if (!res)
00327 res = ast_waitfordigit(chan, 3000);
00328 ast_stopstream(chan);
00329
00330 if (res < 0)
00331 break;
00332 if (res == '1') {
00333 if (fromappvm) {
00334
00335 ast_copy_string(chan->exten, ext, sizeof(chan->exten));
00336 } else {
00337 if (ast_goto_if_exists(chan, dialcontext, ext, 1)) {
00338 ast_log(LOG_WARNING,
00339 "Can't find extension '%s' in context '%s'. "
00340 "Did you pass the wrong context to Directory?\n",
00341 ext, dialcontext);
00342 res = -1;
00343 }
00344 }
00345 break;
00346 }
00347 if (res == '*')
00348 break;
00349
00350
00351 res = 0;
00352 }
00353
00354 return(res);
00355 }
00356
00357 static struct ast_config *realtime_directory(char *context)
00358 {
00359 struct ast_config *cfg;
00360 struct ast_config *rtdata;
00361 struct ast_category *cat;
00362 struct ast_variable *var;
00363 char *mailbox;
00364 const char *fullname;
00365 const char *hidefromdir;
00366 char tmp[100];
00367
00368
00369 cfg = ast_config_load(VOICEMAIL_CONFIG);
00370
00371 if (!cfg) {
00372
00373 ast_log(LOG_WARNING, "Loading config failed.\n");
00374 return NULL;
00375 }
00376
00377
00378
00379 rtdata = ast_load_realtime_multientry("voicemail", "mailbox LIKE", "%", "context", context, NULL);
00380
00381
00382 if (!rtdata)
00383 return cfg;
00384
00385
00386 cat = ast_category_get(cfg, context);
00387 if (!cat) {
00388 cat = ast_category_new(context);
00389 if (!cat) {
00390 ast_log(LOG_WARNING, "Out of memory\n");
00391 ast_config_destroy(cfg);
00392 if (rtdata) {
00393 ast_config_destroy(rtdata);
00394 }
00395 return NULL;
00396 }
00397 ast_category_append(cfg, cat);
00398 }
00399
00400 mailbox = NULL;
00401 while ( (mailbox = ast_category_browse(rtdata, mailbox)) ) {
00402 fullname = ast_variable_retrieve(rtdata, mailbox, "fullname");
00403 hidefromdir = ast_variable_retrieve(rtdata, mailbox, "hidefromdir");
00404 snprintf(tmp, sizeof(tmp), "no-password,%s,hidefromdir=%s",
00405 fullname ? fullname : "",
00406 hidefromdir ? hidefromdir : "no");
00407 var = ast_variable_new(mailbox, tmp);
00408 if (var)
00409 ast_variable_append(cat, var);
00410 else
00411 ast_log(LOG_WARNING, "Out of memory adding mailbox '%s'\n", mailbox);
00412 }
00413 ast_config_destroy(rtdata);
00414
00415 return cfg;
00416 }
00417
00418 static int do_directory(struct ast_channel *chan, struct ast_config *cfg, struct ast_config *ucfg, char *context, char *dialcontext, char digit, int last, int readext, int fromappvm)
00419 {
00420
00421 char ext[NUMDIGITS + 1], *cat;
00422 char name[80] = "";
00423 struct ast_variable *v;
00424 int res;
00425 int found=0;
00426 int lastuserchoice = 0;
00427 char *start, *conv, *stringp = NULL;
00428 const char *pos;
00429 int breakout = 0;
00430
00431 if (ast_strlen_zero(context)) {
00432 ast_log(LOG_WARNING,
00433 "Directory must be called with an argument "
00434 "(context in which to interpret extensions)\n");
00435 return -1;
00436 }
00437 if (digit == '0') {
00438 if (!ast_goto_if_exists(chan, dialcontext, "o", 1) ||
00439 (!ast_strlen_zero(chan->macrocontext) &&
00440 !ast_goto_if_exists(chan, chan->macrocontext, "o", 1))) {
00441 return 0;
00442 } else {
00443 ast_log(LOG_WARNING, "Can't find extension 'o' in current context. "
00444 "Not Exiting the Directory!\n");
00445 res = 0;
00446 }
00447 }
00448 if (digit == '*') {
00449 if (!ast_goto_if_exists(chan, dialcontext, "a", 1) ||
00450 (!ast_strlen_zero(chan->macrocontext) &&
00451 !ast_goto_if_exists(chan, chan->macrocontext, "a", 1))) {
00452 return 0;
00453 } else {
00454 ast_log(LOG_WARNING, "Can't find extension 'a' in current context. "
00455 "Not Exiting the Directory!\n");
00456 res = 0;
00457 }
00458 }
00459 memset(ext, 0, sizeof(ext));
00460 ext[0] = digit;
00461 res = 0;
00462 if (ast_readstring(chan, ext + 1, NUMDIGITS - 1, 3000, 3000, "#") < 0) res = -1;
00463 if (!res) {
00464
00465 v = ast_variable_browse(cfg, context);
00466 while(v && !res) {
00467
00468 while(v) {
00469
00470 start = strdup(v->value);
00471 if (start && !strcasestr(start, "hidefromdir=yes")) {
00472 stringp=start;
00473 strsep(&stringp, ",");
00474 pos = strsep(&stringp, ",");
00475 if (pos) {
00476 ast_copy_string(name, pos, sizeof(name));
00477
00478 if (last && strrchr(pos,' '))
00479 pos = strrchr(pos, ' ') + 1;
00480 conv = convert(pos);
00481 if (conv) {
00482 if (!strncmp(conv, ext, strlen(ext))) {
00483
00484 found++;
00485 free(conv);
00486 free(start);
00487 break;
00488 }
00489 free(conv);
00490 }
00491 }
00492 free(start);
00493 }
00494 v = v->next;
00495 }
00496
00497 if (v) {
00498
00499 res = play_mailbox_owner(chan, context, dialcontext, v->name, name, readext, fromappvm);
00500 switch (res) {
00501 case -1:
00502
00503
00504
00505 lastuserchoice = 0;
00506 break;
00507 case '1':
00508
00509
00510
00511
00512 lastuserchoice = res;
00513 break;
00514 case '*':
00515
00516 lastuserchoice = res;
00517 res = 0;
00518 break;
00519 default:
00520 break;
00521 }
00522 v = v->next;
00523 }
00524 }
00525
00526 if (!res && ucfg) {
00527
00528 for (cat = ast_category_browse(ucfg, NULL); cat && !res ; cat = ast_category_browse(ucfg, cat)) {
00529 if (!strcasecmp(cat, "general"))
00530 continue;
00531 if (!ast_true(ast_config_option(ucfg, cat, "hasdirectory")))
00532 continue;
00533
00534
00535 if ((pos = ast_variable_retrieve(ucfg, cat, "fullname"))) {
00536 ast_copy_string(name, pos, sizeof(name));
00537
00538 if (last && strrchr(pos,' '))
00539 pos = strrchr(pos, ' ') + 1;
00540 conv = convert(pos);
00541 if (conv) {
00542 if (!strcmp(conv, ext)) {
00543
00544 found++;
00545
00546 res = play_mailbox_owner(chan, context, dialcontext, cat, name, readext, fromappvm);
00547 switch (res) {
00548 case -1:
00549
00550
00551
00552 lastuserchoice = 0;
00553 breakout = 1;
00554 break;
00555 case '1':
00556
00557
00558
00559
00560 lastuserchoice = res;
00561 breakout = 1;
00562 break;
00563 case '*':
00564
00565 lastuserchoice = res;
00566 breakout = 0;
00567 res = 0;
00568 break;
00569 default:
00570 breakout = 1;
00571 break;
00572 }
00573 free(conv);
00574 if (breakout)
00575 break;
00576 }
00577 else
00578 free(conv);
00579 }
00580 }
00581 }
00582 }
00583
00584 if (lastuserchoice != '1') {
00585 res = ast_streamfile(chan, found ? "dir-nomore" : "dir-nomatch", chan->language);
00586 if (!res)
00587 res = 1;
00588 return res;
00589 }
00590 return 0;
00591 }
00592 return res;
00593 }
00594
00595 static int directory_exec(struct ast_channel *chan, void *data)
00596 {
00597 int res = 0;
00598 struct ast_module_user *u;
00599 struct ast_config *cfg, *ucfg;
00600 int last = 1;
00601 int readext = 0;
00602 int fromappvm = 0;
00603 const char *dirintro;
00604 char *parse;
00605 AST_DECLARE_APP_ARGS(args,
00606 AST_APP_ARG(vmcontext);
00607 AST_APP_ARG(dialcontext);
00608 AST_APP_ARG(options);
00609 );
00610
00611 if (ast_strlen_zero(data)) {
00612 ast_log(LOG_WARNING, "Directory requires an argument (context[,dialcontext])\n");
00613 return -1;
00614 }
00615
00616 u = ast_module_user_add(chan);
00617
00618 parse = ast_strdupa(data);
00619
00620 AST_STANDARD_APP_ARGS(args, parse);
00621
00622 if (args.options) {
00623 if (strchr(args.options, 'f'))
00624 last = 0;
00625 if (strchr(args.options, 'e'))
00626 readext = 1;
00627 if (strchr(args.options, 'v'))
00628 fromappvm = 1;
00629 }
00630
00631 if (ast_strlen_zero(args.dialcontext))
00632 args.dialcontext = args.vmcontext;
00633
00634 cfg = realtime_directory(args.vmcontext);
00635 if (!cfg) {
00636 ast_log(LOG_ERROR, "Unable to read the configuration data!\n");
00637 ast_module_user_remove(u);
00638 return -1;
00639 }
00640
00641 ucfg = ast_config_load("users.conf");
00642
00643 dirintro = ast_variable_retrieve(cfg, args.vmcontext, "directoryintro");
00644 if (ast_strlen_zero(dirintro))
00645 dirintro = ast_variable_retrieve(cfg, "general", "directoryintro");
00646 if (ast_strlen_zero(dirintro))
00647 dirintro = last ? "dir-intro" : "dir-intro-fn";
00648
00649 if (chan->_state != AST_STATE_UP)
00650 res = ast_answer(chan);
00651
00652 for (;;) {
00653 if (!res)
00654 res = ast_stream_and_wait(chan, dirintro, chan->language, AST_DIGIT_ANY);
00655 ast_stopstream(chan);
00656 if (!res)
00657 res = ast_waitfordigit(chan, 5000);
00658 if (res > 0) {
00659 res = do_directory(chan, cfg, ucfg, args.vmcontext, args.dialcontext, res, last, readext, fromappvm);
00660 if (res > 0) {
00661 res = ast_waitstream(chan, AST_DIGIT_ANY);
00662 ast_stopstream(chan);
00663 if (res >= 0)
00664 continue;
00665 }
00666 }
00667 break;
00668 }
00669 if (ucfg)
00670 ast_config_destroy(ucfg);
00671 ast_config_destroy(cfg);
00672 ast_module_user_remove(u);
00673 return res;
00674 }
00675
00676 static int unload_module(void)
00677 {
00678 int res;
00679 res = ast_unregister_application(app);
00680 return res;
00681 }
00682
00683 static int load_module(void)
00684 {
00685 #ifdef ODBC_STORAGE
00686 struct ast_config *cfg = ast_config_load(VOICEMAIL_CONFIG);
00687 const char *tmp;
00688
00689 if (cfg) {
00690 if ((tmp = ast_variable_retrieve(cfg, "general", "odbcstorage"))) {
00691 ast_copy_string(odbc_database, tmp, sizeof(odbc_database));
00692 }
00693 if ((tmp = ast_variable_retrieve(cfg, "general", "odbctable"))) {
00694 ast_copy_string(odbc_table, tmp, sizeof(odbc_table));
00695 }
00696 if ((tmp = ast_variable_retrieve(cfg, "general", "format"))) {
00697 ast_copy_string(vmfmts, tmp, sizeof(vmfmts));
00698 }
00699 ast_config_destroy(cfg);
00700 } else
00701 ast_log(LOG_WARNING, "Unable to load " VOICEMAIL_CONFIG " - ODBC defaults will be used\n");
00702 #endif
00703
00704 return ast_register_application(app, directory_exec, synopsis, descrip);
00705 }
00706
00707 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Extension Directory");