/*
 *   IRC - Internet Relay Chat, modules/m_die.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 "h.h"
#include "s_conf.h"
#include "event.h"

extern void sigterm_handler(int sig);

static struct Message _msgtab[] = {
    {MSG_DIE, 0, MAXPARA, M_SLOW, 0L,
     m_unregistered, m_permission, m_die, 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_die_init(void)
{
    mod_add_cmd(_msgtab);
}
#endif

static void die_delayed(void *unused)
{
    sigterm_handler(0);
}

int m_die(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    aClient *acptr;
    dlink_node *ptr;
    char *pass = ServerInfo.diepass;
    char passarr[PASSWDLEN];
    int delay = 0;

    if (!OPCanDie(sptr)) {
	send_me_numeric(sptr, ERR_NOPRIVILEGES);
	return 0;
    }

    if (pass) {
	if (parc < 2) {
	    send_me_numeric(sptr, ERR_NEEDMOREPARAMS, MSG_DIE);
	    return 0;
	}
	if (strcmp(pass, calcpass(parv[1], passarr))) {
	    send_me_numeric(sptr, ERR_PASSWDMISMATCH);
	    return 0;
	}
    }

    if (parc > 2)
	delay = atoi(parv[2]);
    if (delay <= 0)
	delay = 0;

    if (delay) {
	sendto_all(":%C %s RDIE :Server Terminate by [%~C] in %d seconds", 
		   &me, MSG_NOTICE, sptr, delay);

	for (ptr = serv_list.head; ptr; ptr = ptr->next) {
	    if (!(acptr = ptr->data))
		continue;
	    sendto_one_server(acptr, &me, TOK1_ERROR, ":Terminated by %C", sptr);
	}
    }

    else {
	sendto_all(":%C %s RDIE :Server Terminate by [%~C]", &me, MSG_NOTICE, sptr);

	for (ptr = serv_list.head; ptr; ptr = ptr->next) {
	    if (!(acptr = ptr->data))
		continue;
	    sendto_one_server(acptr, &me, TOK1_ERROR, ":Terminated by %C", sptr);
	}
    }
    logevent_call(LogSys.operevent, MSG_DIE, sptr, &parv, parc);
    eventAdd("DIE", die_delayed, NULL, delay);
    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1