/*****************************************************************************
 *
 * File ..................: dbase/dbnode.c
 * Purpose ...............: Noderecord Access
 * Last modification date : 19-Mar-1999
 *
 *****************************************************************************
 * Copyright (C) 1997-1999
 *   
 * Michiel Broek		FIDO:		2:2801/16
 * Beekmansbos 10		Internet:	mbroek@ux123.pttnwb.nl
 * 1971 BV IJmuiden
 * the Netherlands
 *
 * This file is part of MBSE BBS.
 *
 * This BBS 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.
 *
 * MBSE BBS 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 MBSE BBS; see the file COPYING.  If not, write to the Free
 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *****************************************************************************/


#include "../include/libs.h"
#include "../include/structs.h"
#include "../include/records.h"
#include "../common/common.h"
#include "dbcfg.h"
#include "dbnode.h"


char		nodes_fil[81];		/* Nodes database filename	    */
long		nodes_pos = -1;		/* Noderecord position		    */
long		nodes_fgp = -1;		/* Nodes files group position	    */
long		nodes_mgp = -1;		/* Nodes message group position	    */
unsigned long	nodes_crc = -1;		/* Noderecord crc value		    */



int InitNode(void)
{
	FILE	*fil;

	memset(&nodes, 0, sizeof(nodes));
	LoadConfig();

	sprintf(nodes_fil, "%s/etc/nodes.data", getenv("MBSE_ROOT"));
	if ((fil = fopen(nodes_fil, "r")) == NULL)
		return FALSE;

	fread(&nodeshdr, sizeof(nodeshdr), 1, fil);
	fseek(fil, 0, SEEK_END);
	nodes_cnt = (ftell(fil) - nodeshdr.hdrsize) / (nodeshdr.recsize + nodeshdr.filegrp + nodeshdr.mailgrp);
	fclose(fil);

	return TRUE;
}



int TestNode(fidoaddr aka, int pointcheck)
{
	int	i, nodeok = FALSE;

	for (i = 0; i < 20; i++) {
		if (((aka.zone == 0) || (aka.zone = nodes.Aka[i].zone)) &&
		    (aka.net == nodes.Aka[i].net) &&
		    (aka.node == nodes.Aka[i].node) &&
		    ((!pointcheck) || (aka.point == nodes.Aka[i].point)))
			nodeok = TRUE;
	}
	return(nodeok);
}



int SearchNode(fidoaddr aka, int pointcheck)
{
	FILE	*fil;

	nodes_pos = -1;
	nodes_crc = -1;

	if ((fil = fopen(nodes_fil, "r")) == NULL)
		return FALSE;

	fread(&nodeshdr, sizeof(nodeshdr), 1, fil);
	while (fread(&nodes, nodeshdr.recsize, 1, fil) == 1) {
		fseek(fil, nodeshdr.filegrp + nodeshdr.mailgrp, SEEK_CUR);
		if (TestNode(aka, pointcheck)) {
			nodes_pos = ftell(fil) - (nodeshdr.recsize + nodeshdr.filegrp + nodeshdr.mailgrp);
			fclose(fil);
			nodes_crc = 0xffffffff;
			nodes_crc = upd_crc32((char *)&nodes, nodes_crc, nodeshdr.recsize);
			return TRUE;
		}
	}

	memset(&nodes, 0, sizeof(nodes));
	fclose(fil);
	return FALSE;
}



/*
 * Update current noderecord if changed.
 */
int UpdateNode()
{
	unsigned long	crc;
	FILE		*fil;

	if (nodes_pos == -1)
		return FALSE;

	crc = 0xffffffff;
	crc = upd_crc32((char *)&nodes, crc, nodeshdr.recsize);
	if (crc != nodes_crc) {
		if ((fil = fopen(nodes_fil, "r+")) == NULL)
			return FALSE;
		fseek(fil, nodes_pos, SEEK_SET);
		fwrite(&nodes, nodeshdr.recsize, 1, fil);
		fclose(fil);
	}

	nodes_crc = -1;
	nodes_pos = -1;
	memset(&nodes, 0, sizeof(nodes));
	return TRUE;
}



char *GetNodeMailGrp(int First)
{
	FILE	*fil;
	char	group[13], *gr;

	if (nodes_pos == -1)
		return NULL;

	if (First)
		nodes_mgp = nodes_pos + nodeshdr.recsize + nodeshdr.filegrp;
	
	if (nodes_mgp > (nodes_pos + nodeshdr.recsize + nodeshdr.filegrp + nodeshdr.mailgrp))
		return NULL;

	if ((fil = fopen(nodes_fil, "r")) == NULL)
		return NULL;

	fseek(fil, nodes_mgp, SEEK_SET);
	fread(&group, sizeof(group), 1, fil);
	fclose(fil);

	nodes_mgp += sizeof(group);

	if (group[0] == '\0')
		return NULL;

	gr = xstrcpy(group);
	return gr;	
}



char *GetNodeFileGrp(int First)
{
	FILE	*fil;
	char	group[13], *gr;

	if (nodes_pos == -1)
		return NULL;

	if (First)
		nodes_fgp = nodes_pos + nodeshdr.recsize;

	if (nodes_fgp > (nodes_pos + nodeshdr.recsize + nodeshdr.filegrp))
		return NULL;

	if ((fil = fopen(nodes_fil, "r")) == NULL)
		return NULL;

	fseek(fil, nodes_fgp, SEEK_SET);
	fread(&group, sizeof(group), 1, fil);
	fclose(fil);

	nodes_fgp += sizeof(group);

	if (group[0] == '\0')
		return NULL;

	gr = xstrcpy(group);
	return gr;
}


