--- app_voicemail.c 2004-07-13 13:18:09.000000000 -0700 +++ asterisk/apps/app_voicemail.c 2004-07-09 00:39:39.000000000 -0700 @@ -40,8 +40,6 @@ #include #include -#define EXTRA_LOG 0 - /* we define USESQLVM when we have MySQL or POSTGRES */ #ifdef USEMYSQLVM #include @@ -360,19 +358,155 @@ } -#ifdef USESQLVM -char db_changepassword_query[1024]; -char db_setpassword_query[1024]; -char db_getvmuser_query[1024]; -#endif - #ifdef USEMYSQLVM #include "mysql-vm-routines.h" #endif #ifdef USEPOSTGRESVM -#include "postgres-vm-routines.h" -#endif + +PGconn *dbhandler; +char dboption[256]; +AST_MUTEX_DEFINE_STATIC(postgreslock); + +static int sql_init(void) +{ + ast_verbose( VERBOSE_PREFIX_3 "Logging into postgres database: %s\n", dboption); +/* fprintf(stderr,"Logging into postgres database: %s\n", dboption); */ + + dbhandler=PQconnectdb(dboption); + if (PQstatus(dbhandler) == CONNECTION_BAD) { + ast_log(LOG_WARNING, "Error Logging into database %s: %s\n",dboption,PQerrorMessage(dbhandler)); + return(-1); + } +/* fprintf(stderr,"postgres login OK\n"); */ + return(0); +} + +static void sql_close(void) +{ + PQfinish(dbhandler); +} + + +static struct ast_vm_user *find_user(struct ast_vm_user *ivm, char *context, char *mailbox) +{ + PGresult *PGSQLres; + + + int numFields, i; + char *fname; + char query[240]; + char options[160] = ""; + struct ast_vm_user *retval; + + retval=malloc(sizeof(struct ast_vm_user)); + +/* fprintf(stderr,"postgres find_user:\n"); */ + + if (retval) { + memset(retval, 0, sizeof(struct ast_vm_user)); + retval->alloced=1; + if (mailbox) { + strcpy(retval->mailbox, mailbox); + } + if (context) { + strcpy(retval->context, context); + } + else + { + strcpy(retval->context, "default"); + } + populate_defaults(retval); + sprintf(query, "SELECT password,fullname,email,pager,options FROM voicemail WHERE context='%s' AND mailbox='%s'", retval->context, mailbox); + +/* fprintf(stderr,"postgres find_user: query = %s\n",query); */ + 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); + free(retval); + return(NULL); + } else { + numFields = PQnfields(PGSQLres); +/* fprintf(stderr,"postgres find_user: query found %d rows with %d fields\n",PQntuples(PGSQLres), numFields); */ + if (PQntuples(PGSQLres) != 1) { + ast_log(LOG_WARNING,"PGSQL_query: Did not find a unique mailbox for %s\n",mailbox); + PQclear(PGSQLres); + ast_mutex_unlock(&postgreslock); + free(retval); + return(NULL); + } + for (i=0; ipassword, PQgetvalue(PGSQLres,0,i),sizeof(retval->password) - 1); + } else if (!strcmp(fname, "fullname")) { + strncpy(retval->fullname, PQgetvalue(PGSQLres,0,i),sizeof(retval->fullname) - 1); + } else if (!strcmp(fname, "email")) { + strncpy(retval->email, PQgetvalue(PGSQLres,0,i),sizeof(retval->email) - 1); + } else if (!strcmp(fname, "pager")) { + strncpy(retval->pager, PQgetvalue(PGSQLres,0,i),sizeof(retval->pager) - 1); + } else if (!strcmp(fname, "options")) { + strncpy(options, PQgetvalue(PGSQLres,0,i), sizeof(options) - 1); + apply_options(retval, options); + } + } + } + PQclear(PGSQLres); + ast_mutex_unlock(&postgreslock); + return(retval); + } + else { + ast_log(LOG_WARNING,"PGSQL_query: Connection Error (%s)\n",PQerrorMessage(dbhandler)); + ast_mutex_unlock(&postgreslock); + free(retval); + return(NULL); + } + /* not reached */ + } /* malloc() retval */ + return(NULL); +} + + +static void vm_change_password(struct ast_vm_user *vmu, char *password) +{ + char query[400]; + + if (*vmu->context) { + sprintf(query, "UPDATE voicemail SET password='%s' WHERE context='%s' AND mailbox='%s' AND (password='%s' OR password IS NULL)", password, vmu->context, vmu->mailbox, vmu->password); + } else { + sprintf(query, "UPDATE voicemail SET password='%s' WHERE mailbox='%s' AND (password='%s' OR password IS NULL)", password, vmu->mailbox, vmu->password); + } +/* fprintf(stderr,"postgres change_password: query = %s\n",query); */ + ast_mutex_lock(&postgreslock); + PQexec(dbhandler, query); + strcpy(vmu->password, password); + ast_mutex_unlock(&postgreslock); +} + +static void reset_user_pw(char *context, char *mailbox, char *password) +{ + char query[320]; + + if (context) { + sprintf(query, "UPDATE voicemail SET password='%s' WHERE context='%s' AND mailbox='%s'", password, context, mailbox); + } else { + sprintf(query, "UPDATE voicemail SET password='%s' WHERE mailbox='%s'", password, mailbox); + } + ast_mutex_lock(&postgreslock); +/* fprintf(stderr,"postgres reset_user_pw: query = %s\n",query); */ + PQexec(dbhandler, query); + ast_mutex_unlock(&postgreslock); +} + +#endif /* Postgres */ #ifndef USESQLVM @@ -4206,33 +4340,6 @@ exitcontext[0] = '\0'; } -#ifdef USESQLVM - if (!(s=ast_variable_retrieve(cfg, "general", "db_changepassword_query"))) { - strncpy(db_changepassword_query, - "UPDATE voicemail SET password='%s' WHERE (password='%s' OR password IS NULL) AND (mailbox='%s') AND (context='%s')", - sizeof(db_changepassword_query)-1); - } - else { - strncpy(db_changepassword_query, s,sizeof(db_changepassword_query)); - } - if (!(s=ast_variable_retrieve(cfg, "general", "db_setpassword_query"))) { - strncpy(db_setpassword_query, - "UPDATE voicemail SET password='%s' WHERE (mailbox='%s') AND (context='%s')", - sizeof(db_setpassword_query)-1); - } - else { - strncpy(db_setpassword_query, s, sizeof(db_setpassword_query)); - } - if (!(s=ast_variable_retrieve(cfg, "general", "db_getvmuser_query"))) { - strncpy(db_getvmuser_query, - "SELECT password,fullname,email,pager,options FROM voicemail WHERE (mailbox='%s') AND (context='%s')", - sizeof(db_getvmuser_query)-1); - } - else { - strncpy(db_getvmuser_query,s,sizeof(db_getvmuser_query)-1); - } -#endif - #ifdef USEMYSQLVM if (!(s=ast_variable_retrieve(cfg, "general", "dbuser"))) { strcpy(dbuser, "test");