/*
 *   IRC - Internet Relay Chat, modules/m_invite.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_invite.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 struct Message _msgtab[] = {
    {MSG_INVITE, 0, MAXPARA, M_SLOW, 0L,
     m_unregistered, m_invite, m_invite, m_invite, m_ignore}
};

static char *token = TOK1_INVITE;

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

/*
 * * m_invite *       parv[0] - sender prefix *       parv[1] - user to
 * invite *     parv[2] - channel number
 */
int m_invite(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    aClient *acptr;
    aChannel *chptr;

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

    if (!(acptr = find_person(parv[1]))) {
	send_me_numeric(sptr, ERR_NOSUCHNICK, parv[1]);
	return 0;
    }

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

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

    /* 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 (chptr && !IsMember(sptr, chptr) && !IsULine(sptr)) {
	send_me_numeric(sptr, ERR_NOTONCHANNEL, parv[2]);
	return -1;
    }

    if (IsMember(acptr, chptr)) {
	send_me_numeric(sptr, ERR_USERONCHANNEL, parv[1], parv[2]);
	return 0;
    }
    if (chptr && !IsULine(sptr)) {
	if (!IsChanUser(sptr, chptr, CHFL_CHANOP)) {
	    send_me_numeric(sptr, ERR_CHANOPRIVSNEEDED, chptr);
	    return -1;
	}
    }

    if (MyConnect(sptr)) {
	send_me_numeric(sptr, RPL_INVITING, acptr->name, ((chptr) ? (chptr->chname) : parv[2]));
	if (acptr->user->away)
	    send_me_numeric(sptr, RPL_AWAY, acptr->name, acptr->user->away);
    }
    if (MyConnect(acptr)) {
	if ((chptr && sptr->user && IsChanUser(sptr, chptr, CHFL_CHANOP)) || IsULine(sptr)) {
	    add_invite(acptr, chptr);
	    sendto_channel_butone(NULL, CHFL_CHANOP, 0, &me, chptr, TOK1_NOTICE,
				  ":%C invited %C into channel %H.", sptr, acptr, chptr);
	}
	sendto_one(acptr, ":%C %s %s :%s", sptr, MSG_INVITE, acptr->name,
		   ((chptr) ? (chptr->chname) : parv[2]));
    } else {
	sendto_one_server(acptr, sptr, TOK1_INVITE, "%~C :%s", acptr, ((chptr) ? (chptr->chname) : parv[2]));
    }

    return 0;

}


syntax highlighted by Code2HTML, v. 0.9.1