#include "config.h"
#include "struct.h"
#include "common.h"
#include "sys.h"
#include "numeric.h"
#include "msg.h"
#include "proto.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

ModuleHeader Mod_Header
  = {
	"Notice Protection",	/* Name of module */
	"v. 01", /* Version */
	"WorD.nonotice ModuLe Install v1.0", /* Short description of module */
	"3.2-b8-1",
	NULL 
    };

ModuleInfo NoNoticeModInfo;

static Hook *CheckUMsg = NULL, *CheckCMsg = NULL, *HookConfRun = NULL, *HookConfTest = NULL;

DLLFUNC char *nonotice_checkusermsg(aClient *, aClient *, aClient *, char *, int);
DLLFUNC char *nonotice_checkchanmsg(aClient *, aClient *, aChannel *, char *, int);
DLLFUNC int nonotice_config_run(ConfigFile *, ConfigEntry *, int);
DLLFUNC int nonotice_config_test(ConfigFile *, ConfigEntry *, int, int *);

static char *nonotice_msg = NULL;

DLLFUNC int Mod_Test(ModuleInfo *modinfo)
{
	HookConfTest = HookAddEx(NoNoticeModInfo.handle, HOOKTYPE_CONFIGTEST, nonotice_config_test);
	return MOD_SUCCESS;
}
DLLFUNC int	Mod_Init(ModuleInfo *modinfo)
{
	bcopy(modinfo,&NoNoticeModInfo,modinfo->size);
	nonotice_msg = strdup("Notice Mesaj Gonderimi Sunucu Uzerinde YasakLanmistir."); /* default */
	CheckUMsg = HookAddPCharEx(NoNoticeModInfo.handle, HOOKTYPE_USERMSG, nonotice_checkusermsg);
	CheckCMsg = HookAddPCharEx(NoNoticeModInfo.handle, HOOKTYPE_CHANMSG, nonotice_checkchanmsg);
	HookConfRun = HookAddEx(NoNoticeModInfo.handle, HOOKTYPE_CONFIGRUN, nonotice_config_run);
	return MOD_SUCCESS;
}

DLLFUNC int	Mod_Load(int module_load)
{
	return MOD_SUCCESS;
}


DLLFUNC int	Mod_Unload(int module_unload)
{
	HookDel(CheckUMsg);
	HookDel(CheckCMsg);
	HookDel(HookConfRun);
	HookDel(HookConfTest);
	return MOD_SUCCESS;
}

DLLFUNC int nonotice_config_run(ConfigFile *cf, ConfigEntry *ce, int type)
{
	if (type != CONFIG_SET)
		return 0;
	if (!strcmp(ce->ce_varname, "nonotice-msg"))
	{
		if (ce->ce_vardata)
		{
			if (nonotice_msg)
				free(nonotice_msg);
			nonotice_msg = strdup(ce->ce_vardata);
		}
		return 1;
	}
	return 0;
}

DLLFUNC int nonotice_config_test(ConfigFile *cf, ConfigEntry *ce, int type, int *errs)
{
	if (type != CONFIG_SET)
		return 0;
	if (!strcmp(ce->ce_varname, "nonotice-msg"))
	{
		if (!ce->ce_vardata)
		{
			config_error("%s:%i: set::nonotice-msg has no value", ce->ce_fileptr->cf_filename, ce->ce_varlinenum);
			*errs = 1;
			return -1;
		}
		return 1;
	}
	return 0;
}
	

DLLFUNC char *nonotice_checkusermsg(aClient *cptr, aClient *sptr, aClient *acptr, char *text, int notice)
{
	if (notice && !IsAnOper(sptr) && !IsULine(sptr) && !IsServer(sptr) && text && (text[0] != '\001'))
	{
		sendto_one(sptr, ":%s NOTICE %s :%s", me.name, sptr->name, nonotice_msg);
		return NULL;
	} else
		return text;
}

DLLFUNC char *nonotice_checkchanmsg(aClient *cptr, aClient *sptr, aChannel *chptr, char *text, int notice)
{
	if (notice && !IsAnOper(sptr) && !IsULine(sptr) && !IsServer(sptr) && text && (text[0] != '\001'))
	{
		sendto_one(sptr, ":%s NOTICE %s :%s", me.name, sptr->name, nonotice_msg);
		return NULL;
	} else
		return text;
}

