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

static struct Message _msgtab[] = {
    {MSG_CONNECT, 0, MAXPARA, M_SLOW, 0L,
     m_unregistered, m_permission, m_connect, m_connect, m_ignore}
};

static char *token = TOK1_CONNECT;

static int log_connect = -1;

#ifndef STATIC_MODULES

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

void _modinit(void)
{
    mod_add_cmd(_msgtab);
    tok1_msgtab[(u_char) *token].msg = _msgtab;
    log_connect = logevent_register("connect", LOG_ALWAYS, LOG_LOGFILE, LOG_NOTICE,
				    "CONNECT From %s : %s %s");
}

void _moddeinit(void)
{
    mod_del_cmd(_msgtab);
    tok1_msgtab[(u_char) *token].msg = NULL;
    logevent_unregister(log_connect);
}
#else
void m_connect_init(void)
{
    mod_add_cmd(_msgtab);
    tok1_msgtab[(u_char) *token].msg = _msgtab;
    log_connect = logevent_register("connect", LOG_ALWAYS, LOG_LOGFILE, LOG_NOTICE,
                                    "CONNECT From %s : %s %s");
}
#endif

/***********************************************************************
 * m_connect() - Added by Jto 11 Feb 1989
 ***********************************************************************/
/*
 * * m_connect
 *      parv[0] = sender prefix
 *      parv[1] = servername   
 *      parv[2] = port number  
 *      parv[3] = remote server
 */
int m_connect(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
    int port, tmpport;
    aConfItem *aconf;
    aClient *acptr;

    if (hunt_server(cptr, sptr, ":%s %s %s %s :%s", TOK1_CONNECT, 3, parc, parv) != HUNTED_ISME)
	return 0;

    if (parc < 2 || *parv[1] == '\0') {
	send_me_numeric(sptr, ERR_NEEDMOREPARAMS, MSG_CONNECT);
	return -1;
    }

    if ((acptr = find_server(parv[1]))) {
	send_me_notice(sptr,
		       ":Connect: Server %s %s %s.",
		       parv[1], "already exists from", acptr->from->name);
	return 0;
    }

    for (aconf = GlobalConfItemList; aconf; aconf = aconf->next)
	if (aconf->status == CONF_SERVER && match(parv[1], aconf->name) == 0)
	    break;
    /*
     * Checked first servernames, then try hostnames.
     */
    if (!aconf)
	for (aconf = GlobalConfItemList; aconf; aconf = aconf->next)
	    if (aconf->status == CONF_SERVER && (match(parv[1], aconf->host) == 0))
		break;

    if (!aconf) {
	send_me_notice(sptr, ":Connect: No C line found for %s.", parv[1]);
	return 0;
    }
    /*
     * * Get port number from user, if given. If not specified, use
     * the default form configuration structure. If missing from   
     * there, then use the precompiled default.
     */
    tmpport = port = aconf->port;
    if (parc > 2 && !BadPtr(parv[2])) {
	if ((port = atoi(parv[2])) <= 0) {
	    send_me_notice(sptr, ":Connect: Illegal port number");
	    return 0;
	}
    }
    sendto_gnotice
	("from %C: %s CONNECT %s %s from %s",
	 &me,
	 IsAnOper(cptr) ? "Local" : "Remote", parv[1],
	 parv[2] ? parv[2] : "", get_client_name(sptr, HIDEME));
    sendto_serv_butone(NULL, &me, TOK1_GNOTICE,
		       ":%s CONNECT %s %s from %s",
		       IsAnOper(cptr) ? "Local" : "Remote",
		       parv[1], parv[2] ? parv[2] : "", get_client_name(sptr, HIDEME));
    logevent_call(log_connect, parv[0], parv[1], parv[2] ? parv[2] : "");
    aconf->port = port;
    if (serv_connect(aconf, sptr))
	send_me_notice(sptr, ":*** Connecting to %s.%d", aconf->name, aconf->port);
    else
	send_me_notice(sptr, ":*** Couldn't connect to %s.%d", aconf->name, aconf->port);
    aconf->port = tmpport;
    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1