/************************************************************************
 *   IRC - Internet Relay Chat, modules/m_squit.c
 *
 *   Copyright (C) 2000-2002 TR-IRCD Development
 *
 *   Copyright (C) 1990 Jarkko Oikarinen and
 *                      University of Oulu, Co Center
 *
 *   See file AUTHORS in IRC package for additional names of
 *   the programmers.
 *
 *   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_squit.c,v 1.4 2004/02/24 15:00:27 tr-ircd Exp $ 
     */

#include "struct.h"
#include "common.h"
#include "sys.h"
#include "numeric.h"
#include "msg.h"
#include "channel.h"
#include "s_conf.h"
#include "h.h"

static char *token = TOK1_SQUIT;

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

#ifndef STATIC_MODULES

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

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

/*
 * m_squit
 * there are two types of squits: those going downstream (to the target server)
 * and those going back upstream (from the target server).
 * previously, it wasn't necessary to distinguish between these two types of 
 * squits because they neatly echoed back all of the QUIT messages during an squit.
 * This, however, is no longer practical.
 * 
 * To clarify here, DOWNSTREAM signifies an SQUIT heading towards the target server
 * UPSTREAM signifies an SQUIT which has successfully completed, heading out everywhere.
 *
 * acptr is the server being squitted.
 * a DOWNSTREAM squit is where the notice did not come from acptr->from.
 * an UPSTREAM squit is where the notice DID come from acptr->from.
 *
 *        parv[0] = sender prefix 
 *        parv[1] = server name 
 *        parv[2] = comment
 */

int m_squit(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    char *server;
    aClient *acptr = NULL;
    dlink_node *ptr, *next_ptr;
    char *comment = (parc > 2) ? parv[2] : sptr->name;

    if (parc > 1) {
	server = parv[1];
	for (ptr = global_serv_list.head; ptr; ptr = next_ptr) {
	    next_ptr = ptr->next;
	    acptr = ptr->data;
	    if (!acptr) {
		dlinkDeleteNode(ptr, &global_serv_list);
		continue;
	    }
	    if (IsMe(acptr))
		continue;
	    if (match(server, acptr->name) == 0) 
		break;
	}	 	
    } else {
 	send_me_numeric(sptr, ERR_NEEDMOREPARAMS, MSG_JOIN);
        return 0;
    }

    if (!acptr || match(server, acptr->name)) {
	send_me_numeric(sptr, ERR_NOSUCHSERVER, server);
	return 0;
    }

    if (IsMe(acptr)) {
	sendto_gnotice("from %C: Received SQUIT from %s (%s)",
                       &me, get_client_name(sptr, HIDEME), comment);
        sendto_serv_butone(NULL, &me, TOK1_GNOTICE,
                           ":Received SQUIT from %s (%s)",
                           get_client_name(sptr, HIDEME), comment);

        sendto_service(SERVICE_SEE_SQUITS, 0, sptr, NULL, TOK1_SQUIT, "%s :%s", server, comment);
        return exit_client(sptr, sptr, comment);	/* Squit origin */
    }

    /*
     * If the server is mine, we don't care about upstream or downstream,
     * just kill it and do the notice. 
     */

    logevent_call(LogSys.operevent, MSG_SQUIT, sptr, &parv, parc);
    if (MyConnect(acptr)) {
	sendto_gnotice("from %C: Received SQUIT %s from %s (%s)",
		       &me, acptr->name, get_client_name(sptr, HIDEME), comment);
	sendto_serv_butone(NULL, &me, TOK1_GNOTICE,
			   ":Received SQUIT %s from %s (%s)", server,
			   get_client_name(sptr, HIDEME), comment);

	sendto_service(SERVICE_SEE_SQUITS, 0, sptr, NULL, TOK1_SQUIT, "%s :%s", server, comment);
	return exit_client(acptr, sptr, comment);
    }

    /*
     * the server is not connected to me. Determine whether this is an upstream
     * or downstream squit 
     */

    if (sptr->from == acptr->from) {	/* upstream */
	sendto_lev(DEBUG_LEV,
		   "Exiting server %s due to upstream squit by %s [%s]",
		   acptr->name, sptr->name, comment);
	sendto_service(SERVICE_SEE_SQUITS, 0, sptr, NULL, TOK1_SQUIT, "%s :%s",
		       acptr->name, comment);
	return exit_client(acptr, sptr, comment);
    }

    /*
     * fallthrough: downstream 
     */

    if (!(IsULine(acptr->from))) {	/* downstream not unconnect capable :P services!. */
	sendto_lev(DEBUG_LEV,
		   "Exiting server %s due to non-unconnect server %s [%s]",
		   acptr->name, acptr->from->name, comment);
	sendto_service(SERVICE_SEE_SQUITS, 0, sptr, NULL, TOK1_SQUIT, "%s :%s",
		       acptr->name, comment);
	return exit_client(acptr, sptr, comment);
    }

    sendto_lev(DEBUG_LEV, "Passing along SQUIT for %s by %s [%s]",
	       acptr->name, sptr->name, comment);
    sendto_service(SERVICE_SEE_SQUITS, 0, sptr, NULL, TOK1_SQUIT, "%~C :%s", acptr, comment);

    sendto_one_server(acptr->from, sptr, TOK1_SQUIT, "%~C :%s", acptr, comment);

    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1