/*
 *  MBTOOL.H -  Definitions for MBX Tool.
 */


#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
#include <malloc.h>

#ifndef O_BINARY
#define O_BINARY 0
#endif

typedef unsigned char  byte;
typedef unsigned short word;

/*
 *  Sizes of things.
 */

#define ln_call  6  /* # chars in a callsign	*/
#define ln_callp 9  /* # chars in callsign+ssid */
#define ln_date  6  /* # chars in date		*/
#define ln_time  4  /* # chars in time		*/
#define maxflds  16

/*
 *  MailBox includes.
 */


/*
 *  Cosmetics.
 */

#define is   ==
#define isnt !=
#define and  &&
#define or   ||
#define not   !

#define setbit	|=
#define clrbit	&= ~
#define flipbit ^=

#define true   1
#define false  0
#define match  !strcmp
#define matchn !strncmp

#define pmode 438  /* Permissions for creat() */
#define TRUE   1
#define FALSE  0

/*
 *  Some character values.
 */

#define ctl_c  '\03'
#define ctl_g  '\07'
#define ctl_v  '\026'
#define ctl_w  '\027'
#define cpmeof '\032'
#define del	0x7f

#define cmdlen	 80
#define linelen 256

/*
 *  System parameters.
 */

#define s_page	   0x0001   /* Page sysop ?		       */
#define s_files    0x0002   /* Messages in files?	       */
#define s_mchange  0x0004   /* Mail file changed?	       */
#define s_kill	   0x0008   /* Kill all msgs after forward     */
#define s_fkill    0x0010   /* Kill F msgs after forward       */
#define s_svc	   0x0020   /* Generate service message on KT  */
#define s_log_on   0x0040   /* Logging enabled		       */
#define s_log_gate 0x0080   /* Log gateway events	       */
#define s_log_file 0x0100   /* Log file events		       */
#define s_log_msg  0x0200   /* Log message events	       */
#define s_log_loc  0x0400   /* Log stuff happens local console */
#define s_nobbs    0x0800   /* Remove @ BBS if @ SYSOP	       */
#define s_cmd	   0x1000   /* Delayed remote sysop cmd to do  */
#define s_edtfc    0x2000   /* Edit traffic command enabled?   */
#define s_dd	   0x4000   /* DoubleDOS active, give slice    */


#define mb_version 7

/*
 *  Prompts associated with message commands.
 */

#define num_mm 19

/*
 *  Message header record.
 *  メッセージ毎に１個作られる
 */

#define m_read	 0x01  /* Message has been read by recipient	*/
#define m_fwd	 0x02  /* Message has been forwarded		*/
#define m_chk	 0x04  /* Forwarding has tried this message	*/
#define m_known  0x08  /* Message destination known locally	*/
#define m_unk	 0x10  /* Message destination not known by wp	*/

#define mhtitl 80      /* Length of title. */

typedef struct msg_hdr_s
{
  word next;	      /* Next message header record			 */
  word prev;	      /* Previous message header record 		 */
  word ext;	      /* Header extension record, or zero		 */
  word number;	      /* Message number 				 */
  word size;	      /* Size in bytes					 */
  char type;	      /* Message type					 */
  byte stat;	      /* Message status, 上にビットの意味があります	 */
  char to  [ln_call]; /* Destination call				 */
  char from[ln_call]; /* Originator call				 */
  char date[ln_date]; /* Entry date					 */
  char time[ln_time]; /* Entry time					 */
  word rn;	      /* Record number of this record			 */
  word read;	      /* # times the message has been read		 */
  word unu3;	      /* Available					 */
  char bbs [ln_call]; /* Destination BBS, or distribution list		 */
  word unu2;	      /* BID/H-ADDR record, or zero			 */
  char title[mhtitl];
} MSG_HDR;

/*
 *  Message header extension item.
 *  ＠ＢＢＳで複数局に転送場合に使われます。MSG_HDRで指定されるレコード。
 */

#define mmesn 17             /* Max calls in distribution list */

typedef struct msg_ext_s
{
  word next;		     /* Next extension item, or free item   */
  char call[mmesn][ln_call]; /* Calls to send it to		    */
  byte flag[mmesn];	     /* TRUE if need to send, FALSE if sent */
  byte count;		     /* Number of calls in list 	    */
  word unu1;
  word unu2;
  word unu3;
} MSG_EXT;

/*
 *  Mail file header record.
 *  MAIL.DATを先頭に１個だけあり全メールを管理するレコード
 */

typedef struct mail_hdr_s
{
  word next;	      /* Next record to allocate	 */
  word first;	      /* First message header record	 */
  word last;	      /* Last message header record	 */
  word ffree;	      /* First record in free chain	 */
  word lfree;	      /* Last record in free chain	 */
  word next_msg;      /* Next message number		 */
  byte version;       /* File format version number	 */
  word free;	      /* Number of records in free chain */
  word count;	      /* Number of messages		 */
  char date[ln_date]; /* Date of last untangle		 */
  char time[ln_time]; /* Time of last untangle		 */
  word unt_msg;       /* next_msg at last untangle	 */
  char unused[99];
} MAIL_HDR;

extern MAIL_HDR *mfhs;
extern MSG_HDR	*tmmhs;
extern MSG_EXT	*mmes;


#define xalloc(type,size) ((type *)malloc((unsigned)(sizeof(type)*(size))))

/*　メッセージのＢＩＤかＨアドレスを保存するMAIL.DATのレコード　*/

#define BIDSIZ		12
#define RECSIZE		128

typedef struct msg_bid
{
  word next;
  char bid[BIDSIZ + 5];
  char domain[RECSIZE - sizeof(word) - (BIDSIZ + 5)];
} MSG_BID;

#ifdef MBBID
char  bidname[BIDSIZ + 1];
MSG_BID *msgbid;
char   showbid[BUFSIZ];
extern char   *noneed;
#else
extern char bidname[];
extern MSG_BID *msgbid;
extern char showbid[];
extern char   *noneed;
#endif


