#include "includes.h"

bool cs_load_config(char *cfgfile, irc_cfg_t *icfg)
{
	config_t *head;
	char *tmp;
	bool ret;
	size_t siz, idx;

	ret = true;
	if(!parse_cfg(cfgfile, &head)) {
		icfg->head = NULL;
		return false;
	}
	icfg->head = head;
	
	icfg->host = cfg_value(head, "irc_host");
	if(!icfg->host) fail(ret, false, c);
	
	tmp = cfg_value(head, "irc_port");
	if(!tmp) fail(ret, false, c);
	icfg->port = atoi(tmp);

	icfg->nick = cfg_value(head, "irc_nick");
	if(!icfg->nick) fail(ret, false, c);

	icfg->channel = cfg_value(head, "irc_channel");
	if(!icfg->channel) fail(ret, false, c);

	icfg->ownernick = cfg_value(head, "irc_ownernick");
	if(!icfg->ownernick) fail(ret, false, c);

	icfg->authpasswd = cfg_value(head, "auth_password");
	if(!icfg->authpasswd) fail(ret, false, c);

	if(cfg_positive(head, "use_ssl"))
		icfg->net_ctx.flags = NET_SSL;
	else icfg->net_ctx.flags = 0;

	idx = 0;
	siz = BUFSIZ;
	icfg->authlist = calloc(idx, sizeof(char *));
	if(!icfg->authlist) fail(ret, false, c);

	tmp = cfg_value(head, "auth_list");
	if(tmp) {
		tmp = strtok(tmp, ";");
		while(tmp) {
			if(idx == siz) {
				siz += BUFSIZ;
				icfg->authlist = realloc(icfg->authlist, siz * sizeof(char *));
				if(!icfg->authlist) fail(ret, false, c);
				memset(icfg->authlist[idx], 0, BUFSIZ * sizeof(char *));
			}
			icfg->authlist[idx] = tmp;
		}
	}

c:
	if(ret == false && head)
		free_cfg(head);
	return ret;
}

bool cs_save_config(char *cfgfile, irc_cfg_t *icfg)
{
	return true;
}

char *cfg_value(config_t *head, char *option)
{
	config_t *cur;

	cur = head;
	while(cur && cur->option) {
		if(!strcasecmp(cur->option, option))
			return cur->value;
		cur = cur->next;
	}

	return NULL;
}

bool cfg_positive(config_t *head, char *option)
{
	static const char *pos_val[] = { "on", "enabled", "active", "true", "positive", "1", NULL, };
	char *val;

	val = cfg_value(head, option);
	if(!val) return false;

	if(strccmp(pos_val, val) != -1)
		return true;

	return false;
}

bool parse_cfg(char *cfgfile, config_t **head)
{
	char str[CS_LIM+1];
	config_t *cur;
	FILE *fp;
	bool ret;

	cur = (*head) = NULL;
	ret = true;
	fp  = fopen(cfgfile, "rb");
	if(!fp) fail(ret, false, c);

	cur = (*head) = calloc(1, sizeof(config_t));
	if(!cur) fail(ret, false, c);

	while(fgets(str, CS_LIM, fp)) {
		char option[CS_LIM+1];
		char value[CS_LIM+1];
		char *tmp;

		tmp = strchr(str, ';');
		if(tmp) *tmp = '\0';
		if(sscanf(str, "%s = %s", option, value) != 2)
			continue;
		printf("%s is %s\n", option, value);
		cur->option = strdup(option);
		if(!cur->option)
			continue;
		cur->value = strdup(value);
		if(!cur->value) {
			free(cur->option);
			cur->option = NULL;
			continue;
		}

		cur->next = calloc(1, sizeof(config_t));
		if(!cur->next) break;
		cur = cur->next;
	}

c:
	if(ret == false && (*head)) {
		free((*head));
		(*head) = NULL;
	}
	if(fp) fclose(fp);
	return ret;
}

void free_cfg(config_t *head)
{
	config_t *cur;

	cur = head;
	while(cur) {
		if(cur->option)
			free(cur->option);
		if(cur->value)
			free(cur->value);
		head = cur;
		cur = cur->next;
		free(head);
	}
}
