/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          imap_connect.cpp  -  description
                             -------------------
    begin                : 26 June 2003
    copyright            : (C) 2000-2005 by Alexey S.Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef __BORLANDC__
#include <vcl.h>
#pragma hdrstop
#endif

#include <stdio.h>
#include <sptk3/CException.h>

#include <sptk3/CImapConnect.h>
#include <sptk3/CRegistry.h>

using namespace std;

void printResponse(const CStrings& response) {
   for (unsigned i = 0; i < response.size(); i++) {
      puts(response[i].c_str());
   }
   puts("---------------------------------");
}

int main( int argc, char *argv[] ) {
   char           buffer[128];
   CImapConnect   IMAP;
   std::string    user, password, server;
   CRegistry      registry("imap_connect.ini","");

   puts("Testing IMAP connectivity.\n");

   try {
      registry.load();
      server = registry["host"].c_str();
      user = registry["user"].c_str();
      password = registry["password"].c_str();
      IMAP.host(server);
   }
   catch (...) {}

   if (!user.length()) {
      printf("IMAP server name: ");
      scanf("%s",buffer);
      server = buffer;
      IMAP.host(server);

      printf("IMAP user name: ");
      scanf("%s",buffer);
      user = buffer;

      printf("IMAP user password: ");
      scanf("%s",buffer);
      password = buffer;
   }

   puts("\nTrying to connect to IMAP server..");

   try {
      IMAP.cmd_login(user,password);
      printResponse(IMAP.response());

      // Connected? Save the logon parameters..
      try {
         registry["host"] = server;
         registry["user"] = user;
         registry["password"] = password;
         registry.save();
      }
      catch (...) {
      }

      // RFC 2060 test message :)
      CBuffer msgBuffer(
            "Date: Mon, 7 Feb 1994 21:52:25 -0800 (PST)\n\r"
            "From: Fred Foobar <foobar@Blurdybloop.COM\n\r"
            "Subject: afternoon meeting\n\r"
            "To: mooch@owatagu.siam.edu\n\r"
            "Message-Id: <B27397-0100000@Blurdybloop.COM>\n\r"
            "MIME-Version: 1.0\n\r"
            "Content-Type: TEXT/PLAIN; CHARSET=US-ASCII\n\r\n\r"
            "Hello Joe, do you think we can meet at 3:30 tomorrow?\n\r");

      IMAP.cmd_append("Sent",msgBuffer);
      printResponse(IMAP.response());

      puts("\nClosing IMAP connection..");
      IMAP.cmd_logout();
      printResponse(IMAP.response());
   }
   catch (exception& e) {
      puts(e.what());
   }
   return 0;
}
