/*
 * Created by k4be, based on extbans/account.c
 * Extended ban to ban/exempt by server name(~b ~s:irc.example.com)
 * (C) Copyright 2011-.. Bram Matthys (Syzop) and the UnrealIRCd team
 *
 * 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 "unrealircd.h"

ModuleHeader MOD_HEADER
= {
	"third/serverban",
	"5.0",
	"ExtBan ~s - Ban/exempt by server name",
	"k4be@PIRC",
	"unrealircd-5",
};

/* Forward declarations */
char *extban_server_conv_param(char *para);
int extban_server_is_banned(Client *client, Channel *channel, char *banin, int type, char **msg, char **errmsg);

/** Called upon module init */
MOD_INIT()
{
	ExtbanInfo req;
	
	req.flag = 's';
	req.is_ok = NULL;
	req.conv_param = extban_server_conv_param;
	req.is_banned = extban_server_is_banned;
	req.options = EXTBOPT_INVEX|EXTBOPT_TKL;
	if (!ExtbanAdd(modinfo->handle, req))
	{
		config_error("could not register extended ban type");
		return MOD_FAILED;
	}

	return MOD_SUCCESS;
}

/** Called upon module load */
MOD_LOAD()
{
	return MOD_SUCCESS;
}

/** Called upon unload */
MOD_UNLOAD()
{
	return MOD_SUCCESS;
}

/** Account bans */
char *extban_server_conv_param(char *para)
{
	char *mask, *srv;
	static char retbuf[NICKLEN + 4];

	strlcpy(retbuf, para, sizeof(retbuf)); /* truncate */

	srv = retbuf+3;
	if (!*srv)
		return NULL; /* don't allow "~s:" */

	return retbuf;
}

int extban_server_is_banned(Client *client, Channel *channel, char *banin, int type, char **msg, char **errmsg)
{
	char *ban = banin+3;

	if (!strcasecmp(ban, client->user->server))
		return 1;

	return 0;
}
