/*

  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 <fcntl.h>

#include <algorithm>
#include <minmax.h>
#include <fstream.h>

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

#include "ostatus.h"
#include "webtools.h"
#include "readwebmail.h"
#include "webmail.h"
#include "challenge.h"
#include "webfolder.h"
#include "attachment.h"
#include "delwebmail.h"

bool webmailt::open_mailbox(string &error)
 {
  string subfolder, 
    mailbox_name=mailbox,
    folder_owner=status.username;

  if(get_subfolder(folder_owner, mailbox_name, subfolder))
   {
    error=nls.get("webonline.mailbox.invalid");
    return TRUE;
   }
 
  if(mailfolderalias(folder_owner, subfolder))
   {
    error=nls.get("webonline.mailbox.alias");
    return TRUE;
   }

  get_folder_grants(folder_owner, subfolder, 
    status.userprofile, status.gruppe, grants);

  if(!grants.read)
   {
    error=nls.get("webonline.mailbox.permission");
    return TRUE;
   }

  if(getrfc.open_mail(folder_owner, subfolder))
   {
    error=nls.get("webonline.mailbox.notavailable");
    return TRUE;
   }

  return FALSE;
 }

void webmailt::read_mail_folder()
 {
  string title=nls.get("webonline.mailbox.title");

  const char *ptr=http.getparameter("mailbox");
  if(ptr!=NULL)
   {
    title+=" \"";
    title+=htmlize_string(ptr);
    title+='"';
   }

  read_web_folder(http, out, title, getrfc, TRUE, TRUE, TRUE);
 }

void webmailt::read_mail()
 {
  dword nr;
  string msg_id;

  if(get_msg_nr(nr, msg_id))
    return;

  const char *attachment=http.getparameter("attachment");
  if(attachment==NULL)
    readwebmail(out, getrfc, nr, msg_id,
                http.getparameter("mimestruct")!=NULL);
  else
    read_attachment(out, getrfc, nr, attachment);
 }

void doemail(httpt &http, ostream &out)
 {
  webmailt webmail(http, out);

  webmail.doemail();
 }

webmailt::webmailt(httpt &_http, ostream &_out):
  http(_http), out(_out)
 {
 }

void webmailt::doemail()
 {
   {
    const char *ptr=http.getparameter("mailbox");
    if(ptr==NULL)
     {
      show_mailbox_list();
      return;
     }
    else
      mailbox=ptr;
   }

  string error;
  if(open_mailbox(error))
   {
    startpage(out, nls.get("webonline.error"));
    out << error << "\n";
    showcopyright();
    endpage();
    return;
   }

   {
    const char *ptr=http.getparameter("action");
    if(ptr==NULL)
     {
      read_mail_folder();
      return;
     }

    if(strcmp(ptr, "read")==0)
      read_mail();
    else if(strcmp(ptr, "delete")==0)
      delete_mail();
    else if(strcmp(ptr, "reply")==0)
      reply_mail();
    else if(strcmp(ptr, "deleteall")==0)
     {
      if(!grants.del)
       {
        startpage(out, nls.get("webonline.error"));
        out << "<p>Permission denied</p>\n";
        showcopyright();
        endpage();
        return;
       }
    
      delwebmail(out, getrfc);
     }
    else
     {
      startpage(out, nls.get("webonline.error"));
      out << "<P>unknown action: " << htmlize_string(ptr) << "</p>";
      endpage(out);
     }
   }
 }

bool webmailt::get_msg_nr(dword &nr, string &msg_id)
 {
  msg_id="&mailbox="+urlize_string(mailbox);  

   {
    const char *ptr=http.getparameter("uid");
    if(ptr==NULL)
     {
      ptr=http.getparameter("nr");
      if(ptr==NULL)
       {
        startpage(out, "Error");
        out << "<p>no uid, no nr</p>";
        endpage(out);
        return TRUE; 
       }

      nr=atol(ptr);

      msg_id+="&nr=";
      msg_id+=i2string((unsigned int)nr);
     }
    else
     {
      dword uid=atol(ptr);

      msg_id+="&uid=";
      msg_id+=i2string((unsigned int)uid);

      if(getrfc.search_uid(uid, nr))
       {
        startpage(out, "Error");
        out << "<p>no such message</p>";
        endpage(out);
        return TRUE;
       }
     }
   }

  return FALSE;
 }


syntax highlighted by Code2HTML, v. 0.9.1