]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-4810 --resolve
authorAnthony Minessale <anthm@freeswitch.org>
Thu, 31 Jan 2013 14:59:51 +0000 (08:59 -0600)
committerAnthony Minessale <anthm@freeswitch.org>
Thu, 31 Jan 2013 14:59:58 +0000 (08:59 -0600)
src/mod/applications/mod_nibblebill/mod_nibblebill.c

index c1a788454a8c2b2cda8485c924e620db505b115f..53b8270b99d6b9faa15a435bcf26c2e83cfcff55 100755 (executable)
@@ -92,9 +92,8 @@ static struct {
        int global_heartbeat;           /* Supervise and bill every X seconds, 0 means off */
 
        /* Database settings */
-       char *db_username;
-       char *db_password;
-       char *db_dsn;
+       char *dbname;
+       char *odbc_dsn;
        char *db_table;
        char *db_column_cash;
        char *db_column_account;
@@ -118,9 +117,6 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_nibblebill_shutdown);
 SWITCH_MODULE_DEFINITION(mod_nibblebill, mod_nibblebill_load, mod_nibblebill_shutdown, NULL);
 
 /* String setting functions */
-SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_db_username, globals.db_username);
-SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_db_password, globals.db_password);
-SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_db_dsn, globals.db_dsn);
 SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_db_table, globals.db_table);
 SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_db_column_cash, globals.db_column_cash);
 SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_db_column_account, globals.db_column_account);
@@ -130,6 +126,24 @@ SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_percall_action, globals.percall_act
 SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_lowbal_action, globals.lowbal_action);
 SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_nobal_action, globals.nobal_action);
 
+static switch_cache_db_handle_t *nibblebill_get_db_handle(void)
+{
+       switch_cache_db_handle_t *dbh = NULL;
+       char *dsn;
+       
+       if (!zstr(globals.odbc_dsn)) {
+               dsn = globals.odbc_dsn;
+       } else {
+               dsn = globals.dbname;
+       }
+
+       if (switch_cache_db_get_db_handle_dsn(&dbh, dsn) != SWITCH_STATUS_SUCCESS) {
+               dbh = NULL;
+       }
+       
+       return dbh;
+}
+
 static int nibblebill_callback(void *pArg, int argc, char **argv, char **columnNames)
 {
        int i = 0;
@@ -144,9 +158,27 @@ static int nibblebill_callback(void *pArg, int argc, char **argv, char **columnN
        return 0;
 }
 
-static switch_status_t load_config(void)
+static switch_bool_t nibblebill_execute_sql_callback(char *sql, switch_core_db_callback_func_t callback, void *pdata)
+{
+       switch_bool_t retval = SWITCH_FALSE;
+       switch_cache_db_handle_t *dbh = NULL;
+       
+       if (globals.odbc_dsn && (dbh = nibblebill_get_db_handle())) {
+               if (switch_cache_db_execute_sql_callback(dbh, sql, callback, pdata, NULL) != SWITCH_STATUS_SUCCESS) {
+                       retval = SWITCH_FALSE;
+               } else {
+                       retval = SWITCH_TRUE;
+               }
+       }
+       switch_cache_db_release_db_handle(&dbh);
+       return retval;
+}
+
+
+static switch_status_t nibblebill_load_config(void)
 {
        char *cf = "nibblebill.conf";
+       switch_cache_db_handle_t *dbh = NULL;
        switch_xml_t cfg, xml = NULL, param, settings;
        switch_status_t status = SWITCH_STATUS_SUCCESS;
 
@@ -160,13 +192,11 @@ static switch_status_t load_config(void)
                for (param = switch_xml_child(settings, "param"); param; param = param->next) {
                        char *var = (char *) switch_xml_attr_soft(param, "name");
                        char *val = (char *) switch_xml_attr_soft(param, "value");
-
-                       if (!strcasecmp(var, "db_username")) {
-                               set_global_db_username(val);
-                       } else if (!strcasecmp(var, "db_password")) {
-                               set_global_db_password(val);
-                       } else if (!strcasecmp(var, "db_dsn")) {
-                               set_global_db_dsn(val);
+                       
+                       if (!strcasecmp(var, "odbc-dsn") && !zstr(val)) {
+                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "odbc_dsn is %s\n", val);
+                               switch_safe_free(globals.odbc_dsn);
+                               globals.odbc_dsn = strdup(val);
                        } else if (!strcasecmp(var, "db_table")) {
                                set_global_db_table(val);
                        } else if (!strcasecmp(var, "db_column_cash")) {
@@ -194,6 +224,7 @@ static switch_status_t load_config(void)
                        }
                }
        }
+       
 
 /* Set defaults for any variables still not set */
   setdefaults:
@@ -207,28 +238,15 @@ static switch_status_t load_config(void)
                set_global_nobal_action("hangup");
        }
 
-       if (switch_odbc_available() && globals.db_dsn) {
-               if (!(globals.master_odbc = switch_odbc_handle_new(globals.db_dsn, globals.db_username, globals.db_password))) {
-                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot create handle to ODBC Database!\n");
-                       status = SWITCH_STATUS_FALSE;
-                       goto done;
-               } else {
-                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Opened ODBC Database handle!\n");
-               }
-
-               if (switch_odbc_handle_connect(globals.master_odbc) != SWITCH_ODBC_SUCCESS) {
-                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT,
-                                                         "Cannot connect to ODBC driver/database %s (user: %s / pass %s)!\n",
-                                                         globals.db_dsn, globals.db_username, globals.db_password);
-                       status = SWITCH_STATUS_FALSE;
-                       goto done;
-               } else {
-                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Opened ODBC Database!\n");
+       if (globals.odbc_dsn) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG
+                                                 , "dsn is \"%s\"\n"
+                                                 , globals.odbc_dsn
+                                                 );
+               if (!(dbh = nibblebill_get_db_handle())) {
+                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
+                       switch_goto_status(SWITCH_STATUS_FALSE, done);
                }
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected ODBC DSN: %s\n", globals.db_dsn);
-       } else {
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT,
-                                                 "ODBC does not appear to be installed in the core or your dsn is empty. You need to run ./configure --enable-core-odbc-support\n");
        }
 
   done:
@@ -314,12 +332,7 @@ static void transfer_call(switch_core_session_t *session, char *destination)
 static switch_status_t bill_event(double billamount, const char *billaccount, switch_channel_t *channel)
 {
        char *sql = NULL, *dsql = NULL;
-       switch_odbc_statement_handle_t stmt = NULL;
-       switch_status_t status = SWITCH_STATUS_FALSE;
-
-       if (!switch_odbc_available()) {
-               return status;
-       }
+       switch_status_t status = SWITCH_FALSE;
 
        if (globals.custom_sql_save) {
                if (switch_string_var_check_const(globals.custom_sql_save) || switch_string_has_escaped_data(globals.custom_sql_save)) {
@@ -336,36 +349,18 @@ static switch_status_t bill_event(double billamount, const char *billaccount, sw
        }
 
        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Doing update query\n[%s]\n", sql);
-
-       if (switch_odbc_handle_exec(globals.master_odbc, sql, &stmt, NULL) != SWITCH_ODBC_SUCCESS) {
-               char *err_str;
-               err_str = switch_odbc_handle_get_error(globals.master_odbc, stmt);
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ERR: [%s]\n[%s]\n", sql, switch_str_nil(err_str));
-               switch_safe_free(err_str);
-       } else {
-               status = SWITCH_STATUS_SUCCESS;
-       }
-
-       if (stmt) {
-               switch_odbc_statement_handle_free(&stmt);
-       }
-       
+       status = nibblebill_execute_sql_callback(sql, nibblebill_callback, NULL);
        switch_safe_free(dsql);
 
        return status;
 }
 
-
 static double get_balance(const char *billaccount, switch_channel_t *channel)
 {
        char *dsql = NULL, *sql = NULL;
        nibblebill_results_t pdata;
        double balance = 0.0;
 
-       if (!switch_odbc_available()) {
-               return -1.0;
-       }
-
        memset(&pdata, 0, sizeof(pdata));
 
        if (globals.custom_sql_lookup) {
@@ -381,11 +376,9 @@ static double get_balance(const char *billaccount, switch_channel_t *channel)
        }
 
        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Doing lookup query\n[%s]\n", sql);
-       
-       if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, nibblebill_callback, &pdata, NULL) != SWITCH_ODBC_SUCCESS) {
+       if (nibblebill_execute_sql_callback(sql, nibblebill_callback, &pdata) != SWITCH_TRUE) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error running this query: [%s]\n", sql);
                /* Return -1 for safety */
-
                balance = -1.0;
        } else {
                /* Successfully retrieved! */
@@ -529,7 +522,7 @@ static switch_status_t do_billing(switch_core_session_t *session)
                                                  uuid, nibble_data->total);
 
                /* DO ODBC BILLING HERE and reset counters if it's successful! */
-               if (bill_event(billamount, billaccount, channel) == SWITCH_STATUS_SUCCESS) {
+               if (bill_event(billamount, billaccount, channel) == SWITCH_TRUE) {
                        /* Increment total cost */
                        nibble_data->total += billamount;
 
@@ -581,14 +574,12 @@ static switch_status_t do_billing(switch_core_session_t *session)
                }
        }
 
-
        /* Done changing - release lock */
        if (globals.mutex) {
                switch_mutex_unlock(globals.mutex);
        }
 
        /* Go check if this call is allowed to continue */
-
        return SWITCH_STATUS_SUCCESS;
 }
 
@@ -955,7 +946,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_nibblebill_load)
        globals.pool = pool;
        switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, globals.pool);
 
-       load_config();
+       nibblebill_load_config();
 
        /* connect my internal structure to the blank pointer passed to me */
        *module_interface = switch_loadable_module_create_module_interface(pool, modname);
@@ -987,10 +978,9 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_nibblebill_shutdown)
        switch_event_unbind(&globals.node);
        switch_core_remove_state_handler(&nibble_state_handler);
        switch_odbc_handle_disconnect(globals.master_odbc);
-
-       switch_safe_free(globals.db_username);
-       switch_safe_free(globals.db_password);
-       switch_safe_free(globals.db_dsn);
+       
+       switch_safe_free(globals.dbname);
+       switch_safe_free(globals.odbc_dsn);
        switch_safe_free(globals.db_table);
        switch_safe_free(globals.db_column_cash);
        switch_safe_free(globals.db_column_account);