Provide the ability to read extension number after name #6938 (delink)
[asterisk/asterisk.git] / apps / app_directory.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Provide a directory of extensions
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27  
28 #include <string.h>
29 #include <ctype.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
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"
47
48 static char *tdesc = "Extension Directory";
49 static char *app = "Directory";
50
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"
61 "  Parameters:\n"
62 "    vm-context   - This is the context within voicemail.conf to use for the\n"
63 "                   Directory.\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"
67 "  Options:\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";
72
73 /* For simplicity, I'm keeping the format compatible with the voicemail config,
74    but i'm open to suggestions for isolating it */
75
76 #define VOICEMAIL_CONFIG "voicemail.conf"
77
78 /* How many digits to read in */
79 #define NUMDIGITS 3
80
81 LOCAL_USER_DECL;
82
83 static char *convert(char *lastname)
84 {
85         char *tmp;
86         int lcount = 0;
87         tmp = ast_malloc(NUMDIGITS + 1);
88         if (tmp) {
89                 while((*lastname > 32) && lcount < NUMDIGITS) {
90                         switch(toupper(*lastname)) {
91                         case '1':
92                                 tmp[lcount++] = '1';
93                                 break;
94                         case '2':
95                         case 'A':
96                         case 'B':
97                         case 'C':
98                                 tmp[lcount++] = '2';
99                                 break;
100                         case '3':
101                         case 'D':
102                         case 'E':
103                         case 'F':
104                                 tmp[lcount++] = '3';
105                                 break;
106                         case '4':
107                         case 'G':
108                         case 'H':
109                         case 'I':
110                                 tmp[lcount++] = '4';
111                                 break;
112                         case '5':
113                         case 'J':
114                         case 'K':
115                         case 'L':
116                                 tmp[lcount++] = '5';
117                                 break;
118                         case '6':
119                         case 'M':
120                         case 'N':
121                         case 'O':
122                                 tmp[lcount++] = '6';
123                                 break;
124                         case '7':
125                         case 'P':
126                         case 'Q':
127                         case 'R':
128                         case 'S':
129                                 tmp[lcount++] = '7';
130                                 break;
131                         case '8':
132                         case 'T':
133                         case 'U':
134                         case 'V':
135                                 tmp[lcount++] = '8';
136                                 break;
137                         case '9':
138                         case 'W':
139                         case 'X':
140                         case 'Y':
141                         case 'Z':
142                                 tmp[lcount++] = '9';
143                                 break;
144                         }
145                         lastname++;
146                 }
147                 tmp[lcount] = '\0';
148         }
149         return tmp;
150 }
151
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
156  */
157 static int play_mailbox_owner(struct ast_channel *chan, char *context, char *dialcontext, char *ext, char *name, int readext) {
158         int res = 0;
159         int loop = 3;
160         char fn[256];
161         char fn2[256];
162
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);
166
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);
170
171         if (ast_fileexists(fn, NULL, chan->language) > 0) {
172                 res = ast_streamfile(chan, fn, chan->language);
173                 if (!res) {
174                         res = ast_waitstream(chan, AST_DIGIT_ANY);
175                 }
176                 ast_stopstream(chan);
177                 /* If Option 'e' was specified, also read the extension number with the name */
178                 if (readext) {
179                         res = ast_streamfile(chan, "vm-extension", chan->language);
180                         if (!res) {
181                                 res = ast_waitstream(chan, AST_DIGIT_ANY);
182                         }
183                         res = ast_say_character_str(chan, ext, AST_DIGIT_ANY, chan->language);
184                 }
185         } else if (ast_fileexists(fn2, NULL, chan->language) > 0) {
186                 res = ast_streamfile(chan, fn2, chan->language);
187                 if (!res) {
188                         res = ast_waitstream(chan, AST_DIGIT_ANY);
189                 }
190                 ast_stopstream(chan);
191                 /* If Option 'e' was specified, also read the extension number with the name */
192                 if (readext) {
193                         res = ast_streamfile(chan, "vm-extension", chan->language);
194                         if (!res) {
195                                 res = ast_waitstream(chan, AST_DIGIT_ANY);
196                         }
197                         res = ast_say_character_str(chan, ext, AST_DIGIT_ANY, chan->language);
198                 }
199         } else {
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);
204                         if (!res) {
205                                 res = ast_waitstream(chan, AST_DIGIT_ANY);
206                         }
207                         res = ast_say_character_str(chan, ext, AST_DIGIT_ANY, chan->language);
208                 }
209         }
210
211         while (loop) {
212                 if (!res) {
213                         res = ast_streamfile(chan, "dir-instr", chan->language);
214                 }
215                 if (!res) {
216                         res = ast_waitstream(chan, AST_DIGIT_ANY);
217                 }
218                 if (!res) {
219                         res = ast_waitfordigit(chan, 3000);
220                 }
221                 ast_stopstream(chan);
222         
223                 if (res > -1) {
224                         switch (res) {
225                                 case '1':
226                                         /* Name selected */
227                                         loop = 0;
228                                         if (ast_goto_if_exists(chan, dialcontext, ext, 1)) {
229                                                 ast_log(LOG_WARNING,
230                                                         "Can't find extension '%s' in context '%s'.  "
231                                                         "Did you pass the wrong context to Directory?\n",
232                                                         ext, dialcontext);
233                                                 res = -1;
234                                         }
235                                         break;
236         
237                                 case '*':   
238                                         /* Skip to next match in list */
239                                         loop = 0;
240                                         break;
241         
242                                 default:
243                                         /* Not '1', or '*', so decrement number of tries */
244                                         res = 0;
245                                         loop--;
246                                         break;
247                         } /* end switch */
248                 } /* end if */
249                 else {
250                         /* User hungup, so jump out now */
251                         loop = 0;
252                 }
253         } /* end while */
254
255         return(res);
256 }
257
258 static struct ast_config *realtime_directory(char *context)
259 {
260         struct ast_config *cfg;
261         struct ast_config *rtdata;
262         struct ast_category *cat;
263         struct ast_variable *var;
264         char *mailbox;
265         char *fullname;
266         char *hidefromdir;
267         char tmp[100];
268
269         /* Load flat file config. */
270         cfg = ast_config_load(VOICEMAIL_CONFIG);
271
272         if (!cfg) {
273                 /* Loading config failed. */
274                 ast_log(LOG_WARNING, "Loading config failed.\n");
275                 return NULL;
276         }
277
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);
281
282         /* if there are no results, just return the entries from the config file */
283         if (!rtdata)
284                 return cfg;
285
286         /* Does the context exist within the config file? If not, make one */
287         cat = ast_category_get(cfg, context);
288         if (!cat) {
289                 cat = ast_category_new(context);
290                 if (!cat) {
291                         ast_log(LOG_WARNING, "Out of memory\n");
292                         ast_config_destroy(cfg);
293                         return NULL;
294                 }
295                 ast_category_append(cfg, cat);
296         }
297
298         mailbox = ast_category_browse(rtdata, NULL);
299         while (mailbox) {
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);
306                 if (var)
307                         ast_variable_append(cat, var);
308                 else
309                         ast_log(LOG_WARNING, "Out of memory adding mailbox '%s'\n", mailbox);
310                 mailbox = ast_category_browse(rtdata, mailbox);
311         }
312         ast_config_destroy(rtdata);
313
314         return cfg;
315 }
316
317 static int do_directory(struct ast_channel *chan, struct ast_config *cfg, char *context, char *dialcontext, char digit, int last, int readext)
318 {
319         /* Read in the first three digits..  "digit" is the first digit, already read */
320         char ext[NUMDIGITS + 1];
321         char name[80] = "";
322         struct ast_variable *v;
323         int res;
324         int found=0;
325         int lastuserchoice = 0;
326         char *start, *pos, *conv,*stringp=NULL;
327
328         if (ast_strlen_zero(context)) {
329                 ast_log(LOG_WARNING,
330                         "Directory must be called with an argument "
331                         "(context in which to interpret extensions)\n");
332                 return -1;
333         }
334         if (digit == '0') {
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))) {
338                         return 0;
339                 } else {
340                         ast_log(LOG_WARNING, "Can't find extension 'o' in current context.  "
341                                 "Not Exiting the Directory!\n");
342                         res = 0;
343                 }
344         }       
345         if (digit == '*') {
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))) {
349                         return 0;
350                 } else {
351                         ast_log(LOG_WARNING, "Can't find extension 'a' in current context.  "
352                                 "Not Exiting the Directory!\n");
353                         res = 0;
354                 }
355         }       
356         memset(ext, 0, sizeof(ext));
357         ext[0] = digit;
358         res = 0;
359         if (ast_readstring(chan, ext + 1, NUMDIGITS - 1, 3000, 3000, "#") < 0) res = -1;
360         if (!res) {
361                 /* Search for all names which start with those digits */
362                 v = ast_variable_browse(cfg, context);
363                 while(v && !res) {
364                         /* Find all candidate extensions */
365                         while(v) {
366                                 /* Find a candidate extension */
367                                 start = strdup(v->value);
368                                 if (start && !strcasestr(start, "hidefromdir=yes")) {
369                                         stringp=start;
370                                         strsep(&stringp, ",");
371                                         pos = strsep(&stringp, ",");
372                                         if (pos) {
373                                                 ast_copy_string(name, pos, sizeof(name));
374                                                 /* Grab the last name */
375                                                 if (last && strrchr(pos,' '))
376                                                         pos = strrchr(pos, ' ') + 1;
377                                                 conv = convert(pos);
378                                                 if (conv) {
379                                                         if (!strcmp(conv, ext)) {
380                                                                 /* Match! */
381                                                                 found++;
382                                                                 free(conv);
383                                                                 free(start);
384                                                                 break;
385                                                         }
386                                                         free(conv);
387                                                 }
388                                         }
389                                         free(start);
390                                 }
391                                 v = v->next;
392                         }
393
394                         if (v) {
395                                 /* We have a match -- play a greeting if they have it */
396                                 res = play_mailbox_owner(chan, context, dialcontext, v->name, name, readext);
397                                 switch (res) {
398                                         case -1:
399                                                 /* user pressed '1' but extension does not exist, or
400                                                  * user hungup
401                                                  */
402                                                 lastuserchoice = 0;
403                                                 break;
404                                         case '1':
405                                                 /* user pressed '1' and extensions exists;
406                                                    play_mailbox_owner will already have done
407                                                    a goto() on the channel
408                                                  */
409                                                 lastuserchoice = res;
410                                                 break;
411                                         case '*':
412                                                 /* user pressed '*' to skip something found */
413                                                 lastuserchoice = res;
414                                                 res = 0;
415                                                 break;
416                                         default:
417                                                 break;
418                                 }
419                                 v = v->next;
420                         }
421                 }
422
423                 if (lastuserchoice != '1') {
424                         if (found) 
425                                 res = ast_streamfile(chan, "dir-nomore", chan->language);
426                         else
427                                 res = ast_streamfile(chan, "dir-nomatch", chan->language);
428                         if (!res)
429                                 res = 1;
430                         return res;
431                 }
432                 return 0;
433         }
434         return res;
435 }
436
437 static int directory_exec(struct ast_channel *chan, void *data)
438 {
439         int res = 0;
440         struct localuser *u;
441         struct ast_config *cfg;
442         int last = 1;
443         int readext = 0;
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);
449         );
450
451         if (ast_strlen_zero(data)) {
452                 ast_log(LOG_WARNING, "Directory requires an argument (context[,dialcontext])\n");
453                 return -1;
454         }
455
456         LOCAL_USER_ADD(u);
457
458         if (!(parse = ast_strdupa(data))) {
459                 LOCAL_USER_REMOVE(u);
460                 return -1; 
461         }
462
463         AST_STANDARD_APP_ARGS(args, parse);
464                 
465         if (args.options) {
466                 if (strchr(args.options, 'f'))
467                         last = 0;
468                 if (strchr(args.options, 'e'))
469                         readext = 1;
470         }
471
472         if (ast_strlen_zero(args.dialcontext))  
473                 args.dialcontext = args.vmcontext;
474
475         cfg = realtime_directory(args.vmcontext);
476         if (!cfg) {
477                 ast_log(LOG_ERROR, "Unable to read the configuration data!\n");
478                 LOCAL_USER_REMOVE(u);
479                 return -1;
480         }
481
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)) {
486                 if (last)
487                         dirintro = "dir-intro"; 
488                 else
489                         dirintro = "dir-intro-fn";
490         }
491
492         if (chan->_state != AST_STATE_UP) 
493                 res = ast_answer(chan);
494
495         for (;;) {
496                 if (!res)
497                         res = ast_streamfile(chan, dirintro, chan->language);
498                 if (!res)
499                         res = ast_waitstream(chan, AST_DIGIT_ANY);
500                 ast_stopstream(chan);
501                 if (!res)
502                         res = ast_waitfordigit(chan, 5000);
503                 if (res > 0) {
504                         res = do_directory(chan, cfg, args.vmcontext, args.dialcontext, res, last, readext);
505                         if (res > 0) {
506                                 res = ast_waitstream(chan, AST_DIGIT_ANY);
507                                 ast_stopstream(chan);
508                                 if (res >= 0) {
509                                         continue;
510                                 }
511                         }
512                 }
513                 break;
514         }
515         ast_config_destroy(cfg);
516         LOCAL_USER_REMOVE(u);
517         return res;
518 }
519
520 int unload_module(void)
521 {
522         int res;
523
524         res = ast_unregister_application(app);
525
526         STANDARD_HANGUP_LOCALUSERS;
527
528         return res;
529 }
530
531 int load_module(void)
532 {
533         return ast_register_application(app, directory_exec, synopsis, descrip);
534 }
535
536 const char *description(void)
537 {
538         return tdesc;
539 }
540
541 int usecount(void)
542 {
543         int res;
544         STANDARD_USECOUNT(res);
545         return res;
546 }
547
548 const char *key()
549 {
550         return ASTERISK_GPL_KEY;
551 }