This library is stripped down version of NetServices library. HTTP server and client function is NOT supported.
services/email/smtp/SMTPClient.h@0:dcf3c92487ca, 2011-07-20 (annotated)
- Committer:
- idinor
- Date:
- Wed Jul 20 11:45:39 2011 +0000
- Revision:
- 0:dcf3c92487ca
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
idinor | 0:dcf3c92487ca | 1 | |
idinor | 0:dcf3c92487ca | 2 | /* |
idinor | 0:dcf3c92487ca | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) y Segundo Equipo |
idinor | 0:dcf3c92487ca | 4 | |
idinor | 0:dcf3c92487ca | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
idinor | 0:dcf3c92487ca | 6 | of this software and associated documentation files (the "Software"), to deal |
idinor | 0:dcf3c92487ca | 7 | in the Software without restriction, including without limitation the rights |
idinor | 0:dcf3c92487ca | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
idinor | 0:dcf3c92487ca | 9 | copies of the Software, and to permit persons to whom the Software is |
idinor | 0:dcf3c92487ca | 10 | furnished to do so, subject to the following conditions: |
idinor | 0:dcf3c92487ca | 11 | |
idinor | 0:dcf3c92487ca | 12 | The above copyright notice and this permission notice shall be included in |
idinor | 0:dcf3c92487ca | 13 | all copies or substantial portions of the Software. |
idinor | 0:dcf3c92487ca | 14 | |
idinor | 0:dcf3c92487ca | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
idinor | 0:dcf3c92487ca | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
idinor | 0:dcf3c92487ca | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
idinor | 0:dcf3c92487ca | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
idinor | 0:dcf3c92487ca | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
idinor | 0:dcf3c92487ca | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
idinor | 0:dcf3c92487ca | 21 | THE SOFTWARE. |
idinor | 0:dcf3c92487ca | 22 | */ |
idinor | 0:dcf3c92487ca | 23 | |
idinor | 0:dcf3c92487ca | 24 | /** \file |
idinor | 0:dcf3c92487ca | 25 | SMTP Client header file |
idinor | 0:dcf3c92487ca | 26 | */ |
idinor | 0:dcf3c92487ca | 27 | |
idinor | 0:dcf3c92487ca | 28 | #ifndef SMTP_CLIENT_H |
idinor | 0:dcf3c92487ca | 29 | #define SMTP_CLIENT_H |
idinor | 0:dcf3c92487ca | 30 | |
idinor | 0:dcf3c92487ca | 31 | class EmailMessage; |
idinor | 0:dcf3c92487ca | 32 | |
idinor | 0:dcf3c92487ca | 33 | #include "core/net.h" |
idinor | 0:dcf3c92487ca | 34 | #include "api/TCPSocket.h" |
idinor | 0:dcf3c92487ca | 35 | #include "api/DNSRequest.h" |
idinor | 0:dcf3c92487ca | 36 | #include "EmailMessage.h" |
idinor | 0:dcf3c92487ca | 37 | #include "mbed.h" |
idinor | 0:dcf3c92487ca | 38 | |
idinor | 0:dcf3c92487ca | 39 | ///SMTP client results |
idinor | 0:dcf3c92487ca | 40 | enum SMTPResult { |
idinor | 0:dcf3c92487ca | 41 | SMTP_OK, ///<Success |
idinor | 0:dcf3c92487ca | 42 | SMTP_PROCESSING, ///<Processing |
idinor | 0:dcf3c92487ca | 43 | SMTP_DNS, ///<Could not resolve name |
idinor | 0:dcf3c92487ca | 44 | SMTP_PRTCL, ///<Protocol error |
idinor | 0:dcf3c92487ca | 45 | SMTP_TIMEOUT, ///<Connection timeout |
idinor | 0:dcf3c92487ca | 46 | SMTP_DISC ///<Disconnected |
idinor | 0:dcf3c92487ca | 47 | }; |
idinor | 0:dcf3c92487ca | 48 | |
idinor | 0:dcf3c92487ca | 49 | ///SMTP authentication |
idinor | 0:dcf3c92487ca | 50 | enum SMTPAuth { |
idinor | 0:dcf3c92487ca | 51 | SMTP_AUTH_NONE, ///<No authentication |
idinor | 0:dcf3c92487ca | 52 | SMTP_AUTH_PLAIN ///<AUTH PLAIN authentication |
idinor | 0:dcf3c92487ca | 53 | }; |
idinor | 0:dcf3c92487ca | 54 | #include "core/netservice.h" |
idinor | 0:dcf3c92487ca | 55 | |
idinor | 0:dcf3c92487ca | 56 | ///A simple SMTP Client |
idinor | 0:dcf3c92487ca | 57 | /** |
idinor | 0:dcf3c92487ca | 58 | The SMTPClient is composed of: |
idinor | 0:dcf3c92487ca | 59 | - The actual client (SMTPClient) |
idinor | 0:dcf3c92487ca | 60 | - A class (EmailMessage) to hold the message addresses and content for sending |
idinor | 0:dcf3c92487ca | 61 | */ |
idinor | 0:dcf3c92487ca | 62 | class SMTPClient : protected NetService { |
idinor | 0:dcf3c92487ca | 63 | public: |
idinor | 0:dcf3c92487ca | 64 | ///Instantiates the SMTP client |
idinor | 0:dcf3c92487ca | 65 | SMTPClient(); |
idinor | 0:dcf3c92487ca | 66 | |
idinor | 0:dcf3c92487ca | 67 | ///Destructor for the SMTP client |
idinor | 0:dcf3c92487ca | 68 | virtual ~SMTPClient(); |
idinor | 0:dcf3c92487ca | 69 | |
idinor | 0:dcf3c92487ca | 70 | ///Full constructor for the SMTP client |
idinor | 0:dcf3c92487ca | 71 | /** |
idinor | 0:dcf3c92487ca | 72 | @param host : SMTP server host |
idinor | 0:dcf3c92487ca | 73 | @param heloDomain : domain name of client |
idinor | 0:dcf3c92487ca | 74 | @param user : username |
idinor | 0:dcf3c92487ca | 75 | @param password : password |
idinor | 0:dcf3c92487ca | 76 | @param auth : authentication type |
idinor | 0:dcf3c92487ca | 77 | */ |
idinor | 0:dcf3c92487ca | 78 | SMTPClient(const Host& host, const char* heloDomain, const char* user, const char* password, SMTPAuth auth); |
idinor | 0:dcf3c92487ca | 79 | |
idinor | 0:dcf3c92487ca | 80 | ///Set server host |
idinor | 0:dcf3c92487ca | 81 | /** |
idinor | 0:dcf3c92487ca | 82 | @param host : SMTP server host |
idinor | 0:dcf3c92487ca | 83 | */ |
idinor | 0:dcf3c92487ca | 84 | void setServer(const Host& host); |
idinor | 0:dcf3c92487ca | 85 | |
idinor | 0:dcf3c92487ca | 86 | ///Provides a plain authentication feature (Base64 encoded username and password) |
idinor | 0:dcf3c92487ca | 87 | /** |
idinor | 0:dcf3c92487ca | 88 | @param user : username |
idinor | 0:dcf3c92487ca | 89 | @param password : password |
idinor | 0:dcf3c92487ca | 90 | */ |
idinor | 0:dcf3c92487ca | 91 | void setAuth(const char* user, const char* password); // Plain authentication |
idinor | 0:dcf3c92487ca | 92 | |
idinor | 0:dcf3c92487ca | 93 | ///Turns off authentication |
idinor | 0:dcf3c92487ca | 94 | void clearAuth(); // Clear authentication |
idinor | 0:dcf3c92487ca | 95 | |
idinor | 0:dcf3c92487ca | 96 | ///Set HELO domain (defaults to localhost if not set) |
idinor | 0:dcf3c92487ca | 97 | /** |
idinor | 0:dcf3c92487ca | 98 | @param heloDomain : domain name of client (strictly should be fully qualified domain name) |
idinor | 0:dcf3c92487ca | 99 | */ |
idinor | 0:dcf3c92487ca | 100 | void setHeloDomain(const char* heloDomain); |
idinor | 0:dcf3c92487ca | 101 | |
idinor | 0:dcf3c92487ca | 102 | //High Level setup functions |
idinor | 0:dcf3c92487ca | 103 | ///Sends the message (blocking) |
idinor | 0:dcf3c92487ca | 104 | /** |
idinor | 0:dcf3c92487ca | 105 | @param pMessage : pointer to a message |
idinor | 0:dcf3c92487ca | 106 | |
idinor | 0:dcf3c92487ca | 107 | Blocks until completion |
idinor | 0:dcf3c92487ca | 108 | */ |
idinor | 0:dcf3c92487ca | 109 | SMTPResult send(EmailMessage* pMessage); //Blocking |
idinor | 0:dcf3c92487ca | 110 | |
idinor | 0:dcf3c92487ca | 111 | ///Sends the message (non blocking) |
idinor | 0:dcf3c92487ca | 112 | /** |
idinor | 0:dcf3c92487ca | 113 | @param pMessage : pointer to a message |
idinor | 0:dcf3c92487ca | 114 | @param pMethod : callback function |
idinor | 0:dcf3c92487ca | 115 | |
idinor | 0:dcf3c92487ca | 116 | The function returns immediately and calls the callback on completion or error |
idinor | 0:dcf3c92487ca | 117 | */ |
idinor | 0:dcf3c92487ca | 118 | SMTPResult send(EmailMessage* pMessage, void (*pMethod)(SMTPResult)); //Non blocking |
idinor | 0:dcf3c92487ca | 119 | |
idinor | 0:dcf3c92487ca | 120 | ///Sends the message (non blocking) |
idinor | 0:dcf3c92487ca | 121 | /** |
idinor | 0:dcf3c92487ca | 122 | @param pMessage : pointer to a message |
idinor | 0:dcf3c92487ca | 123 | @param pItem : instance of class on which to execute the callback method |
idinor | 0:dcf3c92487ca | 124 | @param pMethod : callback method |
idinor | 0:dcf3c92487ca | 125 | |
idinor | 0:dcf3c92487ca | 126 | The function returns immediately and calls the callback on completion or error |
idinor | 0:dcf3c92487ca | 127 | */ |
idinor | 0:dcf3c92487ca | 128 | template<class T> |
idinor | 0:dcf3c92487ca | 129 | SMTPResult send(EmailMessage* pMessage, T* pItem, void (T::*pMethod)(SMTPResult)) { //Non blocking |
idinor | 0:dcf3c92487ca | 130 | setOnResult(pItem, pMethod); |
idinor | 0:dcf3c92487ca | 131 | doSend(pMessage); |
idinor | 0:dcf3c92487ca | 132 | return SMTP_PROCESSING; |
idinor | 0:dcf3c92487ca | 133 | } |
idinor | 0:dcf3c92487ca | 134 | |
idinor | 0:dcf3c92487ca | 135 | ///Sends the message (non blocking) |
idinor | 0:dcf3c92487ca | 136 | /** |
idinor | 0:dcf3c92487ca | 137 | @param pMessage : pointer to a message |
idinor | 0:dcf3c92487ca | 138 | |
idinor | 0:dcf3c92487ca | 139 | The function returns immediately and calls the previously set callback on completion or error |
idinor | 0:dcf3c92487ca | 140 | */ |
idinor | 0:dcf3c92487ca | 141 | void doSend(EmailMessage* pMessage); |
idinor | 0:dcf3c92487ca | 142 | |
idinor | 0:dcf3c92487ca | 143 | ///Setup the result callback |
idinor | 0:dcf3c92487ca | 144 | /** |
idinor | 0:dcf3c92487ca | 145 | @param pMethod : callback function |
idinor | 0:dcf3c92487ca | 146 | */ |
idinor | 0:dcf3c92487ca | 147 | void setOnResult( void (*pMethod)(SMTPResult) ); |
idinor | 0:dcf3c92487ca | 148 | |
idinor | 0:dcf3c92487ca | 149 | ///Setup the result callback |
idinor | 0:dcf3c92487ca | 150 | /** |
idinor | 0:dcf3c92487ca | 151 | @param pItem : instance of class on which to execute the callback method |
idinor | 0:dcf3c92487ca | 152 | @param pMethod : callback method |
idinor | 0:dcf3c92487ca | 153 | */ |
idinor | 0:dcf3c92487ca | 154 | class CDummy; |
idinor | 0:dcf3c92487ca | 155 | template<class T> |
idinor | 0:dcf3c92487ca | 156 | void setOnResult( T* pItem, void (T::*pMethod)(SMTPResult) ) { |
idinor | 0:dcf3c92487ca | 157 | m_pCb = NULL; |
idinor | 0:dcf3c92487ca | 158 | m_pCbItem = (CDummy*) pItem; |
idinor | 0:dcf3c92487ca | 159 | m_pCbMeth = (void (CDummy::*)(SMTPResult)) pMethod; |
idinor | 0:dcf3c92487ca | 160 | } |
idinor | 0:dcf3c92487ca | 161 | |
idinor | 0:dcf3c92487ca | 162 | ///Setup timeout |
idinor | 0:dcf3c92487ca | 163 | /** |
idinor | 0:dcf3c92487ca | 164 | @param ms : time of connection inactivity in ms after which the request should timeout |
idinor | 0:dcf3c92487ca | 165 | */ |
idinor | 0:dcf3c92487ca | 166 | void setTimeout(int ms); |
idinor | 0:dcf3c92487ca | 167 | |
idinor | 0:dcf3c92487ca | 168 | ///Gets the last response from the server |
idinor | 0:dcf3c92487ca | 169 | string& getLastResponse(); |
idinor | 0:dcf3c92487ca | 170 | |
idinor | 0:dcf3c92487ca | 171 | virtual void poll(); //Called by NetServices |
idinor | 0:dcf3c92487ca | 172 | |
idinor | 0:dcf3c92487ca | 173 | protected: |
idinor | 0:dcf3c92487ca | 174 | int rc(char* buf); //Return code |
idinor | 0:dcf3c92487ca | 175 | void process(bool writeable); //Main state-machine |
idinor | 0:dcf3c92487ca | 176 | |
idinor | 0:dcf3c92487ca | 177 | void resetTimeout(); |
idinor | 0:dcf3c92487ca | 178 | |
idinor | 0:dcf3c92487ca | 179 | void init(); |
idinor | 0:dcf3c92487ca | 180 | void close(); |
idinor | 0:dcf3c92487ca | 181 | |
idinor | 0:dcf3c92487ca | 182 | void setup(EmailMessage* pMessage); //Setup request, make DNS Req if necessary |
idinor | 0:dcf3c92487ca | 183 | void connect(); //Start Connection |
idinor | 0:dcf3c92487ca | 184 | |
idinor | 0:dcf3c92487ca | 185 | int tryRead(); //Read data and try to feed output |
idinor | 0:dcf3c92487ca | 186 | void readData(); //Data has been read |
idinor | 0:dcf3c92487ca | 187 | void writeData(); //Data has been written & buf is free |
idinor | 0:dcf3c92487ca | 188 | |
idinor | 0:dcf3c92487ca | 189 | void onTCPSocketEvent(TCPSocketEvent e); |
idinor | 0:dcf3c92487ca | 190 | void onDNSReply(DNSReply r); |
idinor | 0:dcf3c92487ca | 191 | void onResult(SMTPResult r); //Called when exchange completed or on failure |
idinor | 0:dcf3c92487ca | 192 | void onTimeout(); //Connection has timed out |
idinor | 0:dcf3c92487ca | 193 | |
idinor | 0:dcf3c92487ca | 194 | string encodePlainAuth(); // Encode plain authentication (username and password in based 64) |
idinor | 0:dcf3c92487ca | 195 | bool okPostAuthentication(int code); // True if server response is ok following authentication |
idinor | 0:dcf3c92487ca | 196 | |
idinor | 0:dcf3c92487ca | 197 | private: |
idinor | 0:dcf3c92487ca | 198 | SMTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available |
idinor | 0:dcf3c92487ca | 199 | |
idinor | 0:dcf3c92487ca | 200 | CDummy* m_pCbItem; |
idinor | 0:dcf3c92487ca | 201 | void (CDummy::*m_pCbMeth)(SMTPResult); |
idinor | 0:dcf3c92487ca | 202 | void (*m_pCb)(SMTPResult); |
idinor | 0:dcf3c92487ca | 203 | |
idinor | 0:dcf3c92487ca | 204 | TCPSocket* m_pTCPSocket; |
idinor | 0:dcf3c92487ca | 205 | |
idinor | 0:dcf3c92487ca | 206 | Timer m_watchdog; |
idinor | 0:dcf3c92487ca | 207 | int m_timeout; |
idinor | 0:dcf3c92487ca | 208 | |
idinor | 0:dcf3c92487ca | 209 | DNSRequest* m_pDnsReq; |
idinor | 0:dcf3c92487ca | 210 | Host m_server; |
idinor | 0:dcf3c92487ca | 211 | |
idinor | 0:dcf3c92487ca | 212 | bool m_closed; |
idinor | 0:dcf3c92487ca | 213 | |
idinor | 0:dcf3c92487ca | 214 | enum SMTPStep { |
idinor | 0:dcf3c92487ca | 215 | SMTP_HELLO, |
idinor | 0:dcf3c92487ca | 216 | SMTP_AUTH, |
idinor | 0:dcf3c92487ca | 217 | SMTP_FROM, |
idinor | 0:dcf3c92487ca | 218 | SMTP_TO, |
idinor | 0:dcf3c92487ca | 219 | SMTP_DATA, |
idinor | 0:dcf3c92487ca | 220 | SMTP_BODY, |
idinor | 0:dcf3c92487ca | 221 | SMTP_BODYMORE, |
idinor | 0:dcf3c92487ca | 222 | SMTP_EOF, |
idinor | 0:dcf3c92487ca | 223 | SMTP_BYE, |
idinor | 0:dcf3c92487ca | 224 | SMTP_CLOSED |
idinor | 0:dcf3c92487ca | 225 | }; |
idinor | 0:dcf3c92487ca | 226 | |
idinor | 0:dcf3c92487ca | 227 | SMTPStep m_state; |
idinor | 0:dcf3c92487ca | 228 | |
idinor | 0:dcf3c92487ca | 229 | EmailMessage* m_pMessage; |
idinor | 0:dcf3c92487ca | 230 | |
idinor | 0:dcf3c92487ca | 231 | int m_posInMsg; |
idinor | 0:dcf3c92487ca | 232 | int m_posInCRLF; |
idinor | 0:dcf3c92487ca | 233 | |
idinor | 0:dcf3c92487ca | 234 | SMTPResult m_blockingResult; //Result if blocking mode |
idinor | 0:dcf3c92487ca | 235 | string m_response; |
idinor | 0:dcf3c92487ca | 236 | int m_responseCode; |
idinor | 0:dcf3c92487ca | 237 | string m_username; |
idinor | 0:dcf3c92487ca | 238 | string m_password; |
idinor | 0:dcf3c92487ca | 239 | SMTPAuth m_auth; |
idinor | 0:dcf3c92487ca | 240 | string m_heloDomain; |
idinor | 0:dcf3c92487ca | 241 | |
idinor | 0:dcf3c92487ca | 242 | vector<string>::iterator m_To; |
idinor | 0:dcf3c92487ca | 243 | }; |
idinor | 0:dcf3c92487ca | 244 | |
idinor | 0:dcf3c92487ca | 245 | #endif // SMTP_CLIENT_H |