/*
 *   m_hostforward UnrealIRCD module
 *   (C) 2005 John Brooks (http://john.yarbbles.info/ john@whyaresee.net)
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 1, or (at your option)
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#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

/* You can change the next two defines to what you want.
 * After changing, you will need to recompile the module.
 */

/* You can change this to any unused extban character you want to use */
#define HFBANCHAR 'f'

/* -----------------------------------------------
 * Do not change below this line unless you know
 * what you're doing.
 * ----------------------------------------------- */

char *hf_param(char *param);
int hf_isBanned(aClient *sptr, aChannel *chptr, char *ban, int chktype);

Extban *hfExtBan;
 
ModuleHeader MOD_HEADER(m_hostforward)
  = {
	"m_hostforward",
	"$Id: m_hostforward.c,v 1.0.2 2005/02/15 16:13:15 Special Exp $",
	"Forward users by hostmask to another channel", 
	"3.2-b8-1",
	NULL 
    };

DLLFUNC int MOD_INIT(m_hostforward)(ModuleInfo *modinfo)
{
	// Set the ExtbanInfo
	ExtbanInfo hfInfo;
	memset(&hfInfo, 0, sizeof(ExtbanInfo));
	hfInfo.flag = HFBANCHAR;
	hfInfo.conv_param = hf_param;
	hfInfo.is_banned = hf_isBanned;
	// Add the Extban
	hfExtBan = ExtbanAdd(modinfo->handle, hfInfo);
	if (!hfExtBan)
	{
		config_error("m_hostforward module: Adding extban %c failed! Module NOT loaded.",HFBANCHAR);
		return MOD_FAILED;
	}
	// The module must be permanent to specify extbans.
	ModuleSetOptions(modinfo->handle, MOD_OPT_PERM);
	return MOD_SUCCESS;
}

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

DLLFUNC int MOD_UNLOAD(m_hostforward)(int module_unload)
{
	ExtbanDel(hfExtBan);
	return MOD_SUCCESS;
}

// Fix the parameter if it is incorrect

char *hf_param(char *param)
{
	if (match("~?:#*:*!*@*", param) || strchr(param, ',') != NULL)
	{
		return NULL;
	}
	return param;
}

// Check if a user should be banned

int hf_isBanned(aClient *sptr, aChannel *chptr, char *ban, int chktype)
{
	char banstr[512], *rchan, *rmask;
	if (chktype != BANCHK_JOIN)
	{
		return 0;
	}
	strcpy(banstr, ban);
	// Get rid of the first token (~f)
	(void) strtok(banstr, ":");
	// Second token is the channel to redirect to..
	rchan = strtok(NULL, ":");
	// Third token is the mask.
	rmask = strtok(NULL, ":");
	if (!rmask || !rchan)
	{
		return 0;
	}
	if (!match(rmask, ban_ip) || !match(rmask, ban_realhost) || !match(rmask, ban_virthost))
	{
		char *jparv[3];
		// User should be forwarded. Do so.
		sendto_one(sptr, ":%s NOTICE %s :You are not allowed to join %s, so you are being forwarded to %s instead.",
			me.name, sptr->name, chptr->chname, rchan);
		jparv[0] = sptr->name;
		jparv[1] = rchan;
		jparv[2] = NULL;
		do_join(sptr, sptr, 2, jparv);
		return 1;
	}
	return 0;
}
