/*
 * $Id: admin.c,v 0.5 1991/09/15 10:33:25 linus 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. 
 */
/*
 * admin.c
 *
 * Administrative calls.
 */

static char *rcsid = "$Id: admin.c,v 0.5 1991/09/15 10:33:25 linus Exp $";

#include <stdlib.h>
#include "lyskomd.h"
#include <kom-types.h>
#include <services.h>
#include "manipulate.h"
#include <server/smalloc.h>
#include "cache.h"
#include "log.h"
#include "admin.h"
#include <kom-errno.h>
#include "com.h"
#include "connections.h"
#include "send-async.h"
#include "config.h"

Info kom_info = 
{
#include "version.incl"
    ,				/* version */
    1,				/* conf_pres_conf */
    2,				/* pers_pres_conf */
    3,				/* motd_conf */
    4,				/* kom_news_conf */
    0				/* motd_of_lyskom */
};

/*
 * Return info about this server. This can (and should) be done
 * before logging in. modt_of_lyskom should be displayed before
 * prompting for username if it isn't 0.
 */
extern Success
get_info( Info *result )
{
    *result = kom_info;
    return OK;
}

/* /// */
extern Success
set_motd_of_lyskom (Text_no motd)
{
    Text_stat *old_motd = NULL;
    Text_stat *new_motd = NULL;

    CHK_LOGIN(FAILURE);

    if ( !ENA(admin, 1) )
    {
	kom_errno = KOM_PERM;
	return FAILURE;
    }
    
    /* Check that the new motd exists before deleting the old*/

    if ( motd != 0 )
    {
	GET_T_STAT(new_motd, motd, FAILURE);
	if ( new_motd->no_of_marks >= MAX_MARKS_TEXT )
	{
	    log("LIMIT: set_motd_of_lyskom(%d): New motd has %d marks.\n",
		motd, new_motd->no_of_marks);
	    kom_errno = KOM_MARK_LIMIT;
	    return FAILURE;
	}
    }
    
    /* Unmark the previous motd if it exists. */

    if ( kom_info.motd_of_lyskom != 0
	&& (old_motd = cached_get_text_stat(kom_info.motd_of_lyskom)) != NULL)
    {
	if ( old_motd->no_of_marks > 0 )
	{
	    --old_motd->no_of_marks;
	    mark_text_as_changed( kom_info.motd_of_lyskom );
	}
	else
	{
	    log("ERROR: do_set_motd(): Old motd not marked\n");
	}
    }

    /* Mark the new motd */

    if ( motd != 0 )
    {
	++new_motd->no_of_marks;
	mark_text_as_changed( motd );
    }
    
    kom_info.motd_of_lyskom = motd;

    return OK;
}




/*
 * Force all clients to read a message.
 * Sends an asynchronous message. This is obsoleted by send_message.
 */
extern Success
broadcast (String message)
{
    CHK_LOGIN(FAILURE);

    async_broadcast(ACTPERS, message);
    return OK;
}

/*
 * Send a message
 */
extern Success
send_message (Pers_no recipient,
	      const String message)
{
    CHK_LOGIN(FAILURE);

    return async_send_message(recipient, ACTPERS, message);
}



/*
 * Make LysKOM sync its files.
 */
extern Success
sync (void) 
{
    cache_sync_all();
    dump_statistics();
    return OK;
}

/*
 * Close LysKOM. exit_val is (currently) not used. The database is synced.
 */
extern Success
shutdown (int exit_val) 
{
    CHK_LOGIN(FAILURE);
    if ( !ENA(admin, 1) )
    {
	kom_errno = KOM_PERM;
	return FAILURE;
    }

    go_and_die = TRUE;
    return OK;
}