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

#define USER_COL       50

static char buf[BUFSIZE];

static struct Message _msgtab[] = {
    {MSG_MAP, 0, MAXPARA, M_SLOW, 0L,
     m_unregistered, u_map, o_map, 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_map_init(void)
{
    mod_add_cmd(_msgtab);
}
#endif

static void dump_map(struct Client *client_p, struct Client *root_p, char *pbuf)
{
    int i = 0, len;
    int users = 0;
    struct Client *server_p;

    *pbuf = '\0';

    strncat(pbuf, root_p->name, BUFSIZE - ((size_t) pbuf - (size_t) buf));
    len = strlen(buf);
    buf[len] = ' ';

    if (len < USER_COL) {
	for (i = len + 1; i < USER_COL; i++) {
	    buf[i] = '-';
	}
    }

    users = root_p->serv->usercnt;

    ircsnprintf(buf + USER_COL, BUFSIZE - USER_COL,
	     " | Users: %5d (%4.1f%%)", users, 100 * (float) users / (float) Count.total);

    send_me_numeric(client_p, RPL_MAP, buf);

    if (root_p->serv->servercnt) {
	if (pbuf > buf + 3) {
	    pbuf[-2] = ' ';
	    if (pbuf[-3] == '`')
		pbuf[-3] = ' ';
	}
    }
    for (i = 1, server_p = root_p->serv->servers; server_p; server_p = server_p->lnext) {
	if (ServicesConf.hide_ulined_servers) {
	    if (!IsSeeHidden(client_p) && IsULine(server_p))
		continue;
	}

	*pbuf = ' ';
	if (i < root_p->serv->servercnt)
	    *(pbuf + 1) = '|';
	else
	    *(pbuf + 1) = '`';

	*(pbuf + 2) = '-';
	*(pbuf + 3) = ' ';
	dump_map(client_p, server_p, pbuf + 4);

	i++;
    }
}

/*
 * ** m_map
 * **
 * ** parv[0] = sender prefix
 * ** parv[1] = server mask
 * * */
int o_map(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    dump_map(sptr, &me, buf);
    send_me_numeric(sptr, RPL_ENDMAP);
    return 0;
}

int u_map(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    if (ServerHide.enable) {
	send_me_numeric(sptr, ERR_NOPRIVILEGES);
	return 0;
    }
    dump_map(sptr, &me, buf);
    send_me_numeric(sptr, RPL_ENDMAP);
    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1