/************************************************************************
 *   IRC - Internet Relay Chat, modules/operdo/m_operdo_join.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 "chanmode.h"
#include "sys.h"
#include "numeric.h"
#include "msg.h"
#include "h.h"
#include "hook.h"

static char modebuf[REALMODEBUFLEN], parabuf[REALMODEBUFLEN];
static int do_operdo_join(struct hook_data *data);

#ifndef STATIC_MODULES

void _modinit(void)
{
    hook_add_hook("load operdo core module", (hookfn *) do_operdo_join);
}

void _moddeinit(void)
{
    hook_del_hook("load operdo core module", (hookfn *) do_operdo_join);
}

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

#else

void m_operdo_join_init(void)
{
    hook_add_hook("load operdo core module", (hookfn *) do_operdo_join);
}

#endif

static int do_operdo_join(struct hook_data *data)
{
    aChannel *chptr = NULL;
    aClient *sptr = NULL;
    char **parv;
    int parc;
    int link_mode = 0;
    int sendtsm = 0;

    chptr = data->channel;
    sptr = data->source_p;
    parv = data->parv;
    parc = data->parc;
    if (irc_strcmp(data->extra, "JOIN"))
	return 0;

    if (parc < 4) {
	send_me_numeric(sptr, ERR_NEEDMOREPARAMS, MSG_OPERDO);
	return 1;
    }

    privileged_join(sptr, data->client_p, parv[2]);
    chptr = find_channel(parv[2]);
    if (chptr == NULL)
	return 1;
    if (IsChanLinked(chptr))
	link_mode = 1;
    sendtsm = set_mode(&me, &me, chptr, parc - 3, parv + 3, modebuf, parabuf);
    if (strlen(modebuf) > (size_t) 1) {
	sendto_channel_butserv(chptr, sptr, TOK1_MODE, 0, "%s %s", modebuf, parabuf);
	sendto_match_servs(chptr, &me, TOK1_MODE, "%s %s", modebuf, parabuf);
	sendto_serv_butone(NULL, &me, TOK1_GLOBOPS, ":%C used OPERDO JOIN on %H %s%s%s",
			   sptr, chptr, modebuf, (*parabuf != 0 ? " " : ""), parabuf);
	sendto_ops("from %C: %C used OPERDO JOIN on %H with %s%s%s",
		   &me, sptr, chptr, 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 1;
}


syntax highlighted by Code2HTML, v. 0.9.1