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

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

static char nickbuf[BUFSIZE], buf[BUFSIZE];
static char comment[TOPICLEN];
static int do_operdo_kick(struct hook_data *data);

#ifndef STATIC_MODULES

void _modinit(void)
{
    hook_add_hook("load operdo core module", (hookfn *) do_operdo_kick);
}

void _moddeinit(void)
{
    hook_del_hook("load operdo core module", (hookfn *) do_operdo_kick);
}

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

#else

void m_operdo_kick_init(void)
{
    hook_add_hook("load operdo core module", (hookfn *) do_operdo_kick);
}

#endif

static int do_operdo_kick(struct hook_data *data)
{
    char **parv;
    int parc;
    aClient *who;
    aChannel *chptr = NULL;
    int chasing = 0;
    int user_count;
    char *name, *p = NULL, *user, *p2 = NULL;

    parv = data->parv;
    parc = data->parc;
    chptr = data->channel;

    if (irc_strcmp(data->extra, "KICK"))
	return 0;

    if (parc < 4 || *parv[2] == '\0') {
	send_me_numeric(data->source_p, ERR_NEEDMOREPARAMS, MSG_OPERDO);
	return 1;
    }

    memset((char *) &comment, 0, TOPICLEN);

    if (BadPtr(parv[4]))
	strncat((char *) &comment, (const char *) parv[0], strlen(parv[0]));
    else {
    int i = 5;
	strncat((char *) &comment, (const char *) parv[4], strlen(parv[4]));
	while (parv[i]) {
	    strncat((char *) &comment, " ", 1);
	    strncat((char *) &comment, (const char *) parv[i], strlen(parv[i]));
	    i++;
	}
    }

    if (strlen(comment) > (size_t) TOPICLEN)
	comment[TOPICLEN] = '\0';

    *nickbuf = *buf = '\0';
    name = strtoken(&p, parv[2], ",");
    while (name) {
	chptr = find_channel(name);
	if (!chptr) {
	    send_me_numeric(data->source_p, ERR_NOSUCHCHANNEL, name);
	    name = strtoken(&p, (char *) NULL, ",");
	    continue;
	}
	user = strtoken(&p2, parv[3], ",");
	user_count = 4;
	while (user && user_count) {
	    user_count--;
	    if (!(who = find_chasing(data->source_p, user, &chasing))) {
		user = strtoken(&p2, (char *) NULL, ",");
		continue;
	    }

	    if (IsMember(who, chptr)) {
		sendto_channel_butserv(chptr, data->source_p, TOK1_KICK, 0, "%s :%s", who->name,
				       comment);
		sendto_match_servs(chptr, data->client_p, TOK1_KICK, "%s : %s", who->name, comment);
		sendto_serv_butone(NULL, &me, TOK1_GLOBOPS,
				   ":%C used OPERDO KICK on %H for %C", data->source_p, chptr, who);
		sendto_ops("from %C: %C used OPERDO KICK on %C for %C", &me, data->source_p, chptr,
			   who);
		remove_user_from_channel(who, chptr);
	    } else
		send_me_numeric(data->source_p, ERR_USERNOTINCHANNEL, user, name);
	    user = strtoken(&p2, (char *) NULL, ",");
	}
	name = strtoken(&p, (char *) NULL, ",");
    }

    return 1;
}


syntax highlighted by Code2HTML, v. 0.9.1