1999-10-30 01:01:34 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- A telephony toolkit for Linux.
|
|
|
|
*
|
|
|
|
* Provide a directory of extensions
|
|
|
|
*
|
1999-12-11 20:09:45 +00:00
|
|
|
* Copyright (C) 1999, Mark Spencer
|
1999-10-30 01:01:34 +00:00
|
|
|
*
|
|
|
|
* Mark Spencer <markster@linux-support.net>
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License
|
|
|
|
*/
|
|
|
|
|
2002-05-13 22:29:39 +00:00
|
|
|
#include <asterisk/lock.h>
|
1999-10-30 01:01:34 +00:00
|
|
|
#include <asterisk/file.h>
|
|
|
|
#include <asterisk/logger.h>
|
|
|
|
#include <asterisk/channel.h>
|
|
|
|
#include <asterisk/pbx.h>
|
|
|
|
#include <asterisk/module.h>
|
|
|
|
#include <asterisk/config.h>
|
|
|
|
#include <asterisk/say.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "../asterisk.h"
|
2003-01-30 15:03:20 +00:00
|
|
|
#include "../astconf.h"
|
1999-10-30 01:01:34 +00:00
|
|
|
|
|
|
|
static char *tdesc = "Extension Directory";
|
|
|
|
static char *app = "Directory";
|
|
|
|
|
2001-04-10 17:18:04 +00:00
|
|
|
static char *synopsis = "Provide directory of voicemail extensions";
|
|
|
|
static char *descrip =
|
2001-08-05 21:46:13 +00:00
|
|
|
" Directory(context): Presents the user with a directory of extensions from\n"
|
|
|
|
"which they may select by name. The list of names and extensions is\n"
|
|
|
|
"discovered from voicemail.conf. The context argument is required, and\n"
|
2003-09-29 20:10:49 +00:00
|
|
|
"specifies the context in which to interpret the extensions. Returns 0\n"
|
2001-08-05 21:46:13 +00:00
|
|
|
"unless the user hangs up. It also sets up the channel on exit to enter the\n"
|
|
|
|
"extension the user selected.\n";
|
2001-04-10 17:18:04 +00:00
|
|
|
|
1999-10-30 01:01:34 +00:00
|
|
|
/* For simplicity, I'm keeping the format compatible with the voicemail config,
|
|
|
|
but i'm open to suggestions for isolating it */
|
|
|
|
|
|
|
|
#define DIRECTORY_CONFIG "voicemail.conf"
|
|
|
|
|
|
|
|
/* How many digits to read in */
|
|
|
|
#define NUMDIGITS 3
|
|
|
|
|
|
|
|
STANDARD_LOCAL_USER;
|
|
|
|
|
|
|
|
LOCAL_USER_DECL;
|
|
|
|
|
|
|
|
static char *convert(char *lastname)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
int lcount = 0;
|
|
|
|
tmp = malloc(NUMDIGITS + 1);
|
|
|
|
if (tmp) {
|
|
|
|
while((*lastname > 32) && lcount < NUMDIGITS) {
|
|
|
|
switch(toupper(*lastname)) {
|
|
|
|
case '1':
|
|
|
|
tmp[lcount++] = '1';
|
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
case 'A':
|
|
|
|
case 'B':
|
|
|
|
case 'C':
|
|
|
|
tmp[lcount++] = '2';
|
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
case 'D':
|
|
|
|
case 'E':
|
|
|
|
case 'F':
|
|
|
|
tmp[lcount++] = '3';
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
case 'G':
|
|
|
|
case 'H':
|
|
|
|
case 'I':
|
|
|
|
tmp[lcount++] = '4';
|
|
|
|
break;
|
|
|
|
case '5':
|
|
|
|
case 'J':
|
|
|
|
case 'K':
|
|
|
|
case 'L':
|
|
|
|
tmp[lcount++] = '5';
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
case 'M':
|
|
|
|
case 'N':
|
|
|
|
case 'O':
|
|
|
|
tmp[lcount++] = '6';
|
|
|
|
break;
|
|
|
|
case '7':
|
|
|
|
case 'P':
|
|
|
|
case 'Q':
|
|
|
|
case 'R':
|
|
|
|
case 'S':
|
|
|
|
tmp[lcount++] = '7';
|
|
|
|
break;
|
|
|
|
case '8':
|
|
|
|
case 'T':
|
|
|
|
case 'U':
|
|
|
|
case 'V':
|
|
|
|
tmp[lcount++] = '8';
|
|
|
|
break;
|
|
|
|
case '9':
|
|
|
|
case 'W':
|
|
|
|
case 'X':
|
|
|
|
case 'Y':
|
|
|
|
case 'Z':
|
|
|
|
tmp[lcount++] = '9';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
lastname++;
|
|
|
|
}
|
|
|
|
tmp[lcount] = '\0';
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_directory(struct ast_channel *chan, struct ast_config *cfg, char *context, char digit)
|
|
|
|
{
|
|
|
|
/* Read in the first three digits.. "digit" is the first digit, already read */
|
|
|
|
char ext[NUMDIGITS + 1];
|
|
|
|
struct ast_variable *v;
|
|
|
|
int res;
|
|
|
|
int found=0;
|
2003-01-30 15:03:20 +00:00
|
|
|
char *start, *pos, *conv,*stringp=NULL;
|
1999-10-30 01:01:34 +00:00
|
|
|
char fn[256];
|
2003-08-01 23:14:37 +00:00
|
|
|
char fn2[256];
|
2003-03-26 21:09:56 +00:00
|
|
|
if (!context || !strlen(context)) {
|
|
|
|
ast_log(LOG_WARNING, "Directory must be called with an argument (context in which to interpret extensions)\n");
|
|
|
|
return -1;
|
|
|
|
}
|
1999-10-30 01:01:34 +00:00
|
|
|
memset(ext, 0, sizeof(ext));
|
|
|
|
ext[0] = digit;
|
2001-08-05 21:46:13 +00:00
|
|
|
res = 0;
|
2002-05-13 22:29:39 +00:00
|
|
|
if (ast_readstring(chan, ext + 1, NUMDIGITS - 1, 3000, 3000, "#") < 0) res = -1;
|
1999-10-30 01:01:34 +00:00
|
|
|
if (!res) {
|
|
|
|
/* Search for all names which start with those digits */
|
|
|
|
v = ast_variable_browse(cfg, context);
|
|
|
|
while(v && !res) {
|
|
|
|
/* Find all candidate extensions */
|
|
|
|
while(v) {
|
|
|
|
/* Find a candidate extension */
|
|
|
|
start = strdup(v->value);
|
|
|
|
if (start) {
|
2003-01-30 15:03:20 +00:00
|
|
|
stringp=start;
|
|
|
|
strsep(&stringp, ",");
|
|
|
|
pos = strsep(&stringp, ",");
|
1999-10-30 01:01:34 +00:00
|
|
|
if (pos) {
|
|
|
|
/* Grab the last name */
|
|
|
|
if (strrchr(pos, ' '))
|
|
|
|
pos = strrchr(pos, ' ') + 1;
|
|
|
|
conv = convert(pos);
|
|
|
|
if (conv) {
|
|
|
|
if (!strcmp(conv, ext)) {
|
|
|
|
/* Match! */
|
|
|
|
found++;
|
|
|
|
free(conv);
|
|
|
|
free(start);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(conv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(start);
|
|
|
|
}
|
|
|
|
v = v->next;
|
|
|
|
}
|
|
|
|
if (v) {
|
|
|
|
/* We have a match -- play a greeting if they have it */
|
2003-08-01 23:14:37 +00:00
|
|
|
/* Check for the VoiceMail2 greeting first */
|
|
|
|
snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/greet", (char *)ast_config_AST_SPOOL_DIR, context, v->name);
|
|
|
|
/* Otherwise, check for an old-style Voicemail greeting */
|
|
|
|
snprintf(fn2, sizeof(fn2), "%s/vm/%s/greet", (char *)ast_config_AST_SPOOL_DIR, v->name);
|
2002-05-13 22:29:39 +00:00
|
|
|
if (ast_fileexists(fn, NULL, chan->language) > 0) {
|
2000-01-09 19:58:18 +00:00
|
|
|
res = ast_streamfile(chan, fn, chan->language);
|
1999-10-30 01:01:34 +00:00
|
|
|
if (!res)
|
|
|
|
res = ast_waitstream(chan, AST_DIGIT_ANY);
|
|
|
|
ast_stopstream(chan);
|
2003-08-01 23:14:37 +00:00
|
|
|
} else if (ast_fileexists(fn2, NULL, chan->language) > 0) {
|
|
|
|
res = ast_streamfile(chan, fn2, chan->language);
|
|
|
|
if (!res)
|
|
|
|
res = ast_waitstream(chan, AST_DIGIT_ANY);
|
|
|
|
ast_stopstream(chan);
|
1999-10-30 01:01:34 +00:00
|
|
|
} else {
|
2001-12-27 11:07:33 +00:00
|
|
|
res = ast_say_digit_str(chan, v->name, AST_DIGIT_ANY, chan->language);
|
1999-10-30 01:01:34 +00:00
|
|
|
}
|
|
|
|
ahem:
|
|
|
|
if (!res)
|
2000-01-09 19:58:18 +00:00
|
|
|
res = ast_streamfile(chan, "dir-instr", chan->language);
|
1999-10-30 01:01:34 +00:00
|
|
|
if (!res)
|
|
|
|
res = ast_waitstream(chan, AST_DIGIT_ANY);
|
|
|
|
if (!res)
|
|
|
|
res = ast_waitfordigit(chan, 3000);
|
|
|
|
ast_stopstream(chan);
|
|
|
|
if (res > -1) {
|
|
|
|
if (res == '1') {
|
2001-12-27 11:07:33 +00:00
|
|
|
strncpy(chan->exten, v->name, sizeof(chan->exten)-1);
|
1999-10-30 01:01:34 +00:00
|
|
|
chan->priority = 0;
|
2001-12-27 11:07:33 +00:00
|
|
|
strncpy(chan->context, context, sizeof(chan->context)-1);
|
1999-10-30 01:01:34 +00:00
|
|
|
res = 0;
|
|
|
|
break;
|
|
|
|
} else if (res == '*') {
|
|
|
|
res = 0;
|
|
|
|
v = v->next;
|
|
|
|
} else {
|
|
|
|
res = 0;
|
|
|
|
goto ahem;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (found)
|
2000-01-09 19:58:18 +00:00
|
|
|
res = ast_streamfile(chan, "dir-nomore", chan->language);
|
1999-10-30 01:01:34 +00:00
|
|
|
else
|
2000-01-09 19:58:18 +00:00
|
|
|
res = ast_streamfile(chan, "dir-nomatch", chan->language);
|
1999-10-30 01:01:34 +00:00
|
|
|
if (!res)
|
|
|
|
res = 1;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int directory_exec(struct ast_channel *chan, void *data)
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
struct localuser *u;
|
|
|
|
struct ast_config *cfg;
|
|
|
|
if (!data) {
|
|
|
|
ast_log(LOG_WARNING, "directory requires an argument (context)\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cfg = ast_load(DIRECTORY_CONFIG);
|
|
|
|
if (!cfg) {
|
|
|
|
ast_log(LOG_WARNING, "Unable to open directory configuration %s\n", DIRECTORY_CONFIG);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
LOCAL_USER_ADD(u);
|
|
|
|
top:
|
|
|
|
if (!res)
|
2000-01-09 19:58:18 +00:00
|
|
|
res = ast_streamfile(chan, "dir-intro", chan->language);
|
1999-10-30 01:01:34 +00:00
|
|
|
if (!res)
|
|
|
|
res = ast_waitstream(chan, AST_DIGIT_ANY);
|
|
|
|
ast_stopstream(chan);
|
|
|
|
if (!res)
|
|
|
|
res = ast_waitfordigit(chan, 5000);
|
|
|
|
if (res > 0) {
|
|
|
|
res = do_directory(chan, cfg, (char *)data, res);
|
|
|
|
if (res > 0) {
|
|
|
|
res = ast_waitstream(chan, AST_DIGIT_ANY);
|
|
|
|
ast_stopstream(chan);
|
|
|
|
if (res >= 0) {
|
|
|
|
goto top;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast_destroy(cfg);
|
|
|
|
LOCAL_USER_REMOVE(u);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int unload_module(void)
|
|
|
|
{
|
|
|
|
STANDARD_HANGUP_LOCALUSERS;
|
|
|
|
return ast_unregister_application(app);
|
|
|
|
}
|
|
|
|
|
|
|
|
int load_module(void)
|
|
|
|
{
|
2001-04-10 17:18:04 +00:00
|
|
|
return ast_register_application(app, directory_exec, synopsis, descrip);
|
1999-10-30 01:01:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *description(void)
|
|
|
|
{
|
|
|
|
return tdesc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usecount(void)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
STANDARD_USECOUNT(res);
|
|
|
|
return res;
|
|
|
|
}
|
2001-03-07 00:52:22 +00:00
|
|
|
|
|
|
|
char *key()
|
|
|
|
{
|
|
|
|
return ASTERISK_GPL_KEY;
|
|
|
|
}
|