Allow directory to be searched by first name (bug #2208)
[asterisk/asterisk.git] / apps / app_directory.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Provide a directory of extensions
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13  
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/module.h>
20 #include <asterisk/config.h>
21 #include <asterisk/say.h>
22 #include <asterisk/utils.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include "../asterisk.h"
28 #include "../astconf.h"
29
30 static char *tdesc = "Extension Directory";
31 static char *app = "Directory";
32
33 static char *synopsis = "Provide directory of voicemail extensions";
34 static char *descrip =
35 "  Directory(vm-context[|dial-context[|options]]): Presents the user with a directory\n"
36 "of extensions from which they  may  select  by name. The  list  of  names \n"
37 "and  extensions  is discovered from  voicemail.conf. The  vm-context  argument\n"
38 "is required, and specifies  the  context  of voicemail.conf to use.  The\n"
39 "dial-context is the context to use for dialing the users, and defaults to\n"
40 "the vm-context if unspecified. The 'f' option causes the directory to match\n"
41 "based on the first name in voicemail.conf instead of the last name.\n"
42 "Returns 0 unless the user hangs up. It  also sets up the channel on exit\n"
43 "to enter the extension the user selected.\n";
44
45 /* For simplicity, I'm keeping the format compatible with the voicemail config,
46    but i'm open to suggestions for isolating it */
47
48 #define DIRECTORY_CONFIG "voicemail.conf"
49
50 /* How many digits to read in */
51 #define NUMDIGITS 3
52
53 STANDARD_LOCAL_USER;
54
55 LOCAL_USER_DECL;
56
57 static char *convert(char *lastname)
58 {
59         char *tmp;
60         int lcount = 0;
61         tmp = malloc(NUMDIGITS + 1);
62         if (tmp) {
63                 while((*lastname > 32) && lcount < NUMDIGITS) {
64                         switch(toupper(*lastname)) {
65                         case '1':
66                                 tmp[lcount++] = '1';
67                                 break;
68                         case '2':
69                         case 'A':
70                         case 'B':
71                         case 'C':
72                                 tmp[lcount++] = '2';
73                                 break;
74                         case '3':
75                         case 'D':
76                         case 'E':
77                         case 'F':
78                                 tmp[lcount++] = '3';
79                                 break;
80                         case '4':
81                         case 'G':
82                         case 'H':
83                         case 'I':
84                                 tmp[lcount++] = '4';
85                                 break;
86                         case '5':
87                         case 'J':
88                         case 'K':
89                         case 'L':
90                                 tmp[lcount++] = '5';
91                                 break;
92                         case '6':
93                         case 'M':
94                         case 'N':
95                         case 'O':
96                                 tmp[lcount++] = '6';
97                                 break;
98                         case '7':
99                         case 'P':
100                         case 'Q':
101                         case 'R':
102                         case 'S':
103                                 tmp[lcount++] = '7';
104                                 break;
105                         case '8':
106                         case 'T':
107                         case 'U':
108                         case 'V':
109                                 tmp[lcount++] = '8';
110                                 break;
111                         case '9':
112                         case 'W':
113                         case 'X':
114                         case 'Y':
115                         case 'Z':
116                                 tmp[lcount++] = '9';
117                                 break;
118                         }
119                         lastname++;
120                 }
121                 tmp[lcount] = '\0';
122         }
123         return tmp;
124 }
125
126 /* play name of mailbox owner.
127  * returns:  -1 for bad or missing extension
128  *           '1' for selected entry from directory
129  *           '*' for skipped entry from directory
130  */
131 static int play_mailbox_owner(struct ast_channel *chan, char *context, char *dialcontext, char *ext, char *name) {
132         int res = 0;
133         int loop = 3;
134         char fn[256];
135         char fn2[256];
136
137         /* Check for the VoiceMail2 greeting first */
138         snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/greet",
139                 (char *)ast_config_AST_SPOOL_DIR, context, ext);
140
141         /* Otherwise, check for an old-style Voicemail greeting */
142         snprintf(fn2, sizeof(fn2), "%s/vm/%s/greet",
143                 (char *)ast_config_AST_SPOOL_DIR, ext);
144
145         if (ast_fileexists(fn, NULL, chan->language) > 0) {
146                 res = ast_streamfile(chan, fn, chan->language);
147                 if (!res) {
148                         res = ast_waitstream(chan, AST_DIGIT_ANY);
149                 }
150                 ast_stopstream(chan);
151         } else if (ast_fileexists(fn2, NULL, chan->language) > 0) {
152                 res = ast_streamfile(chan, fn2, chan->language);
153                 if (!res) {
154                         res = ast_waitstream(chan, AST_DIGIT_ANY);
155                 }
156                 ast_stopstream(chan);
157         } else {
158                 res = ast_say_character_str(chan, !ast_strlen_zero(name) ? name : ext,
159                                         AST_DIGIT_ANY, chan->language);
160         }
161
162         while (loop) {
163                 if (!res) {
164                         res = ast_streamfile(chan, "dir-instr", chan->language);
165                 }
166                 if (!res) {
167                         res = ast_waitstream(chan, AST_DIGIT_ANY);
168                 }
169                 if (!res) {
170                         res = ast_waitfordigit(chan, 3000);
171                 }
172                 ast_stopstream(chan);
173         
174                 if (res > -1) {
175                         switch (res) {
176                                 case '1':
177                                         /* Name selected */
178                                         loop = 0;
179                                         if (ast_exists_extension(chan,dialcontext,ext,1,chan->callerid)) {
180                                                 strncpy(chan->exten, ext, sizeof(chan->exten)-1);
181                                                 chan->priority = 0;
182                                                 strncpy(chan->context, dialcontext, sizeof(chan->context)-1);
183                                         } else {
184                                                 ast_log(LOG_WARNING,
185                                                         "Can't find extension '%s' in context '%s'.  "
186                                                         "Did you pass the wrong context to Directory?\n",
187                                                         ext, context);
188                                                 res = -1;
189                                         }
190                                         break;
191         
192                                 case '*':   
193                                         /* Skip to next match in list */
194                                         loop = 0;
195                                         break;
196         
197                                 default:
198                                         /* Not '1', or '*', so decrement number of tries */
199                                         res = 0;
200                                         loop--;
201                                         break;
202                         } /* end switch */
203                 } /* end if */
204                 else {
205                         /* User hungup, so jump out now */
206                         loop = 0;
207                 }
208         } /* end while */
209
210         return(res);
211 }
212
213 static int do_directory(struct ast_channel *chan, struct ast_config *cfg, char *context, char *dialcontext, char digit, int last)
214 {
215         /* Read in the first three digits..  "digit" is the first digit, already read */
216         char ext[NUMDIGITS + 1];
217         char name[80] = "";
218         struct ast_variable *v;
219         int res;
220         int found=0;
221         int lastuserchoice = 0;
222         char *start, *pos, *conv,*stringp=NULL;
223
224         if (!context || ast_strlen_zero(context)) {
225                 ast_log(LOG_WARNING,
226                         "Directory must be called with an argument "
227                         "(context in which to interpret extensions)\n");
228                 return -1;
229         }
230         memset(ext, 0, sizeof(ext));
231         ext[0] = digit;
232         res = 0;
233         if (ast_readstring(chan, ext + 1, NUMDIGITS - 1, 3000, 3000, "#") < 0) res = -1;
234         if (!res) {
235                 /* Search for all names which start with those digits */
236                 v = ast_variable_browse(cfg, context);
237                 while(v && !res) {
238                         /* Find all candidate extensions */
239                         while(v) {
240                                 /* Find a candidate extension */
241                                 start = strdup(v->value);
242                                 if (start) {
243                                         stringp=start;
244                                         strsep(&stringp, ",");
245                                         pos = strsep(&stringp, ",");
246                                         if (pos) {
247                                                 strncpy(name, pos, sizeof(name) - 1);
248                                                 /* Grab the last name */
249                                                 if (last && strrchr(pos,' '))
250                                                         pos = strrchr(pos, ' ') + 1;
251                                                 conv = convert(pos);
252                                                 if (conv) {
253                                                         if (!strcmp(conv, ext)) {
254                                                                 /* Match! */
255                                                                 found++;
256                                                                 free(conv);
257                                                                 free(start);
258                                                                 break;
259                                                         }
260                                                         free(conv);
261                                                 }
262                                         }
263                                         free(start);
264                                 }
265                                 v = v->next;
266                         }
267
268                         if (v) {
269                                 /* We have a match -- play a greeting if they have it */
270                                 res = play_mailbox_owner(chan, context, dialcontext, v->name, name);
271                                 switch (res) {
272                                         case -1:
273                                                 /* user pressed '1' but extension does not exist, or
274                                                  * user hungup
275                                                  */
276                                                 lastuserchoice = 0;
277                                                 break;
278                                         case '1':
279                                                 /* user pressed '1' and extensions exists */
280                                                 lastuserchoice = res;
281                                                 strncpy(chan->context, dialcontext, sizeof(chan->context) - 1);
282                                                 strncpy(chan->exten, v->name, sizeof(chan->exten) - 1);
283                                                 chan->priority = 0;
284                                                 break;
285                                         case '*':
286                                                 /* user pressed '*' to skip something found */
287                                                 lastuserchoice = res;
288                                                 res = 0;
289                                                 break;
290                                         default:
291                                                 break;
292                                 }
293                                 v = v->next;
294                         }
295                 }
296
297                 if (lastuserchoice != '1') {
298                         if (found) 
299                                 res = ast_streamfile(chan, "dir-nomore", chan->language);
300                         else
301                                 res = ast_streamfile(chan, "dir-nomatch", chan->language);
302                         if (!res)
303                                 res = 1;
304                         return res;
305                 }
306                 return 0;
307         }
308         return res;
309 }
310
311 static int directory_exec(struct ast_channel *chan, void *data)
312 {
313         int res = 0;
314         struct localuser *u;
315         struct ast_config *cfg;
316         int last = 1;
317         char *context, *dialcontext, *dirintro, *options;
318
319         if (!data) {
320                 ast_log(LOG_WARNING, "directory requires an argument (context[,dialcontext])\n");
321                 return -1;
322         }
323         cfg = ast_load(DIRECTORY_CONFIG);
324         if (!cfg) {
325                 ast_log(LOG_WARNING, "Unable to open directory configuration %s\n", DIRECTORY_CONFIG);
326                 return -1;
327         }
328         LOCAL_USER_ADD(u);
329 top:
330         context = ast_strdupa(data);
331         dialcontext = strchr(context, '|');
332         if (dialcontext) {
333                 *dialcontext = '\0';
334                 dialcontext++;
335                 options = strchr(dialcontext, '|');
336                 if (options) {
337                         *options = '\0';
338                         options++; 
339                         if (strchr(options, 'f'))
340                                 last = 0;
341                 }
342         } else  
343                 dialcontext = context;
344         dirintro = ast_variable_retrieve(cfg, context, "directoryintro");
345         if (!dirintro || ast_strlen_zero(dirintro))
346                 dirintro = ast_variable_retrieve(cfg, "general", "directoryintro");
347         if (!dirintro || ast_strlen_zero(dirintro)) {
348                 if (last)
349                         dirintro = "dir-intro"; 
350                 else
351                         dirintro = "dir-intro-fn";
352         }
353         if (chan->_state != AST_STATE_UP) 
354                 res = ast_answer(chan);
355         if (!res)
356                 res = ast_streamfile(chan, dirintro, chan->language);
357         if (!res)
358                 res = ast_waitstream(chan, AST_DIGIT_ANY);
359         ast_stopstream(chan);
360         if (!res)
361                 res = ast_waitfordigit(chan, 5000);
362         if (res > 0) {
363                 res = do_directory(chan, cfg, context, dialcontext, res, last);
364                 if (res > 0) {
365                         res = ast_waitstream(chan, AST_DIGIT_ANY);
366                         ast_stopstream(chan);
367                         if (res >= 0) {
368                                 goto top;
369                         }
370                 }
371         }
372         ast_destroy(cfg);
373         LOCAL_USER_REMOVE(u);
374         return res;
375 }
376
377 int unload_module(void)
378 {
379         STANDARD_HANGUP_LOCALUSERS;
380         return ast_unregister_application(app);
381 }
382
383 int load_module(void)
384 {
385         return ast_register_application(app, directory_exec, synopsis, descrip);
386 }
387
388 char *description(void)
389 {
390         return tdesc;
391 }
392
393 int usecount(void)
394 {
395         int res;
396         STANDARD_USECOUNT(res);
397         return res;
398 }
399
400 char *key()
401 {
402         return ASTERISK_GPL_KEY;
403 }