Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewbonney 0:ec559500a63f 1
andrewbonney 0:ec559500a63f 2 /*
andrewbonney 0:ec559500a63f 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) y Segundo Equipo
andrewbonney 0:ec559500a63f 4
andrewbonney 0:ec559500a63f 5 Permission is hereby granted, free of charge, to any person obtaining a copy
andrewbonney 0:ec559500a63f 6 of this software and associated documentation files (the "Software"), to deal
andrewbonney 0:ec559500a63f 7 in the Software without restriction, including without limitation the rights
andrewbonney 0:ec559500a63f 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
andrewbonney 0:ec559500a63f 9 copies of the Software, and to permit persons to whom the Software is
andrewbonney 0:ec559500a63f 10 furnished to do so, subject to the following conditions:
andrewbonney 0:ec559500a63f 11
andrewbonney 0:ec559500a63f 12 The above copyright notice and this permission notice shall be included in
andrewbonney 0:ec559500a63f 13 all copies or substantial portions of the Software.
andrewbonney 0:ec559500a63f 14
andrewbonney 0:ec559500a63f 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
andrewbonney 0:ec559500a63f 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
andrewbonney 0:ec559500a63f 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
andrewbonney 0:ec559500a63f 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
andrewbonney 0:ec559500a63f 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
andrewbonney 0:ec559500a63f 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
andrewbonney 0:ec559500a63f 21 THE SOFTWARE.
andrewbonney 0:ec559500a63f 22 */
andrewbonney 0:ec559500a63f 23 #include "core/netservice.h"
andrewbonney 0:ec559500a63f 24 #include "SMTPClient.h"
andrewbonney 0:ec559500a63f 25 #include "../util/base64.h"
andrewbonney 0:ec559500a63f 26
andrewbonney 0:ec559500a63f 27 #define __DEBUG
andrewbonney 0:ec559500a63f 28 #include "dbg/dbg.h"
andrewbonney 0:ec559500a63f 29
andrewbonney 0:ec559500a63f 30 #define CHUNK_SIZE 256 //512
andrewbonney 0:ec559500a63f 31 #define BUF_SIZE (CHUNK_SIZE + 1)
andrewbonney 0:ec559500a63f 32
andrewbonney 0:ec559500a63f 33 #define SMTP_REQUEST_TIMEOUT 15000
andrewbonney 0:ec559500a63f 34 #define SMTP_PORT 25
andrewbonney 0:ec559500a63f 35
andrewbonney 0:ec559500a63f 36 SMTPClient::SMTPClient() : NetService(false) /*Not owned by the pool*/,
andrewbonney 0:ec559500a63f 37 m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL),
andrewbonney 0:ec559500a63f 38 m_watchdog(), m_timeout(SMTP_REQUEST_TIMEOUT), m_pDnsReq(NULL), m_server(),
andrewbonney 0:ec559500a63f 39 m_closed(true), m_state(SMTP_CLOSED), m_pMessage(NULL),
andrewbonney 0:ec559500a63f 40 m_posInMsg(0), m_blockingResult(SMTP_PROCESSING),
andrewbonney 0:ec559500a63f 41 m_username(""), m_password(""), m_auth(SMTP_AUTH_NONE),
andrewbonney 0:ec559500a63f 42 m_heloDomain("localhost") {
andrewbonney 0:ec559500a63f 43 DBG("New SMTPClient %p\n", this);
andrewbonney 0:ec559500a63f 44 }
andrewbonney 0:ec559500a63f 45
andrewbonney 0:ec559500a63f 46 SMTPClient::SMTPClient(const Host& host, const char* heloDomain, const char* user, const char* password, SMTPAuth auth) : NetService(false),
andrewbonney 0:ec559500a63f 47 m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL),
andrewbonney 0:ec559500a63f 48 m_watchdog(), m_timeout(SMTP_REQUEST_TIMEOUT), m_pDnsReq(NULL), m_server(host),
andrewbonney 0:ec559500a63f 49 m_closed(true), m_state(SMTP_CLOSED), m_pMessage(NULL),
andrewbonney 0:ec559500a63f 50 m_posInMsg(0), m_blockingResult(SMTP_PROCESSING),
andrewbonney 0:ec559500a63f 51 m_username(string(user)), m_password(string(password)), m_auth(auth),
andrewbonney 0:ec559500a63f 52 m_heloDomain(heloDomain) {
andrewbonney 0:ec559500a63f 53 DBG("New SMTPClient %p\n", this);
andrewbonney 0:ec559500a63f 54 }
andrewbonney 0:ec559500a63f 55
andrewbonney 0:ec559500a63f 56 SMTPClient::~SMTPClient() {
andrewbonney 0:ec559500a63f 57 close();
andrewbonney 0:ec559500a63f 58 }
andrewbonney 0:ec559500a63f 59
andrewbonney 0:ec559500a63f 60 void SMTPClient::setAuth(const char* user, const char* password) { // Plain authentication
andrewbonney 0:ec559500a63f 61 m_username = string(user);
andrewbonney 0:ec559500a63f 62 m_password = string(password);
andrewbonney 0:ec559500a63f 63 m_auth = SMTP_AUTH_PLAIN;
andrewbonney 0:ec559500a63f 64 }
andrewbonney 0:ec559500a63f 65
andrewbonney 0:ec559500a63f 66 void SMTPClient::clearAuth() { // Clear authentication
andrewbonney 0:ec559500a63f 67 m_username = "";
andrewbonney 0:ec559500a63f 68 m_password = "";
andrewbonney 0:ec559500a63f 69 m_auth = SMTP_AUTH_NONE;
andrewbonney 0:ec559500a63f 70 }
andrewbonney 0:ec559500a63f 71
andrewbonney 0:ec559500a63f 72 string SMTPClient::encodePlainAuth() {
andrewbonney 0:ec559500a63f 73 string decStr = m_username;
andrewbonney 0:ec559500a63f 74 decStr += '\0';
andrewbonney 0:ec559500a63f 75 decStr += m_username;
andrewbonney 0:ec559500a63f 76 decStr += '\0';
andrewbonney 0:ec559500a63f 77 decStr += m_password;
andrewbonney 0:ec559500a63f 78
andrewbonney 0:ec559500a63f 79 string auth = "AUTH PLAIN ";
andrewbonney 0:ec559500a63f 80 return auth.append(Base64::encode(decStr));
andrewbonney 0:ec559500a63f 81 }
andrewbonney 0:ec559500a63f 82
andrewbonney 0:ec559500a63f 83 void SMTPClient::setHeloDomain(const char* heloDomain) {
andrewbonney 0:ec559500a63f 84 m_heloDomain = string(heloDomain);
andrewbonney 0:ec559500a63f 85 }
andrewbonney 0:ec559500a63f 86
andrewbonney 0:ec559500a63f 87 SMTPResult SMTPClient::send(EmailMessage* pMessage) { //Blocking
andrewbonney 0:ec559500a63f 88 doSend(pMessage);
andrewbonney 0:ec559500a63f 89 return blockingProcess();
andrewbonney 0:ec559500a63f 90 }
andrewbonney 0:ec559500a63f 91
andrewbonney 0:ec559500a63f 92 SMTPResult SMTPClient::send(EmailMessage* pMessage, void (*pMethod)(SMTPResult)) { //Non blocking
andrewbonney 0:ec559500a63f 93 setOnResult(pMethod);
andrewbonney 0:ec559500a63f 94 doSend(pMessage);
andrewbonney 0:ec559500a63f 95 return SMTP_PROCESSING;
andrewbonney 0:ec559500a63f 96 }
andrewbonney 0:ec559500a63f 97
andrewbonney 0:ec559500a63f 98 void SMTPClient::doSend(EmailMessage* pMessage) {
andrewbonney 0:ec559500a63f 99 setup(pMessage);
andrewbonney 0:ec559500a63f 100 }
andrewbonney 0:ec559500a63f 101
andrewbonney 0:ec559500a63f 102 void SMTPClient::setOnResult( void (*pMethod)(SMTPResult) ) {
andrewbonney 0:ec559500a63f 103 m_pCb = pMethod;
andrewbonney 0:ec559500a63f 104 m_pCbItem = NULL;
andrewbonney 0:ec559500a63f 105 m_pCbMeth = NULL;
andrewbonney 0:ec559500a63f 106 }
andrewbonney 0:ec559500a63f 107
andrewbonney 0:ec559500a63f 108 void SMTPClient::setTimeout(int ms) {
andrewbonney 0:ec559500a63f 109 m_timeout = ms;
andrewbonney 0:ec559500a63f 110 }
andrewbonney 0:ec559500a63f 111
andrewbonney 0:ec559500a63f 112 void SMTPClient::poll() { //Called by NetServices
andrewbonney 0:ec559500a63f 113 if ( (!m_closed) && (m_watchdog.read_ms() >= m_timeout) ) {
andrewbonney 0:ec559500a63f 114 onTimeout();
andrewbonney 0:ec559500a63f 115 }
andrewbonney 0:ec559500a63f 116 }
andrewbonney 0:ec559500a63f 117
andrewbonney 0:ec559500a63f 118 void SMTPClient::resetTimeout() {
andrewbonney 0:ec559500a63f 119 m_watchdog.reset();
andrewbonney 0:ec559500a63f 120 m_watchdog.start();
andrewbonney 0:ec559500a63f 121 }
andrewbonney 0:ec559500a63f 122
andrewbonney 0:ec559500a63f 123 void SMTPClient::init() { //Create and setup socket if needed
andrewbonney 0:ec559500a63f 124 close(); //Remove previous elements
andrewbonney 0:ec559500a63f 125 if (!m_closed) //Already opened
andrewbonney 0:ec559500a63f 126 return;
andrewbonney 0:ec559500a63f 127 m_state = SMTP_HELLO;
andrewbonney 0:ec559500a63f 128 m_pTCPSocket = new TCPSocket;
andrewbonney 0:ec559500a63f 129 m_pTCPSocket->setOnEvent(this, &SMTPClient::onTCPSocketEvent);
andrewbonney 0:ec559500a63f 130 m_closed = false;
andrewbonney 0:ec559500a63f 131 m_posInMsg = 0;
andrewbonney 0:ec559500a63f 132 m_posInCRLF = 2;
andrewbonney 0:ec559500a63f 133 m_response = "";
andrewbonney 0:ec559500a63f 134 }
andrewbonney 0:ec559500a63f 135
andrewbonney 0:ec559500a63f 136 void SMTPClient::close() {
andrewbonney 0:ec559500a63f 137 if (m_closed)
andrewbonney 0:ec559500a63f 138 return;
andrewbonney 0:ec559500a63f 139 m_state = SMTP_CLOSED;
andrewbonney 0:ec559500a63f 140 m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
andrewbonney 0:ec559500a63f 141 m_watchdog.stop(); //Stop timeout
andrewbonney 0:ec559500a63f 142 m_watchdog.reset();
andrewbonney 0:ec559500a63f 143 m_pTCPSocket->resetOnEvent();
andrewbonney 0:ec559500a63f 144 m_pTCPSocket->close();
andrewbonney 0:ec559500a63f 145 delete m_pTCPSocket;
andrewbonney 0:ec559500a63f 146 m_pTCPSocket = NULL;
andrewbonney 0:ec559500a63f 147 if ( m_pDnsReq ) {
andrewbonney 0:ec559500a63f 148 m_pDnsReq->close();
andrewbonney 0:ec559500a63f 149 delete m_pDnsReq;
andrewbonney 0:ec559500a63f 150 m_pDnsReq = NULL;
andrewbonney 0:ec559500a63f 151 }
andrewbonney 0:ec559500a63f 152 }
andrewbonney 0:ec559500a63f 153
andrewbonney 0:ec559500a63f 154 void SMTPClient::setServer(const Host& host) { //Setup request, make DNS Req if necessary
andrewbonney 0:ec559500a63f 155 m_server = host;
andrewbonney 0:ec559500a63f 156 }
andrewbonney 0:ec559500a63f 157
andrewbonney 0:ec559500a63f 158 void SMTPClient::setup(EmailMessage* pMessage) { //Setup request, make DNS Req if necessary
andrewbonney 0:ec559500a63f 159
andrewbonney 0:ec559500a63f 160 init(); //Initialize client in known state, create socket
andrewbonney 0:ec559500a63f 161 m_pMessage = pMessage;
andrewbonney 0:ec559500a63f 162 resetTimeout();
andrewbonney 0:ec559500a63f 163
andrewbonney 0:ec559500a63f 164 m_To = m_pMessage->m_lTo.begin(); // Point to first to recipient in TO list
andrewbonney 0:ec559500a63f 165
andrewbonney 0:ec559500a63f 166 //If port set to zero then use default port
andrewbonney 0:ec559500a63f 167 if (!m_server.getPort()) {
andrewbonney 0:ec559500a63f 168 m_server.setPort(SMTP_PORT);
andrewbonney 0:ec559500a63f 169 DBG("Using default port %d\n", SMTP_PORT);
andrewbonney 0:ec559500a63f 170 }
andrewbonney 0:ec559500a63f 171
andrewbonney 0:ec559500a63f 172 if (m_server.getIp().isNull()) {
andrewbonney 0:ec559500a63f 173 //DNS query required
andrewbonney 0:ec559500a63f 174 m_pDnsReq = new DNSRequest();
andrewbonney 0:ec559500a63f 175 DBG("DNSRequest %p\r\n", m_pDnsReq);
andrewbonney 0:ec559500a63f 176 m_pDnsReq->setOnReply(this, &SMTPClient::onDNSReply);
andrewbonney 0:ec559500a63f 177 m_pDnsReq->resolve(&m_server);
andrewbonney 0:ec559500a63f 178 return;
andrewbonney 0:ec559500a63f 179 } else
andrewbonney 0:ec559500a63f 180 connect();
andrewbonney 0:ec559500a63f 181
andrewbonney 0:ec559500a63f 182 }
andrewbonney 0:ec559500a63f 183
andrewbonney 0:ec559500a63f 184 void SMTPClient::connect() { //Start Connection
andrewbonney 0:ec559500a63f 185 resetTimeout();
andrewbonney 0:ec559500a63f 186 DBG("Connecting...\n");
andrewbonney 0:ec559500a63f 187 m_pTCPSocket->connect(m_server);
andrewbonney 0:ec559500a63f 188 }
andrewbonney 0:ec559500a63f 189
andrewbonney 0:ec559500a63f 190 void SMTPClient::onTCPSocketEvent(TCPSocketEvent e) {
andrewbonney 0:ec559500a63f 191
andrewbonney 0:ec559500a63f 192 DBG("Event %d in SMTPClient::onTCPSocketEvent()\n", e);
andrewbonney 0:ec559500a63f 193
andrewbonney 0:ec559500a63f 194 if (m_closed) {
andrewbonney 0:ec559500a63f 195 DBG("WARN: Discarded\n");
andrewbonney 0:ec559500a63f 196 return;
andrewbonney 0:ec559500a63f 197 }
andrewbonney 0:ec559500a63f 198
andrewbonney 0:ec559500a63f 199 switch (e) {
andrewbonney 0:ec559500a63f 200 case TCPSOCKET_READABLE:
andrewbonney 0:ec559500a63f 201 resetTimeout();
andrewbonney 0:ec559500a63f 202 process(false);
andrewbonney 0:ec559500a63f 203 break;
andrewbonney 0:ec559500a63f 204 case TCPSOCKET_WRITEABLE:
andrewbonney 0:ec559500a63f 205 resetTimeout();
andrewbonney 0:ec559500a63f 206 process(true);
andrewbonney 0:ec559500a63f 207 break;
andrewbonney 0:ec559500a63f 208 case TCPSOCKET_CONTIMEOUT:
andrewbonney 0:ec559500a63f 209 case TCPSOCKET_CONRST:
andrewbonney 0:ec559500a63f 210 case TCPSOCKET_CONABRT:
andrewbonney 0:ec559500a63f 211 case TCPSOCKET_ERROR:
andrewbonney 0:ec559500a63f 212 DBG("Connection error in SMTP Client.\n");
andrewbonney 0:ec559500a63f 213 close();
andrewbonney 0:ec559500a63f 214 onResult(SMTP_DISC);
andrewbonney 0:ec559500a63f 215 break;
andrewbonney 0:ec559500a63f 216 case TCPSOCKET_DISCONNECTED:
andrewbonney 0:ec559500a63f 217 if (m_state != SMTP_BYE) {
andrewbonney 0:ec559500a63f 218 DBG("Connection error in SMTP Client.\n");
andrewbonney 0:ec559500a63f 219 close();
andrewbonney 0:ec559500a63f 220 onResult(SMTP_DISC);
andrewbonney 0:ec559500a63f 221 }
andrewbonney 0:ec559500a63f 222 break;
andrewbonney 0:ec559500a63f 223 }
andrewbonney 0:ec559500a63f 224 }
andrewbonney 0:ec559500a63f 225
andrewbonney 0:ec559500a63f 226 void SMTPClient::onDNSReply(DNSReply r) {
andrewbonney 0:ec559500a63f 227 if (m_closed) {
andrewbonney 0:ec559500a63f 228 DBG("WARN: Discarded\n");
andrewbonney 0:ec559500a63f 229 return;
andrewbonney 0:ec559500a63f 230 }
andrewbonney 0:ec559500a63f 231
andrewbonney 0:ec559500a63f 232 if ( r != DNS_FOUND ) {
andrewbonney 0:ec559500a63f 233 DBG("Could not resolve hostname.\n");
andrewbonney 0:ec559500a63f 234 close();
andrewbonney 0:ec559500a63f 235 onResult(SMTP_DNS);
andrewbonney 0:ec559500a63f 236 return;
andrewbonney 0:ec559500a63f 237 }
andrewbonney 0:ec559500a63f 238
andrewbonney 0:ec559500a63f 239 DBG("DNS Resolved to %d.%d.%d.%d\n", m_server.getIp()[0], m_server.getIp()[1], m_server.getIp()[2], m_server.getIp()[3]);
andrewbonney 0:ec559500a63f 240 //If no error, m_server has been updated by m_pDnsReq so we're set to go !
andrewbonney 0:ec559500a63f 241 m_pDnsReq->close();
andrewbonney 0:ec559500a63f 242 delete m_pDnsReq;
andrewbonney 0:ec559500a63f 243 m_pDnsReq = NULL;
andrewbonney 0:ec559500a63f 244 connect();
andrewbonney 0:ec559500a63f 245 }
andrewbonney 0:ec559500a63f 246
andrewbonney 0:ec559500a63f 247 void SMTPClient::onResult(SMTPResult r) { //Called when exchange completed or on failure
andrewbonney 0:ec559500a63f 248 if (m_pCbItem && m_pCbMeth)
andrewbonney 0:ec559500a63f 249 (m_pCbItem->*m_pCbMeth)(r);
andrewbonney 0:ec559500a63f 250 else if (m_pCb)
andrewbonney 0:ec559500a63f 251 m_pCb(r);
andrewbonney 0:ec559500a63f 252 m_blockingResult = r; //Blocking mode
andrewbonney 0:ec559500a63f 253 }
andrewbonney 0:ec559500a63f 254
andrewbonney 0:ec559500a63f 255 void SMTPClient::onTimeout() { //Connection has timed out
andrewbonney 0:ec559500a63f 256 DBG("Timed out.\n");
andrewbonney 0:ec559500a63f 257 close();
andrewbonney 0:ec559500a63f 258 onResult(SMTP_TIMEOUT);
andrewbonney 0:ec559500a63f 259 }
andrewbonney 0:ec559500a63f 260
andrewbonney 0:ec559500a63f 261 SMTPResult SMTPClient::blockingProcess() { //Called in blocking mode, calls Net::poll() until return code is available
andrewbonney 0:ec559500a63f 262 //Disable callbacks
andrewbonney 0:ec559500a63f 263 m_pCb = NULL;
andrewbonney 0:ec559500a63f 264 m_pCbItem = NULL;
andrewbonney 0:ec559500a63f 265 m_pCbMeth = NULL;
andrewbonney 0:ec559500a63f 266 m_blockingResult = SMTP_PROCESSING;
andrewbonney 0:ec559500a63f 267 do {
andrewbonney 0:ec559500a63f 268 Net::poll();
andrewbonney 0:ec559500a63f 269 } while (m_blockingResult == SMTP_PROCESSING);
andrewbonney 0:ec559500a63f 270 Net::poll(); //Necessary for cleanup
andrewbonney 0:ec559500a63f 271 return m_blockingResult;
andrewbonney 0:ec559500a63f 272 }
andrewbonney 0:ec559500a63f 273
andrewbonney 0:ec559500a63f 274 int SMTPClient::rc(char* buf) { //Parse return code
andrewbonney 0:ec559500a63f 275 int rc;
andrewbonney 0:ec559500a63f 276 int len = sscanf(buf, "%d %*[^\r\n]\r\n", &rc);
andrewbonney 0:ec559500a63f 277 if (len != 1)
andrewbonney 0:ec559500a63f 278 return -1;
andrewbonney 0:ec559500a63f 279 return rc;
andrewbonney 0:ec559500a63f 280 }
andrewbonney 0:ec559500a63f 281
andrewbonney 0:ec559500a63f 282 bool SMTPClient::okPostAuthentication(int code) {
andrewbonney 0:ec559500a63f 283 return (code == 250) || (code == 235);
andrewbonney 0:ec559500a63f 284 }
andrewbonney 0:ec559500a63f 285
andrewbonney 0:ec559500a63f 286 void SMTPClient::process(bool writeable) { //Main state-machine
andrewbonney 0:ec559500a63f 287
andrewbonney 0:ec559500a63f 288 DBG("In state %d, writeable %d\n", m_state, writeable);
andrewbonney 0:ec559500a63f 289
andrewbonney 0:ec559500a63f 290 // If writeable but nothing to write then return
andrewbonney 0:ec559500a63f 291 if (writeable && (m_state != SMTP_BODYMORE))
andrewbonney 0:ec559500a63f 292 return;
andrewbonney 0:ec559500a63f 293
andrewbonney 0:ec559500a63f 294 // If not writeable then read
andrewbonney 0:ec559500a63f 295 char buf[BUF_SIZE] = {0};
andrewbonney 0:ec559500a63f 296 int responseCode = -1;
andrewbonney 0:ec559500a63f 297 if (!writeable) {
andrewbonney 0:ec559500a63f 298 bool firstBuf = true;
andrewbonney 0:ec559500a63f 299 int read;
andrewbonney 0:ec559500a63f 300 do { // read until nothing left but only process the response from first buffer
andrewbonney 0:ec559500a63f 301 read = m_pTCPSocket->recv(buf, BUF_SIZE - 1);
andrewbonney 0:ec559500a63f 302 if (firstBuf) {
andrewbonney 0:ec559500a63f 303 m_response = string(buf);
andrewbonney 0:ec559500a63f 304 responseCode = rc(buf);
andrewbonney 0:ec559500a63f 305 firstBuf = false;
andrewbonney 0:ec559500a63f 306 DBG("Response %d %d | %s", read, responseCode, buf);
andrewbonney 0:ec559500a63f 307 }
andrewbonney 0:ec559500a63f 308 } while (read > 0);
andrewbonney 0:ec559500a63f 309 }
andrewbonney 0:ec559500a63f 310
andrewbonney 0:ec559500a63f 311 switch (m_state) {
andrewbonney 0:ec559500a63f 312 case SMTP_HELLO:
andrewbonney 0:ec559500a63f 313 if ( responseCode != 220 ) {
andrewbonney 0:ec559500a63f 314 close();
andrewbonney 0:ec559500a63f 315 onResult(SMTP_PRTCL);
andrewbonney 0:ec559500a63f 316 return;
andrewbonney 0:ec559500a63f 317 }
andrewbonney 0:ec559500a63f 318 char* helloCommand;
andrewbonney 0:ec559500a63f 319 if (m_auth == SMTP_AUTH_NONE) {
andrewbonney 0:ec559500a63f 320 helloCommand = "HELO";
andrewbonney 0:ec559500a63f 321 m_state = SMTP_FROM;
andrewbonney 0:ec559500a63f 322 } else {
andrewbonney 0:ec559500a63f 323 helloCommand = "EHLO";
andrewbonney 0:ec559500a63f 324 m_state = SMTP_AUTH;
andrewbonney 0:ec559500a63f 325 }
andrewbonney 0:ec559500a63f 326 snprintf(buf, BUF_SIZE, "%s %s\r\n", helloCommand, m_heloDomain.c_str());
andrewbonney 0:ec559500a63f 327 break;
andrewbonney 0:ec559500a63f 328 case SMTP_AUTH:
andrewbonney 0:ec559500a63f 329 if ( responseCode != 250 ) {
andrewbonney 0:ec559500a63f 330 close();
andrewbonney 0:ec559500a63f 331 onResult(SMTP_PRTCL);
andrewbonney 0:ec559500a63f 332 return;
andrewbonney 0:ec559500a63f 333 }
andrewbonney 0:ec559500a63f 334 snprintf(buf, BUF_SIZE, "%s\r\n", encodePlainAuth().c_str());
andrewbonney 0:ec559500a63f 335 m_state = SMTP_FROM;
andrewbonney 0:ec559500a63f 336 break;
andrewbonney 0:ec559500a63f 337 case SMTP_FROM:
andrewbonney 0:ec559500a63f 338 if (!okPostAuthentication(responseCode)) {
andrewbonney 0:ec559500a63f 339 close();
andrewbonney 0:ec559500a63f 340 onResult(SMTP_PRTCL);
andrewbonney 0:ec559500a63f 341 return;
andrewbonney 0:ec559500a63f 342 }
andrewbonney 0:ec559500a63f 343 snprintf(buf, BUF_SIZE, "MAIL FROM:<%s>\r\n", m_pMessage->m_from.c_str());
andrewbonney 0:ec559500a63f 344 m_state = SMTP_TO;
andrewbonney 0:ec559500a63f 345 break;
andrewbonney 0:ec559500a63f 346 case SMTP_TO:
andrewbonney 0:ec559500a63f 347 if ( responseCode != 250 ) {
andrewbonney 0:ec559500a63f 348 close();
andrewbonney 0:ec559500a63f 349 onResult(SMTP_PRTCL);
andrewbonney 0:ec559500a63f 350 return;
andrewbonney 0:ec559500a63f 351 }
andrewbonney 0:ec559500a63f 352 snprintf(buf, BUF_SIZE, "RCPT TO:<%s>\r\n", (m_To++)->c_str());
andrewbonney 0:ec559500a63f 353 if (m_To == m_pMessage->m_lTo.end())
andrewbonney 0:ec559500a63f 354 m_state = SMTP_DATA;
andrewbonney 0:ec559500a63f 355 break;
andrewbonney 0:ec559500a63f 356 case SMTP_DATA:
andrewbonney 0:ec559500a63f 357 if ( responseCode != 250 ) {
andrewbonney 0:ec559500a63f 358 close();
andrewbonney 0:ec559500a63f 359 onResult(SMTP_PRTCL);
andrewbonney 0:ec559500a63f 360 return;
andrewbonney 0:ec559500a63f 361 }
andrewbonney 0:ec559500a63f 362 snprintf(buf, BUF_SIZE, "DATA\r\n");
andrewbonney 0:ec559500a63f 363 m_state = SMTP_BODY;
andrewbonney 0:ec559500a63f 364 break;
andrewbonney 0:ec559500a63f 365 case SMTP_BODY:
andrewbonney 0:ec559500a63f 366 if ( responseCode != 354 ) {
andrewbonney 0:ec559500a63f 367 close();
andrewbonney 0:ec559500a63f 368 onResult(SMTP_PRTCL);
andrewbonney 0:ec559500a63f 369 return;
andrewbonney 0:ec559500a63f 370 }
andrewbonney 0:ec559500a63f 371 m_state = SMTP_BODYMORE;
andrewbonney 0:ec559500a63f 372 buf[0] = '\0'; // clear buffer before carrying on into next state
andrewbonney 0:ec559500a63f 373 case SMTP_BODYMORE:
andrewbonney 0:ec559500a63f 374 if (strlen(buf) > 0) { // sending interrupted by a server response
andrewbonney 0:ec559500a63f 375 close();
andrewbonney 0:ec559500a63f 376 onResult(SMTP_PRTCL);
andrewbonney 0:ec559500a63f 377 return;
andrewbonney 0:ec559500a63f 378 }
andrewbonney 0:ec559500a63f 379
andrewbonney 0:ec559500a63f 380 if ( m_posInMsg < m_pMessage->m_content.length() ) { // if still something to send
andrewbonney 0:ec559500a63f 381 int sendLen = 0;
andrewbonney 0:ec559500a63f 382 while (sendLen < BUF_SIZE - 1) { // - 1 to allow room for extra dot or CR or LF
andrewbonney 0:ec559500a63f 383 char c = m_pMessage->m_content.at(m_posInMsg++);
andrewbonney 0:ec559500a63f 384 switch (c) { // thanks ExtraDotOutputStream.java (with extra check for naked CR)
andrewbonney 0:ec559500a63f 385 case '.':
andrewbonney 0:ec559500a63f 386 if (m_posInCRLF == 2) // add extra dot
andrewbonney 0:ec559500a63f 387 buf[sendLen++] = '.';
andrewbonney 0:ec559500a63f 388 m_posInCRLF = 0;
andrewbonney 0:ec559500a63f 389 break;
andrewbonney 0:ec559500a63f 390 case '\r':
andrewbonney 0:ec559500a63f 391 if (m_posInCRLF == 1) // two CR in a row, so insert an LF first
andrewbonney 0:ec559500a63f 392 buf[sendLen++] = '\n';
andrewbonney 0:ec559500a63f 393 m_posInCRLF = 1;
andrewbonney 0:ec559500a63f 394 break;
andrewbonney 0:ec559500a63f 395 case '\n':
andrewbonney 0:ec559500a63f 396 if (m_posInCRLF != 1) // convert naked LF to CRLF
andrewbonney 0:ec559500a63f 397 buf[sendLen++] = '\r';
andrewbonney 0:ec559500a63f 398 m_posInCRLF = 2;
andrewbonney 0:ec559500a63f 399 break;
andrewbonney 0:ec559500a63f 400 default:
andrewbonney 0:ec559500a63f 401 if (m_posInCRLF == 1) { // convert naked CR to CRLF
andrewbonney 0:ec559500a63f 402 buf[sendLen++] = '\n';
andrewbonney 0:ec559500a63f 403 m_posInCRLF = 2;
andrewbonney 0:ec559500a63f 404 } else
andrewbonney 0:ec559500a63f 405 m_posInCRLF = 0; // we're no longer at the start of a line
andrewbonney 0:ec559500a63f 406 break;
andrewbonney 0:ec559500a63f 407 }
andrewbonney 0:ec559500a63f 408 buf[sendLen++] = c;
andrewbonney 0:ec559500a63f 409 if ( m_posInMsg == m_pMessage->m_content.length() )
andrewbonney 0:ec559500a63f 410 break;
andrewbonney 0:ec559500a63f 411 }
andrewbonney 0:ec559500a63f 412 m_pTCPSocket->send( buf, sendLen );
andrewbonney 0:ec559500a63f 413 DBG("Sending %d bytes of processed message content\n", sendLen);
andrewbonney 0:ec559500a63f 414 } else {
andrewbonney 0:ec559500a63f 415 if (m_posInCRLF == 0)
andrewbonney 0:ec559500a63f 416 snprintf(buf, BUF_SIZE, "\r\n.\r\n");
andrewbonney 0:ec559500a63f 417 else if (m_posInCRLF == 1)
andrewbonney 0:ec559500a63f 418 snprintf(buf, BUF_SIZE, "\n.\r\n");
andrewbonney 0:ec559500a63f 419 else
andrewbonney 0:ec559500a63f 420 snprintf(buf, BUF_SIZE, ".\r\n");
andrewbonney 0:ec559500a63f 421 m_state = SMTP_EOF;
andrewbonney 0:ec559500a63f 422 }
andrewbonney 0:ec559500a63f 423 break;
andrewbonney 0:ec559500a63f 424 case SMTP_EOF:
andrewbonney 0:ec559500a63f 425 if ( responseCode != 250 ) {
andrewbonney 0:ec559500a63f 426 close();
andrewbonney 0:ec559500a63f 427 onResult(SMTP_PRTCL);
andrewbonney 0:ec559500a63f 428 return;
andrewbonney 0:ec559500a63f 429 }
andrewbonney 0:ec559500a63f 430 snprintf(buf, BUF_SIZE, "QUIT\r\n");
andrewbonney 0:ec559500a63f 431 m_state = SMTP_BYE;
andrewbonney 0:ec559500a63f 432 break;
andrewbonney 0:ec559500a63f 433 case SMTP_BYE:
andrewbonney 0:ec559500a63f 434 if ( responseCode != 221 ) {
andrewbonney 0:ec559500a63f 435 close();
andrewbonney 0:ec559500a63f 436 onResult(SMTP_PRTCL);
andrewbonney 0:ec559500a63f 437 return;
andrewbonney 0:ec559500a63f 438 }
andrewbonney 0:ec559500a63f 439 close();
andrewbonney 0:ec559500a63f 440 onResult(SMTP_OK);
andrewbonney 0:ec559500a63f 441 return;
andrewbonney 0:ec559500a63f 442 }
andrewbonney 0:ec559500a63f 443
andrewbonney 0:ec559500a63f 444 if ( m_state != SMTP_BODYMORE ) {
andrewbonney 0:ec559500a63f 445 DBG("Sending | %s", buf);
andrewbonney 0:ec559500a63f 446 m_pTCPSocket->send( buf, strlen(buf) );
andrewbonney 0:ec559500a63f 447 }
andrewbonney 0:ec559500a63f 448
andrewbonney 0:ec559500a63f 449 }
andrewbonney 0:ec559500a63f 450
andrewbonney 0:ec559500a63f 451 string& SMTPClient::getLastResponse() { // Return last response set on result
andrewbonney 0:ec559500a63f 452 return m_response;
andrewbonney 0:ec559500a63f 453 }