static int mongodb_load_module(int reload) { int res = -1; struct ast_config *cfg = NULL; mongoc_uri_t *uri = NULL; do { const char *tmp; struct ast_variable *var; struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; cfg = ast_config_load(CONFIG_FILE, config_flags); if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) { ast_log(LOG_WARNING, "unable to load config file=%s\n", CONFIG_FILE); res = AST_MODULE_LOAD_DECLINE; break; } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) break; var = ast_variable_browse(cfg, CATEGORY); if (!var) { ast_log(LOG_WARNING, "no category specified.\n"); break; } if ((tmp = ast_variable_retrieve(cfg, CATEGORY, URI)) == NULL) { ast_log(LOG_WARNING, "no uri specified.\n"); break; } uri = mongoc_uri_new(tmp); if (uri == NULL) { ast_log(LOG_ERROR, "parsing uri error, %s\n", tmp); break; } if ((tmp = ast_variable_retrieve(cfg, CATEGORY, DATABSE)) == NULL) { ast_log(LOG_WARNING, "no database specified.\n"); break; } if (dbname) ast_free(dbname); dbname = ast_strdup(tmp); if (dbname == NULL) { ast_log(LOG_ERROR, "not enough memory for dbname\n"); break; } if ((tmp = ast_variable_retrieve(cfg, CATEGORY, COLLECTION)) == NULL) { ast_log(LOG_WARNING, "no collection specified.\n"); break; } if (dbcollection) ast_free(dbcollection); dbcollection = ast_strdup(tmp); if (dbcollection == NULL) { ast_log(LOG_ERROR, "not enough memory for dbcollection\n"); break; } if (!ast_test_flag(&config, CONFIG_REGISTERED)) { res = ast_cdr_register(NAME, ast_module_info->description, mongodb_log); if (res) { ast_log(LOG_ERROR, "unable to register CDR handling\n"); break; } ast_set_flag(&config, CONFIG_REGISTERED); } if ((tmp = ast_variable_retrieve(cfg, CATEGORY, SERVERID)) != NULL) { if (!bson_oid_is_valid (tmp, strlen(tmp))) { ast_log(LOG_ERROR, "invalid server id specified.\n"); break; } serverid = ast_malloc(sizeof(bson_oid_t)); if (serverid == NULL) { ast_log(LOG_ERROR, "not enough memory\n"); break; } bson_oid_init_from_string(serverid, tmp); } if (dbpool) mongoc_client_pool_destroy(dbpool); dbpool = mongoc_client_pool_new(uri); if (dbpool == NULL) { ast_log(LOG_ERROR, "cannot make a connection pool for MongoDB\n"); break; } res = 0; // suceess } while (0); if (uri) mongoc_uri_destroy(uri); if (ast_test_flag(&config, CONFIG_REGISTERED) && (!cfg || dbname == NULL || dbcollection == NULL)) { ast_cdr_backend_suspend(NAME); ast_clear_flag(&config, CONFIG_REGISTERED); } else ast_cdr_backend_unsuspend(NAME); if (cfg && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg != CONFIG_STATUS_FILEINVALID) ast_config_destroy(cfg); return res; }
static int load_config(int reload) { char *cat = NULL; struct ast_config *cfg; struct ast_variable *v; struct ast_flags config_flags = {reload ? CONFIG_FLAG_FILEUNCHANGED : 0}; int newenablecdr = 0; cfg = ast_config_load(CONF_FILE, config_flags); if (cfg == CONFIG_STATUS_FILEUNCHANGED) { return 0; } if (cfg == CONFIG_STATUS_FILEINVALID) { ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE); return -1; } if (!cfg) { /* Standard configuration */ ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n"); if (enablecdr) { ast_cdr_backend_suspend(name); } enablecdr = 0; return -1; } if (reload) { ast_rwlock_wrlock(&config_lock); ast_free(bs_host); ast_free(bs_tube); } /* Bootstrap the default configuration */ bs_host = ast_strdup(DEFAULT_BEANSTALK_HOST); bs_port = DEFAULT_BEANSTALK_PORT; bs_tube = ast_strdup(DEFAULT_BEANSTALK_TUBE); priority = BEANSTALK_JOB_PRIORITY; while ((cat = ast_category_browse(cfg, cat))) { if (!strcasecmp(cat, "general")) { v = ast_variable_browse(cfg, cat); while (v) { if (!strcasecmp(v->name, "enabled")) { newenablecdr = ast_true(v->value); } else if (!strcasecmp(v->name, "host")) { ast_free(bs_host); bs_host = ast_strdup(v->value); } else if (!strcasecmp(v->name, "port")) { bs_port = atoi(v->value); } else if (!strcasecmp(v->name, "tube")) { ast_free(bs_tube); bs_tube = ast_strdup(v->value); } else if (!strcasecmp(v->name, "priority")) { priority = atoi(v->value); } v = v->next; } } } if (reload) { ast_rwlock_unlock(&config_lock); } ast_config_destroy(cfg); if (!newenablecdr) { ast_cdr_backend_suspend(name); } else if (newenablecdr) { ast_cdr_backend_unsuspend(name); ast_log(LOG_NOTICE, "Added beanstalkd server %s at port %d with tube %s", bs_host, bs_port, bs_tube); } enablecdr = newenablecdr; return 0; }
static int odbc_load_module(int reload) { int res = 0; struct ast_config *cfg; struct ast_variable *var; const char *tmp; struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; do { cfg = ast_config_load(config_file, config_flags); if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) { ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config_file); res = AST_MODULE_LOAD_DECLINE; break; } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) break; var = ast_variable_browse(cfg, "global"); if (!var) { /* nothing configured */ break; } if ((tmp = ast_variable_retrieve(cfg, "global", "dsn")) == NULL) { ast_log(LOG_WARNING, "cdr_odbc: dsn not specified. Assuming asteriskdb\n"); tmp = "asteriskdb"; } if (dsn) ast_free(dsn); dsn = ast_strdup(tmp); if (dsn == NULL) { res = -1; break; } if (((tmp = ast_variable_retrieve(cfg, "global", "dispositionstring"))) && ast_true(tmp)) ast_set_flag(&config, CONFIG_DISPOSITIONSTRING); else ast_clear_flag(&config, CONFIG_DISPOSITIONSTRING); if (((tmp = ast_variable_retrieve(cfg, "global", "loguniqueid"))) && ast_true(tmp)) { ast_set_flag(&config, CONFIG_LOGUNIQUEID); ast_debug(1, "cdr_odbc: Logging uniqueid\n"); } else { ast_clear_flag(&config, CONFIG_LOGUNIQUEID); ast_debug(1, "cdr_odbc: Not logging uniqueid\n"); } if (((tmp = ast_variable_retrieve(cfg, "global", "usegmtime"))) && ast_true(tmp)) { ast_set_flag(&config, CONFIG_USEGMTIME); ast_debug(1, "cdr_odbc: Logging in GMT\n"); } else { ast_clear_flag(&config, CONFIG_USEGMTIME); ast_debug(1, "cdr_odbc: Logging in local time\n"); } if (((tmp = ast_variable_retrieve(cfg, "global", "hrtime"))) && ast_true(tmp)) { ast_set_flag(&config, CONFIG_HRTIME); ast_debug(1, "cdr_odbc: Logging billsec and duration fields as floats\n"); } else { ast_clear_flag(&config, CONFIG_HRTIME); ast_debug(1, "cdr_odbc: Logging billsec and duration fields as integers\n"); } if ((tmp = ast_variable_retrieve(cfg, "global", "table")) == NULL) { ast_log(LOG_WARNING, "cdr_odbc: table not specified. Assuming cdr\n"); tmp = "cdr"; } if (table) ast_free(table); table = ast_strdup(tmp); if (table == NULL) { res = -1; break; } if (!ast_test_flag(&config, CONFIG_REGISTERED)) { res = ast_cdr_register(name, ast_module_info->description, odbc_log); if (res) { ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n"); } else { ast_set_flag(&config, CONFIG_REGISTERED); } } } while (0); if (ast_test_flag(&config, CONFIG_REGISTERED) && (!cfg || dsn == NULL || table == NULL)) { ast_cdr_backend_suspend(name); ast_clear_flag(&config, CONFIG_REGISTERED); } else { ast_cdr_backend_unsuspend(name); } if (cfg && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg != CONFIG_STATUS_FILEINVALID) { ast_config_destroy(cfg); } return res; }