/*
 * ===========================================================================
 * Filename:		m_quarantine.c
 * Description:         Commands /viruschan /virusrel
 * Author:		DeadNotBuried
 * Documentation:       will do some when i get time :)
 * Credits:		some code stolen from the Unreal spamfilter system :)
 * ===========================================================================
 */

/*
 * adds commands
 * /viruschan <nick> <reason>
 * /virusrel <nick>
 * to quarantine in the spamfilter viruschan
 * and to release from the viruschan
 */


/*
 * Installation
 * copy to the modules directory,
 * then in the main unreal directory
 * make custommodule MODULEFILE=m_quarantine
 */
 
#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

extern int MODVAR spamf_ugly_vchanoverride;

extern void		sendto_one(aClient *to, char *pattern, ...);
extern void		sendto_serv_butone_token(aClient *one, char *prefix, char *command, char *token, char *pattern, ...);

#define MSG_VIRUSQUARANTINE	"VIRUSCHAN"
#define TOK_VIRUSQUARANTINE	"VQ"
#define MSG_VIRUSRELEASE	"VIRUSREL"
#define TOK_VIRUSRELEASE	"VR"
#define DelCommand(x)	if (x) CommandDel(x); x = NULL
#define IsParam(x)      (parc > (x) && !BadPtr(parv[(x)]))
#define IsNotParam(x)   (parc <= (x) || BadPtr(parv[(x)]))

/* added as the ClearVirus(x) in unreal3.2.4 is bugged */
#define ReleaseVirus(x)		((x)->flags &= ~FLAGS_VIRUS)


static Command		*AddCommand(Module *module, char *msg, char *token, iFP func, int params);
static int		m_viruschan(aClient *cptr, aClient *sptr, int parc, char *parv[]);
static int		m_virusrel(aClient *cptr, aClient *sptr, int parc, char *parv[]);

Command			*CmdVirusChan, *CmdVirusRel;

ModuleHeader MOD_HEADER(m_chgswhois) = {
	"m_quarantine",
	"$Id: m_quarantine.c,v 1.0 2006/02/23 23:03:43 DeadNotBuried Exp $",
	"Quarantine and Release System using Spamfilters VirusChan",
	"3.2-b8-1",
	NULL 
};

DLLFUNC int MOD_INIT(m_chgswhois)(ModuleInfo *modinfo)
{
	CmdVirusChan = AddCommand(modinfo->handle, MSG_VIRUSQUARANTINE, TOK_VIRUSQUARANTINE, m_viruschan, 2);
	CmdVirusRel = AddCommand(modinfo->handle, MSG_VIRUSRELEASE, TOK_VIRUSRELEASE, m_virusrel, 1);
	if( !CmdVirusChan || !CmdVirusRel )
		return MOD_FAILED;
	return MOD_SUCCESS;
}

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

DLLFUNC int MOD_UNLOAD(m_viruschan)(int module_unload)
{
	DelCommand(CmdVirusChan);
	DelCommand(CmdVirusRel);
	return MOD_SUCCESS;
}

static Command *AddCommand(Module *module, char *msg, char *token, iFP func, int params)
{
	Command *cmd;

	if (CommandExists(msg))
    	{
		config_error("Command %s already exists", msg);
		return NULL;
    	}
    	if (CommandExists(token))
	{
		config_error("Token %s already exists", token);
		return NULL;
    	}
	cmd = CommandAdd(module, msg, token, func, params, 0);
#ifndef STATIC_LINKING
	if (ModuleGetError(module) != MODERR_NOERROR || !cmd)
#else
	if (!cmd)
#endif
	{
#ifndef STATIC_LINKING
		config_error("Error adding command %s: %s", msg,
			ModuleGetErrorStr(module));
#else
		config_error("Error adding command %s", msg);
#endif
		return NULL;
	}
	return cmd;
}

static int m_viruschan(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
	aClient *acptr;
	char *xparv[3], buf[2048];
	int ret;

	if( !IsServer(sptr) && !IsOper(sptr) && !IsMe(sptr) && !IsULine(sptr) && !IsAnOper(sptr) )
		return 0;
	if( IsNotParam(1) || IsNotParam(2) )
		return 0;
	if( !( acptr = find_person(parv[1], NULL) ) )
	{
		sendto_one(sptr, err_str(ERR_NOSUCHNICK),
			me.name, sptr->name, parv[1]);
		return 0;
	}
	if( !MyConnect(acptr) )
	{
		sendto_serv_butone(cptr, ":%s VQ %s %s", sptr->name, parv[1], parv[2]);
		return 0;
	}
	snprintf(buf, sizeof(buf), "0,%s", SPAMFILTER_VIRUSCHAN);
	xparv[0] = acptr->name;
	xparv[1] = buf;
	xparv[2] = NULL;
	spamf_ugly_vchanoverride = 1;
	ret = do_cmd(acptr, acptr, "JOIN", 2, xparv);
	spamf_ugly_vchanoverride = 0;
	if (ret == FLUSH_BUFFER)
		return FLUSH_BUFFER;
	sendnotice(acptr, "You are now restricted to talking in %s: %s",
		SPAMFILTER_VIRUSCHAN, parv[2]);
	SetVirus(acptr);
	return 0;
}

static int m_virusrel(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
	aClient *acptr;
	char *xparv[3], chbuf[CHANNELLEN + 16], buf[2048]="0\0";
	aChannel *chptr;
	int ret;

	if( !IsServer(sptr) && !IsOper(sptr) && !IsMe(sptr) && !IsULine(sptr) && !IsAnOper(sptr) )
		return 0;
	if( IsNotParam(1) )
		return 0;
	if( !( acptr = find_person(parv[1], NULL) ) )
	{
		sendto_one(sptr, err_str(ERR_NOSUCHNICK),
			me.name, sptr->name, parv[1]);
		return 0;
	}
	if( !MyConnect(acptr) )
	{
		sendto_serv_butone(cptr, ":%s VR %s", sptr->name, parv[1]);
		return 0;
	}
	xparv[0] = acptr->name;
	xparv[1] = buf;
	xparv[2] = NULL;
	spamf_ugly_vchanoverride = 1;
	ret = do_cmd(acptr, acptr, "JOIN", 2, xparv);
	spamf_ugly_vchanoverride = 0;
	if (ret == FLUSH_BUFFER)
		return FLUSH_BUFFER;
	sendnotice(acptr, "You are now released from Quarantine");
	ReleaseVirus(acptr);
	return 0;
}
