/*
 *   IRC - Internet Relay Chat, modules/m_kick.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.
 */

/*
 * $Id: m_kick.c,v 1.3 2003/06/14 13:55:51 tr-ircd Exp $ 
 */

#include "struct.h"
#include "common.h"
#include "sys.h"
#include "numeric.h"
#include "channel.h"
#include "msg.h"
#include "chanmode.h"
#include "h.h"

static char *token = TOK1_KICK;

static struct Message _msgtab[] = {
    {MSG_KICK, 0, MAXPARA, M_SLOW, 0L,
     m_unregistered, m_kick, m_kick, m_kick, m_ignore}
};

#ifndef STATIC_MODULES

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

void _modinit(void)
{
    mod_add_cmd(_msgtab);
    tok1_msgtab[(u_char) *token].msg = _msgtab;
}

void _moddeinit(void)
{
    mod_del_cmd(_msgtab);
    tok1_msgtab[(u_char) *token].msg = NULL;
}
#else
void m_kick_init(void)
{
    mod_add_cmd(_msgtab);
    tok1_msgtab[(u_char) *token].msg = _msgtab;
}
#endif

/*
 * * m_kick * parv[0] = sender prefix *       parv[1] = channel *
 * parv[2] = client to kick *   parv[3] = kick comment
 */
int m_kick(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    aClient *who;
    aChannel *chptr;
    int chasing = 0;
    int user_count;		/* count nicks being kicked, only allow 4 */
    char *comment, *name, *p = NULL, *user, *p2 = NULL;
    int power = 0;
    int uflags = 0;

    if (parc < 3 || *parv[1] == '\0') {
	send_me_numeric(sptr, ERR_NEEDMOREPARAMS, MSG_KICK);
	return 0;
    }

    /* The following looks like HACK notices of ircu. It is not needed.
     *
     * if (IsServer(sptr) && !IsULine(sptr))
     *  sendto_ops("KICK from %s for %s %s", parv[0], parv[1], parv[2]);
     * -TimeMr14C
     */

    comment = (BadPtr(parv[3])) ? parv[0] : parv[3];
    if (strlen(comment) > (size_t) TOPICLEN)
	comment[TOPICLEN] = '\0';

    name = strtoken(&p, parv[1], ",");

    while (name) {
	chptr = find_channel(name);
	if (!chptr) {
	    send_me_numeric(sptr, ERR_NOSUCHCHANNEL, name);
	    name = strtoken(&p, (char *) NULL, ",");
	    continue;
	}

    	/* We do not need link traversal here, because users
     	 * do not exist in the root channel, and it is already
     	 * set to +tnL, after cleaning. -TimeMr14C
     	 */

	if (!IsServer(sptr) && !IsChanUser(sptr, chptr, CHFL_CHANOP)
	    && !IsChanUser(sptr, chptr, CHFL_HALFOP) && !IsULine(sptr)) {
	    /* We cannot use IsPrivileged in the above line,
	     * in order to prevent Operators using kick without op
	     * -TimeMr14C */

	    if (MyConnect(sptr)) {
		send_me_numeric(sptr, ERR_CHANOPRIVSNEEDED, chptr);
		name = strtoken(&p, (char *) NULL, ",");
		continue;
	    }
	    if (chptr->tsval == 0) {
		send_me_numeric(sptr, ERR_CHANOPRIVSNEEDED, chptr);
		name = strtoken(&p, (char *) NULL, ",");
		continue;
	    }
	}

	user = strtoken(&p2, parv[2], ",");
	user_count = 4;
	while (user && user_count) {
	    user_count--;
	    if (!(who = find_chasing(sptr, user, &chasing))) {
		user = strtoken(&p2, (char *) NULL, ",");
		continue;
	    }
	    uflags = get_flags(who, chptr);
	    if ((uflags & (CHFL_OWNER|CHFL_PROTECT)) && !IsPrivileged(sptr))
		power = 0;
	    else if ((uflags & (CHFL_OWNER|CHFL_PROTECT|CHFL_CHANOP)) && IsChanUser(sptr, chptr, CHFL_HALFOP))
		power = 0;
	    else
		power = 1;
	    if (IsMember(who, chptr) && !IsAnon(who) && power) {
                sendto_channel_butserv(chptr, sptr, TOK1_KICK, CHFL_GETSETTER,
                                           "%s :%s", who->name, comment);
		sendto_match_servs(chptr, sptr, TOK1_KICK, "%s :%s", who->name, comment);
		sendto_service(SERVICE_SEE_KICKS | SERVICE_SEE_PARTS, 0,
			       sptr, chptr, TOK1_KICK, "%s :%s", who->name, comment);
		remove_user_from_channel(who, chptr);
	    } else if (!power) {
		send_me_notice(sptr, ":Permission denied");
	    } else {
		send_me_numeric(sptr, ERR_USERNOTINCHANNEL, user, chptr);
	    }
	    user = strtoken(&p2, (char *) NULL, ",");
	}
	name = strtoken(&p, (char *) NULL, ",");
    }
    return (0);
}


syntax highlighted by Code2HTML, v. 0.9.1