revert my pass through the tree to remove checks of the result of ast_strdupa
[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 STANDARD_LOCAL_USER;
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) {
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         } else if (ast_fileexists(fn2, NULL, chan->language) > 0) {
178                 res = ast_streamfile(chan, fn2, chan->language);
179                 if (!res) {
180                         res = ast_waitstream(chan, AST_DIGIT_ANY);
181                 }
182                 ast_stopstream(chan);
183         } else {
184                 res = ast_say_character_str(chan, !ast_strlen_zero(name) ? name : ext,
185                                         AST_DIGIT_ANY, chan->language);
186         }
187
188         while (loop) {
189                 if (!res) {
190                         res = ast_streamfile(chan, "dir-instr", chan->language);
191                 }
192                 if (!res) {
193                         res = ast_waitstream(chan, AST_DIGIT_ANY);
194                 }
195                 if (!res) {
196                         res = ast_waitfordigit(chan, 3000);
197                 }
198                 ast_stopstream(chan);
199         
200                 if (res > -1) {
201                         switch (res) {
202                                 case '1':
203                                         /* Name selected */
204                                         loop = 0;
205                                         if (ast_goto_if_exists(chan, dialcontext, ext, 1)) {
206                                                 ast_log(LOG_WARNING,
207                                                         "Can't find extension '%s' in context '%s'.  "
208                                                         "Did you pass the wrong context to Directory?\n",
209                                                         ext, dialcontext);
210                                                 res = -1;
211                                         }
212                                         break;
213         
214                                 case '*':   
215                                         /* Skip to next match in list */
216                                         loop = 0;
217                                         break;
218         
219                                 default:
220                                         /* Not '1', or '*', so decrement number of tries */
221                                         res = 0;
222                                         loop--;
223                                         break;
224                         } /* end switch */
225                 } /* end if */
226                 else {
227                         /* User hungup, so jump out now */
228                         loop = 0;
229                 }
230         } /* end while */
231
232         return(res);
233 }
234
235 static struct ast_config *realtime_directory(char *context)
236 {
237         struct ast_config *cfg;
238         struct ast_config *rtdata;
239         struct ast_category *cat;
240         struct ast_variable *var;
241         char *mailbox;
242         char *fullname;
243         char *hidefromdir;
244         char tmp[100];
245
246         /* Load flat file config. */
247         cfg = ast_config_load(VOICEMAIL_CONFIG);
248
249         if (!cfg) {
250                 /* Loading config failed. */
251                 ast_log(LOG_WARNING, "Loading config failed.\n");
252                 return NULL;
253         }
254
255         /* Get realtime entries, categorized by their mailbox number
256            and present in the requested context */
257         rtdata = ast_load_realtime_multientry("voicemail", "mailbox LIKE", "%", "context", context, NULL);
258
259         /* if there are no results, just return the entries from the config file */
260         if (!rtdata)
261                 return cfg;
262
263         /* Does the context exist within the config file? If not, make one */
264         cat = ast_category_get(cfg, context);
265         if (!cat) {
266                 cat = ast_category_new(context);
267                 if (!cat) {
268                         ast_log(LOG_WARNING, "Out of memory\n");
269                         ast_config_destroy(cfg);
270                         return NULL;
271                 }
272                 ast_category_append(cfg, cat);
273         }
274
275         mailbox = ast_category_browse(rtdata, NULL);
276         while (mailbox) {
277                 fullname = ast_variable_retrieve(rtdata, mailbox, "fullname");
278                 hidefromdir = ast_variable_retrieve(rtdata, mailbox, "hidefromdir");
279                 snprintf(tmp, sizeof(tmp), "no-password,%s,hidefromdir=%s",
280                          fullname ? fullname : "",
281                          hidefromdir ? hidefromdir : "no");
282                 var = ast_variable_new(mailbox, tmp);
283                 if (var)
284                         ast_variable_append(cat, var);
285                 else
286                         ast_log(LOG_WARNING, "Out of memory adding mailbox '%s'\n", mailbox);
287                 mailbox = ast_category_browse(rtdata, mailbox);
288         }
289         ast_config_destroy(rtdata);
290
291         return cfg;
292 }
293
294 static int do_directory(struct ast_channel *chan, struct ast_config *cfg, char *context, char *dialcontext, char digit, int last)
295 {
296         /* Read in the first three digits..  "digit" is the first digit, already read */
297         char ext[NUMDIGITS + 1];
298         char name[80] = "";
299         struct ast_variable *v;
300         int res;
301         int found=0;
302         int lastuserchoice = 0;
303         char *start, *pos, *conv,*stringp=NULL;
304
305         if (ast_strlen_zero(context)) {
306                 ast_log(LOG_WARNING,
307                         "Directory must be called with an argument "
308                         "(context in which to interpret extensions)\n");
309                 return -1;
310         }
311         if (digit == '0') {
312                 if (!ast_goto_if_exists(chan, chan->context, "o", 1) ||
313                     (!ast_strlen_zero(chan->macrocontext) &&
314                      !ast_goto_if_exists(chan, chan->macrocontext, "o", 1))) {
315                         return 0;
316                 } else {
317                         ast_log(LOG_WARNING, "Can't find extension 'o' in current context.  "
318                                 "Not Exiting the Directory!\n");
319                         res = 0;
320                 }
321         }       
322         if (digit == '*') {
323                 if (!ast_goto_if_exists(chan, chan->context, "a", 1) ||
324                     (!ast_strlen_zero(chan->macrocontext) &&
325                      !ast_goto_if_exists(chan, chan->macrocontext, "a", 1))) {
326                         return 0;
327                 } else {
328                         ast_log(LOG_WARNING, "Can't find extension 'a' in current context.  "
329                                 "Not Exiting the Directory!\n");
330                         res = 0;
331                 }
332         }       
333         memset(ext, 0, sizeof(ext));
334         ext[0] = digit;
335         res = 0;
336         if (ast_readstring(chan, ext + 1, NUMDIGITS - 1, 3000, 3000, "#") < 0) res = -1;
337         if (!res) {
338                 /* Search for all names which start with those digits */
339                 v = ast_variable_browse(cfg, context);
340                 while(v && !res) {
341                         /* Find all candidate extensions */
342                         while(v) {
343                                 /* Find a candidate extension */
344                                 start = strdup(v->value);
345                                 if (start && !strcasestr(start, "hidefromdir=yes")) {
346                                         stringp=start;
347                                         strsep(&stringp, ",");
348                                         pos = strsep(&stringp, ",");
349                                         if (pos) {
350                                                 ast_copy_string(name, pos, sizeof(name));
351                                                 /* Grab the last name */
352                                                 if (last && strrchr(pos,' '))
353                                                         pos = strrchr(pos, ' ') + 1;
354                                                 conv = convert(pos);
355                                                 if (conv) {
356                                                         if (!strcmp(conv, ext)) {
357                                                                 /* Match! */
358                                                                 found++;
359                                                                 free(conv);
360                                                                 free(start);
361                                                                 break;
362                                                         }
363                                                         free(conv);
364                                                 }
365                                         }
366                                         free(start);
367                                 }
368                                 v = v->next;
369                         }
370
371                         if (v) {
372                                 /* We have a match -- play a greeting if they have it */
373                                 res = play_mailbox_owner(chan, context, dialcontext, v->name, name);
374                                 switch (res) {
375                                         case -1:
376                                                 /* user pressed '1' but extension does not exist, or
377                                                  * user hungup
378                                                  */
379                                                 lastuserchoice = 0;
380                                                 break;
381                                         case '1':
382                                                 /* user pressed '1' and extensions exists;
383                                                    play_mailbox_owner will already have done
384                                                    a goto() on the channel
385                                                  */
386                                                 lastuserchoice = res;
387                                                 break;
388                                         case '*':
389                                                 /* user pressed '*' to skip something found */
390                                                 lastuserchoice = res;
391                                                 res = 0;
392                                                 break;
393                                         default:
394                                                 break;
395                                 }
396                                 v = v->next;
397                         }
398                 }
399
400                 if (lastuserchoice != '1') {
401                         if (found) 
402                                 res = ast_streamfile(chan, "dir-nomore", chan->language);
403                         else
404                                 res = ast_streamfile(chan, "dir-nomatch", chan->language);
405                         if (!res)
406                                 res = 1;
407                         return res;
408                 }
409                 return 0;
410         }
411         return res;
412 }
413
414 static int directory_exec(struct ast_channel *chan, void *data)
415 {
416         int res = 0;
417         struct localuser *u;
418         struct ast_config *cfg;
419         int last = 1;
420         char *dirintro, *parse;
421         AST_DECLARE_APP_ARGS(args,
422                 AST_APP_ARG(vmcontext);
423                 AST_APP_ARG(dialcontext);
424                 AST_APP_ARG(options);
425         );
426
427         if (ast_strlen_zero(data)) {
428                 ast_log(LOG_WARNING, "Directory requires an argument (context[,dialcontext])\n");
429                 return -1;
430         }
431
432         LOCAL_USER_ADD(u);
433
434         parse = ast_strdupa(data);
435
436         if (!parse) {
437                 ast_log(LOG_ERROR, "Out of memory!\n");
438                 LOCAL_USER_REMOVE(u);
439                 return -1; 
440         }
441
442         AST_STANDARD_APP_ARGS(args, parse);
443                 
444         if (args.options) {
445                 if (strchr(args.options, 'f'))
446                         last = 0;
447         }
448
449         if (ast_strlen_zero(args.dialcontext))  
450                 args.dialcontext = args.vmcontext;
451
452         cfg = realtime_directory(args.vmcontext);
453         if (!cfg) {
454                 ast_log(LOG_ERROR, "Unable to read the configuration data!\n");
455                 LOCAL_USER_REMOVE(u);
456                 return -1;
457         }
458
459         dirintro = ast_variable_retrieve(cfg, args.vmcontext, "directoryintro");
460         if (ast_strlen_zero(dirintro))
461                 dirintro = ast_variable_retrieve(cfg, "general", "directoryintro");
462         if (ast_strlen_zero(dirintro)) {
463                 if (last)
464                         dirintro = "dir-intro"; 
465                 else
466                         dirintro = "dir-intro-fn";
467         }
468
469         if (chan->_state != AST_STATE_UP) 
470                 res = ast_answer(chan);
471
472         for (;;) {
473                 if (!res)
474                         res = ast_streamfile(chan, dirintro, chan->language);
475                 if (!res)
476                         res = ast_waitstream(chan, AST_DIGIT_ANY);
477                 ast_stopstream(chan);
478                 if (!res)
479                         res = ast_waitfordigit(chan, 5000);
480                 if (res > 0) {
481                         res = do_directory(chan, cfg, args.vmcontext, args.dialcontext, res, last);
482                         if (res > 0) {
483                                 res = ast_waitstream(chan, AST_DIGIT_ANY);
484                                 ast_stopstream(chan);
485                                 if (res >= 0) {
486                                         continue;
487                                 }
488                         }
489                 }
490                 break;
491         }
492         ast_config_destroy(cfg);
493         LOCAL_USER_REMOVE(u);
494         return res;
495 }
496
497 int unload_module(void)
498 {
499         int res;
500
501         res = ast_unregister_application(app);
502
503         STANDARD_HANGUP_LOCALUSERS;
504
505         return res;
506 }
507
508 int load_module(void)
509 {
510         return ast_register_application(app, directory_exec, synopsis, descrip);
511 }
512
513 char *description(void)
514 {
515         return tdesc;
516 }
517
518 int usecount(void)
519 {
520         int res;
521         STANDARD_USECOUNT(res);
522         return res;
523 }
524
525 char *key()
526 {
527         return ASTERISK_GPL_KEY;
528 }