2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Provide a directory of extensions
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/lock.h"
38 #include "asterisk/file.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/module.h"
43 #include "asterisk/config.h"
44 #include "asterisk/say.h"
45 #include "asterisk/utils.h"
46 #include "asterisk/app.h"
48 static char *tdesc = "Extension Directory";
49 static char *app = "Directory";
51 static char *synopsis = "Provide directory of voicemail extensions";
52 static char *descrip =
53 " Directory(vm-context[|dial-context[|options]]): This application will present\n"
54 "the calling channel with a directory of extensions from which they can search\n"
55 "by name. The list of names and corresponding extensions is retrieved from the\n"
56 "voicemail configuration file, voicemail.conf.\n"
57 " This applicaiton will immediate exit if one of the following DTMF digits are\n"
58 "received and the extension to jump to exists:\n"
59 " 0 - Jump to the 'o' extension, if it exists.\n"
60 " * - Jump to the 'a' extension, if it exists.\n\n"
62 " vm-context - This is the context within voicemail.conf to use for the\n"
64 " dial-context - This is the dialplan context to use when looking for an\n"
65 " extension that the user has selected, or when jumping to the\n"
66 " 'o' or 'a' extension.\n\n"
68 " e - In addition to the name, also read the extension number to the\n"
69 " caller before presenting dialing options.\n"
70 " f - Allow the caller to enter the first name of a user in the directory\n"
71 " instead of using the last name.\n";
73 /* For simplicity, I'm keeping the format compatible with the voicemail config,
74 but i'm open to suggestions for isolating it */
76 #define VOICEMAIL_CONFIG "voicemail.conf"
78 /* How many digits to read in */
83 static char *convert(char *lastname)
87 tmp = ast_malloc(NUMDIGITS + 1);
89 while((*lastname > 32) && lcount < NUMDIGITS) {
90 switch(toupper(*lastname)) {
152 /* play name of mailbox owner.
153 * returns: -1 for bad or missing extension
154 * '1' for selected entry from directory
155 * '*' for skipped entry from directory
157 static int play_mailbox_owner(struct ast_channel *chan, char *context, char *dialcontext, char *ext, char *name, int readext) {
163 /* Check for the VoiceMail2 greeting first */
164 snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/greet",
165 (char *)ast_config_AST_SPOOL_DIR, context, ext);
167 /* Otherwise, check for an old-style Voicemail greeting */
168 snprintf(fn2, sizeof(fn2), "%s/vm/%s/greet",
169 (char *)ast_config_AST_SPOOL_DIR, ext);
171 if (ast_fileexists(fn, NULL, chan->language) > 0) {
172 res = ast_streamfile(chan, fn, chan->language);
174 res = ast_waitstream(chan, AST_DIGIT_ANY);
176 ast_stopstream(chan);
177 /* If Option 'e' was specified, also read the extension number with the name */
179 res = ast_streamfile(chan, "vm-extension", chan->language);
181 res = ast_waitstream(chan, AST_DIGIT_ANY);
183 res = ast_say_character_str(chan, ext, AST_DIGIT_ANY, chan->language);
185 } else if (ast_fileexists(fn2, NULL, chan->language) > 0) {
186 res = ast_streamfile(chan, fn2, chan->language);
188 res = ast_waitstream(chan, AST_DIGIT_ANY);
190 ast_stopstream(chan);
191 /* If Option 'e' was specified, also read the extension number with the name */
193 res = ast_streamfile(chan, "vm-extension", chan->language);
195 res = ast_waitstream(chan, AST_DIGIT_ANY);
197 res = ast_say_character_str(chan, ext, AST_DIGIT_ANY, chan->language);
200 res = ast_say_character_str(chan, S_OR(name, ext),
201 AST_DIGIT_ANY, chan->language);
202 if (!ast_strlen_zero(name) && readext) {
203 res = ast_streamfile(chan, "vm-extension", chan->language);
205 res = ast_waitstream(chan, AST_DIGIT_ANY);
207 res = ast_say_character_str(chan, ext, AST_DIGIT_ANY, chan->language);
213 res = ast_streamfile(chan, "dir-instr", chan->language);
216 res = ast_waitstream(chan, AST_DIGIT_ANY);
219 res = ast_waitfordigit(chan, 3000);
221 ast_stopstream(chan);
228 if (ast_goto_if_exists(chan, dialcontext, ext, 1)) {
230 "Can't find extension '%s' in context '%s'. "
231 "Did you pass the wrong context to Directory?\n",
238 /* Skip to next match in list */
243 /* Not '1', or '*', so decrement number of tries */
250 /* User hungup, so jump out now */
258 static struct ast_config *realtime_directory(char *context)
260 struct ast_config *cfg;
261 struct ast_config *rtdata;
262 struct ast_category *cat;
263 struct ast_variable *var;
269 /* Load flat file config. */
270 cfg = ast_config_load(VOICEMAIL_CONFIG);
273 /* Loading config failed. */
274 ast_log(LOG_WARNING, "Loading config failed.\n");
278 /* Get realtime entries, categorized by their mailbox number
279 and present in the requested context */
280 rtdata = ast_load_realtime_multientry("voicemail", "mailbox LIKE", "%", "context", context, NULL);
282 /* if there are no results, just return the entries from the config file */
286 /* Does the context exist within the config file? If not, make one */
287 cat = ast_category_get(cfg, context);
289 cat = ast_category_new(context);
291 ast_log(LOG_WARNING, "Out of memory\n");
292 ast_config_destroy(cfg);
295 ast_category_append(cfg, cat);
298 mailbox = ast_category_browse(rtdata, NULL);
300 fullname = ast_variable_retrieve(rtdata, mailbox, "fullname");
301 hidefromdir = ast_variable_retrieve(rtdata, mailbox, "hidefromdir");
302 snprintf(tmp, sizeof(tmp), "no-password,%s,hidefromdir=%s",
303 fullname ? fullname : "",
304 hidefromdir ? hidefromdir : "no");
305 var = ast_variable_new(mailbox, tmp);
307 ast_variable_append(cat, var);
309 ast_log(LOG_WARNING, "Out of memory adding mailbox '%s'\n", mailbox);
310 mailbox = ast_category_browse(rtdata, mailbox);
312 ast_config_destroy(rtdata);
317 static int do_directory(struct ast_channel *chan, struct ast_config *cfg, char *context, char *dialcontext, char digit, int last, int readext)
319 /* Read in the first three digits.. "digit" is the first digit, already read */
320 char ext[NUMDIGITS + 1];
322 struct ast_variable *v;
325 int lastuserchoice = 0;
326 char *start, *pos, *conv,*stringp=NULL;
328 if (ast_strlen_zero(context)) {
330 "Directory must be called with an argument "
331 "(context in which to interpret extensions)\n");
335 if (!ast_goto_if_exists(chan, chan->context, "o", 1) ||
336 (!ast_strlen_zero(chan->macrocontext) &&
337 !ast_goto_if_exists(chan, chan->macrocontext, "o", 1))) {
340 ast_log(LOG_WARNING, "Can't find extension 'o' in current context. "
341 "Not Exiting the Directory!\n");
346 if (!ast_goto_if_exists(chan, chan->context, "a", 1) ||
347 (!ast_strlen_zero(chan->macrocontext) &&
348 !ast_goto_if_exists(chan, chan->macrocontext, "a", 1))) {
351 ast_log(LOG_WARNING, "Can't find extension 'a' in current context. "
352 "Not Exiting the Directory!\n");
356 memset(ext, 0, sizeof(ext));
359 if (ast_readstring(chan, ext + 1, NUMDIGITS - 1, 3000, 3000, "#") < 0) res = -1;
361 /* Search for all names which start with those digits */
362 v = ast_variable_browse(cfg, context);
364 /* Find all candidate extensions */
366 /* Find a candidate extension */
367 start = strdup(v->value);
368 if (start && !strcasestr(start, "hidefromdir=yes")) {
370 strsep(&stringp, ",");
371 pos = strsep(&stringp, ",");
373 ast_copy_string(name, pos, sizeof(name));
374 /* Grab the last name */
375 if (last && strrchr(pos,' '))
376 pos = strrchr(pos, ' ') + 1;
379 if (!strcmp(conv, ext)) {
395 /* We have a match -- play a greeting if they have it */
396 res = play_mailbox_owner(chan, context, dialcontext, v->name, name, readext);
399 /* user pressed '1' but extension does not exist, or
405 /* user pressed '1' and extensions exists;
406 play_mailbox_owner will already have done
407 a goto() on the channel
409 lastuserchoice = res;
412 /* user pressed '*' to skip something found */
413 lastuserchoice = res;
423 if (lastuserchoice != '1') {
425 res = ast_streamfile(chan, "dir-nomore", chan->language);
427 res = ast_streamfile(chan, "dir-nomatch", chan->language);
437 static int directory_exec(struct ast_channel *chan, void *data)
441 struct ast_config *cfg;
444 char *dirintro, *parse;
445 AST_DECLARE_APP_ARGS(args,
446 AST_APP_ARG(vmcontext);
447 AST_APP_ARG(dialcontext);
448 AST_APP_ARG(options);
451 if (ast_strlen_zero(data)) {
452 ast_log(LOG_WARNING, "Directory requires an argument (context[,dialcontext])\n");
458 if (!(parse = ast_strdupa(data))) {
459 LOCAL_USER_REMOVE(u);
463 AST_STANDARD_APP_ARGS(args, parse);
466 if (strchr(args.options, 'f'))
468 if (strchr(args.options, 'e'))
472 if (ast_strlen_zero(args.dialcontext))
473 args.dialcontext = args.vmcontext;
475 cfg = realtime_directory(args.vmcontext);
477 ast_log(LOG_ERROR, "Unable to read the configuration data!\n");
478 LOCAL_USER_REMOVE(u);
482 dirintro = ast_variable_retrieve(cfg, args.vmcontext, "directoryintro");
483 if (ast_strlen_zero(dirintro))
484 dirintro = ast_variable_retrieve(cfg, "general", "directoryintro");
485 if (ast_strlen_zero(dirintro)) {
487 dirintro = "dir-intro";
489 dirintro = "dir-intro-fn";
492 if (chan->_state != AST_STATE_UP)
493 res = ast_answer(chan);
497 res = ast_streamfile(chan, dirintro, chan->language);
499 res = ast_waitstream(chan, AST_DIGIT_ANY);
500 ast_stopstream(chan);
502 res = ast_waitfordigit(chan, 5000);
504 res = do_directory(chan, cfg, args.vmcontext, args.dialcontext, res, last, readext);
506 res = ast_waitstream(chan, AST_DIGIT_ANY);
507 ast_stopstream(chan);
515 ast_config_destroy(cfg);
516 LOCAL_USER_REMOVE(u);
520 int unload_module(void)
524 res = ast_unregister_application(app);
526 STANDARD_HANGUP_LOCALUSERS;
531 int load_module(void)
533 return ast_register_application(app, directory_exec, synopsis, descrip);
536 const char *description(void)
544 STANDARD_USECOUNT(res);
550 return ASTERISK_GPL_KEY;