/*
 *   IRC - Internet Relay Chat, modules/m_kline.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_GLINE;
static int do_gline = 0;

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

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

#ifndef STATIC_MODULES

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

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_kline_init(void)
{
    mod_add_cmd(k_msgtab);
    mod_add_cmd(g_msgtab);
    tok1_msgtab[(u_char) *token1].msg = g_msgtab;
}
#endif
/*
 * m_kline()   
 *
 * re-worked a tad ... -Dianora
 *
 */

int m_kline(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    char buffer[1024];
    char *user, *host;
    char *reason;
    char *current_date;
    char tempuser[USERLEN + 2];
    char temphost[HOSTLEN + 1];
    aMaskItem *ami;
    aClient *acptr = NULL;
    int temporary_kline_time = 0;	/* -Dianora */
    time_t temporary_kline_time_seconds = 0;
    int time_specified = 0;
    char *argv;

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

    if (parc < 2) {
	send_me_numeric(sptr, ERR_NEEDMOREPARAMS, MSG_KLINE);
	return 0;
    }
    logevent_call(LogSys.operevent, MSG_KLINE, sptr, &parv, parc);
    argv = parv[1];
    if ((temporary_kline_time = atoi(argv)) >= 0) {
	if (parc < 3) {
	    send_me_numeric(sptr, ERR_NEEDMOREPARAMS, MSG_KLINE);
	    return 0;
	}
	if (temporary_kline_time > (24 * 60))
	    temporary_kline_time = (24 * 60);	/* Max it at 24 hours */
    temporary_kline_time_seconds = (time_t) temporary_kline_time *(time_t) 60;
	argv = parv[2];
	parc--;
	time_specified = 1;
    } else {
	if (ServerOpts.default_kline_time) {
	    temporary_kline_time = ServerOpts.default_kline_time;
    temporary_kline_time_seconds = (time_t) temporary_kline_time *(time_t) 60;
	}
        argv = parv[2];
        parc--;
        time_specified = 1;
    }

    if ((host = strchr(argv, '@')) || *argv == '*') {
	if (host) {		/* Found user@host */
	    user = argv;	/* here is user part */
	    *(host++) = '\0';	/* and now here is host */
	} else {
	    user = "*";		/* no @ found, assume its *@somehost */
	    host = argv;
	}

	if (!*host)
	    host = "*";
	strlcpy_irc(tempuser, user, USERLEN + 1);	/* allow for '*' in front */
	strlcpy_irc(temphost, host, HOSTLEN);
	user = tempuser;
	host = temphost;
    } else {
	if (!(acptr = find_chasing(sptr, argv, NULL)))
	    return 0;
	if (!IsPerson(acptr))
	    return 0;
	user = "*";
	host = acptr->user->host;
    }

    if (time_specified)
	argv = parv[3];
    else
	argv = parv[2];

    if (parc > 2) {
	if (*argv)
	    reason = argv;
	else
	    reason = "No reason";
    } else
	reason = "No reason";

    if (!match(user, "akjhfkahfasfjd") && !match(host, "ldksjfl.kss...kdjfd.jfklsjf")) {
	send_me_notice(sptr, ":Can't K-Line *@*!");
	return 0;
    }

    if ((ami = find_maskitem(host, user, MASKITEM_KLINE, 1))) {
	send_me_notice(sptr,
		       ":[%s@%s] already K-lined by [%s@%s] - %s",
		       user, host, ami->username, ami->string, ami->reason);
	return 0;
    }

    current_date = smalldate((time_t) 0);

    ami = create_maskitem(sptr->name, host, user, MASKITEM_KLINE, temporary_kline_time_seconds);

    ircsprintf(buffer, "%s (%s)", reason, current_date);
    DupString(ami->reason, buffer);

    rehashed = 1;
    send_me_notice(sptr, ":K-Line for [%s@%s] is now added", user, host);
    sendto_lev(KLINE_LEV, "%s added temporary %d min. K-Line for [%s@%s] [%s]",
	       parv[0], temporary_kline_time, user, host, reason);
    if (do_gline) {
	sendto_serv_butone(cptr, sptr, TOK1_GLINE, "%d %s@%s :%s",
			   temporary_kline_time, user, host, reason);
	do_gline = 0;
	return FLUSH_BUFFER;
    }
    return 0;
}

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

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

    if (!OPIsGKLine(acptr) && MyConnect(acptr))
	return m_permission(acptr, acptr, parc, parv);

    do_gline = 1;
    return m_kline(acptr, acptr, parc, parv);
}


syntax highlighted by Code2HTML, v. 0.9.1