/* Copyright (C) All Rights Reserved
** Modul Yazari: QBilisim Int Hizmetleri
*/

#include "config.h"
#include "struct.h"
#include "common.h"
#include "sys.h"
#include "numeric.h"
#include "msg.h"
#include "channel.h"
#include <time.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <io.h>
#endif
#include <fcntl.h>
#include "h.h"
#ifdef STRIPBADWORDS
#include "badwords.h"
#endif
#ifdef _WIN32
#include "version.h"
#endif

#define ircfree(x)		if (x) MyFree(x); x = NULL
#define ircstrdup(x,y)		if (x) MyFree(x); if (!y) x = NULL; else x = strdup(y)
#define CMD_WHOIS "WHOIS"

static ModuleInfo *XWHOIS = NULL;
Cmdoverride *WHOISOVR; 

typedef struct _whoisayar MyConf;
struct _whoisayar {
	int durum;
	char *kanal;
	long ayar;
};

extern ConfigEntry	*config_find_entry(ConfigEntry *ce, char *name);
static int config_test(ConfigFile *, ConfigEntry *, int, int *);
static int config_run(ConfigFile *, ConfigEntry *, int);


static int whoisreg(Cmdoverride *ovr, aClient *cptr, aClient *sptr, int parc, char *parv[]);
static Hook *ConfTest, *ConfRun;

MyConf myconf;

ModuleHeader MOD_HEADER(m_whoisayar)
= {
	"QBilisim Whois Ayar Modulu",
	"OmerAti",
	"QBilisim INT Hizmetleri",
	"3.2-b8-1",
	NULL,
};

DLLFUNC int MOD_TEST(m_whoisayar)(ModuleInfo *modinfo) {
	ConfTest = HookAddEx(modinfo->handle, HOOKTYPE_CONFIGTEST, config_test);
	return MOD_SUCCESS;
}

DLLFUNC int MOD_INIT(m_whoisayar)(ModuleInfo *modinfo)
{
	XWHOIS = modinfo;
	ConfRun	= HookAddEx(modinfo->handle, HOOKTYPE_CONFIGRUN, config_run);
	return MOD_SUCCESS;
}

DLLFUNC int MOD_LOAD(m_whoisayar)(int module_load)
{
WHOISOVR = CmdoverrideAdd(XWHOIS->handle, CMD_WHOIS, whoisreg);

	if(ModuleGetError(XWHOIS->handle) != MODERR_NOERROR || !WHOISOVR) {
		config_error("Modul yuklenirken hata oldu! %s: %s", MOD_HEADER(whoisreg).name, ModuleGetErrorStr(XWHOIS->handle));
		return MOD_FAILED;
	}
	return MOD_SUCCESS;
}

DLLFUNC int MOD_UNLOAD(m_whoisayar)(int module_unload)
{
	return MOD_SUCCESS;
}

static int config_test(ConfigFile *cf, ConfigEntry *ce, int type, int *errs)
{
	ConfigEntry	*cep;
	
	int	hata = 0;

	if (type != CONFIG_MAIN)
		return 0;

	if (!strcmp(ce->ce_varname, "whoisayar"))
	{
		for (cep = ce->ce_entries; cep; cep = cep->ce_next)
		{
			if (!cep->ce_varname)
			{
				config_error("%s:%i: blank whoisayar item",
					cep->ce_fileptr->cf_filename,
					cep->ce_varlinenum);
				hata++;
			}
			else if (!cep->ce_vardata)
			{
				config_error("%s:%i: whoisayar::%s item without value",
					cep->ce_fileptr->cf_filename,
					cep->ce_varlinenum, cep->ce_varname);
				hata++;
			}
			else if (!strcmp(cep->ce_varname, "kanal"))
				;
			else if (!strcmp(cep->ce_varname, "durum"))
				;
			else if (!strcmp(cep->ce_varname, "ayar"))
				;
			else 
			{
				config_error("%s:%i: unknown directive whoisayar::%s",
					cep->ce_fileptr->cf_filename, cep->ce_varlinenum,
					cep->ce_varname);
				hata++;
			}
		}
		*errs = hata;
		return hata ? -1 : 1;
	}
	return 0;
}

static int config_run(ConfigFile *cf, ConfigEntry *ce, int type)
{
	ConfigEntry	*cep;

	if (type != CONFIG_MAIN)
		return 0;

	if (!strcmp(ce->ce_varname, "whoisayar"))
	{
		for (cep = ce->ce_entries; cep; cep = cep->ce_next)
		{
			if (!strcmp(cep->ce_varname, "kanal"))
			{
				ircstrdup(myconf.kanal, cep->ce_vardata);
			}
			else if (!strcmp(cep->ce_varname, "durum"))
				myconf.durum = config_checkval(cep->ce_vardata, CFG_YESNO);
			else if (!strcmp(cep->ce_varname, "ayar"))
				myconf.ayar = config_checkval(cep->ce_vardata, CFG_TIME);
		}
	}
	return 0;
}


static int whoisreg(Cmdoverride *ovr, aClient *cptr, aClient *sptr, int parc, char *parv[]) {

// Kayitsiz Nickler Whois Çekemez Ayar 1
if (!IsARegNick(sptr) && !IsOper(sptr) && (myconf.ayar == 1)) {
sendnotice(sptr, "*** \2WHOIS\2 komutu kullanabilmeniz icin nickinizin kayitli olmasi gerekiyor.");
return 0;
}
// Kayitsiz  ve Kayitli Nickler Whois Çekemez Ayar 2
if ((!IsOper(sptr) && myconf.ayar == 2)) {
sendnotice(sptr, "*** \2WHOIS\2 komutu kullanima kapatilmistir.");
return 0;
}
	return CallCmdoverride(ovr, cptr, sptr, parc, parv);
}