/************************************************************************
 *   IRC - Internet Relay Chat, modules/m_service.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_conf.h"
#include "listener.h"
#include "h.h"

static char buf2[BUFSIZE];
static char *token = TOK1_SERVICE;

static struct Message _msgtab[] = {
    {MSG_SERVICE, 0, MAXPARA, M_SLOW, 0L,
     m_service, m_registered, m_ignore, s_service, 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_service_init(void)
{
    mod_add_cmd(_msgtab);
    tok1_msgtab[(u_char) *token].msg = _msgtab;
}
#endif

/*
 * -TimeMr14C
 * the m_service function
 * This is the function, where a connection defines itself
 * as a service instead of a user or server
 * it will also be parsed, if a remote server
 * introduces a service to us
 *
 *      parv[0] = sender prefix
 *      parv[1] = service name
 *      parv[2] = ident of the service
 *      parv[3] = service description
 *
 * The upper one should only be accepted if arriving from a
 * local client connection, else ignored.
 * 
 *      parv[0] = sender prefix
 *      parv[1] = service name
 *      parv[2] = timestamp of service introduction
 *      parv[3] = hopcount of the service
 *      parv[4] = flags of the service
 *      parv[5] = ident of the service
 *      parv[6] = hostname of the service
 *      parv[7] = server of the service
 *      parv[8] = description of the service
 *
 * This formation results from the SERVICE command arriving from a
 * server. It should be ignored if arriving from a user connection.
 *
 */

int s_service(aClient *cptr, aClient *sptr, int parc, char *parv[])
{

    aClient *acptr;
    aService *svc;

    char *m;
    char name[NICKLEN + 2];
    int *s;
    int flag = 0;
    int flags = 0;
    int modes = 0;

    if (parc < 8) {
	send_me_numeric(sptr, ERR_NEEDMOREPARAMS, MSG_SERVICE);
	return 0;
    }

    if (HasServices(cptr)) {

	if (!IsDigit(parv[2][0]) || !IsDigit(parv[3][0])) {
	    sendto_lev(SERVICE_LEV,
		       "Remote server %s tried to introduce non-ts service %s",
		       cptr->name, parv[1]);
	    return 0;
	}

	strlcpy_irc(name, parv[1], NICKLEN);

	if ((acptr = find_client(parv[1])) != NULL) {
            sendto_gnotice("Service %s already exists", acptr->name);
	    sendto_one(acptr, "%s %C :Service already exists", MSG_SQUIT, acptr);
            return exit_client(acptr, &me, "Service already exists");
	}

	acptr = find_server(parv[7]);

	if (!acptr) {
	    sendto_lev(SERVICE_LEV, "Remote SERVICE %s on unknown server %s", name, parv[7]);
	    return 0;
	}

	sptr = make_client(cptr);
	sptr->servptr = acptr;

	sptr->hopcount = atoi(parv[2]);
	sptr->tsval = atol(parv[3]);

	m = &parv[4][0];
	while (*m) {
	    for (s = service_info; (flag = *s); s += 2)
		if (*m == *(s + 1)) {
		    flags |= flag;
		    break;
		}
	    m++;
	}

	strcpy(sptr->name, name);

	strlcpy_irc(sptr->username, parv[5], USERLEN);
	strlcpy_irc(sptr->sockhost, parv[6], HOSTLEN);
	strlcpy_irc(sptr->info, parv[8], REALLEN);

	sptr->status = STAT_SERVICE;

	svc = make_service(sptr);
	svc->sflags |= flags;
	svc->enable = modes;
	svc->server = strdup(parv[7]);

	add_client_to_list(sptr);
	add_to_client_hash_table(sptr->name, sptr);

	sendto_lev(SERVICE_LEV,
		   "Created service %s (%s@%s) from %s. [%s]",
		   sptr->name, sptr->username, sptr->sockhost, svc->server, sptr->info);

	Count.service++;

	m = buf2;
	*m = '\0';
	for (s = service_info; (flag = *s); s += 2)
	    if (svc->sflags & flag)
		*m++ = *(s + 1);
	*m = '\0';

	sendto_lev(SERVICE_LEV,
		   "Flags for Service %s are: %s%s%s", sptr->name,
		   (strchr(buf2, 'U') ? "U:Lined " : ""), (strchr(buf2, 'A') ? "Akill " : ""));

	if (svc->sflags & SMODE_U)
	    svc->scptr->protoflags |= PFLAGS_ULINE;

	sendto_flag_serv_butone(cptr, CAPAB_SERVICES, 0, cptr, TOK1_SERVICE,
				"%s %d %T %s %s %s %s :%s",
				sptr->name, (sptr->hopcount + 1),
				sptr, service_info_to_char(svc),
				sptr->username, sptr->sockhost, svc->server, sptr->info);

	sendto_service(SERVICE_SEE_SERVICES, 0, cptr, NULL, TOK1_SERVICE,
		       "%s %d %T %s %s %s %s :%s",
		       sptr->name, (sptr->hopcount + 1),
		       sptr, service_info_to_char(svc),
		       sptr->username, sptr->sockhost, svc->server, sptr->info);

    } else if (!HasServices(sptr)) {

	sendto_gnotice("Non-Service capable server %s is trying to introduce service", sptr->name);
    }
    return 0;

}

int m_service(aClient *cptr, aClient *sptr, int parc, char *parv[])
{

    aClient *acptr;
    aService *svc;
    aConfItem *aconf;

    char *m;
    char name[NICKLEN + 2];
    int *s;
    int flag = 0;

    if (parc < 4) {
	send_me_numeric(sptr, ERR_NEEDMOREPARAMS, MSG_SERVICE);
	return 0;
    }

    strlcpy_irc(name, parv[1], NICKLEN);

    if ((aconf = find_conf_by_name(name, CONF_SERVICE)) == NULL) {
	return exit_client(sptr, &me, "Service Does Not Exist");
    }

    if (!BadPtr(aconf->passwd) && !strcmp(aconf->passwd, cptr->passwd)) {
	sendto_lev(REJ_LEV,
		   "Access denied (password mismatch) to %s (%s@%s)",
		   name, parv[2], get_client_name(cptr, TRUE));
	return exit_client(sptr, &me, "Password incorrect");
    }

    if ((acptr = find_client(parv[1])) != NULL) {
	sendto_lev(REJ_LEV, "Service %s already exists", acptr->name);
	return exit_client(sptr, &me, "Service already exists");
    }

    if (!IsListenerService(sptr->listener)) {
	sendto_lev(REJ_LEV, "Unauthorized connection attempt from %s to non-service port", 
		   get_client_name(cptr, TRUE));
	return exit_client(sptr, &me, "You are not authorized to use that port");
    }
 
    strcpy(cptr->name, name);
    strlcpy_irc(cptr->username, parv[2], USERLEN);
    strlcpy_irc(cptr->info, parv[3], REALLEN);
    cptr->tsval = timeofday;
    attach_conf(cptr, aconf);
    cptr->status = STAT_SERVICE;
    cptr->pingval = get_client_ping(cptr);
    cptr->sendqlen = get_sendq(cptr);

    memset(cptr->passwd, '\0', PASSWDLEN);

    svc = make_service(cptr);
    svc->sflags |= aconf->port;
    svc->enable = 0;
    svc->server = strdup(me.name);

    add_to_client_hash_table(cptr->name, cptr);

    send_me_numeric(cptr, RPL_YOURESERVICE);
    send_me_numeric(cptr, RPL_YOURHOST, get_client_name(&me, TRUE), version);
    send_me_numeric(cptr, RPL_CREATED, creation);
    send_me_numeric(cptr, RPL_MYINFO, me.name, version);

    send_supported(sptr);

    sendto_lev(SERVICE_LEV,
	       "Service %s (%s@%s) connected. [%s]",
	       cptr->name, cptr->username, cptr->sockhost, cptr->info);

    Count.myservice++;
    Count.service++;

    m = buf2;
    *m = '\0';
    for (s = service_info; (flag = *s); s += 2)
	if (svc->sflags & flag)
	    *m++ = *(s + 1);
    *m = '\0';

    sendto_lev(SERVICE_LEV,
	       "Flags for Service %s are: %s%s%s", cptr->name,
	       (strchr(buf2, 'U') ? "U:Lined " : ""), (strchr(buf2, 'A') ? "Akill " : ""));

    if (svc->sflags & SMODE_U)
	cptr->protoflags |= PFLAGS_ULINE;

    sendto_flag_serv_butone(cptr, CAPAB_SERVICES, 0, &me, TOK1_SERVICE,
			    "%s 1 %T %s %s %s %s :%s",
			    cptr->name, cptr, service_info_to_char(svc), cptr->username,
			    cptr->sockhost, me.name, cptr->info);

    sendto_service(SERVICE_SEE_SERVICES, 0, &me, NULL, TOK1_SERVICE,
		   "%s %T %s %s %s %s :%s", cptr->name, cptr->tsval,
		   service_info_to_char(svc), cptr->username, cptr->sockhost, me.name, cptr->info);

    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1