#include <fido.h>
#include <ascii.h>
#include <\drivers\driver.h>

/*
	Rob Thomas' mailer.
*/

extern char _spr_sepchar;		/* printf() global */

unsigned _stack = 3500;			/* for autos and stack, */
unsigned zmttype = 0;			/* Zmodem Tx behaviour */
unsigned zmrtype = 0;			/* Zmodem Rx behaviour */
unsigned zmblkmax = 8192;		/* Zmodem maximum block size */
unsigned zmblkst = 1024;		/* Zmodem start with this block size */
char *zmbuff;				/* Zmodem Rx/Tx buffer */

char filelist[SS];			/* list of files to send */
char pwd[9];				/* senders password */
int cd_flag;
int limit;

char *texttbl[4];			/* table of text table pointers */

char *fmtable[] = {			/* Fixed Message Table */
	"",							/* FM0 */
	"\377",							/* FM1 */
	""							/* end of table */
};

char *lmtable[] = {			/* Log Message Table */
	"",							/* LM0 */
	"",							/* LM1 */
	"",							/* LM2 */
	"! wrong/missing nodelist file(s)\377",			/* LM3 */
	"! Cant AutoLog recd #%d?\377",				/* LM4 */
	"! Creating new %s\377",				/* LM5 */
	"! Wrong pwd: %s\377",					/* LM6 */
	"----\377",						/* LM7 */
	"+ %s on at %s, ",					/* LM8 */
	"test mode\377",					/* LM9 */
	"%d baud\377",						/* LM10 */
	"! 24 hr. limit\377",					/* LM11 */
	"! Help file \"%s\" missing\377",			/* LM12 */
	"! no specific help in \"%s\" for '%c'\377",		/* LM13 */
	"! no file areas?\377",					/* LM14 */
	"! outside\377",					/* LM15 */
	"! Disk full writing file \"%s\"\377",			/* LM16 */
	"! DL %s %s\377",					/* LM17 */
	"= DL ",						/* LM18 */
	"* time limit\377",					/* LM19 */
	"* near time limit\377",				/* LM20 */
	"* K limit\377",					/* LM21 */
	" N.F.",						/* LM22 */
	" ABORTED",						/* LM23 */
	" ERROR",						/* LM24 */
	" %d OK\377",						/* LM25 */
	"! UL %s %s\377",					/* LM26 */
	"= UL ",						/* LM27 */
	"! Name changed to: %s\377",				/* LM28 */
	"! Page, NA\377",					/* LM29 */
	"* Inactive\377",					/* LM30 */
	"! Can't find file \"%s\"\377",				/* LM31 */
	"! edit msg %d in area %d\377",				/* LM32 */
	"\377+ %d times, %d min\377\377",			/* LM33 */
	"\377---- Unscheduled Incoming Mail at %s\377",		/* LM34 */
	"---- Complete at %s\377\377",				/* LM35 */
	"\377---- FidoNet \"%c\" begins at %s (%d%c)\377",	/* LM36 */
	"---- FidoNet \"%c\" complete at %s\377\377",		/* LM37 */
	"! no msg areas?\377",					/* LM38 */
	"! Can't create file \"%s\"\377",			/* LM39 */
	""
};

struct _node id;		/* our FidoNet ID */
struct _node node;		/* destination ID */
struct _fido fido;		/* main system file */
int cmtfile = -1;		/* logon and comment file, (.LOG) */

char doscode;			/* DOS error code */
char date[20];			/* caller signon time */

/* These shoudl stay 0. They are dummies for \fido\ code. */

char test;			/* dummy */
char taskid;			/* dummy */
unsigned online_baud;		/* dummy */
int hlimit;			/* dummy */
unsigned datarate;

FLAG conflag;			/* 1 == local console output enabled */
char connects;			/* total actual connects */
char tries;			/* actual tries */
char phone[SS];			/* the phone number */
char rings;			/* number of rings to answer the phone */
char mdmstr[40];		/* modem initialization string */
char dial_interval;		/* how often to dial */
char cont_interval;		/* how often to reenable CONT events */
char dial_pref[30];		/* FidoNet dial string prefix */
char connect_tries;
char dial_tries;

unsigned mdmtype;		/* type of modem */
unsigned maxbaud;		/* maximum baud rate */
unsigned linkrate;		/* modem fixed link rate else 0 */

unsigned totl_files;		/* total files sent/rec'd */
unsigned totl_errors;		/* error count, soft errors incl */
unsigned totl_process;		/* 1 == file is in process */
unsigned totl_recoveries;	/* number of error recoveries */
long totl_bytes;		/* total bytes sent */

unsigned filemode;		/* transfer type; XMODEM, MODEM7, TELINK */

char *mem;			/* start of available memory */
unsigned memsize;		/* how big it is */
char *text;			/* text work space (set at language-load time) */
unsigned textsize;		/* how big it is */

main(argc,argv)
int argc;
char **argv;
{
char brkflg;			/* stored DOS BREAK enable */

	fidoini(argc,argv);			/* one time startup */
	brkflg= _break(0,0);			/* get break, */
	_break(1,0);				/* turn it off */
	fido_settings();
	set_clk();				/* install clock */
	init();					/* install driver */
	modem_type();				/* set modem type/max baud */

	fidonet();				/* deliver mail */

	putsys();				/* any scheduler changes, etc */
	uninit();				/* always ints off, etc */
	reset_clk();				/* turn off clock, */
	_break(1,brkflg);			/* restore break */
	exit(doscode);				/* back to DOS */
}

/* Load modem and related variables from FIDO.SYS. */

fido_settings() {

	rings= fido.rings;			/* number of rings to answer the phone */
	strcpy(mdmstr,fido.mdmstr);		/* modem initialization string */
	connect_tries= fido.connect_tries;	/* attempt with connect limit */
	dial_tries= fido.dial_tries;		/* dial attempts */
	dial_interval= fido.dial_interval;	/* how often to dial */
	cont_interval= fido.cont_interval;	/* how often to reenable CONT events */
	strcpy(dial_pref,fido.dial_pref);	/* FidoNet dial string prefix */

	cd_bit= fido.cd_bit;			/* set modem CD mask bit */
	mdmtype= fido.mdmtype;			/* modem type, */
}

/* Dummy functions to satisfy plink. */

script() {}
mconout() {}
mconoutr() {}

