/*
* CIDR ban.  Copyright Zach Stein [ChickServ]
*
* 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 2
* of the License, 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*
* See README for instructions
*
* m_cidrban.c, ChickServ
*/
#include <string.h>
#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(m_regexcept)
= {
        "m_cidrban",
        "v0.3",
        "ExtBan ~C",
        "3.2.4",
        NULL
};

Extban *extbanC;
/* this one can remain the same */
char *extban_modeC_conv_param(char *para)
{
        char *p =strtok(para,"~C:");
        if (strstr(para,"~C:~")) {
                return NULL;
        } /* this could be bad in parase_netmask */
        for (; *p; p++) {
                if (isalpha(*p)) {
                        return NULL;
                }
                if (*p == '~')
                return NULL;
        } /* prevent a segfault */
        static char retbuf[64];
        strlcpy(retbuf, para, sizeof(retbuf));
        return retbuf;
};

int extban_modeC_is_banned(aClient *sptr, aChannel *chptr, char *xban, int type)
{
        struct irc_netmask tmp;
        struct irc_netmask *nmask;
        if (xban != NULL) {
                char *tempchar = strtok(xban,"~C:");
                tmp.type = parse_netmask(tempchar, &tmp); /* cast to netmask */
                if (tmp.type != HM_HOST)
                {
                        nmask = MyMallocEx(sizeof(struct irc_netmask)); /* copy the struct */
                        bcopy(&tmp, nmask, sizeof(struct irc_netmask));
                        if (match_ip(sptr->ip,NULL, NULL, nmask)) {
                                free(nmask); /* trash collection */
                                return 1;
                        }
                }
        }
        if (&nmask != NULL)
        free(nmask); /* don't dereference a null pointer but clean up if necessary */
        return 0;
} /* test for whether or not the user is banned */

DLLFUNC int MOD_INIT(m_cidrban)(ModuleInfo *modinfo)
{
        ExtbanInfo req;

        memset(&req, 0, sizeof(ExtbanInfo));
        req.flag = 'C';
        req.conv_param = extban_modeC_conv_param;
        req.is_banned = extban_modeC_is_banned;
        extbanC = ExtbanAdd(modinfo->handle, req);
        if (!extbanC)
        {
                config_error("m_regexcept module: adding extban ~C failed! module NOT loaded");
                return MOD_FAILED;
        }


        return MOD_SUCCESS;
} /* add vales to unreal's hash table */

DLLFUNC int MOD_LOAD(m_regexcept)(int module_load)
{
        return MOD_SUCCESS;
} /* load up the module */

DLLFUNC int MOD_UNLOAD(m_regexcept)(int module_unload)
{
        ExtbanDel(extbanC);
        return MOD_SUCCESS;
} /* if we need to unload garbage collect */

