#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#ifdef WIN32
#include <io.h>
#include <fcntl.h>
#include <windows.h>
#else
#include <signal.h>
#endif
#include "dnews.h"
#include "lib.h"
#include "db.h"
#include "group.h"
#include "memdebug.h"
#include "conf.h"
#include "domain.h"
#include "chan.h"
#include "hist.h"

#undef mymalloc
#undef myfree
#define mymalloc(x) mymalloc2(x,M_DOMAIN)
#define myfree(x) myfree2(x,M_DOMAIN)

static Dom *dlist;
static Dom *curd;
int dom_free(void)
{
	Dom *d,*dnext;
	for (d=dlist;d!=NULL;d=dnext) {
		dnext = d->next;
		mystrfree(d->name,SD_DOM);		
		mystrfree(d->ip,SD_DOM);		
		mystrfree(d->org,SD_DOM);		
		mystrfree(d->post_add,SD_DOM);		
		mystrfree(d->path_stamp,SD_DOM);		
		mystrfree(d->addhead1,SD_DOM);		
		mystrfree(d->addhead2,SD_DOM);		
		mystrfree(d->addhead3,SD_DOM);		
		mystrfree(d->addhead4,SD_DOM);		
		mystrfree(d->addhead5,SD_DOM);		
		mystrfree(d->welcome,SD_DOM);		
		myfree(d);
	}
	dlist = NULL;
	curd = NULL;
	return TRUE;
}
int dom_reload(void)
{
	char name[BFSZ];
	char ip[BFSZ];
	char org[BFSZ];
	char bf[BFSZ];
	char *s;
	char cmd[BFSZ];
	char p1[BFSZ];
	char post_add[BFSZ];
	char path_stamp[BFSZ];
	char addhead1[BFSZ];
	char addhead2[BFSZ];
	char addhead3[BFSZ];
	char addhead4[BFSZ];
	char addhead5[BFSZ];
	char welcome[BFSZ];
	FILE *f;

	strcpy(post_add,"");
	strcpy(path_stamp,"");
	strcpy(addhead1,"");
	strcpy(addhead2,"");
	strcpy(addhead3,"");
	strcpy(addhead4,"");
	strcpy(addhead5,"");
	strcpy(name,"");
	strcpy(welcome,"");
	strcpy(ip,"");
	strcpy(org,"");
	dom_free();
	chan_cleardom();
	curd = NULL;
	f = fopen(file_config("domain.conf"),"r");
	if (f==NULL) {
		imsg("lib: dom: Could not find (%s)\n",file_config("domain.conf"));
		return TRUE;
	}
	for (;!feof(f);) {
		if (fgets(bf,BFSZ-1,f)==NULL) break;
		s = strchr(bf,'\n'); if (s!=NULL) *s = 0;
		ncpy(cmd,get_word(bf,0),BFSZ-1);
		ncpy(p1,get_word(bf,1),BFSZ-1);
		if (cmd[0]=='#') continue;
		if (strcmp(cmd,"domain")==0) {
			if (strlen(name)>0) dom_add(name,ip,org,post_add,path_stamp,addhead1,addhead2,addhead3,addhead4,addhead5,welcome);
			ncpy(name,p1,BFSZ-1);
			strcpy(org,"");
			strcpy(ip,"");
		}
		else if (strcmp(cmd,"ip")==0) ncpy(ip,p1,BFSZ-1);
		else if (strcmp(cmd,"org")==0) ncpy(org,get_rest_better(bf,1),BFSZ-1);
	
		else if (strcmp(cmd,"post_add")==0) ncpy(post_add,get_rest_better(bf,1),BFSZ-1);
		else if (strcmp(cmd,"path_stamp")==0) ncpy(path_stamp,get_rest_better(bf,1),BFSZ-1);
		else if (strcmp(cmd,"post_addhead1")==0) ncpy(addhead1,get_rest_better(bf,1),BFSZ-1);
		else if (strcmp(cmd,"post_addhead2")==0) ncpy(addhead2,get_rest_better(bf,1),BFSZ-1);
		else if (strcmp(cmd,"post_addhead3")==0) ncpy(addhead3,get_rest_better(bf,1),BFSZ-1);
		else if (strcmp(cmd,"post_addhead4")==0) ncpy(addhead4,get_rest_better(bf,1),BFSZ-1);
		else if (strcmp(cmd,"post_addhead5")==0) ncpy(addhead5,get_rest_better(bf,1),BFSZ-1);
		else if (strcmp(cmd,"welcome")==0) ncpy(welcome,get_rest_better(bf,1),BFSZ-1);
	
		else if (strcmp(cmd,"organization")==0) ncpy(org,get_rest_better(bf,1),BFSZ-1);

	}
	fclosep(f);
	if (strlen(name)>0) dom_add(name,ip,org,post_add,path_stamp,addhead1,addhead2,addhead3,addhead4,addhead5,welcome);
	return TRUE;
}
void dom_add(char *name, char *ip, char *org, char *post_add, char *path_stamp, char *addhead1,char *addhead2,char *addhead3,char *addhead4,char *addhead5, char *welcome)
{
	Dom *d;
	d = mymalloc(sizeof(*d));
	d->name = mystrdup(name,SD_DOM);
	d->ip = mystrdup(ip,SD_DOM);
	d->org = mystrdup(org,SD_DOM);

	d->post_add = mystrdup(post_add,SD_DOM);
	d->path_stamp = mystrdup(path_stamp,SD_DOM);
	d->addhead1 = mystrdup(addhead1,SD_DOM);
	d->addhead2 = mystrdup(addhead2,SD_DOM);
	d->addhead3 = mystrdup(addhead3,SD_DOM);
	d->addhead4 = mystrdup(addhead4,SD_DOM);
	d->addhead5 = mystrdup(addhead5,SD_DOM);
	d->welcome = mystrdup(welcome,SD_DOM);
	
	d->next = dlist;
	dlist = d;
	imsg("Found virtual domain (%s) ip (%s) org (%s) \n",name,ip,org);
	strcpy(name,"");
	strcpy(ip,"");
	strcpy(org,"");
}
char *nntp_localip(int chan);
int dom_setdom(int chan) /* Set current virtual domain */
{
	Dom *d;
	char *ipname;

	if (dlist==NULL) return TRUE;
	curd = chan_getdom(chan);
	if (curd==NULL) {
		ipname = nntp_localip(chan);
		for (d=dlist; d!=NULL; d=d->next) {
			if (strcmp(ipname,d->ip)==0) {
				curd = d;
				chan_setdom(chan,d);
				return TRUE;
			}
		}
	}
	return TRUE;
}

#define iffset(vvv) if (strlen(vvv)>0) return vvv;
char *dom_ini_get(int x)
{
	/* If domain setting for 'x' exists return it */
	if (curd==NULL) return config_get(x);
	if (x==C_MYNAME) return curd->name;

	if (x==C_POST_ADD) iffset(curd->post_add);
	if (x==C_STAMP_PATH) iffset(curd->path_stamp);
	if (x==C_POST_ADDHEAD1) iffset(curd->addhead1);
	if (x==C_POST_ADDHEAD2) iffset(curd->addhead2);
	if (x==C_POST_ADDHEAD3) iffset(curd->addhead3);
	if (x==C_POST_ADDHEAD4) iffset(curd->addhead4);
	if (x==C_POST_ADDHEAD5) iffset(curd->addhead5);
	if (x==C_WELCOME) iffset(curd->welcome);

	if (x==C_ORG) {
		if (strlen(curd->org)>0) return curd->org;
	}
	return config_get(x);
}

