/************************************************************************
*   IRC - Internet Relay Chat, src/whowas.c
*   Copyright (C) 1990 Markku Savela
*
*   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_whowas.c,v 1.4 2003/06/29 11:03:12 tr-ircd Exp $ 
 */

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

static char *token = TOK1_WHOWAS;

static struct Message _msgtab[] = {
    {MSG_WHOWAS, 0, MAXPARA, M_SLOW, 0L,
     m_unregistered, m_whowas, m_whowas, m_whowas, m_whowas}
};

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

/*
 * * m_whowas *      parv[0] = sender prefix *      parv[1] = nickname
 * queried
 */
int m_whowas(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    aWhowas *temp;
    int cur = 0;
    int max = -1, found = 0;
    char *p, *nick, *s;

    if (parc < 2) {
	send_me_numeric(sptr, ERR_NONICKNAMEGIVEN);
	return 0;
    }
    if (parc > 2)
	max = atoi(parv[2]);
    if (parc > 3)
	if (hunt_server(cptr, sptr, ":%s %s %s %s :%s", TOK1_WHOWAS, 3, parc, parv))
	    return 0;

    parv[1] = canonize(parv[1]);
    if (!MyConnect(sptr) && (max > 20))
	max = 20;
    for (s = parv[1]; (nick = strtoken(&p, s, ",")); s = NULL) {
	temp = WHOWASHASH[hash_whowas_name(nick)];
	found = 0;
	for (; temp; temp = temp->next) {
	    if (!irc_strcmp(nick, temp->name)) {
		send_me_numeric(sptr, RPL_WHOWASUSER, temp->name,
				temp->username, temp->hostname, temp->realname);
		if (IsSeeHidden(sptr))
		    send_me_numeric(sptr, RPL_WHOWASREAL, temp->name, temp->morehost);
		send_me_numeric(sptr, RPL_WHOISSERVER,
				temp->name, temp->servername, smalldate(temp->logoff));
		cur++;
		found++;
	    }
	    if (max > 0 && cur >= max)
		break;
	}
	if (!found)
	    send_me_numeric(sptr, ERR_WASNOSUCHNICK, nick);
	if (p)
	    p[-1] = ',';
    }
    send_me_numeric(sptr, RPL_ENDOFWHOWAS, parv[1]);
    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1