Index: Makefile =================================================================== RCS file: /usr/cvsroot/asterisk/apps/Makefile,v retrieving revision 1.45 diff -u -3 -p -r1.45 Makefile --- a/apps/Makefile 13 Nov 2003 18:30:15 -0000 1.45 +++ b/apps/Makefile 21 Nov 2003 23:18:48 -0000 @@ -77,6 +77,13 @@ else endif endif +app_directory.so: app_directory.o +ifeq ($(USE_POSTGRES_VM_INTERFACE),1) + $(CC) $(SOLINK) -o $@ $(MLFLAGS) $< -lpq +else + $(CC) $(SOLINK) -o $@ $(MLFLAGS) $< +endif + app_sql_postgres.o: app_sql_postgres.c $(CC) -pipe -I/usr/local/pgsql/include $(CFLAGS) -c -o app_sql_postgres.o app_sql_postgres.c Index: app_directory.c =================================================================== RCS file: /usr/cvsroot/asterisk/apps/app_directory.c,v retrieving revision 1.14 diff -u -3 -p -r1.14 app_directory.c --- a/apps/app_directory.c 9 Nov 2003 06:16:07 -0000 1.14 +++ b/apps/app_directory.c 21 Nov 2003 23:19:05 -0000 @@ -26,6 +26,26 @@ #include #include "../asterisk.h" #include "../astconf.h" +/* we define USESQLVM when we have MySQL or POSTGRES */ +#ifdef USEMYSQLVM +#include +#define USESQLVM 1 +#endif + +#ifdef USEPOSTGRESVM +/* + * PostgreSQL routines written by Otmar Lendl + */ +#include +#define USESQLVM 1 +#endif + +#ifndef USESQLVM +static inline int sql_init(void) { return 0; } +static inline void sql_close(void) { } +#endif + +#define VOICEMAIL_CONFIG "voicemail.conf" static char *tdesc = "Extension Directory"; static char *app = "Directory"; @@ -120,16 +140,189 @@ static char *convert(char *lastname) return tmp; } +static int found_match(struct ast_channel *chan,char *context, char *ext) +{ + char fn[256]; + char fn2[256]; + int res; + + /* We have a match -- play a greeting if they have it */ + /* Check for the VoiceMail2 greeting first */ + snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/greet", (char *)ast_config_AST_SPOOL_DIR, context, ext); + /* Otherwise, check for an old-style Voicemail greeting */ + snprintf(fn2, sizeof(fn2), "%s/vm/%s/greet", (char *)ast_config_AST_SPOOL_DIR, ext); + if (ast_fileexists(fn, NULL, chan->language) > 0) { + res = ast_streamfile(chan, fn, chan->language); + if (!res) + res = ast_waitstream(chan, AST_DIGIT_ANY); + ast_stopstream(chan); + } 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); + } else { + /* Last resort, just say the extension number */ + res = ast_say_digit_str(chan, ext, AST_DIGIT_ANY, chan->language); + } +ahem: + if (!res) + res = ast_streamfile(chan, "dir-instr", chan->language); + 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') { + /* Press 1 if it's correct */ + strncpy(chan->exten, ext, sizeof(chan->exten)-1); + chan->priority = 0; + strncpy(chan->context, context, sizeof(chan->context)-1); + res = 0; + } else if (res == '*') { + /* Press * for the next match */ + return -1; + } else { + res = 0; + goto ahem; + } + } + return res; +} + +#ifdef USEPOSTGRESVM + +PGconn *dbhandler; +char dboption[256]; +ast_mutex_t postgreslock; + +static int sql_init(void) +{ + dbhandler=PQconnectdb(dboption); + if (PQstatus(dbhandler) == CONNECTION_BAD) { + ast_log(LOG_WARNING, "Error Logging into database %s: %s\n",dboption,PQerrorMessage(dbhandler)); + return(-1); + } + ast_mutex_init(&postgreslock); + +/* fprintf(stderr,"postgres login OK\n"); */ + return(0); +} + +static void sql_close(void) +{ + PQfinish(dbhandler); +} + +static int postgres_find_match(struct ast_channel *chan,char *context,char *ext) +{ + char query[240]; + PGresult *PGSQLres; + char *mailbox; + char *fullname; + int i; + char *conv; + int found = 0; + char *pos; + int res; + + sprintf(query,"SELECT mailbox,fullname from voicemail where context='%s'",context); + ast_mutex_lock(&postgreslock); + PGSQLres = PQexec(dbhandler,query); + if (PGSQLres != NULL) { + if (PQresultStatus(PGSQLres) == PGRES_BAD_RESPONSE || + PQresultStatus(PGSQLres) == PGRES_NONFATAL_ERROR || + PQresultStatus(PGSQLres) == PGRES_FATAL_ERROR) { + ast_log(LOG_WARNING,"PGSQL_query: Query Error (%s) Calling PQreset\n",PQcmdStatus(PGSQLres)); + PQclear(PGSQLres); + PQreset(dbhandler); + ast_mutex_unlock(&postgreslock); + return -1; + } + } + for(i = 0;i < PQntuples(PGSQLres);i++) { + mailbox = PQgetvalue(PGSQLres,i,0); + fullname = PQgetvalue(PGSQLres,i,1); + pos = fullname; + if (strrchr(fullname,' ')) + pos = strrchr(fullname,' ') + 1; + conv = convert(pos); + if (conv) { + if (!strcmp(conv,ext)) { + found++; + free(conv); + res = found_match(chan,context,mailbox); + if (res == 0) { + found = -1; + break; + } + } + } + } + PQclear(PGSQLres); + ast_mutex_unlock(&postgreslock); + + /* return number of matches found, assists in determining appropriate prompts */ + return found; +} +#endif + +static int find_match(struct ast_channel *chan,struct ast_config *cfg,char *context,char *ext) +{ + struct ast_variable *v; + int res = 0; + char *start; + char *stringp; + char *pos; + char *conv; + int found = 0; + + /* 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) { + stringp=start; + strsep(&stringp, ","); + pos = strsep(&stringp, ","); + 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); + res = found_match(chan,context,v->name); + if (res == 0) + return -1; + } else + free(conv); + } + } else + free(start); + } + v = v->next; + } + } + return found; +} + 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; - char *start, *pos, *conv,*stringp=NULL; - char fn[256]; - char fn2[256]; + int found = 0; + if (!context || !strlen(context)) { ast_log(LOG_WARNING, "Directory must be called with an argument (context in which to interpret extensions)\n"); return -1; @@ -138,92 +331,18 @@ static int do_directory(struct ast_chann ext[0] = digit; res = 0; if (ast_readstring(chan, ext + 1, NUMDIGITS - 1, 3000, 3000, "#") < 0) res = -1; - 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) { - stringp=start; - strsep(&stringp, ","); - pos = strsep(&stringp, ","); - 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 */ - /* 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); - if (ast_fileexists(fn, NULL, chan->language) > 0) { - res = ast_streamfile(chan, fn, chan->language); - if (!res) - res = ast_waitstream(chan, AST_DIGIT_ANY); - ast_stopstream(chan); - } 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); - } else { - res = ast_say_digit_str(chan, v->name, AST_DIGIT_ANY, chan->language); - } -ahem: - if (!res) - res = ast_streamfile(chan, "dir-instr", chan->language); - 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') { - strncpy(chan->exten, v->name, sizeof(chan->exten)-1); - chan->priority = 0; - strncpy(chan->context, context, sizeof(chan->context)-1); - res = 0; - break; - } else if (res == '*') { - res = 0; - v = v->next; - } else { - res = 0; - goto ahem; - } - } - } else { - if (found) - res = ast_streamfile(chan, "dir-nomore", chan->language); - else - res = ast_streamfile(chan, "dir-nomatch", chan->language); - if (!res) - res = 1; - return res; - } - } - - } +#ifdef USESQLVM + found = postgres_find_match(chan,context,ext); +#else + found = find_match(chan,cfg,context,ext); +#endif + if (found > 0) + res = ast_streamfile(chan, "dir-nomore", chan->language); + else if (found == 0) + res = ast_streamfile(chan, "dir-nomatch", chan->language); + else + res = 0; + return res; } @@ -267,15 +386,52 @@ top: return res; } +static int load_config(void) +{ + struct ast_config *cfg; + + cfg = ast_load(VOICEMAIL_CONFIG); + +#ifdef USEPOSTGRESVM + char *s; + + if (!(s=ast_variable_retrieve(cfg, "general", "dboption"))) { + strcpy(dboption, "dboption not-specified in voicemail.conf"); + } else { + strcpy(dboption, s); + } +#endif + return 0; +} + +int reload(void) +{ + return(load_config()); +} + int unload_module(void) { STANDARD_HANGUP_LOCALUSERS; + sql_close(); return ast_unregister_application(app); } int load_module(void) { - return ast_register_application(app, directory_exec, synopsis, descrip); + int res; + + if ((res = ast_register_application(app, directory_exec, synopsis, descrip))) { + return res; + } + + if ((res = load_config())) + return res; + + if ((res = sql_init())) { + ast_log(LOG_WARNING, "SQL init\n"); + return res; + } + return res; } char *description(void)