#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 _WIN32
#include "version.h"
#endif

#define BOTSKANALI "#xline"
#define LINEKANALI "#hzangsystemzzz"

void Delete_Hook(Hook *Dlt);

static Hook *H_TklAdd=NULL, *H_TklDel=NULL, *H_Giris1=NULL, *H_Cikis1=NULL;

static int m_tkl_add(aClient *, aClient *, aTKline *, int, char *[]);
static int m_tkl_del(aClient *, aClient *, aTKline *, int, char *[]);
int m_topicc(aClient *cptr, aClient *sptr, aChannel *chptr, char *topic);

//int m_giris1(aClient *sptr);
//int m_cikis1(aClient *sptr, char *msj);

#ifndef STATIC_LINKING
static ModuleInfo	*Mod_Loglama;
#endif

ModuleHeader MOD_HEADER(KomutLog) = {
	"KomutLog",
	"Komut Loglama",
	"Special QBilisim",
	"3.2-b8-1",
	NULL 
};

DLLFUNC int MOD_TEST(KomutLog)(ModuleInfo *modinfo) {
return MOD_SUCCESS;
}

DLLFUNC int MOD_INIT(KomutLog)(ModuleInfo *modinfo) {
#ifndef STATIC_LINKING
	Mod_Loglama = modinfo;
#endif
	HookAddEx(modinfo->handle, HOOKTYPE_LOCAL_TOPIC, m_topicc);
	H_TklAdd	= HookAddEx(modinfo->handle, HOOKTYPE_TKL_ADD, m_tkl_add);
	H_TklDel	= HookAddEx(modinfo->handle, HOOKTYPE_TKL_DEL, m_tkl_del);
//	H_Giris1	= HookAddEx(modinfo->handle, HOOKTYPE_LOCAL_CONNECT, m_giris1);
//	H_Cikis1	= HookAddEx(modinfo->handle, HOOKTYPE_LOCAL_QUIT, m_cikis1);
return MOD_SUCCESS;
}

DLLFUNC int MOD_LOAD(KomutLog)(int module_load) {
return MOD_SUCCESS;
}

DLLFUNC int MOD_UNLOAD(KomutLog)(int module_unload) {
	Delete_Hook(H_TklAdd);
	Delete_Hook(H_TklDel);
	Delete_Hook(H_Giris1);
	Delete_Hook(H_Cikis1);
return MOD_SUCCESS;
}

void Delete_Hook(Hook *Dlt) {
	if (Dlt) {
		HookDel(Dlt);
		Dlt = NULL;
	}
}

static int m_tkl_add(aClient *cptr, aClient *sptr, aTKline *tk, int parc, char *parv[]) {
	char Buff[512];
	aChannel *ChPtr;
	aClient *acptr;
	int ok=1;
	int Bulundu = 0, Bulundux = 0, Cogalt = 0, i, t;
	char Kllnc[4][256];

	if (parc < 6)
	return 0;
	
	if (me.serv->numeric != 1)
		return;
	memset(Buff,0,sizeof(Buff));

	if ((ChPtr = find_channel(BOTSKANALI, NullChn)) != NullChn) {
		switch (parv[2][0]) {
		case 'k':sprintf(Buff, "K:Line"); break;
		case 'z':sprintf(Buff, "Z:Line"); break;
		case 'G':sprintf(Buff, "G:Line"); break;
		case 'Z':sprintf(Buff, "Global Z:Line"); break;
		case 's':sprintf(Buff, "Shun"); break;
		case 'f':sprintf(Buff, "Spamfilter"); break;
		case 'F':sprintf(Buff, "Global Spamfilter"); break;
		case 'q':sprintf(Buff, "Q:Line"); break;
		case 'Q':sprintf(Buff, "Global Q:Line"); break;
		default :sprintf(Buff, "<Bilinmeyen Line>"); ok=0; break;
		}
		if (ok == 0)
		return 0;
	
		sendto_channel_butserv(ChPtr, &me, ":%s PRIVMSG %s :%s Eklendi. Uygulayan : %s (IP : %s)",
			me.name, ChPtr->chname, Buff, parv[5], parv[4]);

		for (i=0; i<4; i++)
			memset(Kllnc[i], 0, sizeof(Kllnc[i]));
		for (acptr = client; acptr; acptr = acptr->next) {
			if (acptr->name==me.name)
				continue;
			if (IsServer(acptr))
				continue;
			if (IsUnknown(acptr))
				continue;
			char *Bak = GetIP(acptr);
			if (Bak && !match(Bak, parv[4])) {
				if (strlen(Kllnc[Cogalt]) > (255-NICKLEN)) {
					Bulundu=0;
					Cogalt++;
				}
				if (Bulundu != 0) {
					strcat(Kllnc[Cogalt], ", ");
				} else {
					strcat(Kllnc[Cogalt], Bak);
					strcat(Kllnc[Cogalt], " -> ");
				}
				strcat(Kllnc[Cogalt], acptr->name);
				Bulundu++;
				Bulundux++;
			}
		}
		if (Bulundux > 0) {
			for (t=0; t<=Cogalt; t++)
				sendto_channel_butserv(ChPtr, &me, ":%s PRIVMSG %s :%s", me.name, ChPtr->chname, Kllnc[t]);
		}
	}
return 1;
}

static int m_tkl_del(aClient *cptr, aClient *sptr, aTKline *tk, int parc, char *parv[]) {
	char Buff[512];
	aChannel *ChPtr;
	int ok=1;

	if (parc < 6)
	return 0;

	if (me.serv->numeric != 1)
		return;

	memset(Buff,0,sizeof(Buff));

	if ((ChPtr = find_channel(BOTSKANALI, NullChn)) != NullChn) {
		switch (parv[2][0]) {
		case 'k':sprintf(Buff, "K:Line"); break;
		case 'z':sprintf(Buff, "Z:Line"); break;
		case 'G':sprintf(Buff, "G:Line"); break;
		case 'Z':sprintf(Buff, "Global Z:Line"); break;
		case 's':sprintf(Buff, "Shun"); break;
		case 'f':sprintf(Buff, "Spamfilter"); break;
		case 'F':sprintf(Buff, "Global Spamfilter"); break;
		case 'q':sprintf(Buff, "Q:Line"); break;
		case 'Q':sprintf(Buff, "Global Q:Line"); break;
		default :sprintf(Buff, "<Bilinmeyen Line>"); ok=0; break;
		}
		if (ok == 0)
		return 0;
		sendto_channel_butserv(ChPtr, &me, ":%s PRIVMSG %s :%s Silindi. Uygulayan : %s (IP : %s)",
			me.name, ChPtr->chname, Buff, parv[5], parv[4]);
	}

return 1;
}

int m_topicc(aClient *cptr, aClient *sptr, aChannel *chptr, char *topic) {
	aChannel *ChPtr = NullChn;
	if ((ChPtr = find_channel(LINEKANALI, NullChn)) != NullChn && topic) {
		if (stricmp(chptr->chname, "#oyun") &&
			stricmp(chptr->chname, "#kelime") &&
			stricmp(chptr->chname, "#radyo")) {
			sendto_channel_butserv(ChPtr, &me, ":%s PRIVMSG %s :%s Topic'i %s tarafından değişti. (%s)",
				me.name, ChPtr->chname, chptr->chname, sptr->name, topic);
		}
	}
return 1;
}
/*
int m_giris1(aClient *sptr) {
	aClient *acptr;

	if (!sptr->user)
	return 1;

	if (!MyClient(sptr))
	return 1;

	for (acptr = client; acptr; acptr = acptr->next) {
		if ((acptr->umodes & UMODE_SERVICES) || !IsPerson(acptr) || !IsNetAdmin(acptr))
		continue;
		sendto_one(acptr, ":%s NOTICE %s :*** Notice -- Client connecting on port %d: %s (%s@%s) [%s] %s%s%s",
			me.name, acptr->name, sptr->listener->port, sptr->name, sptr->user->username,
			sptr->user->realhost, sptr->class ? sptr->class->name : "",
			#ifdef USE_SSL
			IsSecure(sptr) ? "[secure " : "",
			IsSecure(sptr) ? SSL_get_cipher((SSL *)sptr->ssl) : "",
			IsSecure(sptr) ? "]" : "");
			#else
			"", "", "");
			#endif
	}
return 1;
}

int m_cikis1(aClient *sptr, char *msj) {
	aClient *acptr;

	if (!sptr->user)
	return 1;

	if (!MyClient(sptr))
	return 1;

	for (acptr = client; acptr; acptr = acptr->next) {
		if ((acptr->umodes & UMODE_SERVICES) || !IsPerson(acptr) || !IsNetAdmin(acptr))
		continue;
		sendto_one(acptr, ":%s NOTICE %s :*** Notice -- Client exiting: %s (%s@%s) [%s]",
			me.name, acptr->name, sptr->name, sptr->user->username, sptr->user->realhost, msj);
	}
return 1;
}
*/
