// common/Message.hh
// This file is part of Decimail; see http://decimail.org
// (C) 2004 Philip Endecott
 
// This is version $Name$
//   (if there is no version (e.g. V0-1) mentioned in the previous line,
//    this is probably a snapshot from between "official" releases.)
 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
#ifndef Message_hh
#define Message_hh

#include "ci_string.hh"
#include "Database.hh"

#include <mimetic/rfc822/mailbox.h>
#include <mimetic/rfc822/mailboxlist.h>
#include <mimetic/mimeentity.h>

#include <string>
#include <list>
#include <map>
#include <fstream>
using namespace std;


class Message {

public:

  Message(int i);
  virtual ~Message(void);

  class MalformedMessage: public Exception {
  private:
    string reason;
  public:
    MalformedMessage(string r): reason(r) {}
    void report(ostream& s);
  };

  typedef string msgpart;
  //typedef __gnu_cxx::rope<char> msgpart;

  virtual int get_id(void) const;
  virtual int get_size(void) const;
  virtual msgpart get_entire_message(void) const;
  virtual msgpart get_body(void) const;
  virtual const mimetic::MimeEntity& get_mimeentity(void) const;
  virtual const mimetic::Header& get_mime_header(void) const;
  virtual msgpart get_headers(void) const;
  virtual string get_subject(void) const;
  virtual time_t get_date(void) const;
  virtual string get_str_date(void) const;

  typedef mimetic::Mailbox email_address;
  typedef mimetic::AddressList email_address_list;

  virtual email_address get_from(void) const;
  virtual email_address_list get_to(void) const;
  virtual email_address_list get_cc(void) const;
  virtual email_address_list get_bcc(void) const;
  virtual string get_rfc822_messageid(void) const;
  // worry about Resent-... fields?

protected:
  const int id;
  mutable int size;
  mutable msgpart entire_message;
  mutable unsigned int blank_line_pos;
  mutable msgpart body;
  mutable mimetic::MimeEntity mimeentity;
  mutable msgpart headers;
  mutable string subject;
  mutable time_t date;
  mutable email_address from;
  mutable email_address_list to;
  mutable email_address_list cc;
  mutable email_address_list bcc;
  mutable string rfc822_messageid;

  mutable bool size_valid;
  mutable bool entire_message_valid;
  mutable bool blank_line_pos_valid;
  mutable bool body_valid;
  mutable bool mimeentity_header_valid;
  mutable bool mimeentity_all_valid;
  mutable bool headers_valid;
  mutable bool subject_valid;
  mutable bool date_valid;
  mutable bool from_valid;
  mutable bool to_valid;
  mutable bool cc_valid;
  mutable bool bcc_valid;
  mutable bool rfc822_messageid_valid;

  virtual void obtain_size(void) const;
  virtual void obtain_entire_message(void) const = 0;
  virtual void find_blank_line(void) const;
  virtual void obtain_body(void) const;
  virtual void obtain_mimeentity_header(void) const;
  virtual void obtain_mimeentity_all(void) const;
  virtual void obtain_headers(void) const;
  virtual void obtain_subject(void) const;
  virtual void obtain_date(void) const;
  virtual void obtain_from(void) const;
  virtual void obtain_to(void) const;
  virtual void obtain_cc(void) const;
  virtual void obtain_bcc(void) const;
  virtual void obtain_rfc822_messageid(void) const;
};


#endif
