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 " f - Allow the caller to enter the first name of a user in the directory\n"
69 " instead of using the last name.\n";
71 /* For simplicity, I'm keeping the format compatible with the voicemail config,
72 but i'm open to suggestions for isolating it */
74 #define VOICEMAIL_CONFIG "voicemail.conf"
76 /* How many digits to read in */
81 static char *convert(char *lastname)
85 tmp = ast_malloc(NUMDIGITS + 1);
87 while((*lastname > 32) && lcount < NUMDIGITS) {
88 switch(toupper(*lastname)) {
150 /* play name of mailbox owner.
151 * returns: -1 for bad or missing extension
152 * '1' for selected entry from directory
153 * '*' for skipped entry from directory
155 static int play_mailbox_owner(struct ast_channel *chan, char *context, char *dialcontext, char *ext, char *name) {
161 /* Check for the VoiceMail2 greeting first */
162 snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/greet",
163 (char *)ast_config_AST_SPOOL_DIR, context, ext);
165 /* Otherwise, check for an old-style Voicemail greeting */
166 snprintf(fn2, sizeof(fn2), "%s/vm/%s/greet",
167 (char *)ast_config_AST_SPOOL_DIR, ext);
169 if (ast_fileexists(fn, NULL, chan->language) > 0) {
170 res = ast_streamfile(chan, fn, chan->language);
172 res = ast_waitstream(chan, AST_DIGIT_ANY);
174 ast_stopstream(chan);
175 } else if (ast_fileexists(fn2, NULL, chan->language) > 0) {
176 res = ast_streamfile(chan, fn2, chan->language);
178 res = ast_waitstream(chan, AST_DIGIT_ANY);
180 ast_stopstream(chan);
182 res = ast_say_character_str(chan, S_OR(name, ext),
183 AST_DIGIT_ANY, chan->language);
188 res = ast_streamfile(chan, "dir-instr", chan->language);
191 res = ast_waitstream(chan, AST_DIGIT_ANY);
194 res = ast_waitfordigit(chan, 3000);
196 ast_stopstream(chan);
203 if (ast_goto_if_exists(chan, dialcontext, ext, 1)) {
205 "Can't find extension '%s' in context '%s'. "
206 "Did you pass the wrong context to Directory?\n",
213 /* Skip to next match in list */
218 /* Not '1', or '*', so decrement number of tries */
225 /* User hungup, so jump out now */
233 static struct ast_config *realtime_directory(char *context)
235 struct ast_config *cfg;
236 struct ast_config *rtdata;
237 struct ast_category *cat;
238 struct ast_variable *var;
244 /* Load flat file config. */
245 cfg = ast_config_load(VOICEMAIL_CONFIG);
248 /* Loading config failed. */
249 ast_log(LOG_WARNING, "Loading config failed.\n");
253 /* Get realtime entries, categorized by their mailbox number
254 and present in the requested context */
255 rtdata = ast_load_realtime_multientry("voicemail", "mailbox LIKE", "%", "context", context, NULL);
257 /* if there are no results, just return the entries from the config file */
261 /* Does the context exist within the config file? If not, make one */
262 cat = ast_category_get(cfg, context);
264 cat = ast_category_new(context);
266 ast_log(LOG_WARNING, "Out of memory\n");
267 ast_config_destroy(cfg);
270 ast_category_append(cfg, cat);
273 mailbox = ast_category_browse(rtdata, NULL);
275 fullname = ast_variable_retrieve(rtdata, mailbox, "fullname");
276 hidefromdir = ast_variable_retrieve(rtdata, mailbox, "hidefromdir");
277 snprintf(tmp, sizeof(tmp), "no-password,%s,hidefromdir=%s",
278 fullname ? fullname : "",
279 hidefromdir ? hidefromdir : "no");
280 var = ast_variable_new(mailbox, tmp);
282 ast_variable_append(cat, var);
284 ast_log(LOG_WARNING, "Out of memory adding mailbox '%s'\n", mailbox);
285 mailbox = ast_category_browse(rtdata, mailbox);
287 ast_config_destroy(rtdata);
292 static int do_directory(struct ast_channel *chan, struct ast_config *cfg, char *context, char *dialcontext, char digit, int last)
294 /* Read in the first three digits.. "digit" is the first digit, already read */
295 char ext[NUMDIGITS + 1];
297 struct ast_variable *v;
300 int lastuserchoice = 0;
301 char *start, *pos, *conv,*stringp=NULL;
303 if (ast_strlen_zero(context)) {
305 "Directory must be called with an argument "
306 "(context in which to interpret extensions)\n");
310 if (!ast_goto_if_exists(chan, chan->context, "o", 1) ||
311 (!ast_strlen_zero(chan->macrocontext) &&
312 !ast_goto_if_exists(chan, chan->macrocontext, "o", 1))) {
315 ast_log(LOG_WARNING, "Can't find extension 'o' in current context. "
316 "Not Exiting the Directory!\n");
321 if (!ast_goto_if_exists(chan, chan->context, "a", 1) ||
322 (!ast_strlen_zero(chan->macrocontext) &&
323 !ast_goto_if_exists(chan, chan->macrocontext, "a", 1))) {
326 ast_log(LOG_WARNING, "Can't find extension 'a' in current context. "
327 "Not Exiting the Directory!\n");
331 memset(ext, 0, sizeof(ext));
334 if (ast_readstring(chan, ext + 1, NUMDIGITS - 1, 3000, 3000, "#") < 0) res = -1;
336 /* Search for all names which start with those digits */
337 v = ast_variable_browse(cfg, context);
339 /* Find all candidate extensions */
341 /* Find a candidate extension */
342 start = strdup(v->value);
343 if (start && !strcasestr(start, "hidefromdir=yes")) {
345 strsep(&stringp, ",");
346 pos = strsep(&stringp, ",");
348 ast_copy_string(name, pos, sizeof(name));
349 /* Grab the last name */
350 if (last && strrchr(pos,' '))
351 pos = strrchr(pos, ' ') + 1;
354 if (!strcmp(conv, ext)) {
370 /* We have a match -- play a greeting if they have it */
371 res = play_mailbox_owner(chan, context, dialcontext, v->name, name);
374 /* user pressed '1' but extension does not exist, or
380 /* user pressed '1' and extensions exists;
381 play_mailbox_owner will already have done
382 a goto() on the channel
384 lastuserchoice = res;
387 /* user pressed '*' to skip something found */
388 lastuserchoice = res;
398 if (lastuserchoice != '1') {
400 res = ast_streamfile(chan, "dir-nomore", chan->language);
402 res = ast_streamfile(chan, "dir-nomatch", chan->language);
412 static int directory_exec(struct ast_channel *chan, void *data)
416 struct ast_config *cfg;
418 char *dirintro, *parse;
419 AST_DECLARE_APP_ARGS(args,
420 AST_APP_ARG(vmcontext);
421 AST_APP_ARG(dialcontext);
422 AST_APP_ARG(options);
425 if (ast_strlen_zero(data)) {
426 ast_log(LOG_WARNING, "Directory requires an argument (context[,dialcontext])\n");
432 if (!(parse = ast_strdupa(data))) {
433 LOCAL_USER_REMOVE(u);
437 AST_STANDARD_APP_ARGS(args, parse);
440 if (strchr(args.options, 'f'))
444 if (ast_strlen_zero(args.dialcontext))
445 args.dialcontext = args.vmcontext;
447 cfg = realtime_directory(args.vmcontext);
449 ast_log(LOG_ERROR, "Unable to read the configuration data!\n");
450 LOCAL_USER_REMOVE(u);
454 dirintro = ast_variable_retrieve(cfg, args.vmcontext, "directoryintro");
455 if (ast_strlen_zero(dirintro))
456 dirintro = ast_variable_retrieve(cfg, "general", "directoryintro");
457 if (ast_strlen_zero(dirintro)) {
459 dirintro = "dir-intro";
461 dirintro = "dir-intro-fn";
464 if (chan->_state != AST_STATE_UP)
465 res = ast_answer(chan);
469 res = ast_streamfile(chan, dirintro, chan->language);
471 res = ast_waitstream(chan, AST_DIGIT_ANY);
472 ast_stopstream(chan);
474 res = ast_waitfordigit(chan, 5000);
476 res = do_directory(chan, cfg, args.vmcontext, args.dialcontext, res, last);
478 res = ast_waitstream(chan, AST_DIGIT_ANY);
479 ast_stopstream(chan);
487 ast_config_destroy(cfg);
488 LOCAL_USER_REMOVE(u);
492 int unload_module(void)
496 res = ast_unregister_application(app);
498 STANDARD_HANGUP_LOCALUSERS;
503 int load_module(void)
505 return ast_register_application(app, directory_exec, synopsis, descrip);
508 const char *description(void)
516 STANDARD_USECOUNT(res);
522 return ASTERISK_GPL_KEY;