2 * Asterisk -- A telephony toolkit for Linux.
4 * Provide a directory of extensions
6 * Copyright (C) 1999, Adtran Inc. and Linux Support Services, LLC
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/file.h>
15 #include <asterisk/logger.h>
16 #include <asterisk/channel.h>
17 #include <asterisk/pbx.h>
18 #include <asterisk/module.h>
19 #include <asterisk/config.h>
20 #include <asterisk/say.h>
26 #include "../asterisk.h"
28 static char *tdesc = "Extension Directory";
29 static char *app = "Directory";
31 /* For simplicity, I'm keeping the format compatible with the voicemail config,
32 but i'm open to suggestions for isolating it */
34 #define DIRECTORY_CONFIG "voicemail.conf"
36 /* How many digits to read in */
43 static char *convert(char *lastname)
47 tmp = malloc(NUMDIGITS + 1);
49 while((*lastname > 32) && lcount < NUMDIGITS) {
50 switch(toupper(*lastname)) {
113 static int do_directory(struct ast_channel *chan, struct ast_config *cfg, char *context, char digit)
115 /* Read in the first three digits.. "digit" is the first digit, already read */
116 char ext[NUMDIGITS + 1];
117 struct ast_variable *v;
120 char *start, *pos, *conv;
122 memset(ext, 0, sizeof(ext));
124 res = ast_readstring(chan, ext + 1, NUMDIGITS, 3000, 3000, "#");
126 /* Search for all names which start with those digits */
127 v = ast_variable_browse(cfg, context);
129 /* Find all candidate extensions */
131 /* Find a candidate extension */
132 start = strdup(v->value);
135 pos = strtok(NULL, ",");
137 /* Grab the last name */
138 if (strrchr(pos, ' '))
139 pos = strrchr(pos, ' ') + 1;
142 if (!strcmp(conv, ext)) {
157 /* We have a match -- play a greeting if they have it */
158 snprintf(fn, sizeof(fn), "%s/vm/%s/greet", AST_SPOOL_DIR, v->name);
159 if (ast_fileexists(fn, NULL)) {
160 res = ast_streamfile(chan, fn);
162 res = ast_waitstream(chan, AST_DIGIT_ANY);
163 ast_stopstream(chan);
165 res = ast_say_digit_str(chan, v->name);
169 res = ast_streamfile(chan, "dir-instr");
171 res = ast_waitstream(chan, AST_DIGIT_ANY);
173 res = ast_waitfordigit(chan, 3000);
174 ast_stopstream(chan);
177 strncpy(chan->exten, v->name, sizeof(chan->exten));
179 strncpy(chan->context, context, sizeof(chan->context));
182 } else if (res == '*') {
192 res = ast_streamfile(chan, "dir-nomore");
194 res = ast_streamfile(chan, "dir-nomatch");
205 static int directory_exec(struct ast_channel *chan, void *data)
209 struct ast_config *cfg;
211 ast_log(LOG_WARNING, "directory requires an argument (context)\n");
214 cfg = ast_load(DIRECTORY_CONFIG);
216 ast_log(LOG_WARNING, "Unable to open directory configuration %s\n", DIRECTORY_CONFIG);
222 res = ast_streamfile(chan, "dir-intro");
224 res = ast_waitstream(chan, AST_DIGIT_ANY);
225 ast_stopstream(chan);
227 res = ast_waitfordigit(chan, 5000);
229 res = do_directory(chan, cfg, (char *)data, res);
231 res = ast_waitstream(chan, AST_DIGIT_ANY);
232 ast_stopstream(chan);
239 LOCAL_USER_REMOVE(u);
243 int unload_module(void)
245 STANDARD_HANGUP_LOCALUSERS;
246 return ast_unregister_application(app);
249 int load_module(void)
251 return ast_register_application(app, directory_exec);
254 char *description(void)
262 STANDARD_USECOUNT(res);