--- asterisk.orig/apps/app_directory.c 2003-11-28 21:12:51.000000000 -0600 +++ asterisk/apps/app_directory.c 2003-11-29 12:38:09.000000000 -0600 @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include #include #include @@ -27,6 +29,23 @@ #include "../asterisk.h" #include "../astconf.h" +#ifdef USEUNIXODBCVM +/* + * unixODBC routines written by Brian K. West + */ +#include +#include +#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 +139,344 @@ 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 VoiceMail greeting first */ + snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/greet", (char *)ast_config_AST_SPOOL_DIR, context, 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 USEUNIXODBCVM + +SQLHENV ODBC_env; /* global ODBC Environment */ +int ODBC_res; /* global ODBC Result of Functions */ +SQLHDBC ODBC_con; /* global ODBC Connection Handle */ +SQLHSTMT ODBC_stmt; /* global ODBC Statement Handle */ + +ast_mutex_t unixodbc_lock; + +extern int unixodbc_do_query(char *query); + +char unixodbcdsn[80]; +char unixodbcusername[80]; +char unixodbcpassword[80]; + +static int unixodbc_init(void) +{ + long int ODBC_err; + short int ODBC_mlen; + char ODBC_msg[200], ODBC_stat[10]; + + // 1. allocate env handle and register version + ODBC_res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &ODBC_env); + + if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) + { + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: Error AllocHandle\n"); + return -1; + } + + ODBC_res = SQLSetEnvAttr(ODBC_env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); + + if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) + { + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: Error SetEnv\n"); + SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env); + return -1; + } + + // 2. allocate connection handle, set timeout + ODBC_res = SQLAllocHandle(SQL_HANDLE_DBC, ODBC_env, &ODBC_con); + + if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) + { + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: Error AllocHDB %d\n", ODBC_res); + SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env); + return -1; + } + SQLSetConnectAttr(ODBC_con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)10, 0); + + // 3. connect to the datasource + ODBC_res = SQLConnect(ODBC_con, (SQLCHAR*)unixodbcdsn, SQL_NTS, (SQLCHAR*)unixodbcusername, SQL_NTS, (SQLCHAR*)unixodbcpassword, SQL_NTS); + + if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) + { + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: Error SQLConnect %d\n", ODBC_res); + SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen); + SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env); + return -1; + } + else + { + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: Connected to %s\n", unixodbcdsn); + } + + /* if we made it this far we should be ok... lets init unixodbc_lock */ + ast_mutex_init(&unixodbc_lock); + return 0; +} + +static void unixodbc_disconnect(void) +{ + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: Disconnecting from %s\n", unixodbcdsn); + SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt); + SQLDisconnect(ODBC_con); + SQLFreeHandle(SQL_HANDLE_DBC, ODBC_con); + SQLFreeHandle(SQL_HANDLE_ENV, ODBC_env); +} + +int unixodbc_do_query(char *query) +{ + long int ODBC_err; + short int ODBC_mlen; + char ODBC_msg[200], ODBC_stat[10]; + + ODBC_res = SQLAllocHandle(SQL_HANDLE_STMT, ODBC_con, &ODBC_stmt); + + if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) + { + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: Failure in AllocStatement %d\n", ODBC_res); + SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen); + SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt); + return 0; + } + + ODBC_res = SQLPrepare(ODBC_stmt, query, SQL_NTS); + + if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) + { + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: Error in PREPARE %d\n", ODBC_res); + SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen); + SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt); + return 0; + } + + ODBC_res = SQLExecute(ODBC_stmt); + + if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) + { + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: Error in SELECT %d\n", ODBC_res); + SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_con, 1, ODBC_stat, &ODBC_err, ODBC_msg, 100, &ODBC_mlen); + SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt); + /* woops it totally failed so lest tell everything we aren't connected so it will retry */ + return 0; + } + else + { + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: ODBC SQL Query Successful!\n"); + /* TAKE THAT MYSQL..... :P */ + } + return 1; +} + +static int sql_find_match(struct ast_channel *chan, char *context, char *ext) +{ + long int ODBC_err, ODBC_rows; + short int ODBC_columns; + char query[256]; + memset(query,0,256); + char ODBC_mailbox[80] = "", ODBC_fullname[80] = ""; + char *pos; + char *conv; + int found = 0; + int res; + sprintf(query, "SELECT mailbox,fullname FROM users WHERE context='%s'", context); + ast_mutex_lock(&unixodbc_lock); + + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: %s\n", query); + + res = unixodbc_do_query(query); + if(!res) + goto out; + + SQLBindCol(ODBC_stmt, 1, SQL_C_CHAR, &ODBC_mailbox, 80, &ODBC_err); + SQLBindCol(ODBC_stmt, 2, SQL_C_CHAR, &ODBC_fullname, 80, &ODBC_err); + + ODBC_res = SQLNumResultCols(ODBC_stmt, &ODBC_columns); + + if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) + { + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: Number of columns %d\n", ODBC_columns); + SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt); + goto out; + } + + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: Number of columns %d\n", ODBC_columns); + + ODBC_res = SQLRowCount(ODBC_stmt, &ODBC_rows); + + if((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) + { + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: Number of rows %lu\n", ODBC_rows); + SQLFreeHandle(SQL_HANDLE_STMT, ODBC_stmt); + goto out; + } + + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: Number of rows %lu\n", ODBC_rows); + + + while(ODBC_res != SQL_NO_DATA) + { + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: %s (%s)\n", ODBC_fullname, ODBC_mailbox); + pos = ODBC_fullname; + if(strrchr(ODBC_fullname,' ')) + pos = strrchr(ODBC_fullname,' ') + 1; + conv = convert(pos); + if(conv) + { + if(!strcmp(conv,ext)) + { + found++; + if(option_verbose > 3) + ast_verbose( VERBOSE_PREFIX_4 "directory_unixodbc: Found Match!\n"); + free(conv); + res = found_match(chan,context,ODBC_mailbox); + if(res == 0) + { + found = -1; + break; + } + } + } + strcpy(ODBC_fullname,""); + strcpy(ODBC_mailbox,""); + ODBC_res = SQLFetch(ODBC_stmt); + } + +out: + ast_mutex_unlock(&unixodbc_lock); + return found; +} + +static int sql_init(void) +{ + static int res; + if((res=unixodbc_init())) + { + return res; + } + return(0); +} + +static void sql_close(void) +{ + unixodbc_disconnect(); +} +#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 +485,18 @@ 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 = sql_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 +540,64 @@ return res; } +static int load_config(void) +{ + struct ast_config *cfg; + char *s; + + cfg = ast_load(VOICEMAIL_CONFIG); + +#ifdef USEUNIXODBCVM + if (!(s=ast_variable_retrieve(cfg, "general", "unixodbcusername"))) { + strcpy(unixodbcusername, "test"); + } else { + strcpy(unixodbcusername, s); + } + if (!(s=ast_variable_retrieve(cfg, "general", "unixodbcpassword"))) { + strcpy(unixodbcpassword, "test"); + } else { + strcpy(unixodbcpassword, s); + } + if (!(s=ast_variable_retrieve(cfg, "general", "unixodbcdsn"))) { + strcpy(unixodbcdsn, "vmdb"); + } else { + strcpy(unixodbcdsn, 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; + +#ifdef USESQLVM + if ((res = sql_init())) { + ast_log(LOG_WARNING, "SQL init\n"); + return res; + } +#endif + return res; } char *description(void)