Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Revision:
5:fa27dde97304
Parent:
0:ac1725ba162c
--- a/services/email/smtp/SMTPClient.h	Sun Nov 21 17:13:44 2010 +0000
+++ b/services/email/smtp/SMTPClient.h	Sat Nov 27 23:23:43 2010 +0000
@@ -1,17 +1,17 @@
 
 /*
-Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
- 
+Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) y Segundo Equipo
+
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
- 
+
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
- 
+
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -21,6 +21,10 @@
 THE SOFTWARE.
 */
 
+/** \file
+SMTP Client header file
+*/
+
 #ifndef SMTP_CLIENT_H
 #define SMTP_CLIENT_H
 
@@ -28,82 +32,214 @@
 
 #include "core/net.h"
 #include "api/TCPSocket.h"
-#include "../emailMessage.h"
-
+#include "api/DNSRequest.h"
+#include "EmailMessage.h"
 #include "mbed.h"
 
-#define SMTP_REQUEST_TIMEOUT 5000
-
-enum SMTPResult
-{
-  SMTP_OK,
-  SMTP_PRTCL, //Protocol error
-  SMTP_TIMEOUT, //Connection timeout
-  SMTP_DISC //Disconnected 
+///SMTP client results
+enum SMTPResult {
+    SMTP_OK, ///<Success
+    SMTP_PROCESSING, ///<Processing
+    SMTP_DNS, ///<Could not resolve name
+    SMTP_PRTCL, ///<Protocol error
+    SMTP_TIMEOUT, ///<Connection timeout
+    SMTP_DISC ///<Disconnected
 };
 
-class SMTPClient /*: public NetService*/
-{
+///SMTP authentication
+enum SMTPAuth {
+    SMTP_AUTH_NONE, ///<No authentication
+    SMTP_AUTH_PLAIN ///<AUTH PLAIN authentication
+};
+#include "core/netservice.h"
+
+///A simple SMTP Client
+/**
+The SMTPClient is composed of:
+- The actual client (SMTPClient)
+- A class (EmailMessage) to hold the message addresses and content for sending
+*/
+class SMTPClient : protected NetService {
 public:
-  SMTPClient();
-  virtual ~SMTPClient();
-  
-  void setHost(const Host& host);
-  void send(EmailMessage* pMessage);
-  
-  class CDummy;
-  template<class T> 
-  //Linker bug : Must be defined here :(
-  void setOnResult( T* pItem, void (T::*pMethod)(SMTPResult) )
-  {
-    m_pCbItem = (CDummy*) pItem;
-    m_pCbMeth = (void (CDummy::*)(SMTPResult)) pMethod;
-  }
-  
-  void init(); //Create and setup socket if needed
-  void close();
-  
-private:
-  int rc(char* buf); //Return code
-  void process(bool moreData); //Main state-machine
+    ///Instantiates the SMTP client
+    SMTPClient();
+
+    ///Destructor for the SMTP client
+    virtual ~SMTPClient();
+
+    ///Full constructor for the SMTP client
+    /**
+    @param host : SMTP server host
+    @param heloDomain : domain name of client
+    @param user : username
+    @param password : password
+    @param auth : authentication type
+    */
+    SMTPClient(const Host& host, const char* heloDomain, const char* user, const char* password, SMTPAuth auth);
+
+    ///Set server host
+    /**
+    @param host : SMTP server host
+    */
+    void setServer(const Host& host);
+
+    ///Provides a plain authentication feature (Base64 encoded username and password)
+    /**
+    @param user : username
+    @param password : password
+    */
+    void setAuth(const char* user, const char* password); // Plain authentication
+
+    ///Turns off authentication
+    void clearAuth(); // Clear authentication
+
+    ///Set HELO domain (defaults to localhost if not set)
+    /**
+    @param heloDomain : domain name of client (strictly should be fully qualified domain name)
+    */
+    void setHeloDomain(const char* heloDomain);
+
+    //High Level setup functions
+    ///Sends the message (blocking)
+    /**
+    @param pMessage : pointer to a message
+
+    Blocks until completion
+    */
+    SMTPResult send(EmailMessage* pMessage); //Blocking
+
+    ///Sends the message (non blocking)
+    /**
+    @param pMessage : pointer to a message
+    @param pMethod : callback function
+
+    The function returns immediately and calls the callback on completion or error
+    */
+    SMTPResult send(EmailMessage* pMessage, void (*pMethod)(SMTPResult)); //Non blocking
+
+    ///Sends the message (non blocking)
+    /**
+    @param pMessage : pointer to a message
+    @param pItem : instance of class on which to execute the callback method
+    @param pMethod : callback method
+
+    The function returns immediately and calls the callback on completion or error
+    */
+    template<class T>
+    SMTPResult send(EmailMessage* pMessage, T* pItem, void (T::*pMethod)(SMTPResult)) { //Non blocking
+        setOnResult(pItem, pMethod);
+        doSend(pMessage);
+        return SMTP_PROCESSING;
+    }
+
+    ///Sends the message (non blocking)
+    /**
+    @param pMessage : pointer to a message
+
+    The function returns immediately and calls the previously set callback on completion or error
+    */
+    void doSend(EmailMessage* pMessage);
 
-  void setTimeout(int ms);
-  void resetTimeout();
-  
-  void onTimeout(); //Connection has timed out
-  void onTCPSocketEvent(TCPSocketEvent e);
-  void onResult(SMTPResult r); //Called when exchange completed or on failure
-  
-  EmailMessage* m_pMessage;
+    ///Setup the result callback
+    /**
+    @param pMethod : callback function
+    */
+    void setOnResult( void (*pMethod)(SMTPResult) );
+
+    ///Setup the result callback
+    /**
+    @param pItem : instance of class on which to execute the callback method
+    @param pMethod : callback method
+    */
+    class CDummy;
+    template<class T>
+    void setOnResult( T* pItem, void (T::*pMethod)(SMTPResult) ) {
+        m_pCb = NULL;
+        m_pCbItem = (CDummy*) pItem;
+        m_pCbMeth = (void (CDummy::*)(SMTPResult)) pMethod;
+    }
 
-  TCPSocket* m_pTCPSocket;
+    ///Setup timeout
+    /**
+    @param ms : time of connection inactivity in ms after which the request should timeout
+    */
+    void setTimeout(int ms);
+
+    ///Gets the last response from the server
+    string& getLastResponse();
+
+    virtual void poll(); //Called by NetServices
+
+protected:
+    int rc(char* buf); //Return code
+    void process(bool writeable); //Main state-machine
+
+    void resetTimeout();
+
+    void init();
+    void close();
+
+    void setup(EmailMessage* pMessage); //Setup request, make DNS Req if necessary
+    void connect(); //Start Connection
+
+    int  tryRead(); //Read data and try to feed output
+    void readData(); //Data has been read
+    void writeData(); //Data has been written & buf is free
 
-  enum SMTPStep
-  {
-    SMTP_HELLO,
-    SMTP_FROM,
-    SMTP_TO,
-    SMTP_DATA,
-    SMTP_BODY,
-    SMTP_BODYMORE,
-    SMTP_EOF,
-    SMTP_BYE
-  };
-  
-  SMTPStep m_nextState;
-  
-  CDummy* m_pCbItem;
-  void (CDummy::*m_pCbMeth)(SMTPResult);
-  
-  Timeout m_watchdog;
-  int m_timeout;
-  
-  int m_posInMsg;
-  
-  bool m_closed;
-  
-  Host m_host;
+    void onTCPSocketEvent(TCPSocketEvent e);
+    void onDNSReply(DNSReply r);
+    void onResult(SMTPResult r); //Called when exchange completed or on failure
+    void onTimeout(); //Connection has timed out
+
+    string encodePlainAuth(); // Encode plain authentication (username and password in based 64)
+    bool okPostAuthentication(int code); // True if server response is ok following authentication
+
+private:
+    SMTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available
+
+    CDummy* m_pCbItem;
+    void (CDummy::*m_pCbMeth)(SMTPResult);
+    void (*m_pCb)(SMTPResult);
+
+    TCPSocket* m_pTCPSocket;
+
+    Timer m_watchdog;
+    int m_timeout;
+
+    DNSRequest* m_pDnsReq;
+    Host m_server;
+
+    bool m_closed;
 
+    enum SMTPStep {
+        SMTP_HELLO,
+        SMTP_AUTH,
+        SMTP_FROM,
+        SMTP_TO,
+        SMTP_DATA,
+        SMTP_BODY,
+        SMTP_BODYMORE,
+        SMTP_EOF,
+        SMTP_BYE,
+        SMTP_CLOSED
+    };
+
+    SMTPStep m_state;
+
+    EmailMessage* m_pMessage;
+
+    int m_posInMsg;
+    int m_posInCRLF;
+
+    SMTPResult m_blockingResult; //Result if blocking mode
+    string m_response;
+    int m_responseCode;
+    string m_username;
+    string m_password;
+    SMTPAuth m_auth;
+    string m_heloDomain;
+
+    vector<string>::iterator m_To;
 };
 
-#endif
+#endif // SMTP_CLIENT_H
\ No newline at end of file