Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

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