/* Discuss meeting interface.

   TODO:
   Infinite amounts of cleanup.
   Make C++ kludges for C library routines cleaner.  (Find header files
   to use?)
   Think harder about interfaces.
   USP-based meeting interface.
   Find a decent place to hide the magic numbers.
 */

#define __eprintf	__hide___eprintf
#define strcpy		__hide_strcpy
#define com_err		__hide_com_err
#define sprintf		__hide_sprintf
#define fprintf		__hide_fprintf

extern "C" {
#include <stdio.h>
#include <assert.h>
#include <pwd.h>
#include <string.h>
#include <com_err.h>
}

#undef __eprintf
#undef strcpy
#undef com_err
#undef sprintf
#undef fprintf

#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <discuss/discuss.h>

#define MTG_SUPER_1		1
#define MTG_SUPER_UNIQUE	100866
#define CHAIN_BLK_1		1
#define CHAIN_BLK_UNIQUE	102966
#define TRN_BASE_1		1
#define TRN_BASE_UNIQUE		070476
#define TRN_HDR_1		1
#define TRN_HDR_UNIQUE		102463
#define CB_DELETED		1

extern "C" {
  int open (const char *, int, ...);
  int close (int);
  int stat (char *, struct stat *);
  int lstat (char *, struct stat *);
  int fstat (int, struct stat *);
  int flock (int, int);
  long lseek (int, long, int);
  int writev (int, struct iovec *, int);
  int umask (int);

  int sprintf (char *, const char *, ...);
  int fprintf (FILE *, const char *, ...);

  char *strcpy (char *, const char *);
  void com_err (const char *, unsigned long, ...);
  void __eprintf (const char *, ...);
}

typedef /*unsigned*/ long ErrorCode;
typedef mtg_info DiscussMeetingInfo;

class Transaction {
  int valid : 1;
  int user_writeable : 1;
 public:
  Transaction (const char *text, unsigned int len,
	       const char *author, const char *signature,
	       ...);
};

class DiscussMeeting {
protected:
  DiscussMeeting () {}
public:
  virtual ErrorCode open () = 0;
  virtual ErrorCode close () = 0;
  virtual ErrorCode add_transaction (Transaction *&) = 0; // includes reply
  virtual Transaction operator[] (unsigned long) = 0;
  virtual ErrorCode expunge () = 0;
  // public create operation is in subclass only
  virtual ErrorCode destroy () = 0;
  virtual ErrorCode status (DiscussMeetingInfo &) = 0;
  virtual ErrorCode updated (time_t date, int last_trn, int& updated) {
    DiscussMeetingInfo info;
    ErrorCode code = status (info);
    if (code)
      return code;
    updated = (date < info.date_modified
	       || !(last_trn >= info.last
		    && last_trn <= info.highest));
    return 0;
  }
};

class DiscussUSPMeeting : public DiscussMeeting {
  /* ... */
 public:
  virtual foo ();
};

struct DiscussFileSuperblock {
  int version;
  int unique;
  int first, last, lowest, highest, highest_chain;
  time_t created, modified;

  int long_name_addr, chairman_addr;
  short long_name_len, chairman_len;
  short flags;
  int chain_start, high_water, trn_fsize, highest_trn_addr;
};

struct DiscussFileTrnBase {
  int version;
  int unique;
  time_t created;
  int long_name_addr, chairman_addr;
  short long_name_len, chairman_len;
  short is_public;
};

struct DiscussFileTrnHeader {
  int version;
  int unique;
  int current, orig_pref;
  time_t date_entered;
  int n_lines, n_chars;
  int prev_trn_addr, subject_addr, author_addr, text_addr;
  short subject_len, author_len;
};

struct DiscussFileChainBlk {
  /* info for transaction N */
  int version;
  int unique;
  int current, prev, next, pref, nref, trn_chain;
  int trn_addr;
  short flags, mbz;

  /* info for chain N */
  int chain_fref, chain_lref;

  void print (FILE *f);
  int deleted () { return flags & CB_DELETED; }
  int set_deleted (int newval) {
    if (newval)
      flags |= CB_DELETED;
    else if (trn_addr == 0)
      return 1;
    else
      flags &= ~CB_DELETED;
    return 0;
  }
  void expunge () { trn_addr = 0; }
};

class DiscussFileMeeting : public DiscussMeeting {
protected:
  static ErrorCode read_from (int fd, long offset, void *ptr, unsigned len);
  static ErrorCode write_to (int fd, long offset, void *ptr, unsigned len);
  static ErrorCode rename (const char *dir,
			   const char *old_name, const char *new_name);
};

class DiscussCachedMeeting : public DiscussFileMeeting {
  char *pathname;
  int pathlen;
  char *mtg_name, *chairman;
  int ctl_fd;
  DiscussFileSuperblock ctl_super;
  DiscussFileTrnBase trn_base;
  DiscussFileChainBlk *chain_blks;
  int n_chain_blks;
  char *trn_data;
  int trn_data_len;
public:
  DiscussCachedMeeting (char *path);
  ErrorCode open ();
  ErrorCode expunge ();
  ErrorCode close ();
  ErrorCode add_transaction (Transaction *&) { abort (); }
  Transaction operator[] (unsigned long l) { abort (); }
  ErrorCode set_deleted_flag (int, int);
  ErrorCode destroy ();
  ErrorCode status (DiscussMeetingInfo &);
  ErrorCode transaction (Transaction *&);
};
