/*

  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 <algorithm>
#include <minmax.h>

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

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

struct msginfot 
 {
  folderindext fi; 
  dword msgnr;
  bool highlight;
 };

void show_mail_line(ostream &out,
                    const string &base_url,
                    const string &sort_str,
                    bool bg, const msginfot &msginfo,
                    const string &challenge,
                    const string &mailbox_str,
                    bool show_delete,
                    bool show_reply)
 {
  out << "<TR bgcolor=\"" << 
    (bg?config.webonline_tablebgcolor1:config.webonline_tablebgcolor2) <<
    "\">";

  out << "<TD ALIGN=CENTER>";

  if(show_delete)
   {
    out << "<A HREF=\"" << base_url << "?action=delete" 
        << "&uid=" << msginfo.fi.id
        << "&" << sort_str << "&challenge=" << challenge 
        << mailbox_str
        << "\"><IMG SRC=\"/html/webonline/webmail/email-delete.gif\" "
           "border=0 alt=\"Nachricht löschen\"></a> ";
   }
  
  if(show_reply)
   {
    out << "<A TARGET=_NEW HREF=\"" << base_url << "?action=reply" 
        << "&uid=" << msginfo.fi.id
        << "&challenge=" << challenge 
        << mailbox_str
        << "\"><IMG SRC=\"/html/webonline/webmail/email-answer.gif\" "
           "border=0 alt=\"Nachricht beantworten (E-Mail)\"></a> ";
   }

  if(!show_reply && !show_delete)
    out << "&nbsp;";

  out << "</TD>";

   {
    struct tm *tm=localtime(&msginfo.fi.received);
    char size[TEXTLEN];

    if(msginfo.fi.len<10000)
      snprintf(size, TEXTLEN, "%lu", msginfo.fi.len);
    else
     {
      dword l=msginfo.fi.len/1024;
      if(l>999)
       {
        l/=1024;
        if(l==0) l=1;
        snprintf(size, TEXTLEN, "%luM", l);
       }
      else
       {
        if(l==0) l=1;
        snprintf(size, TEXTLEN, "%luK", l);
       }
     }

    out.form("<TD><img src=\"/html/icons/anthony-%s.gif\"></TD>"
             "<TD><center><code>%02u</code>.<code>%02u</code>.</center></TD>"
             "<TD ALIGN=RIGHT><code>%s</code></TD>",
             msginfo.fi.flags.binary?"unknown":"text",
             tm->tm_mday, tm->tm_mon+1, size);
   }

  out << "<TD>";

  if(msginfo.highlight) out << "<b>";

   {
    char tempstr[TEXTLEN];
    string from, mailto;

    strmaxcpy(tempstr, msginfo.fi.abs, TEXTLEN-1);
    ibm2iso(tempstr, strlen(tempstr));
    char *tptr=strstr(tempstr, " (");
    if(tptr==NULL)
      mailto=from=tempstr;
    else
     {
      *tptr=0;
      tptr+=2;
      char *tptr2=strrchr(tptr, ')');
      if(tptr2!=NULL) *tptr2=0;
      mailto=htmlize_string(tempstr);
      from=tptr;
     }

    htmlize_string(from);
    htmlize_string(mailto);

    if(msginfo.fi.flags.deleted)
      out << "<strike>" << from << "</strike>";
    else
      out << "<a href=\"mailto:" << mailto << "\">" << from << "</a>";
   }

  if(msginfo.highlight) out << "</b>";

  out << "</TD>";

  out << "<TD>";

  if(msginfo.highlight) out << "<b>";

   {
    string subject=msginfo.fi.bet;
    ibm2iso(subject);
    htmlize_string(subject);

    if(msginfo.fi.flags.deleted)
      out << "<strike>" << subject << "</strike>";
    else
      out << "<a "
             "href=\"" << base_url << "?action=read"
          << "&uid=" << msginfo.fi.id << mailbox_str
          << "\">" << subject << "</a>";
   }
  
  if(msginfo.highlight) out << "</b>";

  out << "</TD>";

  out << "\n";
 }

bool sort_by_from(const msginfot &a, const msginfot &b)
 {
  return stringcasecmp(a.fi.abs, b.fi.abs)<0;
 }

bool sort_by_subj(const msginfot &a, const msginfot &b)
 {
  return stringcasecmp(a.fi.bet, b.fi.bet)<0;
 }

bool sort_by_size(const msginfot &a, const msginfot &b)
 {
  return a.fi.len<b.fi.len;
 }

bool sort_by_date(const msginfot &a, const msginfot &b)
 {
  return a.fi.received<b.fi.received;
 }

bool sort_by_from_reverse(const msginfot &a, const msginfot &b)
 {
  return stringcasecmp(a.fi.abs, b.fi.abs)>=0;
 }

bool sort_by_subj_reverse(const msginfot &a, const msginfot &b)
 {
  return stringcasecmp(a.fi.bet, b.fi.bet)>=0;
 }

bool sort_by_size_reverse(const msginfot &a, const msginfot &b)
 {
  return a.fi.len>=b.fi.len;
 }

bool sort_by_date_reverse(const msginfot &a, const msginfot &b)
 {
  return a.fi.received>=b.fi.received;
 }

void read_web_folder(httpt &http, ostream &out,
                     const string &title, getrfct &getrfc,
                     bool show_delete,
                     bool show_reply,
                     bool highlight_unseen)
 {
  startpage(out, title);
  out << "<center><H1>" << title << "</H1></center><p>\n";

  string mailbox_str;

   {
    const char *ptr;
    ptr=http.getparameter("group");
    if(ptr!=NULL) { mailbox_str+="&group="; mailbox_str+=urlize_string(ptr); }
    ptr=http.getparameter("mailbox");
    if(ptr!=NULL) { mailbox_str+="&mailbox="; mailbox_str+=urlize_string(ptr); }
   }

  vector<msginfot> index;

   {
    msginfot msginfo;
    dword number_of_messages=getrfc.get_number_of_messages();
    for(unsigned i=0; i<number_of_messages; i++)
     {
      getrfc.get_folderindex(i);
      msginfo.fi=getrfc.fi;
      msginfo.highlight=(highlight_unseen && !getrfc.fi.flags.seen);
      msginfo.msgnr=i;
      index.push_back(msginfo);
     }
   }

  if(index.size()==0)
   {
    out << "<b>Dieses Fach ist leer!</b><p>\n";
   }
  else
   {
    bool reverse_sort=(atol(status.userprofile.get("Nachrichtenanzeige"))!=0);
    const char *sort_by;
    dword msg_per_page=50;
    dword start_nr=0, end_nr=min((dword)index.size(), msg_per_page)-1;

     {
      const char *ptr;
      ptr=http.getparameter("start_nr");
      if(ptr!=NULL) start_nr=min((unsigned long)atol(ptr), (unsigned long)index.size()-1);
      ptr=http.getparameter("end_nr");
      if(ptr!=NULL) end_nr=min((unsigned long)atol(ptr), (unsigned long)index.size()-1);
     }

    out << "<CENTER><TABLE CELLPADDING=2 CELLSPACING=0>\n";

    if(http.getparameter("reverse")!=NULL) 
      reverse_sort=(atol(http.getparameter("reverse"))!=0);

    sort_by=http.getparameter("sort");
    if(sort_by==NULL) sort_by="date";

    string sort_str;
    sort_str="sort=";
    sort_str+=sort_by;
    if(reverse_sort) sort_str+="&reverse=1"; else sort_str+="&reverse=0";

    string msg_str=sort_str;

    if(start_nr!=0 || end_nr!=index.size()-1)
     {
      char tempstr[TEXTLEN];
      snprintf(tempstr, TEXTLEN, "&start_nr=%lu&end_nr=%lu", start_nr, end_nr);
      msg_str+=tempstr;

      out << "<TR><TD><TD><TD><TD><TD><TD ALIGN=RIGHT>";
      if(start_nr!=0) 
       {
        dword new_start_nr=0;
        if(start_nr>msg_per_page) new_start_nr=start_nr-msg_per_page;
        out << "<a href=\"" << http.url << "?start_nr=" << new_start_nr << "&end_nr="
            << start_nr-1 << "&" << sort_str << mailbox_str << "\">" 
            << new_start_nr+1 << "-" << start_nr << "</a> ";
       }

      out << start_nr+1 << "-" << end_nr+1;

      if(end_nr!=index.size()-1)
       {
        dword new_end_nr=min((dword)(index.size()-1), end_nr+msg_per_page);
        out << " <a href=\"" << http.url << "?start_nr=" << end_nr+1 << "&end_nr=" 
            << new_end_nr << "&" << sort_str << mailbox_str << "\">" 
            << end_nr+2 << "-" << new_end_nr+1 << "</a>";
       }
      out << "</td>\n";
     }

     {
      const char *reverse;
      if(reverse_sort) reverse="&reverse=0"; else reverse="&reverse=1";

      out <<
        "<TR bgcolor=\"" << config.webonline_thbgcolor << "\">"
        "<TH><font color=\"" << config.webonline_thfcolor << "\">Aktionen</font>"
        "<TH><font color=\"" << config.webonline_thfcolor << "\">Typ</font>"
        "<TH><a href=\"" << http.url << "?sort=date" << reverse << mailbox_str << "\"><font color=\"" << config.webonline_thfcolor << "\">Datum</font></a>"
        "<TH><a href=\"" << http.url << "?sort=size" << reverse << mailbox_str << "\"><font color=\"" << config.webonline_thfcolor << "\">Gr&ouml;&szlig;e</font></a>"
        "<TH><a href=\"" << http.url << "?sort=from" << reverse << mailbox_str << "\"><font color=\"" << config.webonline_thfcolor << "\">Absender</font></a>"
        "<TH><a href=\"" << http.url << "?sort=subj" << reverse << mailbox_str << "\"><font color=\"" << config.webonline_thfcolor << "\">Betreff</font></a>"
        "\n";
     }

    if(stricmp(sort_by, "size")==0)
      sort(index.begin(), index.end(),
        reverse_sort?sort_by_size_reverse:sort_by_size);
    else if(stricmp(sort_by, "from")==0)
      sort(index.begin(), index.end(),
        reverse_sort?sort_by_from_reverse:sort_by_from);
    else if(stricmp(sort_by, "subj")==0)
      sort(index.begin(), index.end(),
        reverse_sort?sort_by_subj_reverse:sort_by_subj);
    else if(stricmp(sort_by, "date")==0)
      sort(index.begin(), index.end(),
        reverse_sort?sort_by_date_reverse:sort_by_date);

    bool bg=FALSE;

    string challenge;
    get_challenge(challenge, status.userprofile);

    for(dword msgnr=start_nr; msgnr<=end_nr; msgnr++)
      show_mail_line(out, http.url, msg_str, bg^=1, 
        index[msgnr], challenge, mailbox_str, show_delete, show_reply);

    out << "</TABLE><p>\n";
    out << index.size() << " Nachricht";
    if(index.size()!=1) out << "en";
    out << " insgesamt im Folder.\n";
    out << "</CENTER>\n";
   }

  showcopyright(out);
  endpage(out);
 }




syntax highlighted by Code2HTML, v. 0.9.1