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

static char modebuf[REALMODEBUFLEN], parabuf[REALMODEBUFLEN];

static struct Message _msgtab[] = {
    {MSG_SAMODE, 0, MAXPARA, M_SLOW, 0L,
     m_unregistered, m_permission, m_samode, m_ignore, m_ignore}
};

#ifndef STATIC_MODULES

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

void _modinit(void)
{
    mod_add_cmd(_msgtab);
}

void _moddeinit(void)
{
    mod_del_cmd(_msgtab);
}
#else
void m_samode_init(void)
{
    mod_add_cmd(_msgtab);
}
#endif

/*
 * m_samode - Just bout the same as df
 * *  - Raistlin
 * * parv[0] = sender
 * * parv[1] = channel
 * * parv[2] = modes   
 */
int m_samode(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    int sendts;
    int link_mode = 0;
    aChannel *chptr;

    if (!IsSAdmin(cptr) || parc < 2)
	return 0;
    chptr = find_channel(parv[1]);
    if (!chptr)
	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 (IsChanAnon(chptr)) {
	send_me_notice(sptr, ":SAMODE Cannot be used on anonymous channels.");
	return 0;
    }

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

    SetOperMode(sptr);
  
    if (IsChanLinked(chptr))
	link_mode = 1;

    sendts = set_mode(cptr, sptr, chptr, parc - 2, parv + 2, modebuf, parabuf);

    if (strlen(modebuf) > (size_t) 1) {

	/* in SAMODE the MODE command arrives from a non-chanop, therefore
	 * there is no use in differentiating between ops and nonops
	 * in a hideops case 
	 * -TimeMr14C
	 */
	logevent_call(LogSys.operevent, MSG_SAMODE, sptr, &parv, parc);
	sendto_channel_butserv(chptr, sptr, TOK1_MODE, 0, "%s %s",
			       modebuf, parabuf);
	sendto_match_servs(chptr, cptr, TOK1_MODE, "%s %s", modebuf, parabuf);
	if (MyClient(sptr)) {
	    sendto_serv_butone(NULL, &me, TOK1_GLOBOPS,
			       ":%C used SAMODE (%H %s%s%s)",
			       sptr, chptr, modebuf, (*parabuf != 0 ? " " : ""), parabuf);
	    sendto_ops("from %C: %s used SAMODE (%s %s%s%s)", &me,
			   sptr->name, chptr->chname, modebuf, (*parabuf != 0 ? " " : ""), parabuf);
	}
    }
    if (link_mode) {
        if (!IsChanLinked(chptr)) {
            remove_user_from_channel(&me, chptr);
        }
    } else {
        if (IsChanLinked(chptr)) {
            link_add_server_to_channel(&me, chptr);
            link_remove_users_from_channel(chptr);
            link_set_modes_in_channel(chptr);
        }
    }
	
    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1