//      $Id: mailbox.h,v 1.4 1994-10-24 15:06:27 guy 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
{
  TString16 _from;
  TString16 _to;
  TString80 _subject;
  TString _text;
  byte  _flags;
  int   _number;

  friend class TMailbox;

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

public:
  const TString& from() const { return _from; }
  const TString& to() const { return _to; }
  const TString& subject() const { return _subject; }
  const TString& body() const { return _text; }

  const TString& from(const char* f) { return _from = f; }
  const TString& to(const char* t) { return _to = t; }
  const TString& subject(const char* s) { return _subject = s; }
  const TString& body(const char* b) { return _text = b; }
  
  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

