#if 1
#define NDEBUG 1
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <io.h>
#include <conio.h>
#include <dos.h>

#ifndef max
#define max(a,b) (a > b) ? a : b
#endif

#ifndef min
#define min(a,b) (a < b) ? a : b
#endif

#include "nedit.h"
#include "screen.h"
#include "debug.h"

#define VERSION "1.96S ZTC"
	
#define FALSE 		0
#define TRUE 	   !FALSE

#define NO              0
#define YES		1
#define HIDE		2

#define GDOMAINS	1
#define GZONES		2

#define USENET		1
#define FIDONET 	2

#define PATHLEN        64

#ifdef MAIN
#define global
#else
#define global extern
#endif

#if defined(__TURBOC__) || defined(__MSC__)
#define strcmpl strcmpi
#endif

#define checkmem(x)	{ if ((x) == NULL) outamemory(); } 

/* structures and typedefs */

struct _msgheader {
	char from[36];		   /* who from,                */
	char to[36];               /* who to,                  */
	char subj[72];             /* message subject,         */
	char date[20];             /* creation date,           */
	int times;                 /* number of times read,    */
	int dest;                  /* destination node,        */
	int orig;                  /* originating node         */
	int cost;                  /* actual cost this msg     */
	int orig_net;              /* originating net          */
	int dest_net;              /* destination net          */
	long timestamp;		   /* unix style timestamp     */
	long fill;
	unsigned int reply;        /* thread to previous msg   */
/*	message attributes -- 1 bit flags                      */
	unsigned int private:1;    /* private message flag     */
	unsigned int crash:1;      /* crash mail               */
	unsigned int recvd:1;      /* received by addressee    */
	unsigned int sent:1;       /* message sent             */
	unsigned int attached:1;   /* file attached            */
	unsigned int forward:1;    /* message in transit       */
	unsigned int orphan:1;     /* unknown destination      */
	unsigned int killsent:1;   /* kill after sending       */
	unsigned int local:1;      /* local message            */
	unsigned int hold:1;       /* hold for pickup          */
	unsigned int xx2:1;        /* reserved, stripped by net*/
	unsigned int freq:1;       /* file request             */
	unsigned int rreq:1;       /* return receipt requested */
	unsigned int rcpt:1;       /* return receipt           */
	unsigned int areq:1;       /* audit trail request      */
	unsigned int ureq:1;       /* update file request      */
	int up;                    /* thread to next msg       */
};

struct _address {
	int 	zone;
	int 	net;
	int 	node;
	int 	point;
	char   *domain;
};

struct _area {
	char *description;	/* what the user calls the area */
	char *tag;		/* what confmail calls it! */
	char *path;		/* where the area is on disk */
	unsigned int local:1;	/* local message area */
	unsigned int netmail:1;	/* netmail message area */
	unsigned int echomail:1;/* echomail area */
	unsigned int new:1;	/* a message has been entered */
	int first;		/* first message in the area */
	int last;		/* last message in the area */
	int current;		/* current message in the area */
	int messages;		/* how many messages in the area */
	int lastread;		/* the highest message read */
};

typedef struct _address ADDRESS;
typedef struct _msgheader MSGHEADER;
typedef struct _area AREA;

struct _msg {
	int msgnum;		/* local message number		*/
	MSGHEADER header;	/* header above			*/
	ADDRESS to;		/* extended destination address of message  */
	ADDRESS from;		/* extended origin address of message */
};

typedef struct _msg MSG;

struct _command {
	char *label;
	void (*action)();
};

/* imports from the screen file */

extern int	normfore,		/* normal foreground color */
		normback,		/* normal background color */
		maxy,			/* how many screen lines? */
		maxx,			/* how many screen columns? */
                videomethod,            /* DIRECT, BIOS or FOSSIL */
                statfore,statback;      /* intense foreground and background */

extern unsigned int vbase;              /* the video segment */

/* global variables */

global AREA 	*arealist;

global char     *username,              /* who is you */
                *messages,              /* what messages exist in this area */
                *quotestr,              /* how to prefix a quote */
                *fidolist,              /* nodelist user list */
                *userlist,              /* personal user list */
		*origin,		/* origin line */
		*outfile,		/* default export filename */
                *confmail;              /* confmail log file name */

global BUFFER 	msgbuf;

global LINE	*curline;

global int	area,			/* current area number */
		kludgelines,		/* include ^A lines in buffer */
		lastline,		/* last line in message */
		rm,			/* the right margin */
		shownotes,		/* show hidden lines */
		seenbys,		/* show seenby lines */
		pointnet,		/* private net number of point */
		tabsize,		/* how many spaces for a tab */
		tearline,		/* show a tearline? */
		domains,		/* how many domains listed */
                confirmations,          /* confirm deletes, aborts? */
                softcr,                 /* put in soft carriage returns? */
                readthread,             /* read threads */
                flashhelp,              /* use the flashup help system? */
		override,		/* override the area origin line */
		gate,			/* zone/domain gate messages? */
		quotestyle,		/* usenet or fidonet */
                areas;                  /* how many message areas */

global struct {
	unsigned int private:1;    /* private message flag     */
	unsigned int crash:1;      /* crash mail               */
	unsigned int recvd:1;      /* received by addressee    */
	unsigned int sent:1;       /* message sent             */
	unsigned int attached:1;   /* file attached            */
        unsigned int forward:1;    /* message in transit       */
        unsigned int orphan:1;     /* unknown destination      */
        unsigned int killsent:1;   /* kill after sending       */
	unsigned int local:1;      /* local message            */
	unsigned int hold:1;       /* hold for pickup          */
	unsigned int xx2:1;        /* reserved, stripped by net*/
	unsigned int freq:1;       /* file request             */
	unsigned int rreq:1;       /* return receipt requested */
	unsigned int rcpt:1;       /* return receipt           */
	unsigned int areq:1;       /* audit trail request      */
        unsigned int ureq:1;       /* update file request      */
} msgbits;

global ADDRESS  thisnode;
	    	
global MSG 	message;

global ADDRESS *domain_list;

/* prototypes */

void    main(int argc, char *argv[]);
void	outamemory(void);
int  	scanmail(char *area);			/* readmail.c   */
int  	readmsg(MSG *message,int number);	/* readmail.c 	*/
int  	showmsg(MSG);                      	/* showmail.c   */
LINE   *buffer(char **);                        /* bufmsg.c     */
void    opening(char *,char *);                 /* config.c     */
void 	parseareas(char *);                	/* areas.c      */
int  	nextmsg(int);                      	/* msged.c      */
void    setlast(void);                          /* areas.c      */
int  	selectarea(void);                  	/* areas.c      */
int  	writemsg(MSG ,int);			/* writmail.c	*/
void 	createmsg(void);                   	/* makemsg.c	*/
MSGHEADER readheader(int n);			/* readmail.c   */
int 	writeheader(MSGHEADER m,int n);		/* writmail.c   */
void 	showheader(MSG m);                 	/* showmail.c   */
ADDRESS parsenode(char *s);             	/* config.c     */
void 	clearbuffer(void);			/* bufmsg.c	*/
void	import(LINE *l);			/* textfile.c	*/
void	export(LINE *f);			/* textfile.c	*/
char   *readtext(int n);			/* readmail.c	*/
void	timestr(char *s);			/* makemsg.c	*/
int	confirm(void);				/* msged.c	*/
void	editheader(void);			/* makemsg.c	*/
void    save(MSG message);                      /* makemsg.c    */
ADDRESS lookup(char *name, char *fn);           /* userlist.c   */

void	deletemsg(void);			/* maintmsg.c	*/
void 	newmsg(void);                   	/* makemsg.c	*/
void 	reply(void);				/* makemsg.c	*/
void 	quote(void);				/* makemsg.c	*/
void	movemsg(void);				/* maintmsg.c	*/
void	writetxt(void); 			/* textfile.c	*/
void    settings(void);                         /* settings.c   */
void    help(void);                             /* help.c       */
void    list(void);                             /* msglist.c    */
void	change(void);				/* makemsg.c	*/

/* command key scancode table */

#define DONE    0x001b          /* <ESC> done   */
#define RUBOUT  0x007f          /* DEL          */
#define SPACE   0x0020          /* space bar    */
#define ENTER   0x000d          /* <enter>      */
#define LINKTO	0x7400		/* <CTL><right> */
#define LINKFR	0x7300		/* <CTL><left>	*/
#define ROOT    0x4700          /* <Home>       */
#define LAST    0x4f00          /* <End>        */
#define AREA	0x1e00		/* <ALT><A>	*/
#define CHANGE  0x2e00          /* <ALT><C>     */
#define DELMSG  0x2000          /* <ALT><D>     */
#define NEWMSG  0x1200          /* <ALT><E>     */
#define SEARCH	0x2100		/* <ALT><F>	*/
#define GOTO    0x2200          /* <ALT><G>     */
#define HELP    0x2300          /* <ALT><H>     */
#define LIST	0x2600		/* <ALT><L>	*/
#define MOVE	0x3200		/* <ALT><M>	*/
#define OS	0x1800		/* <ALT><O>	*/
#define REPLY	0x1300		/* <ALT><R>	*/
#define QUOTE   0x1000          /* <ALT><Q>     */
#define SCAN    0x1f00          /* <ALT><S>     */
#define SET     0x1f00          /* <ALT><S>     */
#define VIEW    0x2f00          /* <ALT><V>     */
#define WRITE   0x1100          /* <ALT><W>     */
