// imapd/SelectOrExamineCmd.cc
// This file is part of Decimail; see http://decimail.org
// (C) 2004-2007 Philip Endecott
 
// 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.
 
#include "SelectOrExamineCmd.hh"

#include "utils.hh"

using namespace std;
using namespace pbe;


namespace Imapd {

  void SelectOrExamineCmd::runbody(Session& session)
  {
    session.check_state(Session::auth|Session::selected);
    try {
      session.set_mailbox(mailbox_name);
    }
    catch(ImapdDb::MailboxDoesNotExist& E) {
      session.clear_mailbox();
      session.set_state(Session::auth);
      throw RespondNo("Mailbox "+mailbox_name+" does not exist");
    }
    catch(...) {
      session.clear_mailbox();
      session.set_state(Session::auth);
      throw;
    }
    session.set_state(Session::selected);

    session.get_msg_ids();

    session.respond_untagged("FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)");
    session.respond_untagged("OK [PERMANENTFLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)]");
    int exists = session.seqnum_to_msgid.size()-1;
    session.respond_untagged(boost::lexical_cast<string>(exists)+" EXISTS");
    int recent = exists;
    session.respond_untagged(boost::lexical_cast<string>(recent)+" RECENT");
    session.respond_untagged("OK [UIDVALIDITY 1]");

    // This would benefit from keeping a list of msgids for the currently selected 
    // mailbox in a temporary table.
    Query<> any_unseen_messages
      (session.get_db(),
       "(" + session.get_mailbox_query() +  ")"
       " intersect (select msg_id from u_imap_message_flags where flag='\\Unseen') "
       " order by msg_id limit 1");
    Result r = any_unseen_messages.runonce();
    if (r.rows!=0) {
      int first_unseen_msgid = r.get<int>(0,0);
      int first_unseen_seqnum = session.msgid_to_seqnum[first_unseen_msgid];
      session.respond_untagged("OK [UNSEEN "+boost::lexical_cast<string>(first_unseen_seqnum)+"]");
    }

    ImapdDb::msg_id_t last_uid;
// Hmm, is the last/next/+1/-1 stuff here right?
    if (session.seqnum_to_msgid.empty()) {
      last_uid = session.get_db().get_next_uid();
    } else {
      last_uid = *(session.seqnum_to_msgid.end()-1);
    }
    int next_uid = last_uid+1;
    session.respond_untagged("OK [UIDNEXT "+boost::lexical_cast<string>(next_uid)+"]");
    
    if (readonly) {
      response_code = "[READ-ONLY] ";
    } else {
      response_code = "[READ-WRITE] ";
    }
  }

};
