Example #1
0
static double get_balance(const char *billaccount, switch_channel_t *channel)
{
	char *dsql = NULL, *sql = NULL;
	nibblebill_results_t pdata;
	double balance = 0.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 (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! */
		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;
}
Example #2
0
/* At this time, billing never succeeds if you don't have a database. */
static switch_bool_t bill_event(double billamount, const char *billaccount, switch_channel_t *channel)
{
	char *sql = NULL, *dsql = NULL;
	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)) {
			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);
	status = nibblebill_execute_sql_callback(sql, nibblebill_callback, NULL);
	switch_safe_free(dsql);

	return status;
}
Example #3
0
static void translate_number(char *number, char *profile, char **translated, switch_core_session_t *session, switch_event_t *event)
{
	translate_rule_t *hi = NULL;
	translate_rule_t *rule = NULL;
	switch_regex_t *re = NULL;
	int proceed = 0, ovector[30];
	char *substituted = NULL;
	uint32_t len = 0;

	if (!profile) {
		profile = "US";
	}

	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "translating [%s] against [%s] profile\n", number, profile);

	hi = switch_core_hash_find_rdlock(globals.translate_profiles, (const char *)profile, globals.profile_hash_rwlock);
	if (!hi) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "can't find key for profile matching [%s]\n", profile);
		return;
	}

	for (rule = hi; rule; rule = rule->next) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s =~ /%s/\n", number, rule->regex);
		if ((proceed = switch_regex_perform(number, rule->regex, &re, ovector, sizeof(ovector) / sizeof(ovector[0])))) {
			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s matched %s, replacing with %s\n", number, rule->regex, rule->replace);
			if (!(substituted = malloc(len))) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error!\n");
				switch_regex_safe_free(re);
				goto end;
			}
			memset(substituted, 0, len);

			switch_perform_substitution(re, proceed, rule->replace, number, substituted, len, ovector);

			if ((switch_string_var_check_const(substituted) || switch_string_has_escaped_data(substituted))) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "perform variable expansion\n");
				if (session) {
					substituted = switch_channel_expand_variables(switch_core_session_get_channel(session), substituted);
				} else if (event) {
					substituted = switch_event_expand_headers(event, substituted);
				}
			}

			break;
		}
	}

 end:
	*translated = substituted ? substituted : NULL;
}
Example #4
0
/* 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;
}