/*
 *   IRC - Internet Relay Chat, modules/m_knock.c
 *
 *   Copyright (C) 2000-2003 TR-IRCD Development
 *
 *
 *   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_knock.c,v 1.1 2004/02/24 15:02:21 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 "s_conf.h"
#include "h.h"

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

static char *token = TOK1_KNOCK;

#ifndef STATIC_MODULES

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

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

/*
 * parv[0] sender
 * parv[1] channel
 */
int m_knock(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    aChannel *chptr;

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

    if (!check_channelname(sptr, (unsigned char *) parv[1]))
	return 0;

    if (!(chptr = find_channel(parv[1]))) {
	send_me_numeric(sptr, ERR_NOSUCHCHANNEL , parv[1]);
	return 0;
    }

    if (IsMember(sptr, chptr)) {
	send_me_numeric(sptr, ERR_USERONCHANNEL, parv[0], parv[1]);
	return 0;
    }

    if (IsChanNoKnock(chptr)) {
	send_me_numeric(sptr, ERR_CHANOPRIVSNEEDED, chptr);
	return 0;
    }

    if (is_nuhed(sptr, &chptr->banlist) && !is_nuhed(sptr, &chptr->banexlist)) {
        send_me_numeric(sptr, ERR_BANNEDINCHAN, parv[0]);
	return 0;
    }

    if (is_nuhed(sptr, &chptr->stoplist)) {
	send_me_numeric(sptr, ERR_CANNOTSENDTOCHAN, parv[0]);
	return 0;
    }

    if (sptr->last_knock + ServerOpts.max_knock_time > time(NULL))
	return 0;

    if (MyConnect(sptr)) {
	sptr->last_knock = time(NULL);
	sendto_channel_butone(NULL, CHFL_CHANOP, 0, &me, chptr, TOK1_NOTICE,
				  ":%C knocks the channel %H to get invited", sptr, chptr);
	sendto_serv_butone(sptr, sptr, TOK1_KNOCK, ":%H", chptr);
    }

    return 0;

}


syntax highlighted by Code2HTML, v. 0.9.1