/************************************************************************                 
 *   IRC - Internet Relay Chat, chanmodes/cm_link.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 "h.h"
#include "msg.h"
#include "hook.h"
#include "chanmode.h"
#include "s_conf.h"

static int set_link(int adl, aChannel *chptr, int nmodes,
		    int *argnum, int *pidx, int *mbix, char *mbuf, char *pbuf,
		    aClient *cptr, aClient *sptr, int parc, char **parv)
{

    char *pptr;			/* temporary paramater pointer */
    char *morig = mbuf;		/* beginning of mbuf */

    int prelen = strlen(cptr->name) + strlen(chptr->chname) + 16;

    int fargnum = *argnum;
    int fmbix = *mbix;
    int fpidx = *pidx;

    if (adl == CMODE_DEL) {
	if ((prelen + (mbuf - morig) + fpidx + 1) > REALMODEBUFLEN)
	    return nmodes;
	mbuf[fmbix++] = 'L';
	chptr->mode.mode &= ~MODE_LINKED;
	chptr->mode.link[0] = '\0';
	nmodes++;
	*mbix = fmbix;
	return nmodes;
    }

    if (adl == CMODE_ADD) {

	if (parv[fargnum] == NULL) {
	    send_me_numeric(sptr, ERR_NEEDMOREPARAMS, MSG_MODE);
	    return nmodes;
	}

	if (*parv[fargnum] == ':' || *parv[fargnum] == '\0' ||
	    ((*parv[fargnum] != '#') && (*parv[fargnum] != '&'))) {
	    send_me_numeric(sptr, ERR_NEEDMOREPARAMS, MSG_MODE);
	    fargnum++;
	    *argnum = fargnum;
	    return nmodes;
	}

	/*
	 * if we're going to overflow our mode buffer,
	 * * drop the change instead
	 */

	if ((prelen + (mbuf - morig) + fpidx + CHANNELLEN + 2) > REALMODEBUFLEN) {
	    fargnum++;
	    *argnum = fargnum;
	    return nmodes;
	}

	parv[fargnum][CHANNELLEN] = '\0';
	chptr->mode.mode |= MODE_LINKED;
	strlcpy_irc(chptr->mode.link, parv[fargnum], CHANNELLEN);
	pptr = chptr->mode.link;
	if (fpidx)
	    pbuf[fpidx++] = ' ';
	while (*pptr)
	    pbuf[fpidx++] = *pptr++;

	mbuf[fmbix++] = 'L';
	fargnum++;
	nmodes++;
	*mbix = fmbix;
	*pidx = fpidx;
	*argnum = fargnum;
    }
    return nmodes;
}

static int sL_service(int adl, aChannel *chptr, int nmodes,
		      int *argnum, int *pidx, int *mbix, char *mbuf, char *pbuf,
		      aClient *cptr, aClient *sptr, int parc, char **parv)
{
    if (MyClient(sptr))
	send_me_numeric(sptr, ERR_CHANOPRIVSNEEDED, chptr);
    else
	ircstp->is_fake++;
    return nmodes;
}

static int sL_server(int adl, aChannel *chptr, int nmodes,
		     int *argnum, int *pidx, int *mbix, char *mbuf, char *pbuf,
		     aClient *cptr, aClient *sptr, int parc, char **parv)
{
    return set_link(adl, chptr, nmodes, argnum, pidx, mbix, mbuf, pbuf, cptr, sptr, parc, parv);
}

static int sL_uline(int adl, aChannel *chptr, int nmodes,
		    int *argnum, int *pidx, int *mbix, char *mbuf, char *pbuf,
		    aClient *cptr, aClient *sptr, int parc, char **parv)
{
    return set_link(adl, chptr, nmodes, argnum, pidx, mbix, mbuf, pbuf, cptr, sptr, parc, parv);
}

static int sL_oper(int adl, aChannel *chptr, int nmodes,
		   int *argnum, int *pidx, int *mbix, char *mbuf, char *pbuf,
		   aClient *cptr, aClient *sptr, int parc, char **parv)
{
    if (IsChanUser(sptr, chptr, CHFL_CHANOP) || IsOperMode(sptr) || IsServer(cptr)) {
	return set_link(adl, chptr, nmodes, argnum, pidx, mbix, mbuf, pbuf, cptr, sptr, parc, parv);
    } else
	send_me_numeric(sptr, ERR_NOPRIVILEGES, chptr);
    return nmodes;
}

static int sL_user(int adl, aChannel *chptr, int nmodes,
		   int *argnum, int *pidx, int *mbix, char *mbuf, char *pbuf,
		   aClient *cptr, aClient *sptr, int parc, char **parv)
{
    if (MyClient(sptr))
	send_me_numeric(sptr, ERR_NOPRIVILEGES, chptr);
    else
	ircstp->is_fake++;
    return nmodes;
}

static struct ChanMode mode_link[] = {
    {MODE_LINKED, 1, MFLG_PARAMCHN, CMTYPE_PARAMETRIC | CMTYPE_REMWOPARA,
     sL_user, sL_oper, sL_uline, sL_server, sL_service}
};

#ifndef STATIC_MODULES
int _persistent = 1;
void _modinit(void)
#else
void link_modinit(void)
#endif
{
    modetab[(int) 'L'] = mode_link[0];
    GeneralOpts.lists_created = 0;
}


syntax highlighted by Code2HTML, v. 0.9.1