//      $Id: mailbox.h,v 1.3 1994-09-28 10:35:46 villa Exp $

/* si', trattasi di -*-c++-*- */
// Mailbox.h
// Message passing between applications and command lines

#ifndef __MAILBOX_H
#define __MAILBOX_H

#ifndef __ARRAY_H
#include <array.h>
#endif

#ifndef __STRINGS_H
#include <strings.h>
#endif

// TMessage flags
#define MSG_READ (0x01)

class TMessage : public TObject
// basic message
{
  TString _from;
  TString _to;
  TString _subject;
  TString _text;
  byte  _flags;
  int   _number;

  friend class TMailbox;

  void setread() { _flags |= MSG_READ; }
  bool isread()  { return _flags & MSG_READ; }
  int  number(int n = -1)
  { return (n == -1 ? _number : (_number = n)); }

public:

  const char* from(const char* f = NULL)     // get-set sender
  { return f == NULL ? _from : (_from = f); }
  const char* to  (const char* f = NULL)     // get-set recipient
  { return f == NULL ? _to : (_to = f); }
  const char* subject(const char* f = NULL)  // get-set subject
  { return f == NULL ? _subject : (_subject = f); }
  const char* body(const char* f = NULL)     // get-set body
  { return f == NULL ? _text : (_text = f); }

  void send();

  TMessage(const char* to, const char* sub, const char* text,
           const char* from = NULL);
};

class TMailbox : public TObject
{
  TFilename _path;
  TArray    _msgs;
  int       _lastread, n_new;
  long      _lastpos;
  int                           _cnt;

protected:

  void reread();
  TMessage* next_read();
  TMessage* next_unread();

public:
  
  int check()                        // return
  { reread(); return n_new; }    // n. of messages waiting
  void restart() { _cnt = 0;}                            // reinitialize read phase
  int items()                                                                                            // n. of read+unread messages
  { return _msgs.items(); }        // in mailbox
  TMessage* next(bool read = FALSE);      // get next message; if
  // TRUE passed, get read mess. too
  TMessage* next_s(char* s, bool read = FALSE); // next w/ matching subject
  TMessage* next_f(char* f, bool read = FALSE); // next w/ matching sender
  void send(TMessage& m);                   // send a message
  void sendcmd(int argc, char* argv[],
               char* to);                  // transform commandline args
  // in mail messages
  // NOT IMPLEMENTED
  char*  readcmd(char* to = NULL);         // transform mailbox in
  // cmdline parms for calling
  // non_compliant applications
  // NOT IMPLEMENTED
  TMessage* get(int n)                     // nth message
  { return (TMessage*)_msgs.objptr(n); }

  TMailbox::TMailbox(const char* appname = NULL);
  virtual ~TMailbox();
};


// instead of using commandline args directly, user posts
// messages to application, then calls it without arguments
// If the application is called by the user with command_line
// arguments, sendcmd parses them and translates to messages
// according to the following rules: 
//       
//    from is "cmdline"
//    
//    subject comes from arguments of type -opt=value
//    (opt becomes subject and value becomes body)
//
//    args like -opt="val, val, val" create
//    one message for each val, with the same subject=opt
//
//    -opt arg creates subject=opt and empty body
//
//    one-word arguments create a message with "null" subject
// 
// A mailbox should be member of applicat and sendcmd should be
// called automatically upon startup 
// Non compliant applications (which do not use mailbox) can be 
// called consistently by sending messages, 
// appending TMailbox::readcmd(appname) to
// their name and doing an exec()

#endif