/*
 *   IRC - Internet Relay Chat, modules/m_unkline.c
 *
 *   Copyright (C) 2000-2003 TR-IRCD Development
 *
 *   Copyright (C) 1990 Jarkko Oikarinen and
 *                      University of Oulu, Co Center
 *
 *   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, 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 "struct.h"
#include "common.h"
#include "sys.h"
#include "numeric.h"
#include "msg.h"
#include "channel.h"
#include "h.h"
#include "s_conf.h"

static char *token1 = TOK1_UNGLINE;
static int do_ungline = 0;

static struct Message k_msgtab[] = {
    {MSG_UNKLINE, 0, MAXPARA, M_SLOW, 0L,
     m_unregistered, m_permission, m_unkline, m_ignore, m_unkline}
};

static struct Message g_msgtab[] = {
    {MSG_UNGLINE, 0, MAXPARA, M_SLOW, 0L,
     m_unregistered, m_permission, m_ungline, m_ungline, m_ignore}
};

#ifndef STATIC_MODULES

char *_version = "$Revision: 1.4 $";

void _modinit(void)
{
    mod_add_cmd(k_msgtab);
    mod_add_cmd(g_msgtab);
    tok1_msgtab[(u_char) *token1].msg = g_msgtab;
}

void _moddeinit(void)
{
    mod_del_cmd(k_msgtab);
    mod_del_cmd(g_msgtab);
    tok1_msgtab[(u_char) *token1].msg = NULL;
}
#else
void m_unkline_init(void)
{
    mod_add_cmd(k_msgtab);
    mod_add_cmd(g_msgtab);
    tok1_msgtab[(u_char) *token1].msg = g_msgtab;
}
#endif

/*
 * * m_unkline
 * Added Aug 31, 1997
 * common (Keith Fralick) fralick@gate.net
 * 
 *      parv[0] = sender
 *      parv[1] = address to remove
 *      
 * re-worked and cleanedup for use in hybrid-5 -Dianora
 * re-worked and cleanedup for use in trircd-5 -TimeMr14C
 */
int m_unkline(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    char *user, *host;

    if (!OPIsGKLine(sptr)) {
        return m_permission(cptr, sptr, parc, parv);
    }

    if (parc < 2) {
	send_me_numeric(sptr, ERR_NEEDMOREPARAMS, MSG_UNKLINE);
	return 0;
    }

    if ((host = strchr(parv[1], '@')) || *parv[1] == '*') {
	if (host) {		/* Found user@host */
	    user = parv[1];	/* here is user part */
	    *(host++) = '\0';	/* and now here is host */
	} else {
	    user = "*";		/* no @ found, assume its *@somehost */
	    host = parv[1];
	}
    } else {
	send_me_notice(sptr, ":Invalid parameters");
	return 0;
    }

    terminate_maskitem(host, user, MASKITEM_KLINE);
    logevent_call(LogSys.operevent, MSG_UNKLINE, sptr, &parv, parc);
    send_me_notice(sptr, ":K-Line for [%s@%s] is removed", user, host);
    sendto_lev(KLINE_LEV, "%s has removed the K-Line for: [%s@%s]", parv[0], user, host);
    if (do_ungline) {
	sendto_serv_butone(cptr, sptr, TOK1_UNGLINE, ":%s@%s", user, host);
        do_ungline = 0;
    }
    return 0;
}

int m_ungline(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    aClient *acptr;

    if (!OPIsGKLine(sptr)) 
        return m_permission(cptr, sptr, parc, parv);

    if ((acptr = find_person(parv[0])) == NULL)
        return 0;

    do_ungline = 1;
    return m_unkline(acptr, acptr, parc, parv);
}


syntax highlighted by Code2HTML, v. 0.9.1