/*
 *   IRC - Internet Relay Chat, modules/m_part.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 "channel.h"
#include "msg.h"
#include "h.h"
#include "s_conf.h"

static char *PartFmtS = "";
static char *PartFmtS2 = ":%s";

static char *token = TOK1_PART;

static struct Message _msgtab[] = {
    {MSG_PART, 0, MAXPARA, M_SLOW | M_FLOOD_END, 0L,
     m_unregistered, m_part, m_part, m_part, m_ignore}
};

#ifndef STATIC_MODULES

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

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_part_init(void)
{
    mod_add_cmd(_msgtab);
    tok1_msgtab[(u_char) *token].msg = _msgtab;
}
#endif

/*
 * * m_part * parv[0] = sender prefix *       parv[1] = channel *
 * parv[2] = Optional part reason
 */
int m_part(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    aChannel *chptr;
    char *p, *name;
    char *reason = (parc > 2 && parv[2]) ? parv[2] : NULL;
    int ret = 0;

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

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

    if (ServerOpts.anti_spambot) {
	if (name && MyConnect(sptr) && !IsAnOper(sptr)) {
	    if (sptr->count_join_part >= GeneralOpts.spam_num) {
		sendto_lev(SPAM_LEV, "User %^C is a possible spambot", sptr);
		sptr->oper_warn_count_down = ServerOpts.oper_spam_countdown;
	    } else {
    int t_delta;

		if ((t_delta =
		     (NOW - sptr->last_part_time)) > ServerOpts.join_leave_count_expire_time) {
    int decrement_count;

		    decrement_count = (t_delta / ServerOpts.join_leave_count_expire_time);
		    if (decrement_count > sptr->count_join_part)
			sptr->count_join_part = 0;
		    else
			sptr->count_join_part -= decrement_count;
		} else {
		    if ((NOW - (sptr->last_join_time)) < GeneralOpts.spam_time)
			sptr->count_join_part++;
		}
		sptr->last_part_time = NOW;
	    }
	}
    }

    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 (!IsMember(sptr, chptr)) {
	    send_me_numeric(sptr, ERR_NOTONCHANNEL, name);
	    name = strtoken(&p, (char *) NULL, ",");
	    continue;
	}
	if (parc < 3 || (ret = can_send(sptr, chptr, reason)) || IsChanHidePartQuit(chptr)) {

			/* can_send != 0 means, user cannot send */		

	    sendto_match_servs(chptr, sptr, TOK1_PART, PartFmtS);
	    sendto_channel_butserv(chptr, sptr, TOK1_PART, CHFL_RESANON, PartFmtS);
	    sendto_service(SERVICE_SEE_PARTS | SERVICE_SEE_JOINS, 0, sptr, chptr, TOK1_PART, "");
	} else {
	    sendto_match_servs(chptr, sptr, TOK1_PART, PartFmtS2, reason);
	    sendto_channel_butserv(chptr, sptr, TOK1_PART, CHFL_RESANON, PartFmtS2, reason);
	    sendto_service(SERVICE_SEE_PARTS | SERVICE_SEE_JOINS, 0, sptr,
			   chptr, TOK1_PART, ":%s", reason);
	}
	remove_user_from_channel(sptr, chptr);
	name = strtoken(&p, (char *) NULL, ",");
    }
    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1