/************************************************************************
 *   IRC - Internet Relay Chat, modules/m_links.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_links.c,v 1.4 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 "s_conf.h"
#include "channel.h"
#include "h.h"

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

#ifndef STATIC_MODULES

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

void _modinit(void)
{
    mod_add_cmd(_msgtab);
}

void _moddeinit(void)
{
    mod_del_cmd(_msgtab);
}
#else
void m_links_init(void)
{
    mod_add_cmd(_msgtab);
}
#endif

/*
 * * m_links 
 *      parv[0] = sender prefix 
 * or 
 *      parv[0] = sender prefix 
 *      parv[1] = server to query 
 * 
 * You either do a total LINKS or not. No servername masks anymore.
 */
int m_links(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    dlink_node *ptr;
    aMotdItem *temp;
    aClient *acptr;

    if (!MyConnect(sptr))
	return 0;		/* Reject non-local LINKS */

    if (MyConnect(sptr) && IsPerson(sptr))
	sendto_lev(SPY_LEV, "LINKS requested by %^C [%s]", sptr, sptr->user->server);

    if (!ServerHide.enable) {
	/* well known behavior of /links */
	for (ptr = global_serv_list.head; ptr; ptr = ptr->next) {
	    acptr = ptr->data;
	    if (ServicesConf.hide_ulined_servers && !IsSeeHidden(sptr) && IsULine(acptr))
		continue;
	    send_me_numeric(sptr, RPL_LINKS, acptr->name, acptr->servptr->name,
			    acptr->hopcount, (acptr->info[0] ? acptr->info : "(Unknown Location)"));
	}
    } else {
	/* server hiding is active 
	 * now we have multiple options. */

	/* First alternative is flattenning the output of /links 
	 * that every server looks like as if connected to us */

	if (ServerHide.flatten_links) {
	    send_me_numeric(sptr, RPL_LINKS, me.name, me.name, 0, me.info);
	    for (ptr = global_serv_list.head; ptr; ptr = ptr->next) {
		acptr = ptr->data;
		if (ServicesConf.hide_ulined_servers && !IsSeeHidden(sptr) && IsULine(acptr))
		    continue;
		if (IsMe(acptr))
		    continue;
		send_me_numeric(sptr, RPL_LINKS, acptr->name, me.name, 1, ServerInfo.networkdesc);
	    }

	    /* next alternative is making /links oper only */

	} else if (ServerHide.links_oper_only) {
	    if (!IsSeeHidden(sptr)) {
		send_me_numeric(sptr, ERR_NOPRIVILEGES);
	    } else {
		for (ptr = global_serv_list.head; ptr; ptr = ptr->next) {
		    acptr = ptr->data;
		    send_me_numeric(sptr, RPL_LINKS, acptr->name, acptr->servptr->name,
				    acptr->hopcount,
				    (acptr->info[0] ? acptr->info : "(Unknown Location)"));
		}
	    }

	    /* Next alternative is a file from which we read the output of /links */

	} else if (ServerHide.links_from_file) {
	    temp = (&(GeneralOpts.linksfile))->content;
	    if (temp) {
		while (temp) {
		    sendto_one(sptr, ":%C %N %s %s", &me, RPL_LINKS, sptr->name, temp->line);
		    temp = temp->next;
		}
	    } else {
		send_me_numeric(sptr, RPL_LINKS, me.name, me.name, 0, me.info);
	    }

	}
    }

    send_me_numeric(sptr, RPL_ENDOFLINKS, "*");
    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1