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