/*
    Pirate Bulletin Board System
    Copyright (C) 1990, Edward Luke

    This program 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 1, or (at your option)
    any later version.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


    Email address:
    lush@Athena.EE.MsState.EDU
*/
#ifndef NULL

#include <stdio.h>
#include <sgtty.h>
#include <setjmp.h>
#include <strings.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/dir.h>
#include <errno.h>

#ifdef XINU
extern int errno ;
#endif

#endif

#define YEA (1)        /* Booleans  (Yep, for true and false) */
#define NA  (0) 

char *strdup() ;       /* External function declarations */
char *bfile() ;

extern FILE *ufp ;     /* External variable declarations */
extern long ti ;

#define STRLEN   80    /* Length of most string data */
#define PASSLEN  14    /* Length of encrypted passwd field */
#define MAXUSERS 780   /* Maximum number of users,780 makes file structures 1k*/
                       /* The actual maximum number of users is 2*MAXUSERS    */
#define MAXACTIVE 36   /* Maximum number of users active at one time */
#define MAXBOARD 80    /* Size of board cache */


#define PASSFILE ".PASSWDS"      /* Name of file User records stored in */
#define FLUSH    ".PASSFLUSH"    /* Stores date for user cache flushing */
#define BOARDS   ".BOARDS"       /* File containing list of boards */
#define DIR      "/.DIR"         /* Name of Directory file info */
#define ULIST    ".UTMP"         /* Names of users currently on line */
#define NAMEFILE "BoardName"     /* File containing site name of bbs */
#define EDITOR   "ve"            /* Name of the editor */
#define QUIT 0x666               /* Return value to abort recursive functions */

#define FILE_READ  0x1        /* Ownership flags used in fileheader structure */
#define FILE_OWND  0x2        /* accessed array */
#define FILE_VISIT 0x4
#define FILE_SPARE 0x8

#define SHIFTMODE(usernum,mode) ((usernum<MAXUSERS)?mode:mode<<4)

#define SETFILEMODE(array,usernum,mode) \
     (array[usernum%MAXUSERS] |= ((usernum<MAXUSERS)?mode:mode<<4))

#define CLRFILEMODE(array,usernum,mode) \
     (array[usernum%MAXUSERS] &= ((usernum<MAXUSERS)?~mode:~(mode<<4)))

#define CHECKFILEMODE(array,usernum,mode) \
     (array[usernum%MAXUSERS] & ((usernum<MAXUSERS)?mode:mode<<4))


#define USERIDSIZE (16)
#define USERNAMESZ (24)
#define TERMTYPESZ (10)

struct user {                  /* Structure used to hold information in */
	char userid[USERIDSIZE] ;    /* PASSFILE */
	char passwd[PASSLEN] ;
	char username[USERNAMESZ] ;
	char termtype[TERMTYPESZ] ;
	int userlevel ;
	time_t lastlogin ;
	int protocol ;
} ;

struct commands {              /* Structure used to hold descriptions of */
	char *cmdname ;            /* menu commands for docmd function.      */
	int (*cmdfunc)() ;         /* Usually this info is stored in an array*/
	int level ;                /* that is initialized at compile time.   */
	char *nextcmd ;            /* this field tells where to go after this one */
	char *errorcmd ;           /* this field tells where if error occurs      */
} ;


struct fileheader {             /* This structure is used to hold data in */
	char filename[STRLEN] ;     /* the BOARDS and DIR files               */
	char owner[STRLEN] ;
	char title[STRLEN] ;
	unsigned char accessed[MAXUSERS] ;
	int numread ;
} ;


struct shortfile {               /* used for caching files and boards */
    char filename[STRLEN] ;
    char owner[STRLEN] ;
    char title[STRLEN] ;
} ;

struct user_info {               /* Structure used in UTMP file */
	int active ;             /* When allocated this field is true */
    int uid ;                /* Used to find user name entry in passwd file */
	int pid ;                /* kill() is used to notify user of talk request*/
	int invisible ;          /* Used by cloaking function in Xyz menu */
	int sockactive ;         /* Used to coordinate talk requests */
	int sockaddr ;           /* ... */
	int destuid ;            /* talk uses this to identify who called */
	int mode ;               /* UL/DL, Talk Mode, Chat Mode, ... */
    char from[STRLEN] ;      /* machine name the user called in from */
} ;

struct one_key {                  /* Used to pass commands to the readmenu */
	int key ;
	int (*fptr)() ;
} ;

#define IDLE    0                 /* Modes a user can be in */
#define ULDL    1                 /* see mode in struct user_info */
#define TALK    2
#define NEW     3
#define CHAT    4
#define READNEW 5
#define POSTING 6
#define MAIL    7
#define IRC     8

#define DONOTHING  0              /* read menu command return states */
#define FULLUPDATE 1              /* Entire screen was destroyed in this oper*/
#define PARTUPDATE 2              /* Only the top three lines were destroyed */
#define DOQUIT     4              /* Exit read menu was executed */
#define NEWDIRECT  8              /* Directory has changed, re-read files */


#define SCREEN_SIZE (23)         /* Used by read menu  */

extern struct user currentuser ;  /*  user structure is loaded from passwd */
                                  /*  file at logon, and remains for the   */
                                  /*  entire session */

extern int usernum ;      /* Index into passwds file user record */
extern int utmpent ;      /* Index into this users utmp file entry */

extern struct user lookupuser ; /* Used when searching for other user info */

extern char currboard[STRLEN] ; /* name of currently selected board */
extern int selboard ;           /* THis flag is true if above is active */

extern char genbuf[4096] ;      /* generally used global buffer */

extern struct commands cmdlist[] ; /* main menu command list */

extern jmp_buf byebye ;        /* Used for exception condition like I/O error*/

extern struct commands xyzlist[] ;   /* These are command lists for all the */
extern struct commands talklist[] ;  /* sub-menus */
extern struct commands filelist[] ;
extern struct commands maillist[] ;
extern struct commands dellist[] ;
extern struct commands maintlist[] ;

extern char save_title[] ;    /* These are used by the editor when inserting */
extern char save_filename[] ; /* header information */
extern int in_mail ;

extern int dumb_term ;

#define MIN(a,b) ((a<b)?a:b)
#define MAX(a,b) ((a>b)?a:b)
