#ifndef RECCOON_SHARED_H
#define RECCOON_SHARED_H TRUE
/*
**  $Filename: Reccoon/Shared.h $
**  $Release: 0.59 $
**  $Revision: 59.3 $
**  $Date: 94/12/24 $
**
**  Reccoon shared data structures.
**
**  (C) Copyright 1991-94 Niclas Emdelius.
**          All Rights Reserved
*/

#ifndef RECCOON_RECCOON_H
#include <Reccoon/Reccoon2.h>
#endif

#ifndef RECCOON_MSGBASE_H
#include <Reccoon/MsgBase.h>
#endif

#ifndef RECCOON_FILEBASE_H
#include <Reccoon/FileBase.h>
#endif

#ifndef RECCOON_USER_H
#include <Reccoon/User.h>
#endif

#ifndef EXEC_PORTS_H
#include <exec/ports.h>
#endif

#ifndef EXEC_TYPES_H
#include <exec/types.h>
#endif

struct CtrlFlags
{
    unsigned int        wantchat   :1;  // 1=User want to chat
    unsigned int        useronline :1;  // 1=User online
    unsigned int        running    :1;  // 1=Line operational
    unsigned int        online     :1;  // 1=Connected
    unsigned int        chatmode   :1;  // 1=Chat engaged
    unsigned int        download   :1;  // Downloading file
    unsigned int        upload     :1;  // Uploading file
    unsigned int        unused     :25;
};

//** THE structure. This one keeps track of all the other
//** structures in the system, using linked lists.
//**
//** If you want to get your hands on this structure, you must use
//** reccoon.library's OpenRCtrl(), or send a CTRL_OPEN followed
//** by a CTRL_GETPTR to ReccControl.
//** OpenRCtrl() is the easiest way to do it.

struct control
{
    struct  Screen *    screen;         // ReccControl's Screen
    struct  Task *      task;           // ReccControl's task
    UWORD           signal;         // Signal for screen update
     WORD           nextevent;      // Minutes left before a event
    UBYTE           eventtype;      // Unused
    UBYTE           lockeduser;     // Unused

    struct  fig *       fig;            // Shared config
    UBYTE *         localtable;     // Local xlate table
    struct  KeyMap *    keymap;         // Local keymap

    struct  ReccNode *  reccnodes;      // Linked list of ReccNodes
    struct  MsgList *   msgareas;       // Linked list of MsgAreas
    struct  FileList *  fileareas;      // Linked list of FileAreas
    struct  UsrList *   users;          // Linked list of loaded users
    struct  StringList *    bstrings;       // Linked list of menusets

    ULONG           nrmsgareas;     // Number of msgareas
    ULONG           nrfileareas;        // Number of fileareas

    UWORD           version;        // Reccoon version.revision
    UWORD           revision;

    UBYTE           nrcomputers;        // Number of elements in computer array
    UBYTE           (*computers)[30];   // Available computer types

    struct  RcnData *   rcndata;        // Some other data
    BOOL            rcndatachanged;     // Data in rcndata changed, save on next SaveFig()

    struct  RList       msgphore;       // Linked list of Remaphores, msgarea-files. (unused)
    struct  RList       filephore;      // Linked list of Remaphores, filearea-files. (unused)
    struct  RList       userphore;      // Linked list of Remaphores, user structures. (unused)

    BOOL            figchanged;     // ctrl->fig structure changed, save on next SaveFig()

    struct  RList       readtables;     // Linked list of LookupTableNodes for chr-translation
    struct  RList       writetables;        // Linked list of LookupTableNodes for chr-translation
};

//** Named RData:OtherData.DAT on the disk.

struct RcnData
{
    UBYTE       laston[36];     // Last user online
    ULONG       calls;          // Total number of calls
    ULONG       byteul;         // Total number of kb uploaded
    ULONG       fileul;         // Total number of files uploaded
    ULONG       bytedl;         // Total number of kb downloaded
    ULONG       filedl;         // Total number of files downloaded
    ULONG       logons;         // Total number of logons
    struct TimeDate created;        // File-creation time (unused)
    ULONG       msgs;           // Total number of messages written

    ULONG       byteultoday;        // Bytes uploaded today;
    ULONG       bytedltoday;        // Bytes downloaded today;
    ULONG       fileultoday;        // Files uploaded today;
    ULONG       filedltoday;        // Files downloaded today;
    ULONG       msgstoday;      // Messages written today
    ULONG       callstoday;     // Calls today
    ULONG       logonstoday;        // Logons today
    ULONG       mimptoday;      // Msgs imported today
    ULONG       mexptoday;      // Msgs exported today
    ULONG       fimptoday;      // Files imported today
    ULONG       fexptoday;      // Files exported today
    struct TimeDate lastsaved;      // Last time this structure was saved
    ULONG       pages;          // How many times have somebody yelled at you today?

   /* Something else I should keep track on? Tell me! */

};

//** General configuration
//** This info is never altered by Rcn, just by Config.

struct fig
{

    UBYTE       txtpath[PATHSIZE];  // Path to .txt files
    UBYTE       menupath[PATHSIZE];     // Path to menufiles
    UBYTE       filpath[PATHSIZE];  // Path to FileBasex.DAT files
    UBYTE       m_path[PATHSIZE];   // Path to messages
    UBYTE       wmailpath[PATHSIZE];    // Path to waiting mail files
    UBYTE       tmppath[PATHSIZE];  // Path to tmp uploads

    UBYTE       sysopname[22];      // Sysop's name on the BBS
    UBYTE       font[36];       // Font name
    UBYTE       keymap[10];     // Name of keymap
    UBYTE       macros[20][60];     // F-key Macros

    UBYTE       usered[PATHSIZE+18];    // Name and path to usered door
    UBYTE       remotedos[PATHSIZE+18]; // Name and path to dos door (Obsolete)
    UBYTE       fsed[PATHSIZE+18];  // Name and path to fseditor door

    UBYTE       xpr_libname[26][26];    // XPR libname [0]=A [1]=B etc
    UBYTE       xpr_protname[26][30];   // Name of each protocol
    UBYTE       xpr_options[26][80];    // XPR options for each library
    UWORD       xpr_minaccess[26];  // Minimum access level required

    UWORD       fido_zone[MAXAKAS]; // Zone(s)
    UWORD       fido_net[MAXAKAS];  // Net(s)
    UWORD       fido_node[MAXAKAS]; // Node(s)
    UWORD       fido_point[MAXAKAS];    // Point(s)
    UBYTE       fido_deforigin[80]; // Default origin string
    UBYTE       fido_outdir[PATHSIZE];  // Outbound directory
    UBYTE       fido_indir[PATHSIZE];   // Inbound directory

    UWORD       defmore;        // Default value for screen length
    UWORD       idleout;        // Inactivity timer
     LONG       user_freebytes;     // Newuser Free bytes
     WORD       user_freefiles;     // Newuser free files
    UWORD       user_credits;       // Newuser Network credits
    UWORD       user_startaccess;   // Newuser accesslevel
    UWORD       user_maxusers;      // Maximum number of users
    UWORD       user_startmsgarea;  // Start message area
    UWORD       user_startmsgsig;   // Start MsgSIG
    UWORD       user_startfilearea; // Start filearea
    UWORD       user_startfilesig;  // Start FileSIG

    UWORD       localxlate;     // Local screen xlate
    BYTE        ctrlpri;        // ReccControl priority

    UBYTE       scrautoscroll;      // Screen autoscroll (boolean)
    UWORD       scrdepth;       // Screen depth
    UWORD       scrscantype;        // Screen overscantype
    ULONG       scrwidth;       // Screen width
    ULONG       scrheight;      // Screen height
    ULONG       scrid;          // Screen resolution
    UWORD       scrcolors[16];      // Screen colors (12 bit only)

    struct {
        unsigned int        securecheck     :1; // Shall we do a securitycheck once in a while?
        unsigned int        askareaul       :1; // Should we ask user for destination area when UL'ing?
        unsigned int        notifypwfailure :1; // Notify user if password OR securitycheck fails
        unsigned int        stopultime      :1; // Should we stop the time during uploads?
        unsigned int        no16color       :1; // If this is set, reccoon will NOT allow 16 color
                                                // ansi, even IF you have a 16 (or more) color screen.
        unsigned int        use_xem         :1; // Use eXternal EMulation library for display
        unsigned int        menufy          :1; // Menufy the nodes instead of Iconify
        unsigned int        unusedbits      :25;// Unused bits
    } bits;

    ULONG       pwarea;             // Area for Security messages
    UBYTE       secureprop;         // Propability for securitycheck

    UWORD       lastversion;        // Version of ReccControl that saved
                                    // this structure on disk.

    UWORD       secureaccess;       // Access to give a user when failing security check
    UBYTE       logpath[50];        // Where to store all ReccNode logs
    UBYTE       bbsname[30];        // Name of BBS              (future)
    UBYTE       bbsplace[30];       // Where the BBS is located (future)
    UBYTE       bbsphone[30];       // Telephone nr to bbs      (future)
    UBYTE       xemlib[30];         // eXternal EMulation library
};

/* Reccoon-Line structure */

struct ReccNode
{

    struct  ReccNode *  prev;           // A double linked list
    struct  ReccNode *  next;
    UBYTE           message[80];        // RCtrl message
    struct  CtrlFlags   flags;          // Flags
    struct  usr *       usr;            // User currently online
    UWORD           usernum;        // User number
     WORD           time;           // Remaining time (minutes)
    UWORD           usedtime;       // Usr's used minutes (this day)
    UWORD           line;           // Line number
    UWORD           ondisk;         // Unused

    struct InternalData *internaldata;      // Internal Reccoon-data, for doors etc.
    struct DoorCallback *doorcallback;      // Door callback functions

/* Data from here is loaded from disk */

    ULONG           commbaud;       // Commandbaudrate (initstrings etc)
    UWORD           serunit;        // Serial unit
    struct {
        unsigned int        ctsrts    :1;   // CTS/RTS handshaking
        unsigned int        locked    :1;   // If 1, baud always commbaud
        unsigned int        idle      :1;   // 1 = No idleout
        unsigned int        local     :1;   // Local only line
        unsigned int        ownscreen :1;   // Force ownscreen?
        unsigned int        winfront  :1;   // Window to front?
        unsigned int        winactivate :1; // Activate window?
        unsigned int        scrfront  :1;   // Screen to front?
        unsigned int        pubscreen :1;   // Should it be public? (only if ownscreen selected)
        unsigned int        unusedbits :23;
    } bits;

    UBYTE           logname[30];        // Name of this nodes logfile

    UBYTE           sername[30];        // serial.device
    UBYTE           initstring[80];     // Modem initstring

    UBYTE           mod_ok[16];     // Modem result OK
    UBYTE           mod_ring[16];       // Modem result RING
    UBYTE           mod_nocarrier[16];  // Modem result NO CARRIER
    UBYTE           mod_connect[16];    // Modem result CONNECT
    UBYTE           mod_nodialtone[16]; // Modem result NO DIALTONE
    UBYTE           mod_busy[16];       // Modem result BUSY

    UBYTE           mod_ata;        // Send ATA after X rings
     BYTE           normpri;        // Normal priority
     BYTE           ulpri;          // Upload priority
     BYTE           dlpri;          // Download priority
     BYTE           msgportpri;     // MsgPorts priority
     BYTE           serpri;         // serial.device priority
     BYTE           conpri;         // console.device priority
    UBYTE           password[40];       // System password
    UBYTE           nothing;
    ULONG           minbaud;        // Minimum allowed baudrate (future)


    UWORD           leftedge;       // Window definitions
    UWORD           topedge;
    UWORD           width;
    UWORD           height;
    WORD            zoomdef[4];     // Zoomed window definitions
    UBYTE           pubscr[30];

};

/* Memory-list of all message-areas */

struct MsgList
{
    struct  MsgList *   prev;           // A double linked list
    struct  MsgList *   next;
    ULONG           areanum;        // Area number
    UWORD           changes;        // >=1 - Save this area to disk when the CTRL_SAVEFIG command is recieved.
    UWORD           ondisk;         // Unused
    struct  MsgArea     msgarea;        // Data
};

/* Memory-list of all file-areas */

struct FileList
{
    struct  FileList *  prev;           // A double linked list
    struct  FileList *  next;
    ULONG           areanum;        // Area number
    UWORD           changes;        // >=1 - Save this area to disk when the CTRL_SAVEFIG command is recieved.
    UWORD           ondisk;         // Unused
    struct  FileArea    filearea;       // Data
};

/* Memory-list of all users currently loaded */

struct UsrList
{
    struct  UsrList *   prev;           // A double linked list
    struct  UsrList *   next;
    UWORD           usernum;        // User number
    UWORD           usecount;       // Usecount
    struct  MsgAccess *     msgaccess;      // One structure per msgarea
    struct  FileAccess *    fileaccess;     // One structure per filearea
    ULONG *         lastread;       // 4 bytes per msgarea
    struct  usr *       usr;            // User Data
    struct AppInfo *    appinfo;        // Linked list of application info
};

/*
** Applications are now allowed to insert it's own datafields
** in the user-structure, maintaned by ReccControl. That means that
** if you for example have written a timebank, you don't have to
** create your own datafiles for all users, you just
** GetAppInfo(usernum,key) and you'll receive a pointer to your own
** userdata. (OOOH I like it!)
*/

struct AppInfo
{
    struct AppInfo *    prev;       // Double linked list
    struct AppInfo *    next;
    ULONG           key;        // AppInfo-key
    ULONG           size;       // Size of data
    UWORD           usecount;   // Usage counter
    UWORD           apppos;     // Offset in AppInfo.DAT (not really)
    BOOL            changed;    // Data changed? (Need to be saved)
    void *          data;       // Application data
};

/* List of all menu-set-strings currently loaded */

struct StringList
{
    struct  StringList *    prev;           // A double linked list
    struct  StringList *    next;
    UWORD           mset;           // Menuset number
    UWORD           usecount;
    ULONG           bstringstart;       // Start of strings in memory
    ULONG           bstringlength;      // nr of allocated bytes
    UBYTE *         bstrings[STR_MAXSTRINGS]; // An array of pointers to the strings
};

/* List of lookup-tables for FTSC-0054 character-translation */

struct LookupTableNodes
{
    struct LookupTableNodes *next;
    struct LookupTableNodes *prev;
    UBYTE           name[8];    // Sourcetable-name, not 0-termintaed

    struct RList        lookuptables;   // Dest tables, (struct LookupTableNode)
};

struct LookupTableNode
{
    struct LookupTableNode *next;
    struct LookupTableNode *prev;
    struct LookupTable  table;  // Loaded from disk.
};

/*
**   This is a message ment to be sent to a MsgPort
**   named 'ReccControl-Port'. If you wish, you can use the
**   Recclib functions instead.
*/

struct CtrlMessage
{
    struct  Message Ctrl_Message;
    UBYTE       line;       // Unused. Set to 0xff.
    UWORD       command;    // Se CTRL_ defines below
    ULONG       data;       // Arguments
    ULONG       data2;
    ULONG       data3;
    ULONG       data4;
    ULONG       data5;
    ULONG       data6;
    ULONG       retvalue;   // Return value
    UBYTE       unused[20];
};

/*** CtrlMessage command codes ***/

#define CTRL_NOP    0   // No operation
#define CTRL_GETPTR     1   // Get pointer to shared datastructure
#define CTRL_SAVEFIG    2   // Save fig structure
#define CTRL_OPENCTRL   3   // You MUST use this before anything else!!!
#define CTRL_CLOSECTRL  4   // You MUST use this one too!

#define CTRL_OPENMENUSET 5  // Open a menuset
#define CTRL_CLOSEMENUSET 6 // Close a menuset
#define CTRL_RELOADMSETS 7  // Reload menusets from disk

#define CTRL_UPDATE 8   // Update ReccControl's window

#define CTRL_INDEXUSER  9   // ReIndex userlist
#define CTRL_OPENUSERLIST 10    // unused
#define CTRL_CLOSEUSERLIST 11   // unused
#define CTRL_GETUSER    12  // Load an user from disk.
#define CTRL_FREEUSER   13  // Remove user from memory
#define CTRL_SEARCHUSER 14  // Search userlist for an user
#define CTRL_SAVEUSER   15  // Save user back to disk
#define CTRL_SLOADEDUSRNUM 16   // Search among loaded users, by ID
#define CTRL_SLOADEDUSRNAME 17  // Search among loaded users, by name
#define CTRL_ADDUSER    18  // Add an user to the userlist

#define CTRL_FLUSHFIG 19    // Remove config from memory (if unused)
#define CTRL_EXPUNGE 20     // PRIVATE!!

#define CTRL_KEYMAP 21      // Reload the keymap

#define CTRL_OPENSCREEN 22  // Open ReccControl's screen (if not open)
#define CTRL_CLOSESCREEN 23 // Close ReccControl's screen (if unused)

#define CTRL_QUIT 24        // Shutdown ReccControl

#define CTRL_LOCK 25        // Lock ReccControl
#define CTRL_XLATE 26       // Reload local translationtable

#define CTRL_GETAPPINFO 27  // Get application-info
#define CTRL_FREEAPPINFO 28 // Free application-info
#define CTRL_SAVEAPPINFO 29 // Save application-info
#define CTRL_ADDAPPINFO 30  // Add application-info

#define CTRL_SETNODESTATUS 31   // Set textstring for a rcn-node
#define CTRL_UPDATENODE 32  // Update rctrl-data for a node
#define CTRL_UPDATEDATA 33  // Update global rctrl-data
#define CTRL_ADDHISTORY 34  // Add line to node-history
#define CTRL_FLUSHHISTORY 35    // Flush node's history-buffer



    /*********************************************************/
    /********* IGNORE EVERYTHING BELOW THIS POINT! ***********/
    /*********************************************************/


/*
** This structure is used when a program wants access to a data-file on
** the disk. They are stored in double linked lists, with its head-pointer
** in the control structure. You shouldn't use those lists if you don't
** know exactly what you're doing!! Reccoon.library have some functions
** that will do the trick, and if you miss something, tell me instead of
** making your own!!
**
** The way I handle these Remaphores is rather strange, and it's really
** important they are used properly.
**
** (Amen!)
*/

/* Remaphores are currently unused -- ignore! */

struct Remaphore
{
    struct RNode    node;       // A double linked list
    struct Task *   task;       // Task waiting for access
    ULONG       data;       // Data, like File or Msg area number
    BYTE        signal;     // Signal this bit when access granted
    UBYTE       exclusive;  // Bit0: Want exclusive access. Bit1: Access granted.
};

#endif
