static int parse_line(char* line, int line_count, void* ptr_data) { struct linked_list* users = (struct linked_list*) ptr_data; struct cfg_tokens* tokens = cfg_tokenize(line); enum auth_credentials cred; char* credential; char* username; char* password; if (cfg_token_count(tokens) == 0) { cfg_tokens_free(tokens); return 0; } if (cfg_token_count(tokens) < 2) { cfg_tokens_free(tokens); return -1; } credential = cfg_token_get_first(tokens); username = cfg_token_get_next(tokens); password = cfg_token_get_next(tokens); if (!auth_string_to_cred(credential, &cred)) { cfg_tokens_free(tokens); return -1; } insert_user(users, username, password, cred); cfg_tokens_free(tokens); return 0; }
static struct patterns_data* parse_config(const char* line, struct plugin_handle* plugin) { struct patterns_data* data = (struct patterns_data*) hub_malloc_zero(sizeof(struct patterns_data)); struct cfg_tokens* tokens = cfg_tokenize(line); char* token = cfg_token_get_first(tokens); if (!data) return 0; while (token) { struct cfg_settings* setting = cfg_settings_split(token); if (!setting) { set_error_message(plugin, "Unable to parse startup parameters"); cfg_tokens_free(tokens); hub_free(data); return 0; } if (strcmp(cfg_settings_get_key(setting), "file") == 0) { if (!data->db) { if (sqlite3_open(cfg_settings_get_value(setting), &data->db)) { cfg_tokens_free(tokens); cfg_settings_free(setting); hub_free(data); set_error_message(plugin, "Unable to open database file"); return 0; } } } else { set_error_message(plugin, "Unknown startup parameters given"); cfg_tokens_free(tokens); cfg_settings_free(setting); hub_free(data); return 0; } cfg_settings_free(setting); token = cfg_token_get_next(tokens); } cfg_tokens_free(tokens); if (!data->db) { set_error_message(plugin, "No database file is given, use file=<database>"); hub_free(data); return 0; } return data; }
static struct log_data* parse_config(const char* line, struct plugin_handle* plugin) { struct log_data* data = (struct log_data*) hub_malloc_zero(sizeof(struct log_data)); struct cfg_tokens* tokens = cfg_tokenize(line); char* token = cfg_token_get_first(tokens); uhub_assert(data != NULL); data->srvtdiff = 0; while (token) { struct cfg_settings* setting = cfg_settings_split(token); if (!setting) { set_error_message(plugin, "Unable to parse startup parameters"); cfg_tokens_free(tokens); hub_free(data); return 0; } if (strcmp(cfg_settings_get_key(setting), "file") == 0) { if (!data->db) { if (sqlite3_open(cfg_settings_get_value(setting), &data->db)) { cfg_tokens_free(tokens); cfg_settings_free(setting); hub_free(data); set_error_message(plugin, "Unable to open database file"); return 0; } } } else if (strcmp(cfg_settings_get_key(setting), "server_time_diff") == 0) { data->srvtdiff = uhub_atoi(cfg_settings_get_value(setting)); } else { set_error_message(plugin, "Unknown startup parameters given"); cfg_tokens_free(tokens); cfg_settings_free(setting); hub_free(data); return 0; } cfg_settings_free(setting); token = cfg_token_get_next(tokens); } cfg_tokens_free(tokens); return data; }
static int plugin_parse_line(char* line, int line_count, void* ptr_data) { struct hub_info* hub = (struct hub_info*) ptr_data; struct uhub_plugins* handle = hub->plugins; struct cfg_tokens* tokens = cfg_tokenize(line); struct plugin_handle* plugin; char *directive, *soname, *params; if (cfg_token_count(tokens) == 0) { cfg_tokens_free(tokens); return 0; } if (cfg_token_count(tokens) < 2) { cfg_tokens_free(tokens); return -1; } directive = cfg_token_get_first(tokens); soname = cfg_token_get_next(tokens); params = cfg_token_get_next(tokens); if (strcmp(directive, "plugin") == 0 && soname && *soname) { if (!params) params = ""; LOG_PLUGIN("Load plugin: \"%s\", params=\"%s\"", soname, params); plugin = plugin_load(soname, params, hub); if (plugin) { list_append(handle->loaded, plugin); cfg_tokens_free(tokens); return 0; } } cfg_tokens_free(tokens); return -1; }
static struct chat_history_data* parse_config(const char* line, struct plugin_handle* plugin) { struct chat_history_data* data = (struct chat_history_data*) hub_malloc_zero(sizeof(struct chat_history_data)); struct cfg_tokens* tokens = cfg_tokenize(line); char* token = cfg_token_get_first(tokens); uhub_assert(data != NULL); data->history_max = 200; data->history_default = 25; data->history_connect = 5; data->chat_history = list_create(); while (token) { struct cfg_settings* setting = cfg_settings_split(token); if (!setting) { set_error_message(plugin, "Unable to parse startup parameters"); cfg_tokens_free(tokens); hub_free(data); return 0; } if (strcmp(cfg_settings_get_key(setting), "history_max") == 0) { data->history_max = (size_t) uhub_atoi(cfg_settings_get_value(setting)); } else if (strcmp(cfg_settings_get_key(setting), "history_default") == 0) { data->history_default = (size_t) uhub_atoi(cfg_settings_get_value(setting)); } else if (strcmp(cfg_settings_get_key(setting), "history_connect") == 0) { data->history_connect = (size_t) uhub_atoi(cfg_settings_get_value(setting)); } else { set_error_message(plugin, "Unknown startup parameters given"); cfg_tokens_free(tokens); cfg_settings_free(setting); hub_free(data); return 0; } cfg_settings_free(setting); token = cfg_token_get_next(tokens); } cfg_tokens_free(tokens); return data; }
static struct sql_data* parse_config(const char* line, struct plugin_handle* plugin) { struct sql_data* data = (struct sql_data*) hub_malloc_zero(sizeof(struct sql_data)); struct cfg_tokens* tokens = cfg_tokenize(line); char* token = cfg_token_get_first(tokens); if (!data) return 0; while (token) { char* split = strchr(token, '='); size_t len = strlen(token); size_t key = split ? (split - token) : len; if (key == 4 && strncmp(token, "file", 4) == 0 && data->db == 0) { if (sqlite3_open(split + 1, &data->db)) { cfg_tokens_free(tokens); hub_free(data); set_error_message(plugin, "Unable to open database file"); return 0; } } else if (key == 9 && strncmp(token, "exclusive", 9) == 0) { if (!string_to_boolean(split + 1, &data->exclusive)) data->exclusive = 1; } else { set_error_message(plugin, "Unable to parse startup parameters"); cfg_tokens_free(tokens); hub_free(data); return 0; } token = cfg_token_get_next(tokens); } cfg_tokens_free(tokens); if (!data->db) { set_error_message(plugin, "No database file is given, use file=<database>"); hub_free(data); return 0; } return data; }
static struct acl_data* parse_config(const char* line) { struct acl_data* data = (struct acl_data*) hub_malloc_zero(sizeof(struct acl_data)); struct cfg_tokens* tokens = cfg_tokenize(line); char* token = cfg_token_get_first(tokens); if (!data) return 0; // set defaults data->readonly = 1; data->exclusive = 0; data->users = list_create(); while (token) { char* split = strchr(token, '='); size_t len = strlen(token); size_t key = split ? (split - token) : len; if (key == 4 && strncmp(token, "file", 4) == 0) { if (data->file) hub_free(data->file); data->file = strdup(split + 1); } else if (key == 8 && strncmp(token, "readonly", 8) == 0) { if (!string_to_boolean(split + 1, &data->readonly)) data->readonly = 1; } else if (key == 9 && strncmp(token, "exclusive", 9) == 0) { if (!string_to_boolean(split + 1, &data->exclusive)) data->exclusive = 1; } else { cfg_tokens_free(tokens); free_acl(data); return 0; } token = cfg_token_get_next(tokens); } cfg_tokens_free(tokens); return data; }
struct cfg_settings* cfg_settings_split(const char* line) { struct cfg_settings* s = NULL; struct cfg_tokens* tok = NULL; char* pos = NULL; if ( !line || !*line || ((pos = (char*) strchr(line, '=')) == NULL) || ((s = hub_malloc_zero(sizeof(struct cfg_settings))) == NULL) || ((tok = cfg_tokenize(line)) == NULL) || (cfg_token_count(tok) < 1) || (cfg_token_count(tok) > 3) || (cfg_token_count(tok) == 3 && strcmp(cfg_token_get(tok, 1), "=")) ) { cfg_tokens_free(tok); cfg_settings_free(s); return NULL; } if (cfg_token_count(tok) == 1) { char* key = cfg_token_get_first(tok); pos = strchr(key, '='); if (!pos) { cfg_tokens_free(tok); cfg_settings_free(s); return NULL; } pos[0] = 0; key = strip_white_space(key); if (!*key) { cfg_tokens_free(tok); cfg_settings_free(s); return NULL; } s->key = strdup(key); s->value = strdup(strip_white_space(pos+1)); } else if (cfg_token_count(tok) == 2) { char* key = cfg_token_get_first(tok); char* val = cfg_token_get_next(tok); if ((pos = strchr(key, '='))) { pos[0] = 0; key = strip_white_space(key); } else if ((pos = strchr(val, '='))) { val = strip_white_space(pos+1); } else { cfg_tokens_free(tok); cfg_settings_free(s); return NULL; } if (!*key) { cfg_tokens_free(tok); cfg_settings_free(s); return NULL; } s->key = strdup(key); s->value = strdup(val); } else { s->key = strdup(strip_white_space(cfg_token_get(tok, 0))); s->value = strdup(strip_white_space(cfg_token_get(tok, 2))); } cfg_tokens_free(tok); return s; }