void
   POP3ClientConnection::QuitNow_()
   {
      String sResponse;
      sResponse.Format(_T("QUIT"));
   
      EnqueueWrite_(sResponse);

      SetReceiveBinary(false);
      current_state_ = StateQUITSent;
   }
   void
   SpamAssassinClient::OnConnected()
   {
      // We'll handle all incoming data as binary.
      SetReceiveBinary(true);
      message_size_ = FileUtilities::FileSize(message_file_);
      EnqueueWrite("PROCESS SPAMC/1.2\r\n");
	  //LOG_DEBUG("SENT: PROCESS SPAMC/1.2");
	  String sConLen;
	  sConLen.Format(_T("Content-length: %d\r\n"), message_size_);
	  EnqueueWrite(sConLen);
	  EnqueueWrite("\r\n");
     SendFileContents_(message_file_);
   }
   void
   SpamAssassinClient::OnConnected()
   {
      // We'll handle all incoming data as binary.
      SetReceiveBinary(true);
      m_iMessageSize = FileUtilities::FileSize(m_sMessageFile);
      SendData("PROCESS SPAMC/1.2\r\n");
	  //LOG_DEBUG("SENT: PROCESS SPAMC/1.2");
	  String sConLen;
	  sConLen.Format(_T("Content-length: %d\r\n"), m_iMessageSize);
	  SendData(sConLen);
	  /*sConLen.Format(_T("Sent: Content-length: %d"), m_iMessageSize);
	  LOG_DEBUG(sConLen);*/
	  //Future feature, per user scanning. SendData("User: "******"\r\n");
      _SendFileContents(m_sMessageFile);
   }
   void
   POP3ClientConnection::HandlePOP3FinalizationTaskCompleted_()
   {
      // The entire message has now been downloaded from the
      // remote POP3 server. Save it in the database and deliver
      // it to the account.
      String fileName = PersistentMessage::GetFileName(current_message_);
      current_message_->SetSize(FileUtilities::FileSize(fileName));

      if (current_message_->GetSize() == 0)
      {
         // Error handling.
         LOG_DEBUG("POP3 External Account: Message is 0 bytes.");
         QuitNow_();
         return;
      }

      ParseMessageHeaders_();

      if (DoSpamProtection_())
      {
         // should we scan this message for virus later on?
         current_message_->SetFlagVirusScan(account_->GetUseAntiVirus());

         FireOnExternalAccountDownload_(current_message_, (*cur_message_).second);

         // the message was not classified as spam which we should delete.
         SaveMessage_();

         // Notify the SMTP deliverer that there is a new message.
         Application::Instance()->SubmitPendingEmail();
      }

      MarkCurrentMessageAsRead_();

      // Switch to ASCII since we're going to request a new message.
      SetReceiveBinary(false);

      // Move on to the next message to download
      cur_message_++;

      RequestNextMessage_();
   
      EnqueueRead("");
   }
   void 
   POP3ClientConnection::ParseRETRResponse_(const String &sData)
   {
      if (CommandIsSuccessfull_(sData))
      {
         // Log that this message has been downloaded.
         int iID = (*cur_message_).first;
         String sCurrentUID = (*cur_message_).second;
         downloaded_messages_[iID] = sCurrentUID;

         return;
      }

      SetReceiveBinary(false);
      
      // Do a mailbox cleanup and disconnect after that.
      StartMailboxCleanup_();
   }
   bool
   POP3ClientConnection::RequestNextMessage_()
   {
      while (cur_message_ != uidlresponse_.end())
      {
         String sCurrentUID = (*cur_message_).second;

         // Check if the current message is already in the list
         // of fetch UID's

         bool bMessageDownloaded = GetUIDList_()->IsUIDInList(sCurrentUID);

         if (bMessageDownloaded)
         {  
            // Mark this message as downloaded. This is so that we can
            // drop it later on when purging the mailbox. (We only purge
            // items we have downloaded). And since it was downloaded during
            // a previous session, we can safely drop it..
            int iID = (*cur_message_).first;
            downloaded_messages_[iID] = sCurrentUID;

            // The message has already been downloaded. Give scripts a chance
            // to override the default delete behavior.
            std::shared_ptr<Message> messageEmpty;
            FireOnExternalAccountDownload_(messageEmpty, sCurrentUID);
         }
         else
         {
            // Request message download now.

            current_message_ = std::shared_ptr<Message> (new Message);

            int iMessageIdx = (*cur_message_).first;

            String sResponse;
            sResponse.Format(_T("RETR %d"), iMessageIdx);

            EnqueueWrite_(sResponse);

            current_state_ = StateRETRSent;

            // Reset the transmission buffer. It will be
            // recreated when we receive binary the next time.

            transmission_buffer_.reset();

            SetReceiveBinary(true);
                          
            return true;
         }
      
         cur_message_++;

      }

      // We reached the end of the message list.
      if (cur_message_ == uidlresponse_.end())
      {
         StartMailboxCleanup_();
      }


      return false;
   }