/* This is where all the recycling functions are */

#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include "include.h"
#include "channel.h"

extern char str_empty[1];

/* The free list declarations */
ACCOUNT			*		acnt_free;
ACNT_CMD		*		acnt_cmd_free;


DESCRIPTOR_DATA *copyover_desc(void)
{
    static DESCRIPTOR_DATA d_zero;
    DESCRIPTOR_DATA *d;

    if (descriptor_free == NULL)
	d = (DESCRIPTOR_DATA *) alloc_perm(sizeof(*d));
    else
    {
	d = descriptor_free;
	descriptor_free = descriptor_free->next;
    }
	
    *d = d_zero;
    d->connected	= CON_GET_ACCOUNT_NAME;
    free_string(d->showstr_head);
    free_string(d->showstr_point);
    d->outsize	= 2000;
    d->outbuf	= (char *) alloc_mem( d->outsize );
	d->fcommand = true;
	d->incomm[0] = '\0';
	d->pagelen = 20;
    d->pEdit = NULL;
	d->pString = NULL;
    return d;
}

/* Account recycling */
ACCOUNT *account_new()
{	ACCOUNT *pAcnt;
	if(!acnt_free )
		pAcnt = new account_data();
	else
	{	pAcnt = acnt_free;
		acnt_free = pAcnt->next;
	}
	pAcnt->name = &str_empty[0];
	pAcnt->password = &str_empty[0];
	pAcnt->lastfun = 0;
	pAcnt->next = account_list;
	account_list = pAcnt;
	pAcnt->struct_id = ACNTID;
	clear_bits(pAcnt->common_flags, COMMON_MAX );
	clear_bits(pAcnt->channel, CHANNEL_MAX);
	pAcnt->afk_string = &str_empty[0];
	pAcnt->mud = &str_empty[0];
	pAcnt->url = &str_empty[0];
	pAcnt->read = str_dup("<0>");
	pAcnt->editing = false;
	pAcnt->buffer = NULL;
	pAcnt->initLogin = 0;
	pAcnt->buffer = NULL;
	pAcnt->reply = NULL;
	pAcnt->bufPos = 0;
	pAcnt->custPost = &str_empty[0];
	pAcnt->custReply = &str_empty[0];
	pAcnt->custTopic = &str_empty[0];
	pAcnt->custPost = str_dup(CUSTPOST);
	pAcnt->custTopic = str_dup(CUSTTOPIC);
	pAcnt->custReply = str_dup(CUSTREPLY);
	for(int i = 0; i < CHANNEL_MAX ; ++i)
		pAcnt->lastTID[i] = 0;
	return pAcnt;
}

void free_account(ACCOUNT *pAcnt )
{	ACCOUNT *tmp;
	if(pAcnt == account_list)
		account_list = pAcnt->next;
	else
	for(tmp = account_list ; tmp ; tmp = tmp->next )
	{	if(tmp->next == pAcnt )
			tmp->next = pAcnt->next;
	}
	free_string(pAcnt->name);
	free_string(pAcnt->password);
	pAcnt->next = acnt_free;
	acnt_free = pAcnt;
	pAcnt->reply = NULL;
	free_string(pAcnt->mud);
	free_string(pAcnt->url);
	free_string(pAcnt->read);
	free_string(pAcnt->custPost);
	free_string(pAcnt->custReply);
	free_string(pAcnt->custTopic);
	if(pAcnt->buffer && !pAcnt->buffer->isBlob)
	{	pAcnt->buffer->Empty();
		delete pAcnt->buffer;
	}
	return;
}

/* Account Command Recycling */
ACNT_CMD *acnt_cmd_new()
{	ACNT_CMD *cmd;


	if(acnt_cmd_free == NULL )
		cmd = (ACNT_CMD *) alloc_perm(sizeof(*cmd) );
	else
	{	cmd = acnt_cmd_free;
		acnt_cmd_free = acnt_cmd_free->next;
	}
	free_string(cmd->name);
	cmd->name = str_dup("New Command");
	cmd->do_fun = NULL;
	cmd->level = 0;
	
	return cmd;
}

void free_acnt_cmd(ACNT_CMD *cmd)
{	ACNT_CMD *tmp;

	for(tmp = acnt_cmd_list; tmp; tmp = tmp->next )
	{	if (tmp->next == cmd)
			tmp->next = cmd->next;	
	}
	if(acnt_cmd_list == cmd )
		acnt_cmd_list = acnt_cmd_list->next;
	free_string(cmd->name);
	cmd->next = acnt_cmd_free;
	acnt_cmd_free = cmd;
	return;
}