/* 
 *   m_killprotect.c:  Kill Protection Module for UnrealIRCd - Umode (+K)
 * 
 *   Copyright (C) 2005 Manuel Burki aka Darwin - All rights reserved.
 *
 *   See http://www.mburki.com 
 *
 *   Redistribution and use in source and binary forms, with or without 
 *   modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright notice, this
 *     list of conditions and the following  disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright notice, this 
 *     list of conditions and the following  disclaimer in the documentation and/or other
 *     materials provided with the package.
 *
 *   THIS SOFTWARE IS PROVIDED 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 *   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR
 *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY  OF SUCH DAMAGE.
 *
 *   $Id: m_killprotect.c,v 1.0 2005/02/26 23:54:13 Darwin Exp $
 */

#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

static ModuleInfo	*KModInfo;
static long 		UMODE_KILLPROTECT = 0;
static Umode		*UmodeKillProtect = NULL;
static 			int (ovr_kill)(Cmdoverride *ovr, aClient *cptr, aClient *sptr, int parc, char *parv[]);
static Cmdoverride 	*AddOverride(char *msg, iFP cb);
static Cmdoverride	*OvrKill;

ModuleHeader MOD_HEADER(m_killprotect)
  = {
	"Killprotect",
	"v 1.0 ",
	"Umode +K, Kill Protection",
	"3.2-b8-1",
	NULL 
    };
    
int umode_allow_netadmin(aClient *sptr, int what)
{
        if (MyClient(sptr))
                return IsNetAdmin(sptr) ? 1 : 0;
        else
                return 1;
}

DLLFUNC int MOD_INIT(m_killprotect)(ModuleInfo *modinfo)
{
	KModInfo = modinfo;
        UmodeKillProtect = UmodeAdd(modinfo->handle, 'K', UMODE_GLOBAL, umode_allow_netadmin, &UMODE_KILLPROTECT);
	if (!UmodeKillProtect)
	{
		config_error("m_killprotect: Unable to add usermode 'K': %s", ModuleGetErrorStr(modinfo->handle));
		return MOD_FAILED;
	}	    
	return MOD_SUCCESS;
}

DLLFUNC int MOD_LOAD(m_killprotect)(int module_load)
{
        int result = MOD_SUCCESS;
        
        OvrKill = AddOverride("kill", ovr_kill);
        if (!OvrKill) result = MOD_FAILED;

	return result;
}

DLLFUNC int MOD_UNLOAD(m_killprotect)(int module_unload)
{
	if (OvrKill && CommandExists("kill")) CmdoverrideDel(OvrKill); OvrKill = NULL;
        return MOD_SUCCESS;
}

Cmdoverride *AddOverride(char *msg, iFP cb)
{
	Cmdoverride *ovr = CmdoverrideAdd(KModInfo->handle, msg, cb);

	if (ModuleGetError(KModInfo->handle) != MODERR_NOERROR ||!ovr)
	{
		config_error("Unable to replace command %s - module: %s: %s",
			msg, MOD_HEADER(m_killprotect).name, ModuleGetErrorStr(KModInfo->handle));
		return NULL;
	}
	return ovr;
}

static int (ovr_kill)(Cmdoverride *ovr, aClient *cptr, aClient *sptr, int parc, char *parv[])
{
	static char	buf[BUFSIZE + 1];
	char		*mask, *reason, *nick;
	char		*p = NULL;
	aClient		*acptr;

	if (IsServer(sptr) || IsULine(sptr) || !IsAnOper(sptr) || IsNetAdmin(sptr) || !(parc > (2) && !BadPtr(parv[(2)])))
		return CallCmdoverride(ovr, cptr, sptr, parc, parv);

	mask	= parv[1];
	reason	= parv[2];
	buf[0]	= 0;

	if (MyClient(sptr))
		mask = canonize(mask);

	for (nick = strtoken(&p, mask, ","); nick; nick = strtoken(&p, NULL, ","))
	{
		if (!(acptr = find_client(nick, NULL)) && !(acptr = get_history(nick, (long)KILLCHASETIMELIMIT)))
		{
			sendto_one(sptr, err_str(ERR_NOSUCHNICK),
				me.name, sptr->name, nick);
			continue;
		}

		if (IsOper(acptr) && IsNetAdmin(acptr) && (acptr->umodes & UMODE_KILLPROTECT))
		{
			sendto_one(sptr, ":%s NOTICE %s :You are not allowed to KILL %s",
				me.name, sptr->name, acptr->name);
			sendto_realops("*** %s tried to KILL %s (%s)",
				sptr->name, acptr->name, reason);
			continue;
		}

		if (buf[0])
			strcat(buf, ",");
		strcat(buf, acptr->name);
	}

	if (!buf[0])
		return 0;

	parv[1] = buf;
	return CallCmdoverride(ovr, cptr, sptr, parc, parv);
}
