/************************************************************************
 *   IRC - Internet Relay Chat, modules/m_svsnick.c
 *
 *   Copyright (C) 2000-2003 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_svsnick.c,v 1.3 2003/06/14 13:55:51 tr-ircd Exp $ 
 */

#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"

static char *token = TOK1_SVSNICK;

static struct Message _msgtab[] = {
    {MSG_SVSNICK, 0, MAXPARA, M_SLOW, 0L,
     m_unregistered, m_ignore, m_ignore, m_svsnick, m_ignore}
};

#ifndef STATIC_MODULES

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

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

/*
 * s_svsnick - Pretty straight forward.  Mostly straight outta df
 * *  - Raistlin
 * * parv[0] = sender
 * * parv[1] = old nickname
 * * parv[2] = new nickname
 * * parv[3] = timestamp
 */
int m_svsnick(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    aClient *acptr;

    if (!IsULine(sptr) || parc < 4 || (strlen(parv[2]) > NICKLEN))
	return 0;
    /*
     * if we can't SVSNICK them to something because the nick is in use, KILL them 
     */
    acptr = find_person(parv[2]);
    if (acptr != NULL) {
	sendto_serv_butone(cptr, sptr, TOK1_KILL, "%s :%s (SVSNICK Collide)", parv[1], sptr->name);
	sendto_one(cptr, ":%C %s %s :%s (SVSNICK Collide)", sptr, MSG_KILL, parv[2], sptr->name);
	return 0;
    }
    if (hunt_server(cptr, sptr, ":%s %s %s %s :%s", TOK1_SVSNICK, 1, parc, parv) == HUNTED_ISME) {
	if ((acptr = find_person(parv[1])) != NULL) {
	    acptr->umode &= ~UMODE_r;
	    if (ServerOpts.anti_nick_flood)
		acptr->last_nick_time = atoi(parv[3]);
	    sendto_common_channels(acptr, ":%C %s :%s", acptr, MSG_NICK, parv[2]);
	    add_history(acptr, 1);
	    sendto_serv_butone(NULL, acptr, TOK1_NICK, "%s :%i", parv[2], atoi(parv[3]));
	    if (acptr->name[0])
		del_from_client_hash_table(acptr->name, acptr);
	    strcpy(acptr->name, parv[2]);
	    add_to_client_hash_table(parv[2], acptr);
	}
    }
    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1