Index: apps/app_directory.c =================================================================== --- apps/app_directory.c (revision 78604) +++ apps/app_directory.c (working copy) @@ -77,7 +77,9 @@ " e - In addition to the name, also read the extension number to the\n" " caller before presenting dialing options.\n" " f - Allow the caller to enter the first name of a user in the directory\n" -" instead of using the last name.\n"; +" instead of using the last name.\n" +" m - Instead of reading each name sequentially and asking for confirmation,\n" +" create a menu of up to 8 names.\n"; /* For simplicity, I'm keeping the format compatible with the voicemail config, but i'm open to suggestions for isolating it */ @@ -87,7 +89,25 @@ /* How many digits to read in */ #define NUMDIGITS 3 +enum { + OPT_LISTBYFIRSTNAME = (1 << 0), + OPT_SAYEXTENSION = (1 << 1), + OPT_FROMVOICEMAIL = (1 << 2), + OPT_SELECTFROMMENU = (1 << 3), +} directory_option_flags; +struct items { + char exten[AST_MAX_EXTENSION + 1]; + char name[AST_MAX_EXTENSION + 1]; +}; + +AST_APP_OPTIONS(directory_app_options, { + AST_APP_OPTION('f', OPT_LISTBYFIRSTNAME), + AST_APP_OPTION('e', OPT_SAYEXTENSION), + AST_APP_OPTION('v', OPT_FROMVOICEMAIL), + AST_APP_OPTION('m', OPT_SELECTFROMMENU), +}); + #ifdef ODBC_STORAGE struct generic_prepare_struct { const char *sql; @@ -278,11 +298,9 @@ * '*' for skipped entry from directory */ static int play_mailbox_owner(struct ast_channel *chan, char *context, - char *dialcontext, char *ext, char *name, int readext, - int fromappvm) + char *ext, char *name, struct ast_flags *flags) { int res = 0; - int loop; char fn[256]; /* Check for the VoiceMail2 greeting first */ @@ -305,13 +323,13 @@ res = ast_stream_and_wait(chan, fn, AST_DIGIT_ANY); ast_stopstream(chan); /* If Option 'e' was specified, also read the extension number with the name */ - if (readext) { + if (ast_test_flag(flags, OPT_SAYEXTENSION)) { ast_stream_and_wait(chan, "vm-extension", AST_DIGIT_ANY); res = ast_say_character_str(chan, ext, AST_DIGIT_ANY, chan->language); } } else { res = ast_say_character_str(chan, S_OR(name, ext), AST_DIGIT_ANY, chan->language); - if (!ast_strlen_zero(name) && readext) { + if (!ast_strlen_zero(name) && ast_test_flag(flags, OPT_SAYEXTENSION)) { ast_stream_and_wait(chan, "vm-extension", AST_DIGIT_ANY); res = ast_say_character_str(chan, ext, AST_DIGIT_ANY, chan->language); } @@ -320,6 +338,15 @@ ast_filedelete(fn, NULL); #endif + return res; +} + +static int get_mailbox_response(struct ast_channel *chan, char *context, char *dialcontext, char *ext, char *name, struct ast_flags *flags) +{ + int res = 0; + int loop = 3; + + res = play_mailbox_owner(chan, context, ext, name, flags); for (loop = 3 ; loop > 0; loop--) { if (!res) res = ast_stream_and_wait(chan, "dir-instr", AST_DIGIT_ANY); @@ -330,7 +357,7 @@ if (res < 0) /* User hungup, so jump out now */ break; if (res == '1') { /* Name selected */ - if (fromappvm) { + if (ast_test_flag(flags, OPT_FROMVOICEMAIL)) { /* We still want to set the exten though */ ast_copy_string(chan->exten, ext, sizeof(chan->exten)); } else { @@ -354,6 +381,59 @@ return(res); } +static int select_item(struct ast_channel *chan, struct items *items, int itemcount, char *context, char *dialcontext, struct ast_flags *flags) +{ + int i, res = 0; + char buf[9]; + for (i = 0; i < itemcount; i++) { + snprintf(buf, sizeof(buf), "digits/%d", i + 1); + /* For , press */ + if (!res) + res = ast_streamfile(chan, "digits/4", chan->language); + if (!res) + res = ast_waitstream(chan, AST_DIGIT_ANY); + if (!res) + res = play_mailbox_owner(chan, context, items[i].exten, items[i].name, flags); + if (!res) + res = ast_waitstream(chan, AST_DIGIT_ANY); + if (!res) + res = ast_streamfile(chan, "vm-press", chan->language); + if (!res) + res = ast_waitstream(chan, AST_DIGIT_ANY); + if (!res) + res = ast_streamfile(chan, buf, chan->language); + if (!res) + res = ast_waitstream(chan, AST_DIGIT_ANY); + if (!res) + res = ast_waitfordigit(chan, 800); + if (res) + break; + } + /* For more names, press "9" */ + if (!res) + res = ast_waitstream(chan, AST_DIGIT_ANY); + if (!res && itemcount == 8) + res = ast_streamfile(chan, "dir-press9", chan->language); + if (!res) + res = ast_waitstream(chan, AST_DIGIT_ANY); + if (!res) + res = ast_waitfordigit(chan, 3000); + + if (res && res > '0' && res < '9') { + if (ast_test_flag(flags, OPT_FROMVOICEMAIL)) { + /* We still want to set the exten */ + ast_copy_string(chan->exten, items[res - '0'].exten, sizeof(chan->exten)); + } else if (ast_goto_if_exists(chan, dialcontext, items[res - '1'].exten, 1)) { + ast_log(LOG_WARNING, + "Can't find extension '%s' in context '%s'. " + "Did you pass the wrong context to Directory?\n", + items[res - '0'].exten, dialcontext); + res = -1; + } + } + return res; +} + static struct ast_config *realtime_directory(char *context) { struct ast_config *cfg; @@ -412,7 +492,7 @@ return cfg; } -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) +static int do_directory(struct ast_channel *chan, struct ast_config *vmcfg, struct ast_config *ucfg, char *context, char *dialcontext, char digit, struct ast_flags *flags) { /* Read in the first three digits.. "digit" is the first digit, already read */ char ext[NUMDIGITS + 1], *cat; @@ -422,7 +502,7 @@ int found=0; int lastuserchoice = 0; char *start, *conv, *stringp = NULL; - const char *pos; + char *pos; int breakout = 0; if (ast_strlen_zero(context)) { @@ -458,43 +538,94 @@ res = 0; if (ast_readstring(chan, ext + 1, NUMDIGITS - 1, 3000, 3000, "#") < 0) res = -1; if (!res) { - /* Search for all names which start with those digits */ - v = ast_variable_browse(cfg, context); - while(v && !res) { - /* Find all candidate extensions */ - while(v) { - /* Find a candidate extension */ - start = ast_strdup(v->value); - if (start && !strcasestr(start, "hidefromdir=yes")) { - stringp=start; - strsep(&stringp, ","); - pos = strsep(&stringp, ","); - if (pos) { - ast_copy_string(name, pos, sizeof(name)); - /* Grab the last name */ - if (last && strrchr(pos,' ')) - pos = strrchr(pos, ' ') + 1; - conv = convert(pos); - if (conv) { - if (!strncmp(conv, ext, strlen(ext))) { - /* Match! */ - found++; + if (ast_test_flag(flags, OPT_SELECTFROMMENU)) { + char buf[AST_MAX_EXTENSION + 1], *bufptr, *fullname; + struct items menuitems[8]; + int menucount = 0; + for (v = ast_variable_browse(vmcfg, context); v; v = v->next) { + if (strcasestr(v->value, "hidefromdir=yes") == NULL) { + ast_copy_string(buf, v->value, sizeof(buf)); + bufptr = buf; + /* password,Full Name,email,pager,options */ + strsep(&bufptr, ","); + pos = strsep(&bufptr, ","); + fullname = pos; + + if (ast_test_flag(flags, OPT_LISTBYFIRSTNAME) && strrchr(pos, ' ')) + pos = strrchr(pos, ' ') + 1; + conv = convert(pos); + + if (conv && strcmp(conv, ext) == 0) { + /* Match */ + found = 1; + ast_copy_string(menuitems[menucount].name, fullname, sizeof(menuitems[0].name)); + ast_copy_string(menuitems[menucount].exten, v->name, sizeof(menuitems[0].exten)); + menucount++; + } + + if (menucount == 8) { + /* We have a full menu */ + res = select_item(chan, menuitems, menucount, context, dialcontext, flags); + menucount = 0; + if (res != '9' && res != 0) { + if (res != -1) + lastuserchoice = res; + break; + } + } + } + } + + if (menucount > 0) { + /* We have a partial menu left over */ + res = select_item(chan, menuitems, menucount, context, dialcontext, flags); + if (res != '9') { + if (res != -1) + lastuserchoice = res; + } + } + + /* Make this flag conform to the old expected result */ + if (lastuserchoice > '1' && lastuserchoice < '9') + lastuserchoice = '1'; + } else { + /* Search for all names which start with those digits */ + v = ast_variable_browse(vmcfg, context); + while(v && !res) { + /* Find all candidate extensions */ + while(v) { + /* Find a candidate extension */ + start = ast_strdup(v->value); + if (start && !strcasestr(start, "hidefromdir=yes")) { + stringp=start; + strsep(&stringp, ","); + pos = strsep(&stringp, ","); + if (pos) { + ast_copy_string(name, pos, sizeof(name)); + /* Grab the last name */ + if (!ast_test_flag(flags, OPT_LISTBYFIRSTNAME) && strrchr(pos,' ')) + pos = strrchr(pos, ' ') + 1; + conv = convert(pos); + if (conv) { + if (!strncmp(conv, ext, strlen(ext))) { + /* Match! */ + found++; + ast_free(conv); + ast_free(start); + break; + } ast_free(conv); - ast_free(start); - break; } - ast_free(conv); } + ast_free(start); } - ast_free(start); + v = v->next; } - v = v->next; - } - if (v) { - /* We have a match -- play a greeting if they have it */ - res = play_mailbox_owner(chan, context, dialcontext, v->name, name, readext, fromappvm); - switch (res) { + if (v) { + /* We have a match -- play a greeting if they have it */ + res = get_mailbox_response(chan, context, dialcontext, v->name, name, flags); + switch (res) { case -1: /* user pressed '1' but extension does not exist, or * user hungup @@ -503,7 +634,7 @@ break; case '1': /* user pressed '1' and extensions exists; - play_mailbox_owner will already have done + get_mailbox_response will already have done a goto() on the channel */ lastuserchoice = res; @@ -515,68 +646,120 @@ break; default: break; + } + v = v->next; } - v = v->next; } } if (!res && ucfg) { /* Search users.conf for all names which start with those digits */ - for (cat = ast_category_browse(ucfg, NULL); cat && !res ; cat = ast_category_browse(ucfg, cat)) { - if (!strcasecmp(cat, "general")) - continue; - if (!ast_true(ast_config_option(ucfg, cat, "hasdirectory"))) - continue; + if (ast_test_flag(flags, OPT_SELECTFROMMENU)) { + char *fullname; + struct items menuitems[8]; + int menucount = 0; + for (cat = ast_category_browse(ucfg, NULL); cat && !res ; cat = ast_category_browse(ucfg, cat)) { + const char *pos; + if (!strcasecmp(cat, "general")) + continue; + if (!ast_true(ast_config_option(ucfg, cat, "hasdirectory"))) + continue; - /* Find all candidate extensions */ - if ((pos = ast_variable_retrieve(ucfg, cat, "fullname"))) { - ast_copy_string(name, pos, sizeof(name)); - /* Grab the last name */ - if (last && strrchr(pos,' ')) - pos = strrchr(pos, ' ') + 1; - conv = convert(pos); - if (conv) { - if (!strcmp(conv, ext)) { + /* Find all candidate extensions */ + if ((pos = ast_variable_retrieve(ucfg, cat, "fullname"))) { + ast_copy_string(name, pos, sizeof(name)); + /* Grab the last name */ + if (!ast_test_flag(flags, OPT_LISTBYFIRSTNAME) && strrchr(pos,' ')) + pos = strrchr(pos, ' ') + 1; + conv = convert(pos); + if (conv && strcmp(conv, ext) == 0) { + /* Match */ + found = 1; + ast_copy_string(menuitems[menucount].name, fullname, sizeof(menuitems[0].name)); + ast_copy_string(menuitems[menucount].exten, v->name, sizeof(menuitems[0].exten)); + menucount++; + + if (menucount == 8) { + /* We have a full menu */ + res = select_item(chan, menuitems, menucount, context, dialcontext, flags); + menucount = 0; + if (res != '9' && res != 0) { + if (res != -1) + lastuserchoice = res; + break; + } + } + } + } + } + if (menucount > 0) { + /* We have a partial menu left over */ + res = select_item(chan, menuitems, menucount, context, dialcontext, flags); + if (res != '9') { + if (res != -1) + lastuserchoice = res; + } + } + + /* Make this flag conform to the old expected result */ + if (lastuserchoice > '1' && lastuserchoice < '9') + lastuserchoice = '1'; + } else { /* !menu */ + for (cat = ast_category_browse(ucfg, NULL); cat && !res ; cat = ast_category_browse(ucfg, cat)) { + const char *pos; + if (!strcasecmp(cat, "general")) + continue; + if (!ast_true(ast_config_option(ucfg, cat, "hasdirectory"))) + continue; + + /* Find all candidate extensions */ + if ((pos = ast_variable_retrieve(ucfg, cat, "fullname"))) { + ast_copy_string(name, pos, sizeof(name)); + /* Grab the last name */ + if (ast_test_flag(flags, OPT_LISTBYFIRSTNAME) && strrchr(pos,' ')) + pos = strrchr(pos, ' ') + 1; + conv = convert(pos); + if (conv && strcmp(conv, ext) == 0) { /* Match! */ found++; /* We have a match -- play a greeting if they have it */ - res = play_mailbox_owner(chan, context, dialcontext, cat, name, readext, fromappvm); - switch (res) { - case -1: - /* user pressed '1' but extension does not exist, or - * user hungup - */ - lastuserchoice = 0; - breakout = 1; - break; - case '1': - /* user pressed '1' and extensions exists; - play_mailbox_owner will already have done - a goto() on the channel - */ - lastuserchoice = res; - breakout = 1; - break; - case '*': - /* user pressed '*' to skip something found */ - lastuserchoice = res; - breakout = 0; - res = 0; - break; - default: - breakout = 1; - break; - } - ast_free(conv); - if (breakout) - break; - } else - ast_free(conv); - } + res = get_mailbox_response(chan, context, dialcontext, cat, name, flags); + } + switch (res) { + case -1: + /* user pressed '1' but extension does not exist, or + * user hungup + */ + lastuserchoice = 0; + breakout = 1; + break; + case '1': + /* user pressed '1' and extensions exists; + play_mailbox_owner will already have done + a goto() on the channel + */ + lastuserchoice = res; + breakout = 1; + break; + case '*': + /* user pressed '*' to skip something found */ + lastuserchoice = res; + breakout = 0; + res = 0; + break; + default: + breakout = 1; + break; + } + ast_free(conv); + if (breakout) + break; + } else + ast_free(conv); } } } - + if (lastuserchoice != '1') { res = ast_streamfile(chan, found ? "dir-nomore" : "dir-nomatch", chan->language); if (!res) @@ -592,11 +775,9 @@ { int res = 0; struct ast_config *cfg, *ucfg; - int last = 1; - int readext = 0; - int fromappvm = 0; const char *dirintro; - char *parse; + char *parse, *opts[0]; + struct ast_flags flags = { 0 }; AST_DECLARE_APP_ARGS(args, AST_APP_ARG(vmcontext); AST_APP_ARG(dialcontext); @@ -611,16 +792,10 @@ parse = ast_strdupa(data); AST_STANDARD_APP_ARGS(args, parse); - - if (args.options) { - if (strchr(args.options, 'f')) - last = 0; - if (strchr(args.options, 'e')) - readext = 1; - if (strchr(args.options, 'v')) - fromappvm = 1; - } + if (args.options && ast_app_parse_options(directory_app_options, &flags, opts, args.options)) + return -1; + if (ast_strlen_zero(args.dialcontext)) args.dialcontext = args.vmcontext; @@ -636,7 +811,7 @@ if (ast_strlen_zero(dirintro)) dirintro = ast_variable_retrieve(cfg, "general", "directoryintro"); if (ast_strlen_zero(dirintro)) - dirintro = last ? "dir-intro" : "dir-intro-fn"; + dirintro = ast_test_flag(&flags, OPT_LISTBYFIRSTNAME) ? "dir-intro-fn" : "dir-intro"; if (chan->_state != AST_STATE_UP) res = ast_answer(chan); @@ -648,7 +823,7 @@ if (!res) res = ast_waitfordigit(chan, 5000); if (res > 0) { - res = do_directory(chan, cfg, ucfg, args.vmcontext, args.dialcontext, res, last, readext, fromappvm); + res = do_directory(chan, cfg, ucfg, args.vmcontext, args.dialcontext, res, &flags); if (res > 0) { res = ast_waitstream(chan, AST_DIGIT_ANY); ast_stopstream(chan);