EthernetNetIf Compatibility.
Dependents: XBeeWiFi_SPI_example
Fork of NetServicesSource by
services/email/smtp/SMTPClient.cpp@5:dd63a1e02b1b, 2010-07-27 (annotated)
- Committer:
- donatien
- Date:
- Tue Jul 27 15:59:42 2010 +0000
- Revision:
- 5:dd63a1e02b1b
- Parent:
- 1:abb442332fa8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:632c9925f013 | 1 | |
donatien | 0:632c9925f013 | 2 | /* |
donatien | 0:632c9925f013 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
donatien | 0:632c9925f013 | 4 | |
donatien | 0:632c9925f013 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
donatien | 0:632c9925f013 | 6 | of this software and associated documentation files (the "Software"), to deal |
donatien | 0:632c9925f013 | 7 | in the Software without restriction, including without limitation the rights |
donatien | 0:632c9925f013 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
donatien | 0:632c9925f013 | 9 | copies of the Software, and to permit persons to whom the Software is |
donatien | 0:632c9925f013 | 10 | furnished to do so, subject to the following conditions: |
donatien | 0:632c9925f013 | 11 | |
donatien | 0:632c9925f013 | 12 | The above copyright notice and this permission notice shall be included in |
donatien | 0:632c9925f013 | 13 | all copies or substantial portions of the Software. |
donatien | 0:632c9925f013 | 14 | |
donatien | 0:632c9925f013 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
donatien | 0:632c9925f013 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
donatien | 0:632c9925f013 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
donatien | 0:632c9925f013 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
donatien | 0:632c9925f013 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 0:632c9925f013 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
donatien | 0:632c9925f013 | 21 | THE SOFTWARE. |
donatien | 0:632c9925f013 | 22 | */ |
donatien | 0:632c9925f013 | 23 | |
donatien | 1:abb442332fa8 | 24 | #include "SMTPClient.h" |
donatien | 0:632c9925f013 | 25 | |
donatien | 5:dd63a1e02b1b | 26 | /* |
donatien | 5:dd63a1e02b1b | 27 | Provided as reference only, this code has not been tested. |
donatien | 5:dd63a1e02b1b | 28 | */ |
donatien | 0:632c9925f013 | 29 | #if 0 |
donatien | 0:632c9925f013 | 30 | |
donatien | 0:632c9925f013 | 31 | #include <stdio.h> |
donatien | 0:632c9925f013 | 32 | |
donatien | 0:632c9925f013 | 33 | #define __DEBUG |
donatien | 0:632c9925f013 | 34 | #include "dbg.h" |
donatien | 0:632c9925f013 | 35 | |
donatien | 0:632c9925f013 | 36 | #define BUF_SIZE 128 |
donatien | 0:632c9925f013 | 37 | #define CHUNK_SIZE 512 |
donatien | 0:632c9925f013 | 38 | |
donatien | 0:632c9925f013 | 39 | SMTPClient::SMTPClient() : m_pMessage(NULL), m_nextState(SMTP_HELLO), |
donatien | 0:632c9925f013 | 40 | m_pCbItem(NULL), m_pCbMeth(NULL), m_watchdog(), m_timeout(0), m_posInMsg(0), m_closed(true), m_host() |
donatien | 0:632c9925f013 | 41 | { |
donatien | 0:632c9925f013 | 42 | setTimeout(SMTP_REQUEST_TIMEOUT); |
donatien | 0:632c9925f013 | 43 | } |
donatien | 0:632c9925f013 | 44 | |
donatien | 0:632c9925f013 | 45 | SMTPClient::~SMTPClient() |
donatien | 0:632c9925f013 | 46 | { |
donatien | 0:632c9925f013 | 47 | close(); |
donatien | 0:632c9925f013 | 48 | } |
donatien | 0:632c9925f013 | 49 | |
donatien | 0:632c9925f013 | 50 | void SMTPClient::setHost(const Host& host) |
donatien | 0:632c9925f013 | 51 | { |
donatien | 0:632c9925f013 | 52 | m_host = host; |
donatien | 0:632c9925f013 | 53 | } |
donatien | 0:632c9925f013 | 54 | |
donatien | 0:632c9925f013 | 55 | void SMTPClient::send(EmailMessage* pMessage) |
donatien | 0:632c9925f013 | 56 | { |
donatien | 0:632c9925f013 | 57 | init(); |
donatien | 0:632c9925f013 | 58 | m_posInMsg = 0; |
donatien | 0:632c9925f013 | 59 | m_nextState = SMTP_HELLO; |
donatien | 0:632c9925f013 | 60 | if( !m_pTCPSocket->connect(m_host) ) |
donatien | 0:632c9925f013 | 61 | { |
donatien | 0:632c9925f013 | 62 | close(); |
donatien | 0:632c9925f013 | 63 | onResult(SMTP_DISC); |
donatien | 0:632c9925f013 | 64 | } |
donatien | 0:632c9925f013 | 65 | } |
donatien | 0:632c9925f013 | 66 | |
donatien | 0:632c9925f013 | 67 | void SMTPClient::init() //Create and setup socket if needed |
donatien | 0:632c9925f013 | 68 | { |
donatien | 0:632c9925f013 | 69 | close(); //Remove previous elements |
donatien | 0:632c9925f013 | 70 | if(!m_closed) //Already opened |
donatien | 0:632c9925f013 | 71 | return; |
donatien | 0:632c9925f013 | 72 | m_nextState = SMTP_HELLO; |
donatien | 0:632c9925f013 | 73 | m_pTCPSocket = new TCPSocket; |
donatien | 0:632c9925f013 | 74 | m_pTCPSocket->setOnEvent(this, &SMTPClient::onTCPSocketEvent); |
donatien | 0:632c9925f013 | 75 | m_closed = false; |
donatien | 0:632c9925f013 | 76 | } |
donatien | 0:632c9925f013 | 77 | |
donatien | 0:632c9925f013 | 78 | void SMTPClient::close() |
donatien | 0:632c9925f013 | 79 | { |
donatien | 0:632c9925f013 | 80 | if(m_closed) |
donatien | 0:632c9925f013 | 81 | return; |
donatien | 0:632c9925f013 | 82 | m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else |
donatien | 0:632c9925f013 | 83 | m_watchdog.detach(); |
donatien | 0:632c9925f013 | 84 | m_pTCPSocket->resetOnEvent(); |
donatien | 0:632c9925f013 | 85 | m_pTCPSocket->close(); |
donatien | 0:632c9925f013 | 86 | delete m_pTCPSocket; |
donatien | 0:632c9925f013 | 87 | m_pTCPSocket = NULL; |
donatien | 0:632c9925f013 | 88 | } |
donatien | 0:632c9925f013 | 89 | |
donatien | 0:632c9925f013 | 90 | int SMTPClient::rc(char* buf) //Parse return code |
donatien | 0:632c9925f013 | 91 | { |
donatien | 0:632c9925f013 | 92 | int rc; |
donatien | 0:632c9925f013 | 93 | int len = sscanf(buf, "%d %*[^\r\n]\r\n", &rc); |
donatien | 0:632c9925f013 | 94 | if(len != 1) |
donatien | 0:632c9925f013 | 95 | return -1; |
donatien | 0:632c9925f013 | 96 | return rc; |
donatien | 0:632c9925f013 | 97 | } |
donatien | 0:632c9925f013 | 98 | |
donatien | 0:632c9925f013 | 99 | #define MIN(a,b) ((a)<(b))?(a):(b) |
donatien | 0:632c9925f013 | 100 | void SMTPClient::process(bool moreData) //Main state-machine |
donatien | 0:632c9925f013 | 101 | { |
donatien | 0:632c9925f013 | 102 | char buf[BUF_SIZE] = {0}; |
donatien | 0:632c9925f013 | 103 | if(moreData) |
donatien | 0:632c9925f013 | 104 | { |
donatien | 0:632c9925f013 | 105 | if( m_nextState != SMTP_BODYMORE ) |
donatien | 0:632c9925f013 | 106 | { |
donatien | 0:632c9925f013 | 107 | return; |
donatien | 0:632c9925f013 | 108 | } |
donatien | 0:632c9925f013 | 109 | } |
donatien | 0:632c9925f013 | 110 | if(!moreData) //Receive next frame |
donatien | 0:632c9925f013 | 111 | { |
donatien | 0:632c9925f013 | 112 | m_pTCPSocket->recv(buf, BUF_SIZE - 1); |
donatien | 0:632c9925f013 | 113 | } |
donatien | 0:632c9925f013 | 114 | |
donatien | 0:632c9925f013 | 115 | IpAddr myIp(0,0,0,0); |
donatien | 0:632c9925f013 | 116 | string to; |
donatien | 0:632c9925f013 | 117 | int sendLen; |
donatien | 0:632c9925f013 | 118 | |
donatien | 0:632c9925f013 | 119 | DBG("In state %d", m_nextState); |
donatien | 0:632c9925f013 | 120 | |
donatien | 0:632c9925f013 | 121 | switch(m_nextState) |
donatien | 0:632c9925f013 | 122 | { |
donatien | 0:632c9925f013 | 123 | case SMTP_HELLO: |
donatien | 0:632c9925f013 | 124 | if( rc(buf) != 220 ) |
donatien | 0:632c9925f013 | 125 | { close(); onResult(SMTP_PRTCL); return; } |
donatien | 0:632c9925f013 | 126 | myIp = Net::getDefaultIf()->getIp(); |
donatien | 0:632c9925f013 | 127 | sprintf(buf, "HELO %d.%d.%d.%d\r\n", myIp[0], myIp[1], myIp[2], myIp[3]); |
donatien | 0:632c9925f013 | 128 | m_nextState = SMTP_FROM; |
donatien | 0:632c9925f013 | 129 | break; |
donatien | 0:632c9925f013 | 130 | case SMTP_FROM: |
donatien | 0:632c9925f013 | 131 | if( rc(buf) != 250 ) |
donatien | 0:632c9925f013 | 132 | { close(); onResult(SMTP_PRTCL); return; } |
donatien | 0:632c9925f013 | 133 | sprintf(buf, "MAIL FROM:<%s>\r\n", m_pMessage->m_from.c_str()); |
donatien | 0:632c9925f013 | 134 | break; |
donatien | 0:632c9925f013 | 135 | case SMTP_TO: |
donatien | 0:632c9925f013 | 136 | if( rc(buf) != 250 ) |
donatien | 0:632c9925f013 | 137 | { close(); onResult(SMTP_PRTCL); return; } |
donatien | 0:632c9925f013 | 138 | to = m_pMessage->m_lTo.front(); |
donatien | 0:632c9925f013 | 139 | sprintf(buf, "RCPT TO:<%s>\r\n", to.c_str()); |
donatien | 0:632c9925f013 | 140 | m_pMessage->m_lTo.pop(); |
donatien | 0:632c9925f013 | 141 | if(m_pMessage->m_lTo.empty()) |
donatien | 0:632c9925f013 | 142 | { |
donatien | 0:632c9925f013 | 143 | m_nextState = SMTP_DATA; |
donatien | 0:632c9925f013 | 144 | } |
donatien | 0:632c9925f013 | 145 | break; |
donatien | 0:632c9925f013 | 146 | case SMTP_DATA: |
donatien | 0:632c9925f013 | 147 | if( rc(buf) != 250 ) |
donatien | 0:632c9925f013 | 148 | { close(); onResult(SMTP_PRTCL); return; } |
donatien | 0:632c9925f013 | 149 | sprintf(buf, "DATA\r\n"); |
donatien | 0:632c9925f013 | 150 | break; |
donatien | 0:632c9925f013 | 151 | case SMTP_BODY: |
donatien | 0:632c9925f013 | 152 | if( rc(buf) != 354 ) |
donatien | 0:632c9925f013 | 153 | { close(); onResult(SMTP_PRTCL); return; } |
donatien | 0:632c9925f013 | 154 | m_nextState = SMTP_BODYMORE; |
donatien | 0:632c9925f013 | 155 | case SMTP_BODYMORE: |
donatien | 0:632c9925f013 | 156 | sendLen = 0; |
donatien | 0:632c9925f013 | 157 | if( m_posInMsg < m_pMessage->m_content.length() ) |
donatien | 0:632c9925f013 | 158 | { |
donatien | 0:632c9925f013 | 159 | sendLen = MIN( (m_pMessage->m_content.length() - m_posInMsg), CHUNK_SIZE ); |
donatien | 0:632c9925f013 | 160 | m_pTCPSocket->send( m_pMessage->m_content.c_str() + m_posInMsg, sendLen ); |
donatien | 0:632c9925f013 | 161 | m_posInMsg += sendLen; |
donatien | 0:632c9925f013 | 162 | } |
donatien | 0:632c9925f013 | 163 | if( m_posInMsg == m_pMessage->m_content.length() ) |
donatien | 0:632c9925f013 | 164 | { |
donatien | 0:632c9925f013 | 165 | sprintf(buf, "\r\n.\r\n"); //EOF |
donatien | 0:632c9925f013 | 166 | m_nextState = SMTP_EOF; |
donatien | 0:632c9925f013 | 167 | } |
donatien | 0:632c9925f013 | 168 | break; |
donatien | 0:632c9925f013 | 169 | case SMTP_EOF: |
donatien | 0:632c9925f013 | 170 | if( rc(buf) != 250 ) |
donatien | 0:632c9925f013 | 171 | { close(); onResult(SMTP_PRTCL); return; } |
donatien | 0:632c9925f013 | 172 | sprintf(buf, "QUIT\r\n"); |
donatien | 0:632c9925f013 | 173 | m_nextState = SMTP_BYE; |
donatien | 0:632c9925f013 | 174 | break; |
donatien | 0:632c9925f013 | 175 | case SMTP_BYE: |
donatien | 0:632c9925f013 | 176 | if( rc(buf) != 221 ) |
donatien | 0:632c9925f013 | 177 | { close(); onResult(SMTP_PRTCL); return; } |
donatien | 0:632c9925f013 | 178 | close(); |
donatien | 0:632c9925f013 | 179 | onResult(SMTP_OK); |
donatien | 0:632c9925f013 | 180 | break; |
donatien | 0:632c9925f013 | 181 | } |
donatien | 0:632c9925f013 | 182 | |
donatien | 0:632c9925f013 | 183 | if( m_nextState != SMTP_BODYMORE ) |
donatien | 0:632c9925f013 | 184 | { |
donatien | 0:632c9925f013 | 185 | m_pTCPSocket->send( buf, strlen(buf) ); |
donatien | 0:632c9925f013 | 186 | } |
donatien | 0:632c9925f013 | 187 | } |
donatien | 0:632c9925f013 | 188 | |
donatien | 0:632c9925f013 | 189 | void SMTPClient::setTimeout(int ms) |
donatien | 0:632c9925f013 | 190 | { |
donatien | 0:632c9925f013 | 191 | m_timeout = 1000*ms; |
donatien | 0:632c9925f013 | 192 | resetTimeout(); |
donatien | 0:632c9925f013 | 193 | } |
donatien | 0:632c9925f013 | 194 | |
donatien | 0:632c9925f013 | 195 | void SMTPClient::resetTimeout() |
donatien | 0:632c9925f013 | 196 | { |
donatien | 0:632c9925f013 | 197 | m_watchdog.detach(); |
donatien | 0:632c9925f013 | 198 | m_watchdog.attach_us<SMTPClient>(this, &SMTPClient::onTimeout, m_timeout); |
donatien | 0:632c9925f013 | 199 | } |
donatien | 0:632c9925f013 | 200 | |
donatien | 0:632c9925f013 | 201 | void SMTPClient::onTimeout() //Connection has timed out |
donatien | 0:632c9925f013 | 202 | { |
donatien | 0:632c9925f013 | 203 | close(); |
donatien | 0:632c9925f013 | 204 | onResult(SMTP_TIMEOUT); |
donatien | 0:632c9925f013 | 205 | } |
donatien | 0:632c9925f013 | 206 | |
donatien | 0:632c9925f013 | 207 | void SMTPClient::onTCPSocketEvent(TCPSocketEvent e) |
donatien | 0:632c9925f013 | 208 | { |
donatien | 0:632c9925f013 | 209 | switch(e) |
donatien | 0:632c9925f013 | 210 | { |
donatien | 0:632c9925f013 | 211 | case TCPSOCKET_READABLE: |
donatien | 0:632c9925f013 | 212 | resetTimeout(); |
donatien | 0:632c9925f013 | 213 | process(false); |
donatien | 0:632c9925f013 | 214 | break; |
donatien | 0:632c9925f013 | 215 | case TCPSOCKET_WRITEABLE: |
donatien | 0:632c9925f013 | 216 | resetTimeout(); |
donatien | 0:632c9925f013 | 217 | process(true); |
donatien | 0:632c9925f013 | 218 | break; |
donatien | 0:632c9925f013 | 219 | case TCPSOCKET_CONTIMEOUT: |
donatien | 0:632c9925f013 | 220 | case TCPSOCKET_CONRST: |
donatien | 0:632c9925f013 | 221 | case TCPSOCKET_CONABRT: |
donatien | 0:632c9925f013 | 222 | case TCPSOCKET_ERROR: |
donatien | 0:632c9925f013 | 223 | onResult(SMTP_DISC); |
donatien | 0:632c9925f013 | 224 | DBG("\r\nConnection error in SMTP Client.\r\n"); |
donatien | 0:632c9925f013 | 225 | close(); |
donatien | 0:632c9925f013 | 226 | break; |
donatien | 0:632c9925f013 | 227 | case TCPSOCKET_DISCONNECTED: |
donatien | 0:632c9925f013 | 228 | if(m_nextState != SMTP_BYE) |
donatien | 0:632c9925f013 | 229 | { |
donatien | 0:632c9925f013 | 230 | onResult(SMTP_DISC); |
donatien | 0:632c9925f013 | 231 | DBG("\r\nConnection error in SMTP Client.\r\n"); |
donatien | 0:632c9925f013 | 232 | close(); |
donatien | 0:632c9925f013 | 233 | } |
donatien | 0:632c9925f013 | 234 | break; |
donatien | 0:632c9925f013 | 235 | } |
donatien | 0:632c9925f013 | 236 | } |
donatien | 0:632c9925f013 | 237 | |
donatien | 0:632c9925f013 | 238 | void SMTPClient::onResult(SMTPResult r) //Must be called by impl when the request completes |
donatien | 0:632c9925f013 | 239 | { |
donatien | 0:632c9925f013 | 240 | if(m_pCbItem && m_pCbMeth) |
donatien | 0:632c9925f013 | 241 | (m_pCbItem->*m_pCbMeth)(r); |
donatien | 0:632c9925f013 | 242 | } |
donatien | 0:632c9925f013 | 243 | |
donatien | 0:632c9925f013 | 244 | #endif |