/* Copyright (C) 2000-1 drscholl@users.sourceforge.net
   This is free software distributed under the terms of the
   GNU Public License.  See the file COPYING for details.

   $Id: remove_file.c,v 1.2 2002/07/24 22:47:08 khaytsus Exp $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include "opennap.h"
#include "debug.h"

#ifndef ROUTING_ONLY

/* 102 <filename> */
HANDLER (remove_file)
{
    USER   *user;
    DATUM  *info;
    unsigned int fsize;

    (void) tag;
    (void) len;
    ASSERT (validate_connection (con));
    CHECK_USER_CLASS ("remove_file");
    user = con->user;
    if (!user->shared)
    {
	send_cmd (con, MSG_SERVER_NOSUCH, "Not sharing any files");
	return;
    }

    ASSERT (pkt != 0);
    if (!*pkt)
    {
	send_cmd (con, MSG_SERVER_NOSUCH, "remove file failed: missing argument");
	return;
    }

    /* find the file in the user's list */
    info = hash_lookup (con->uopt->files, pkt);
    if (!info)
    {
	send_cmd (con, MSG_SERVER_NOSUCH, "Not sharing that file");
	return;
    }

    /* adjust the global state information */
    fsize = info->size / 1024;	/* kB */

    if (fsize > user->libsize)
    {
	log_message_level (LOG_LEVEL_SHARE, "remove_file: bad lib size for %s, fsize=%u user->libsize=%u",
	     user->nick, fsize, user->libsize);
	user->libsize = fsize;	/* prevent negative count */
    }
    user->libsize -= fsize;

    if (fsize > Num_Gigs)
    {
	log_message_level (LOG_LEVEL_SHARE, "remove_file: bad lib size for %s, fsize=%u Num_Gigs=%f",
	     user->nick, fsize, Num_Gigs);
	Num_Gigs = fsize;	/* prevent negative count */
    }
    Num_Gigs -= fsize;

    ASSERT (Num_Files > 0);
    Num_Files--;

    ASSERT (Local_Files > 0);
    Local_Files--;

    user->shared--;
    if (user->shared_total > 0) {
        user->shared_total--;
    } else {
        log_message_level (LOG_LEVEL_SHARE, "remove_file: counts for %s mixed up (%d / %d). Unsharing %s.",
            user->nick, user->shared, user->shared_total, info->filename);
    }

    user->unsharing = 1;	/* note that we are unsharing */

    /* this invokes free_datum() indirectly */
    hash_remove (con->uopt->files, info->filename);
}
#endif /* ! ROUTING_ONLY */
