static switch_status_t load_config(switch_bool_t reload) { switch_status_t status = SWITCH_STATUS_SUCCESS; switch_xml_t cfg, xml = NULL, settings, param, x_profiles, x_profile; switch_cache_db_handle_t *dbh = NULL; if (!(xml = switch_xml_open_cfg(global_cf, &cfg, NULL))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", global_cf); return SWITCH_STATUS_TERM; } switch_mutex_lock(globals.mutex); if ((settings = switch_xml_child(cfg, "settings"))) { 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, "odbc-dsn") && !zstr(val)) { if (switch_odbc_available()) { switch_set_string(globals.odbc_dsn, val); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n"); } } else if (!strcasecmp(var, "dbname") && !zstr(val)) { globals.dbname = switch_core_strdup(globals.pool, val); } if (!strcasecmp(var, "debug")) { globals.debug = atoi(val); } } } if ((x_profiles = switch_xml_child(cfg, "profiles"))) { for (x_profile = switch_xml_child(x_profiles, "profile"); x_profile; x_profile = x_profile->next) { load_profile(switch_xml_attr_soft(x_profile, "name")); } } if (zstr(globals.odbc_dsn) && zstr(globals.dbname)) { globals.dbname = switch_core_sprintf(globals.pool, "directory"); } dbh = directory_get_db_handle(); if (dbh) { if (!reload) { switch_cache_db_test_reactive(dbh, "delete from directory_search where uuid != '' and name_visible != '' ", "drop table directory_search", dir_sql); } switch_cache_db_release_db_handle(&dbh); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot open DB!2\n"); status = SWITCH_STATUS_TERM; goto end; } end: switch_mutex_unlock(globals.mutex); switch_xml_free(xml); return status; }
/* At this time, billing never succeeds if you don't have a database. */ 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; } if (globals.custom_sql_save) { if (switch_string_var_check_const(globals.custom_sql_save) || switch_string_has_escaped_data(globals.custom_sql_save)) { switch_channel_set_variable_printf(channel, "nibble_bill", "%f", billamount, SWITCH_FALSE); sql = switch_channel_expand_variables(channel, globals.custom_sql_save); if (sql != globals.custom_sql_save) dsql = sql; } else { sql = globals.custom_sql_save; } } else { sql = dsql = switch_mprintf("UPDATE %s SET %s=%s-%f WHERE %s='%s'", globals.db_table, globals.db_column_cash, globals.db_column_cash, billamount, globals.db_column_account, billaccount); } 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); } switch_safe_free(dsql); return status; }
static switch_status_t config_callback_dsn(switch_xml_config_item_t *data, const char *newvalue, switch_config_callback_type_t callback_type, switch_bool_t changed) { switch_status_t status = SWITCH_STATUS_SUCCESS; switch_cache_db_handle_t *dbh = NULL; if (!switch_odbc_available()) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC is not compiled in. Do not configure odbc-dsn parameter!\n"); return SWITCH_STATUS_FALSE; } if ((callback_type == CONFIG_LOAD || callback_type == CONFIG_RELOAD) && changed) { if (zstr(newvalue)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "No local database defined.\n"); } else { switch_safe_free(globals.odbc_dsn); globals.odbc_dsn = strdup(newvalue); if ((globals.odbc_user = strchr(globals.odbc_dsn, ':'))) { *globals.odbc_user++ = '\0'; if ((globals.odbc_pass = strchr(globals.odbc_user, ':'))) { *globals.odbc_pass++ = '\0'; } } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Connecting to dsn: %s\n", globals.odbc_dsn); if (!(dbh = cidlookup_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_goto_status(SWITCH_STATUS_SUCCESS, done); done: switch_cache_db_release_db_handle(&dbh); 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) { if (switch_string_var_check_const(globals.custom_sql_lookup) || switch_string_has_escaped_data(globals.custom_sql_lookup)) { sql = switch_channel_expand_variables(channel, globals.custom_sql_lookup); if (sql != globals.custom_sql_lookup) dsql = sql; } else { sql = globals.custom_sql_lookup; } } else { sql = dsql = switch_mprintf("SELECT %s AS nibble_balance FROM %s WHERE %s='%s'", globals.db_column_cash, globals.db_table, globals.db_column_account, billaccount); } 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) { 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! */ balance = pdata.balance; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Retrieved current balance for account %s (balance = %f)\n", billaccount, balance); } switch_safe_free(dsql); return balance; }
static switch_status_t load_config(void) { char *cf = "nibblebill.conf"; switch_xml_t cfg, xml = NULL, param, settings; switch_status_t status = SWITCH_STATUS_SUCCESS; if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf); status = SWITCH_STATUS_SUCCESS; /* We don't fail because we can still write to a text file or buffer */ goto setdefaults; } if ((settings = switch_xml_child(cfg, "settings"))) { 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); } else if (!strcasecmp(var, "db_table")) { set_global_db_table(val); } else if (!strcasecmp(var, "db_column_cash")) { set_global_db_column_cash(val); } else if (!strcasecmp(var, "db_column_account")) { set_global_db_column_account(val); } else if (!strcasecmp(var, "custom_sql_save")) { set_global_custom_sql_save(val); } else if (!strcasecmp(var, "custom_sql_lookup")) { set_global_custom_sql_lookup(val); } else if (!strcasecmp(var, "percall_action")) { set_global_percall_action(val); } else if (!strcasecmp(var, "percall_max_amt")) { globals.percall_max_amt = atof(val); } else if (!strcasecmp(var, "lowbal_action")) { set_global_lowbal_action(val); } else if (!strcasecmp(var, "lowbal_amt")) { globals.lowbal_amt = atof(val); } else if (!strcasecmp(var, "nobal_action")) { set_global_nobal_action(val); } else if (!strcasecmp(var, "nobal_amt")) { globals.nobal_amt = atof(val); } else if (!strcasecmp(var, "global_heartbeat")) { globals.global_heartbeat = atoi(val); } } } /* Set defaults for any variables still not set */ setdefaults: if (zstr(globals.percall_action)) { set_global_percall_action("hangup"); } if (zstr(globals.lowbal_action)) { set_global_lowbal_action("play ding"); } if (zstr(globals.nobal_action)) { 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"); } 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: if (xml) { switch_xml_free(xml); } return status; }
static cid_data_t *do_lookup(switch_memory_pool_t *pool, switch_event_t *event, const char *num, switch_bool_t skipurl, switch_bool_t skipcitystate) { char *number = NULL; char *name = NULL; char *url_query = NULL; cid_data_t *cid = NULL; cid_data_t *cidtmp = NULL; switch_bool_t save_cache = SWITCH_FALSE; cid = switch_core_alloc(pool, sizeof(cid_data_t)); switch_assert(cid); number = string_digitsonly(pool, num); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "caller_id_number", number); /* database always wins */ if (switch_odbc_available() && globals.odbc_dsn && globals.sql) { name = do_db_lookup(pool, event, number, globals.sql); if (name) { cid->name = name; cid->src = "phone_database"; goto done; } } if (globals.cache) { cidtmp = check_cache(pool, number); if (cidtmp) { cid = cidtmp; cid->src = switch_core_sprintf(pool, "%s (cache)", cid->src); goto done; } } if (!skipurl && globals.whitepages_apikey) { cid = do_whitepages_lookup(pool, event, number); if (cid && cid->name) { /* only cache if we have a name */ save_cache = SWITCH_TRUE; goto done; } } if (!skipurl && globals.url) { url_query = switch_event_expand_headers(event, globals.url); do_lookup_url(pool, event, &name, url_query, NULL, NULL, 0); if (name) { cid->name = name; cid->src = "url"; save_cache = SWITCH_TRUE; } if (url_query != globals.url) { switch_safe_free(url_query); } } done: if (!cid) { cid = switch_core_alloc(pool, sizeof(cid_data_t)); switch_assert(cid); } /* append area if we can */ if (!cid->area && !skipcitystate && strlen(number) == 11 && number[0] == '1' && switch_odbc_available() && globals.odbc_dsn && globals.citystate_sql) { /* yes, this is really area */ name = do_db_lookup(pool, event, number, globals.citystate_sql); if (name) { cid->area = name; if (cid->src) { cid->src = switch_core_sprintf(pool, "%s,%s", cid->src, "npanxx_database"); } else { cid->src = "npanxx_database"; } } } if (!cid->area) { cid->area = "UNKNOWN"; } if (!cid->name) { if (skipcitystate) { if (strlen(number) == 11 && number[0] == '1') { int a, b, c; sscanf(number, "1%3d%3d%4d", &a, &b, &c); cid->name = switch_core_sprintf(pool, "%03d-%03d-%04d", a, b, c); } else { cid->name = number; } } else { cid->name = cid->area; } } if (!cid->src) { cid->src = "UNKNOWN"; } if (globals.cache && save_cache) { set_cache(pool, number, cid); } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "cidlookup source: %s\n", cid->src); return cid; }
static switch_status_t load_config(void) { char *cf = "easyroute.conf"; switch_xml_t cfg, xml = NULL, param, settings; switch_status_t status = SWITCH_STATUS_SUCCESS; if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf); status = SWITCH_STATUS_FALSE; goto done; } if ((settings = switch_xml_child(cfg, "settings"))) { 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); } else if (!strcasecmp(var, "default-techprofile")) { set_global_default_techprofile(val); } else if (!strcasecmp(var, "default-gateway")) { set_global_default_gateway(val); } else if (!strcasecmp(var, "custom-query")) { set_global_custom_query(val); } } } done: if (zstr(globals.db_username)) { set_global_db_username("root"); } if (zstr(globals.db_password)) { set_global_db_password("password"); } if (zstr(globals.db_dsn)) { set_global_db_dsn("easyroute"); } 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 Open ODBC Database!\n"); status = SWITCH_STATUS_FALSE; goto reallydone; } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Opened ODBC Database!\n"); } if (switch_odbc_handle_connect(globals.master_odbc) != SWITCH_ODBC_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n"); status = SWITCH_STATUS_FALSE; goto reallydone; } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Opened ODBC Database!\n"); } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected ODBC DSN: %s\n", globals.db_dsn); if (!globals.custom_query) { if (switch_odbc_handle_exec(globals.master_odbc, "select count(*) from numbers", NULL, NULL) != SWITCH_ODBC_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot find SQL Database! (Where\'s the numbers table\?\?)\n"); } if (switch_odbc_handle_exec(globals.master_odbc, "select count(*) from gateways", NULL, NULL) != SWITCH_ODBC_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot find SQL Database! (Where\'s the gateways table\?\?)\n"); } } } else if (globals.db_dsn) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Connection (did you enable it?!)\n"); } reallydone: if (xml) { switch_xml_free(xml); } if (!globals.default_techprofile) { set_global_default_techprofile("sofia/default"); } if (!globals.default_gateway) { set_global_default_gateway("192.168.1.1"); } return status; }
static switch_status_t route_lookup(char *dn, easyroute_results_t *results, int noat, char *separator) { switch_status_t sstatus = SWITCH_STATUS_SUCCESS; char *sql = NULL; route_callback_t pdata; if (!switch_odbc_available()) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "mod_easyroute requires core ODBC support. Please refer to the documentation on how to enable this\n"); return sstatus; } memset(&pdata, 0, sizeof(pdata)); if (!globals.custom_query) { sql = switch_mprintf(SQL_LOOKUP, dn); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Doing static Query\n[%s]\n", sql); } else { sql = switch_mprintf(globals.custom_query, dn); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Doing custom Query\n[%s]\n", sql); } if (globals.mutex) { switch_mutex_lock(globals.mutex); } /* Do the Query */ if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, route_callback, &pdata, NULL) == SWITCH_ODBC_SUCCESS) { char tmp_profile[129]; char tmp_gateway[129]; if (zstr(pdata.limit)) { switch_set_string(results->limit, "9999"); } else { switch_set_string(results->limit, pdata.limit); } if (zstr(pdata.techprofile)) { switch_set_string(tmp_profile, globals.default_techprofile); } else { switch_set_string(tmp_profile, pdata.techprofile); } if (zstr(pdata.gateway)) { switch_set_string(tmp_gateway, globals.default_gateway); } else { switch_set_string(tmp_gateway, pdata.gateway); } if (zstr(pdata.translated)) { switch_set_string(results->translated, dn); } else { switch_set_string(results->translated, pdata.translated); } if (noat) { switch_snprintf(results->dialstring, 256, "%s/%s%s", tmp_profile, results->translated, tmp_gateway); } else if (separator) { switch_snprintf(results->dialstring, 256, "%s/%s%s%s", tmp_profile, results->translated, separator, tmp_gateway); } else { switch_snprintf(results->dialstring, 256, "%s/%s@%s", tmp_profile, results->translated, tmp_gateway); } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "THE ROUTE [%s]\n", results->dialstring); if (zstr(pdata.group)) { switch_set_string(results->group, ""); } else { switch_set_string(results->group, pdata.group); } if (zstr(pdata.acctcode)) { switch_set_string(results->acctcode, ""); } else { switch_set_string(results->acctcode, pdata.acctcode); } } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DB Error Setting Default Route!\n"); switch_set_string(results->limit, "9999"); if (noat) { switch_snprintf(results->dialstring, 256, "%s/%s%s", globals.default_techprofile, dn, globals.default_gateway); } else if (separator) { switch_snprintf(results->dialstring, 256, "%s/%s%s%s", globals.default_techprofile, dn, separator, globals.default_gateway); } else { switch_snprintf(results->dialstring, 256, "%s/%s@%s", globals.default_techprofile, dn, globals.default_gateway); } switch_set_string(results->group, ""); switch_set_string(results->acctcode, ""); } switch_safe_free(sql); if (globals.mutex) { switch_mutex_unlock(globals.mutex); } return sstatus; }