--- asterisk/apps/app_directory.c 2004-08-29 20:44:41.000000000 -0500 +++ asterisk-new/apps/app_directory.c 2004-09-17 15:44:11.000000000 -0500 @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -27,6 +28,17 @@ #include "../asterisk.h" #include "../astconf.h" +#ifdef USEMYSQLVM + #include + AST_MUTEX_DEFINE_STATIC( mysql_lock ); + #define VOICEMAIL_TABLE "voicemail_users" + char dbuser[80]; + char dbpass[80]; + char dbhost[80]; + char dbname[80]; + int dbport = 0; +#endif + static char *tdesc = "Extension Directory"; static char *app = "Directory"; @@ -308,6 +320,112 @@ return res; } +#ifdef USEMYSQLVM +static struct ast_config *cfg_from_mysql(void) +{ + int counter = 0; + + MYSQL mysql; + MYSQL_RES *result; + MYSQL_ROW row; + my_ulonglong num_rows; + + char sql_query[240]; + char var_tmp[100]; + + struct ast_config *new_config; + struct ast_variable *curr_v, *new_v; + struct ast_category *curr_cat, *new_cat; + + curr_cat = new_cat = NULL; + curr_v = new_v = NULL; + + new_config = ast_new_config(); + + mysql_init(&mysql); + if(!mysql_real_connect(&mysql, dbhost[0] ? dbhost : NULL, dbuser, dbpass, dbname, dbport, NULL, 0)) { + ast_log(LOG_ERROR, "Directory cannot connect to database %s on %s.\n", dbname, dbhost); + return NULL; + } + + sprintf(sql_query, "SELECT context, mailbox, password, fullname, email FROM %s ORDER BY context, mailbox", VOICEMAIL_TABLE); + + ast_mutex_lock(&mysql_lock); + + if(mysql_real_query(&mysql, sql_query, strlen(sql_query))) { + ast_log(LOG_WARNING, "Directory failed to query database.\n"); + ast_log(LOG_DEBUG, "Directory Quers: %s\n", sql_query); + ast_mutex_unlock(&mysql_lock); + return NULL; + } + + ast_mutex_unlock(&mysql_lock); + + result = mysql_store_result(&mysql); + num_rows = mysql_num_rows(result); + + ast_log(LOG_DEBUG, "Directory found %i rows \n", (int)num_rows); + + for(counter = 0; counter < num_rows; counter++) { + row = mysql_fetch_row(result); + + if(curr_cat->name) { + /* if there is a current category, check it against next row */ + if( strcmp(curr_cat->name, (char *)row[0])) { + + /* ast_log( LOG_DEBUG, "curr_cat and next row differ: %s - %s \n", curr_cat->name, row[0] ); */ + /* they are different. make a new cat and assign the new cat to the old cat->next */ + + new_cat = ast_new_category(row[0]); + curr_cat->next = new_cat; + curr_cat = curr_cat->next; + } + } else { + /* there is no current category, meaning our config is still blank + make a new cat and assign it to the configs root */ + + curr_cat = ast_new_category(row[0]); + new_config->root = curr_cat; + curr_cat = new_config->root; + ast_log(LOG_DEBUG, "No root cat. Make brand new cat: %s \n", row[0]); + } + + if(!curr_cat->root) { + /* current category doesn't have a root variable. + make this row the root variable + =,, */ + + sprintf(var_tmp, "%s,%s,%s", row[2], row[3], row[4]); + ast_log(LOG_DEBUG, "curr_cat has no root var: new variable: %s => %s \n", row[1], var_tmp); + + curr_v = ast_new_variable(row[1], var_tmp); + + curr_cat->root = curr_v; + curr_v = curr_cat->root; + + } else { + /* curr_cat already has a root, therefore curr_v must also be set + make new var and set curr_v->next to new var */ + + sprintf(var_tmp, "%s,%s,%s", row[2], row[3], row[4]); + ast_log(LOG_DEBUG, "new curr_cat->next variable: %s => %s \n", row[1], var_tmp); + + new_v = ast_new_variable(row[1], var_tmp); + + curr_v->next = new_v; + curr_v = curr_v->next; + } + + } + + mysql_close(&mysql); + + /* ast_save( "test.cfg", new_config, "mygenn" ); */ + + return new_config; +} +#endif + static int directory_exec(struct ast_channel *chan, void *data) { int res = 0; @@ -320,11 +438,19 @@ ast_log(LOG_WARNING, "directory requires an argument (context[,dialcontext])\n"); return -1; } +#ifdef USEMYSQLVM + cfg = cfg_from_mysql(); + if (!cfg) { + ast_log(LOG_WARNING, "Unable to get config from database.\n" ); + return -1; + } +#else cfg = ast_load(DIRECTORY_CONFIG); if (!cfg) { ast_log(LOG_WARNING, "Unable to open directory configuration %s\n", DIRECTORY_CONFIG); return -1; } +#endif LOCAL_USER_ADD(u); top: context = ast_strdupa(data); @@ -382,6 +508,53 @@ int load_module(void) { +#ifdef USEMYSQLVM + struct ast_config *cfg; + char *s; + + /* We will use the db paramaters that are already associated with the voicemail db */ + cfg = ast_load(DIRECTORY_CONFIG); + + if(cfg) { + if (!(s=ast_variable_retrieve(cfg, "general", "dbuser"))) { + strncpy(dbuser, "asterisk", sizeof(dbuser) - 1); + } else { + strncpy(dbuser, s, sizeof(dbuser) - 1); + } + + if (!(s=ast_variable_retrieve(cfg, "general", "dbpass"))) { + strncpy(dbpass, "asterisk", sizeof(dbpass) - 1); + } else { + strncpy(dbpass, s, sizeof(dbpass) - 1); + } + + if (!(s=ast_variable_retrieve(cfg, "general", "dbhost"))) { + dbhost[0] = '\0'; + } else { + strncpy(dbhost, s, sizeof(dbhost) - 1); + } + + if (!(s=ast_variable_retrieve(cfg, "general", "dbname"))) { + strncpy(dbname, "asterisk", sizeof(dbname) - 1); + } else { + strncpy(dbname, s, sizeof(dbname) - 1); + } + + if (!(s=ast_variable_retrieve(cfg, "general", "dbport"))) { + dbport = 3306; + } else { + dbport = atoi(s); + } + } + ast_destroy(cfg); + + ast_log(LOG_DEBUG, "Directory host: %s\n", dbhost); + ast_log(LOG_DEBUG, "Directory user: %s\n", dbuser); + ast_log(LOG_DEBUG, "Directory pass: %s\n", dbpass); + ast_log(LOG_DEBUG, "Directory db: %s\n", dbname); + ast_log(LOG_DEBUG, "Directory port: %i\n", dbport); + +#endif return ast_register_application(app, directory_exec, synopsis, descrip); }