# include "__SMTP_RCPT.h"
# include "SMTPServer.h"

namespace mail {
      
   int __SMTP_RCPT::processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data) {
      
      if((session.state == MAIL) || (session.state == RCPT)) {
	 std::string recipient;
	 if(request.ifNext("RCPT TO:")) {	      
	    request.skipWhitespace();
	    if(request.ifNext("<")) {
	       recipient = request.getTokenExclude(">");
	       if(server.mailFileSystem.ifMailBoxExists(recipient)) {	    
		  session.recipientList.push_back(recipient);
		  data << "250 OK" << CRLF;
		  session.state = RCPT;	       
	       } else if(session.relay) { 
		  session.recipientList.push_back(recipient);
		  data << "250 OK" << CRLF;
		  session.state = RCPT;		     
	       } else 
		 data << "550 Mailbox does not exist" << CRLF;      	          	       
	    } else
	      data << "550 Usage: RCPT TO:<email-address>" << CRLF;
	 } else
	   data << "550 Usage: RCPT TO:<email-address>" << CRLF;	 	 
      } else 
	data << "503 Please use MAIL first" << CRLF;
      
      return 1;
   }
   
}
