/*

  DBOX Module
  Copyright (C) 1994-2000 Daniel Kroening <kroening@handshake.de>

  Purpose: 

*/

#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>

#include <string>

#include <dboxpath.h>
#include <tools.h>
#include <folder.h>
#include <charsettools.h>
#include <stringtools.h>
#include <htmltools.h>
#include <mimedecode.h>

#include "ostatus.h"
#include "webgetrfc.h"
#include "webtools.h"
#include "attachment.h"

void parse_attachment(ostream &out,
                      const string &attachment,
                      const rfcmessaget &rfcm,
                      const mimestructt &mimestruct)
 {
  if(attachment=="")
   {
    //out << "HTTP/1.0 200 Keep cool\r\n";
    //out << "\r\n";

    out << "HTTP/1.0 200 Keep cool\r\n"
           "MIME-Version: 1.0\r\n"
           "Server: DBOX-WWW Interface\r\n"
           "Content-Type: "
        << mimeheader2str(mimestruct.content_type) << "\r\n"
           "Content-Encoding: binary\r\n";

    string data=mime_decode(mimestruct, rfcm);

    out << "Content-Location: test\r\n";

    out << "Content-Lenght: " << data.size() << "\r\n";

    out << "\r\n";
    
    out << data;
   }
  else
   {
    unsigned nr=atoi(attachment.c_str())-1;
    if(nr<mimestruct.sub.size())
     {
      char *ptr=strchr(attachment.c_str(), '.');
      if(ptr==NULL) ptr=""; else ptr++;
      parse_attachment(out, ptr, rfcm, mimestruct.sub[nr]);
     }
   }
 }

void read_attachment(ostream &out, getrfct &getrfc,
                     dword nr, 
                     const string &attachment)
 {
  getrfc.get_structure(nr);

  parse_attachment(out, attachment, 
                   getrfc.rfcm, *getrfc.mimestruct);
 }

#if 0
void read_attachment(ostream &out, const string &folder,
                     dword nr, const string &attachment)
 {
  webgetrfct getrfc;

  if(getrfc.open_mail(status.username, folder))
   {
    string title="Mail aus Postfach von User ";
    title+=status.username;
    startpage(out, title);
    out << "Mailbox nicht verf&uuml;gbar!\n";
    showcopyright(out);
    endpage(out);
    return;
   }

  read_attachment(out, getrfc, nr, attachment);
 }
#endif


syntax highlighted by Code2HTML, v. 0.9.1