#include <string.h>
#include <exec/types.h>
#include <proto/exec.h>
#include "nl.h"
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <dos/datetime.h>
#include <proto/dos.h>

/*------------------------------------------------------------------------*/
// A "real" application should look it up in some config!

#define NODELISTDIR	"Nodelist:"

/*-------------------------------------------------------------------------*/
typedef struct
	{
	ULONG Cost;						// what we've payed for calls to him so far
	UWORD CallsMade;				// how many calls we placed (incl. unsuccessful ones)
	UWORD CallsReceived;			// how many inbound calls we got from him
	UWORD SessionSuccess;		// how many sessions were okay
	UWORD SessionFailure;		// how many sessions failed
	UWORD CallsBusy;				// times BUSY
	UWORD CallsVoice;				// times VOICE
	UWORD CallsNoSomething;		// times NO CARRIER or NO DIAL TONE
	} OldNodeAccount;

typedef struct
	{
	ULONG Cost;						// what we've payed for calls to him so far
	UWORD CallsMade;				// how many calls we placed (incl. unsuccessful ones)
	UWORD CallsReceived;			// how many inbound calls we got from him
	UWORD SessionSuccess;		// how many sessions were okay
	UWORD SessionFailure;		// how many sessions failed
	UWORD CallsBusy;				// times BUSY
	UWORD CallsVoice;				// times VOICE
	UWORD CallsNoSomething;		// times NO CARRIER or NO DIAL TONE
	ULONG LastTrxID;				// our TrxID of last session with this system
	ULONG HisLastTrxID;			// his last TrxID with us (if available, zero otherwise)
	} NodeAccount;
/// new and improved accounting.

/*-------------------------------------------------------------------------*/
extern void printf(char *,...);
extern void puts(char *);
extern void exit(int);

void main(void);

/*------------------------------------------------------------------------*/
struct Library *NodelistBase;

extern struct DosLibrary *DOSBase;

/*-------------------------------------------------------------------------*/
BOOL call_me(Addr *addr, Extra xtra)
	{
	char temp[40];
	char ttime[40];
	struct DateStamp ds;
	struct DateTime dt;
	NodeAccount *acct;
	ULONG size=sizeof(NodeAccount);

	if(acct = ExtraFindLock(xtra, addr, ID_ACCT, &size))
		{
		ExtraTagDate(xtra, acct, &ds);
		temp[0] = '\0';
		ttime[0]= '\0';

		if(ds.ds_Days)
			{
			if(DOSBase->dl_lib.lib_Version>=37)
				{
				dt.dat_Stamp = ds;
				dt.dat_Format = FORMAT_DOS;
				dt.dat_Flags = DTF_SUBST;
				dt.dat_StrDay = NULL;
				dt.dat_StrDate = temp;
				dt.dat_StrTime = ttime;
				DateToStr(&dt);
				}
			}
		else
			{
			strcpy(temp, "(unset)");
			}

		printf("  %ld:%ld/%ld.%ld\t| %4ld.%02ld  "
				 "%3ld %3ld  "
				 "%3ld %3ld  "
				 "%3ld %3ld %3ld   %s %s\n",
			addr->Zone, addr->Net, addr->Node, addr->Point,
			acct->Cost/100, acct->Cost%100,
			acct->CallsMade, acct->CallsReceived,
			acct->SessionSuccess, acct->SessionFailure,
			acct->CallsBusy, acct->CallsNoSomething, acct->CallsVoice,
			temp, ttime);
		if(size>=sizeof(NodeAccount) && acct->LastTrxID)
			printf("\t\t|\tLast session: %08lx/%08lx\n",
										acct->LastTrxID, acct->HisLastTrxID);

		ExtraUnlockTag(xtra, acct);
		}

	return TRUE;
	}

/*-------------------------------------------------------------------------*/
void main()
	{
	Extra xtra;

	if(!(NodelistBase = OpenLibrary(TRAPLIST_NAME, TRAPLIST_VER)))
		{
		printf("Can't open library\n");
		exit(5);
		}

	/* First, open the Extra database */

	if((xtra = ExtraOpen(NODELISTDIR)))
		{
		puts("   Node         |    Cost   CO  CI   S+  S-    B   N   V     Last");
		puts("----------------+---------------------------------------------------");
		ExtraEnumNode(xtra, call_me, xtra);

		ExtraClose(xtra);
		}
	else	
		puts("Extra won't open");

	CloseLibrary(NodelistBase);
	}

/*-------------------------------------------------------------------------*/
/* ID: 88.1@3883  Last Changed: 10 Sep 1992 20:09:01 by max */
