#ifndef _COMMON_H
#define	_COMMON_H


#pragma pack(1)

#define	LEAVE	0
#define	KFS	1
#define	TFS	2
#define	DSF	3

#define	S(x) (x)?(x):"(null)"

#define MAXNAME 35
#define MAXUFLAGS 16


#define METRIC_EQUAL 0
#define METRIC_POINT 1
#define METRIC_NODE 2
#define METRIC_NET 3
#define METRIC_ZONE 4
#define METRIC_DOMAIN 5
#define METRIC_MAX METRIC_DOMAIN



/*
 * Fidonet message status bits
 */
#define	M_PVT		0x0001
#define	M_CRASH		0x0002
#define	M_RCVD		0x0004
#define	M_SENT		0x0008
#define	M_FILE		0x0010
#define	M_TRANSIT	0x0020
#define	M_ORPHAN	0x0040
#define	M_KILLSENT	0x0080
#define	M_LOCAL		0x0100
#define	M_HOLD		0x0200
#define	M_REQ		0x0800
#define	M_RRQ		0x1000
#define	M_IRR		0x2000
#define	M_AUDIT		0x4000
#define	M_FILUPD	0x8000



/*
 * Analogue Modem flag values
 */
#define NL_V22		0x00000001L
#define NL_V29		0x00000002L
#define	NL_V32		0x00000004L
#define NL_V32B		0x00000008L
#define NL_V34		0x00000010L
#define NL_V42		0x00000020L
#define NL_V42B		0x00000030L
#define NL_MNP		0x00000080L
#define NL_H96		0x00000100L
#define NL_HST		0x00000200L
#define NL_H14		0x00000300L
#define NL_H16		0x00000800L
#define NL_MAX		0x00001000L
#define NL_PEP		0x00002000L
#define NL_CSP		0x00004000L
#define NL_V32T		0x00008000L
#define NL_VFC		0x00010000L
#define NL_ZYX		0x00020000L
#define	NL_X2C		0x00040000L
#define	NL_X2S		0x00080000L
#define	NL_V90C		0x00100000L
#define	NL_V90S		0x00200000L



/*
 * ISDN Flags
 */
#define ND_V110L	0x00000001L
#define	ND_V110H	0x00000002L
#define	ND_V120L	0x00000004L
#define	ND_V120H	0x00000008L
#define	ND_X75		0x00000010L



/*
 * TCP/IP flags
 */
#define	IP_IBN		0x00000001L
#define	IP_IFC		0x00000002L
#define	IP_ITN		0x00000004L
#define	IP_IVM		0x00000008L
#define	IP_IP		0x00000010L



/*
 * Online special flags
 */
#define OL_CM		0x00000001L
#define	OL_MO		0x00000002L
#define	OL_LO		0x00000004L
#define	OL_MN		0x00000008L



/*
 * Request flags
 */
#define RQ_RQMODE	0x0000000fL 
#define RQ_RQ_BR	0x00000001L
#define RQ_RQ_BU	0x00000002L
#define RQ_RQ_WR	0x00000004L
#define RQ_RQ_WU	0x00000008L
#define RQ_XA	(RQ_RQ_BR | RQ_RQ_BU | RQ_RQ_WR | RQ_RQ_WU)
#define RQ_XB	(RQ_RQ_BR | RQ_RQ_BU | RQ_RQ_WR           )
#define RQ_XC	(RQ_RQ_BR |            RQ_RQ_WR | RQ_RQ_WU)
#define RQ_XP	(RQ_RQ_BR | RQ_RQ_BU                      )
#define RQ_XR	(RQ_RQ_BR |            RQ_RQ_WR           )
#define RQ_XW	(                      RQ_RQ_WR           )
#define RQ_XX	(                      RQ_RQ_WR | RQ_RQ_WU)



/*
 *  Returned function keys
 */
#define	KEY_BACKSPACE	8
#define	KEY_LINEFEED	10
#define KEY_ENTER	13
#define	KEY_ESCAPE	27
#define KEY_RUBOUT	127
#define	KEY_UP		200
#define	KEY_DOWN	201
#define	KEY_LEFT	202
#define	KEY_RIGHT	203
#define	KEY_HOME	204
#define	KEY_END		205
#define	KEY_INS		206
#define	KEY_DEL		207
#define	KEY_PGUP	208
#define	KEY_PGDN	209


#define	LINES		24
#define	COLS		80


/*
 * ANSI colors
 */
#define BLACK		0
#define	BLUE		1
#define	GREEN		2
#define	CYAN		3
#define	RED		4
#define	MAGENTA		5
#define	BROWN		6
#define	LIGHTGRAY	7
#define	DARKGRAY	8
#define	LIGHTBLUE	9
#define	LIGHTGREEN	10
#define	LIGHTCYAN	11
#define	LIGHTRED	12
#define	LIGHTMAGENTA	13
#define	YELLOW		14
#define	WHITE		15



typedef struct _faddr {
	char *name;
	unsigned int point;
	unsigned int node;
	unsigned int net;
	unsigned int zone;
	char *domain;
} faddr;



typedef struct _fa_list {
		struct _fa_list *next;
		faddr *addr;
} fa_list;



extern struct _ftscprod {
	unsigned short code;
	char *name;
} ftscprod[];



/*
 *  Nodelist entry
 */
typedef struct	_node {
	faddr		addr;			/* Node address		*/
	unsigned short	upnet;			/* Uplink netnumber	*/
	unsigned short  upnode;			/* Uplink nodenumber	*/
	unsigned short	region;			/* Region belongin to	*/
	unsigned char	type;
	unsigned char	pflag;
	char		*name;			/* System name		*/
	char		*location;		/* System location	*/
	char		*sysop;			/* Sysop name		*/
	char		*phone;			/* Phone number		*/
	unsigned	speed;			/* Baudrate		*/
	unsigned long	mflags;			/* Modem flags		*/
	unsigned long	dflags;			/* ISDN flags		*/
	unsigned long	iflags;			/* TCP-IP flags		*/
	unsigned long	oflags;			/* Online flags		*/
	unsigned long	xflags;			/* Request flags	*/
	char		*uflags[MAXUFLAGS];	/* User flags		*/
} node;



extern struct _fkey {
	char		*key;
	unsigned long	flag;
} fkey[];



extern struct _dkey {
	char		*key;
	unsigned long	flag;
} dkey[];



extern struct _ikey {
	char		*key;
	unsigned long	flag;
} ikey[];



extern struct _okey {
	char		*key;
	unsigned long	flag;
} okey[];



extern struct _xkey {
	char		*key;
	unsigned long	flag;
} xkey[];



extern struct _nodelist {
	char		*domain;
	FILE		*fp;
} *nodevector;



struct _ixentry {
	unsigned short	zone;
	unsigned short	net;
	unsigned short	node;
	unsigned short	point;
};



extern struct _pkey {
	char		*key;
	unsigned char	type;
	unsigned char	pflag;
} pkey[];


extern char SigName[32][16];


int	ttyfd;				/* Filedescriptor for raw mode	    */
struct	termio	tbuf, tbufsav;		/* Structure for raw mode	    */



/*
 * From attach.c
 */
int		attach(faddr, char *, int, char);



/*
 *  From clcomm.c
 */
void		InitClient(char *, char *, char *, char *, long);
void		ExitClient(int);
void		SockS(const char *, ...);
char		*SockR(const char *, ...);
void		WriteError(const char *, ...);
void		Syslog(int, const char *, ...);
void		IsDoing(const char *, ...);
void		SetTTY(char *);
void		DoNop(void);
void		Nopper(void);
void		Altime(int);
unsigned long	sequencer(void);
char		*printable(char *, int);
char		*printablec(char);



/*
 * From client.c
 */
int		socket_connect(char *, char *, char *, char *, char *);
int		socket_send(char *);
char		*socket_receive(void);
int		socket_shutdown(pid_t);



/*
 *  From crc.c
 */
unsigned long  crc32ccitt(char *, int);
unsigned short crc16ccitt(char *, int);
unsigned long  str_crc32(char *str);
unsigned long  StringCRC32(char *);
unsigned long  upd_crc32(char *buf, unsigned long crc, int len);
unsigned long  norm_crc32(unsigned long crc);
unsigned short crc16xmodem(char *, int);
unsigned char  checksum(char *, int);



/*
 * From dostran.c
 */
char		*Dos2Unix(char *);
char		*Unix2Dos(char *);



/*
 * From execute.h
 */
int		execute(char *, char *, char *, char *, char *, char *);
int		execsh(char *, char *, char *, char *);



/*
 * From faddr.c
 */
char		*aka2str(fidoaddr aka);
fidoaddr	str2aka(char *addr);



/*
 * From falists.c
 */
void		tidy_falist(fa_list **);
void		fill_list(fa_list **,char *,fa_list **);
void		fill_path(fa_list **,char *);
void		sort_list(fa_list **);
void		uniq_list(fa_list **);
int		in_list(faddr *,fa_list **, int);




/*
 * From ftn.c
 */
faddr		*parsefnode(char *);
char		*ascinode(faddr *,int);
char		*ascfnode(faddr *,int); 
void		tidy_faddr(faddr *);
int		metric(faddr *, faddr *);
faddr		*fido2faddr(fidoaddr);
fidoaddr	*faddr2fido(faddr *);
faddr		*bestaka_s(faddr *);
int		is_local(faddr *);



/*
 * From getheader.c
 */
int		getheader(faddr *, faddr *, FILE *, char *);



/*
 * From gmtoffset.c
 */
long		gmt_offset(time_t);
char		*gmtoffset(time_t);
char		*str_time(time_t);
char		*t_elapsed(time_t, time_t);


/*
 * From mbfile.c
 */
int		file_cp(char *from, char *to);
int		file_rm(char *path);
int		file_mv(char *oldpath, char *newpath);
int		file_exist(char *path, int mode);
long		file_size(char *path);
long		file_crc(char *path);
time_t		file_time(char *path);



/*
 * From mkdirs.c
 */
int		mkdirs(char *name);



/*
 * From nodelist.c
 */
int		initnl(void);
node		*getnlent(faddr *);
void		olflags(unsigned long);
void		rqflags(unsigned long);
void		moflags(unsigned long);
void		diflags(unsigned long);
void		ipflags(unsigned long);



/*
 * From nodelock.c
 */
int		nodelock(faddr *);
int		nodeulock(faddr *);


/*
 * From noderecord.c
 */
int		noderecord(faddr *);



/*
 * From pktname.c
 */
char		*prepbuf(faddr *);	
char		*pktname(faddr *, char);
char		*reqname(faddr *);
char		*floname(faddr *, char);
char		*splname(faddr *);
char		*bsyname(faddr *);
char		*stsname(faddr *);
char		*polname(faddr *);
char		*dayname(void);
char		*arcname(faddr *, unsigned short, int);



/*
 * From rawio.c
 */
void		Setraw(void);			/* Set raw mode		    */
void		Unsetraw(void);			/* Unset raw mode	    */
unsigned char	Getone(void);			/* Get one raw character    */
int		Speed(void);			/* Get (locked) tty speed   */
int		Waitchar(unsigned char *, int);	/* Wait n * 10mSec for char */
int		Escapechar(unsigned char *);	/* Escape sequence test	    */
unsigned char	Readkey(void);			/* Read a translated key    */



/*
 *  from semafore.c
 */
void		CreateSema(char *);
void		RemoveSema(char *);
int 		IsSema(char *);



/*
 * From strutil.c
 */
char		*xmalloc(size_t);
char		*xstrcpy(char *);
char		*xstrcat(char *, char *);
char		*padleft(char *str, int size, char pad);
char		*tl(char *str);
void		Striplf(char *String);
void		tlf(char *str);
char		*tu(char *str);
char		*tlcap(char *);
char		*Hilite(char *, char *);
void		Addunderscore(char *);
void		strreplace(char *, char *, char*);
char		*GetLocalHM(void); 
char		*StrTimeHM(time_t);
char		*StrTimeHMS(time_t);
char		*GetLocalHMS(void);
char		*StrDateMDY(time_t *);
char		*StrDateDMY(time_t);
char		*GetDateDMY(void);



/*
 * From term.c
 */
void		TermInit(int);
void		Enter(int);
void		pout(int, int, char *);
void		poutCR(int, int, char *);
void		poutCenter(int,int,char *);
void		colour(int, int);
void		Center(char *);
void		clear(void);
void		locate(int, int);
void		fLine(int);
void		sLine(void);
void		mvprintw(int, int, const char *, ...);



/*
 * From unpacker.c
 */
char		*unpacker(char *);
int 		getarchiver(char *);



#endif

