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