/*
 * $Id: send-async.c,v 0.4 1991/12/16 16:43:56 ceder Exp $
 * Copyright (C) 1991  Lysator Academic Computer Association.
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM 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 1, or (at your option) 
 * any later version.
 * 
 * LysKOM 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 LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */
/*
 * send-async.c -- Send messages about events to all connected clients.
 *
 * Written by Per Cederqvist 1990-07-22--23
 */

static char *rcsid = "$Id: send-async.c,v 0.4 1991/12/16 16:43:56 ceder Exp $";


#include <stdio.h>
#include "lyskomd.h"
#include <kom-types.h>
#include <services.h>
#include "async.h"
#include "manipulate.h"
#include "com.h"
#include "connections.h"
#include "send-async.h"
#include "prot-a-send-async.h"
#include "prot-a-output.h"
#include "log.h"
#include "internal-connections.h"
    

extern Bool		  send_async_messages;


void
async_new_text(struct connection *cptr,
	       Text_no    	  text_no, 
	       Text_stat         *text_s)
{
    if (!send_async_messages)
	return;

    switch(cptr->protocol)
    {
    case 0:
	break;
    case 'A':
	prot_a_async_new_text(cptr, text_no, text_s);
	break;
    default:
	restart_kom("async_new_text(): bad protocol.\n");
	break;
    }
}

    
    
	    
void
async_i_am_on(Who_info info)
{
    Connection *cptr;
    Session_no i = 0;

    if (!send_async_messages)
	return;

    while ( (i = traverse_connections(i)) != 0 )
    {
	cptr = get_conn_by_number(i);

	switch(cptr->protocol)
	{
	case 0:			/* Not yet logged on. */
	    break;
	case 'A':
	    prot_a_async_i_am_on(cptr, info);
	    break;
	default:
	    restart_kom("async_i_am_on(): bad protocol.\n");
	    break;
	}
    }
}


void
async_i_am_off(Pers_no pers_no)
{
    Connection *cptr;
    Session_no i = 0;

    if (!send_async_messages)
	return;

    while ( (i = traverse_connections(i)) != 0 )
    {
	cptr = get_conn_by_number(i);

	if ( cptr == NULL )
	{
	    log("async_i_am_off(): cptr == NULL\n");
	    return;
	}

	switch(cptr->protocol)
	{
	case 0:
	    break;
	case 'A':
	    prot_a_async_i_am_off(cptr, pers_no);
	    break;
	default:
	    restart_kom("async_i_am_off(): bad protocol.\n");
	    break;
	}
    }
}

void
async_logout(Pers_no pers_no, 
	     Session_no session_no)
{
    Connection *cptr;
    Session_no i = 0;

    if (!send_async_messages)
	return;

    while ( (i = traverse_connections(i)) != 0)
    {
	cptr = get_conn_by_number(i);

	if ( cptr == NULL )
	{
	    log("async_logout(): cptr == NULL\n");
	    return;
	}

	switch(cptr->protocol)
	{
	case 0:
	    break;
	case 'A':
	    prot_a_async_logout(cptr, pers_no, session_no);
	    break;
	default:
	    restart_kom("async_logout(): bad protocol.\n");
	    break;
	}
    }
}



void
async_new_name(Conf_no 	     conf_no,
	       const String  old_name,
	       const String  new_name)
{
    Connection *cptr;
    Session_no i = 0;

    if (!send_async_messages)
	return;

    while ( (i = traverse_connections(i)) != 0)
    {
	cptr = get_conn_by_number(i);

	if ( cptr == NULL )
	{
	    log("async_new_name(): cptr == NULL\n");
	    return;
	}

	switch(cptr->protocol)
	{
	case 0:
	    break;
	case 'A':
	    prot_a_async_new_name(cptr, conf_no, old_name, new_name);
	    break;
		
	default:
	    restart_kom("async_new_name(): bad protocol.\n");
	    break;
	}
    }
}




#if 0    
conf_deleted
conf_created
#endif

void
async_sync_db(void)
{
    Connection *cptr;
    Session_no i = 0;

    if (!send_async_messages)
	return;

    while ( (i = traverse_connections(i)) != 0 )
    {
	cptr = get_conn_by_number(i);

	if ( cptr == NULL )
	{
	    log("async_sync_db(): cptr == NULL\n");
	    return;
	}

	switch(cptr->protocol)
	{
	case 0:
	    break;
	case 'A':
	    prot_a_async_sync_db(cptr);
	    break;
	default:
	    restart_kom("async_sync_db(): bad protocol.\n");
	    break;
	}
    }
}


extern void
async_forced_leave_conf (struct connection *cptr,
			 Conf_no 	   conf_no)
{
    if (!send_async_messages)
	return;

    switch(cptr->protocol)
    {
    case 0:
	break;
    case 'A':
	prot_a_async_forced_leave_conf(cptr, conf_no);
	break;
    default:
	restart_kom("async_forced_leave_conf(): bad protocol.\n");
	break;
    }
}

void
async_login(Pers_no	pers_no,
	    int		client_no)
{
    Connection *cptr;
    Session_no i = 0;

    if (!send_async_messages)
	return;

    while ( (i = traverse_connections(i)) != 0)
    {
	cptr = get_conn_by_number(i);

	if ( cptr == NULL )
	{
	    log("async_login(): cptr == NULL\n");
	    return;
	}

	switch(cptr->protocol)
	{
	case 0:
	    break;
	case 'A':
	    prot_a_async_login(cptr, pers_no, client_no);
	    break;
		
	default:
	    restart_kom("async_login(): bad protocol.\n");
	    break;
	}
    }
}

void
async_broadcast(Pers_no	pers_no,
		String	message)
{
    Connection *cptr;
    Session_no i = 0;

    if (!send_async_messages)
	return;

    while ( (i = traverse_connections(i)) != 0)
    {
	cptr = get_conn_by_number(i);

	if ( cptr == NULL )
	{
	    log("async_broadcast(): cptr == NULL\n");
	    return;
	}

	switch(cptr->protocol)
	{
	case 0:
	    break;
	case 'A':
	    prot_a_async_broadcast(cptr, pers_no, message);
	    break;
		
	default:
	    restart_kom("async_broadcast(): bad protocol.\n");
	    break;
	}
    }
}


void
async_rejected_connection(void)
{
    Connection *cptr;
    Session_no i = 0;

    if (!send_async_messages)
	return;

    while ( (i = traverse_connections(i)) != 0)
    {
	cptr = get_conn_by_number(i);

	if ( cptr == NULL )
	{
	    log("async_rejected_connections(): cptr == NULL\n");
	    return;
	}

	switch(cptr->protocol)
	{
	case 0:
	    break;
	case 'A':
	    prot_a_async_rejected_connection(cptr);
	    break;
		
	default:
	    restart_kom("async_rejected_connection(): bad protocol.\n");
	    break;
	}
    }
}


/*
 * Returns failure if no message was sent.
 */
Success
async_send_message(Pers_no recipient,
		   Pers_no sender,
		   String  message)
{
    Connection *cptr;
    Session_no i = 0;
    Success retval = FAILURE;

    if (!send_async_messages)
	return FAILURE;

    while ( (i = traverse_connections(i)) != 0)
    {
	cptr = get_conn_by_number(i);

	if ( cptr == NULL )
	{
	    log("async_send_message(): cptr == NULL\n");
	    return FAILURE;
	}

	switch(cptr->protocol)
	{
	case 0:
	    break;
	case 'A':
	    if ( recipient == 0 ||
		(recipient == cptr->pers_no && recipient != 0 ))
	    {
		prot_a_async_send_message(cptr, recipient, sender, message);
		retval = OK;
	    }
	    break;
		
	default:
	    restart_kom("async_send_message(): bad protocol.\n");
	    break;
	}
    }
    
    return retval;
}