private fork
Fork of GSwifiInterface by
GSwifi/GSwifi_smtp.cpp@22:2085f090fe31, 2014-06-06 (annotated)
- Committer:
- okini3939
- Date:
- Fri Jun 06 00:41:27 2014 +0000
- Revision:
- 22:2085f090fe31
- Parent:
- 12:057089026a20
modify httpd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsfan | 12:057089026a20 | 1 | /* Copyright (C) 2013 gsfan, MIT License |
gsfan | 12:057089026a20 | 2 | * |
gsfan | 12:057089026a20 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
gsfan | 12:057089026a20 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
gsfan | 12:057089026a20 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
gsfan | 12:057089026a20 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
gsfan | 12:057089026a20 | 7 | * furnished to do so, subject to the following conditions: |
gsfan | 12:057089026a20 | 8 | * |
gsfan | 12:057089026a20 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
gsfan | 12:057089026a20 | 10 | * substantial portions of the Software. |
gsfan | 12:057089026a20 | 11 | * |
gsfan | 12:057089026a20 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
gsfan | 12:057089026a20 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
gsfan | 12:057089026a20 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
gsfan | 12:057089026a20 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
gsfan | 12:057089026a20 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
gsfan | 12:057089026a20 | 17 | */ |
gsfan | 12:057089026a20 | 18 | |
gsfan | 12:057089026a20 | 19 | #include "GSwifi.h" |
gsfan | 12:057089026a20 | 20 | |
gsfan | 12:057089026a20 | 21 | #ifdef CFG_ENABLE_SMTP |
gsfan | 12:057089026a20 | 22 | |
gsfan | 12:057089026a20 | 23 | int GSwifi::mail (const char *host, int port, const char *to, const char *from, const char *subject, const char *mesg, const char *user, const char *pwd) { |
gsfan | 12:057089026a20 | 24 | int ret = -1; |
gsfan | 12:057089026a20 | 25 | int cid; |
gsfan | 12:057089026a20 | 26 | char cmd[CFG_DATA_SIZE]; |
gsfan | 12:057089026a20 | 27 | char ip[17]; |
gsfan | 12:057089026a20 | 28 | |
gsfan | 12:057089026a20 | 29 | if (!isAssociated() || _state.status != STAT_READY) return -1; |
gsfan | 12:057089026a20 | 30 | |
gsfan | 12:057089026a20 | 31 | if (getHostByName(host, ip)) return -1; |
gsfan | 12:057089026a20 | 32 | if (! port) { |
gsfan | 12:057089026a20 | 33 | port = 25; |
gsfan | 12:057089026a20 | 34 | } |
gsfan | 12:057089026a20 | 35 | |
gsfan | 12:057089026a20 | 36 | cid = open(PROTO_TCP, ip, port); |
gsfan | 12:057089026a20 | 37 | if (cid < 0) return -1; |
gsfan | 12:057089026a20 | 38 | DBG("cid %d\r\n", cid); |
gsfan | 12:057089026a20 | 39 | |
gsfan | 12:057089026a20 | 40 | if (smtpWait(cid ,220)) goto exit; |
gsfan | 12:057089026a20 | 41 | |
gsfan | 12:057089026a20 | 42 | // send request |
gsfan | 12:057089026a20 | 43 | snprintf(cmd, sizeof(cmd), "EHLO %s\r\n", _state.name); |
gsfan | 12:057089026a20 | 44 | send(cid, cmd, strlen(cmd)); |
gsfan | 12:057089026a20 | 45 | wait_ms(100); |
gsfan | 12:057089026a20 | 46 | if (smtpWait(cid ,250)) goto quit; |
gsfan | 12:057089026a20 | 47 | smtpWait(cid ,0); |
gsfan | 12:057089026a20 | 48 | |
gsfan | 12:057089026a20 | 49 | if (user && pwd) { |
gsfan | 12:057089026a20 | 50 | // smtp auth |
gsfan | 12:057089026a20 | 51 | int len; |
gsfan | 12:057089026a20 | 52 | snprintf(cmd, sizeof(cmd), "%s%c%s%c%s", user, 0, user, 0, pwd); |
gsfan | 12:057089026a20 | 53 | len = strlen(user) * 2 + strlen(pwd) + 2; |
gsfan | 12:057089026a20 | 54 | char tmp[len + (len / 2)]; |
gsfan | 12:057089026a20 | 55 | base64encode(cmd, len, tmp, sizeof(tmp)); |
gsfan | 12:057089026a20 | 56 | snprintf(cmd, sizeof(cmd), "AUTH PLAIN %s\r\n", tmp); |
gsfan | 12:057089026a20 | 57 | send(cid, cmd, strlen(cmd)); |
gsfan | 12:057089026a20 | 58 | if (smtpWait(cid ,235)) goto quit; |
gsfan | 12:057089026a20 | 59 | } |
gsfan | 12:057089026a20 | 60 | |
gsfan | 12:057089026a20 | 61 | snprintf(cmd, sizeof(cmd), "MAIL FROM: %s\r\n", from); |
gsfan | 12:057089026a20 | 62 | send(cid, cmd, strlen(cmd)); |
gsfan | 12:057089026a20 | 63 | if (smtpWait(cid ,250)) goto quit; |
gsfan | 12:057089026a20 | 64 | |
gsfan | 12:057089026a20 | 65 | snprintf(cmd, sizeof(cmd), "RCPT TO: %s\r\n", to); |
gsfan | 12:057089026a20 | 66 | send(cid, cmd, strlen(cmd)); |
gsfan | 12:057089026a20 | 67 | if (smtpWait(cid ,250)) goto quit; |
gsfan | 12:057089026a20 | 68 | |
gsfan | 12:057089026a20 | 69 | strcpy(cmd, "DATA\r\n"); |
gsfan | 12:057089026a20 | 70 | send(cid, cmd, strlen(cmd)); |
gsfan | 12:057089026a20 | 71 | if (smtpWait(cid ,354)) goto quit; |
gsfan | 12:057089026a20 | 72 | |
gsfan | 12:057089026a20 | 73 | // mail data |
gsfan | 12:057089026a20 | 74 | snprintf(cmd, sizeof(cmd), "From: %s\r\n", from); |
gsfan | 12:057089026a20 | 75 | send(cid, cmd, strlen(cmd)); |
gsfan | 12:057089026a20 | 76 | snprintf(cmd, sizeof(cmd), "To: %s\r\n", to); |
gsfan | 12:057089026a20 | 77 | send(cid, cmd, strlen(cmd)); |
gsfan | 12:057089026a20 | 78 | snprintf(cmd, sizeof(cmd), "Subject: %s\r\n\r\n", subject); |
gsfan | 12:057089026a20 | 79 | send(cid, cmd, strlen(cmd)); |
gsfan | 12:057089026a20 | 80 | |
gsfan | 12:057089026a20 | 81 | send(cid, mesg, strlen(mesg)); |
gsfan | 12:057089026a20 | 82 | strcpy(cmd, "\r\n.\r\n"); |
gsfan | 12:057089026a20 | 83 | send(cid, cmd, strlen(cmd)); |
gsfan | 12:057089026a20 | 84 | if (smtpWait(cid ,250)) goto quit; |
gsfan | 12:057089026a20 | 85 | ret = 0; |
gsfan | 12:057089026a20 | 86 | |
gsfan | 12:057089026a20 | 87 | INFO("Mail, from: %s, to: %s %d\r\n", from, to, strlen(mesg)); |
gsfan | 12:057089026a20 | 88 | |
gsfan | 12:057089026a20 | 89 | quit: |
gsfan | 12:057089026a20 | 90 | strcpy(cmd, "QUIT\r\n"); |
gsfan | 12:057089026a20 | 91 | send(cid, cmd, strlen(cmd)); |
gsfan | 12:057089026a20 | 92 | smtpWait(cid ,221); |
gsfan | 12:057089026a20 | 93 | exit: |
gsfan | 12:057089026a20 | 94 | close(cid); |
gsfan | 12:057089026a20 | 95 | return ret; |
gsfan | 12:057089026a20 | 96 | } |
gsfan | 12:057089026a20 | 97 | |
gsfan | 12:057089026a20 | 98 | int GSwifi::smtpWait (int cid, int code) { |
gsfan | 12:057089026a20 | 99 | Timer timeout; |
gsfan | 12:057089026a20 | 100 | int i, n, len = 0; |
gsfan | 12:057089026a20 | 101 | char buf[CFG_CMD_SIZE], data[CFG_CMD_SIZE]; |
gsfan | 12:057089026a20 | 102 | |
gsfan | 12:057089026a20 | 103 | if (code == 0) { |
gsfan | 12:057089026a20 | 104 | // dummy read |
gsfan | 12:057089026a20 | 105 | timeout.start(); |
gsfan | 12:057089026a20 | 106 | while (timeout.read_ms() < CFG_TIMEOUT) { |
gsfan | 12:057089026a20 | 107 | wait_ms(10); |
gsfan | 12:057089026a20 | 108 | if (!readable(cid)) break; |
gsfan | 12:057089026a20 | 109 | n = recv(cid, buf, sizeof(buf)); |
gsfan | 12:057089026a20 | 110 | if (n <= 0) break; |
gsfan | 12:057089026a20 | 111 | } |
gsfan | 12:057089026a20 | 112 | timeout.stop(); |
gsfan | 12:057089026a20 | 113 | return 0; |
gsfan | 12:057089026a20 | 114 | } |
gsfan | 12:057089026a20 | 115 | |
gsfan | 12:057089026a20 | 116 | // wait responce |
gsfan | 12:057089026a20 | 117 | len = 0; |
gsfan | 12:057089026a20 | 118 | timeout.start(); |
gsfan | 12:057089026a20 | 119 | while (timeout.read_ms() < CFG_TIMEOUT) { |
gsfan | 12:057089026a20 | 120 | wait_ms(10); |
gsfan | 12:057089026a20 | 121 | n = recv(cid, buf, sizeof(buf)); |
gsfan | 12:057089026a20 | 122 | for (i = 0; i < n; i ++) { |
gsfan | 12:057089026a20 | 123 | if (buf[i] == '\r') continue; |
gsfan | 12:057089026a20 | 124 | if (buf[i] == '\n') { |
gsfan | 12:057089026a20 | 125 | if (len == 0) continue; |
gsfan | 12:057089026a20 | 126 | goto next; |
gsfan | 12:057089026a20 | 127 | } else |
gsfan | 12:057089026a20 | 128 | if (len < sizeof(data) - 1) { |
gsfan | 12:057089026a20 | 129 | data[len] = buf[i]; |
gsfan | 12:057089026a20 | 130 | len ++; |
gsfan | 12:057089026a20 | 131 | } |
gsfan | 12:057089026a20 | 132 | } |
gsfan | 12:057089026a20 | 133 | } |
gsfan | 12:057089026a20 | 134 | next: |
gsfan | 12:057089026a20 | 135 | data[len] = 0; |
gsfan | 12:057089026a20 | 136 | DBG("smtp: %s\r\n", data); |
gsfan | 12:057089026a20 | 137 | timeout.stop(); |
gsfan | 12:057089026a20 | 138 | |
gsfan | 12:057089026a20 | 139 | // check return code |
gsfan | 12:057089026a20 | 140 | i = atoi(data); |
gsfan | 12:057089026a20 | 141 | DBG("smtp status %d\r\n", i); |
gsfan | 12:057089026a20 | 142 | return i == code ? 0 : -1; |
gsfan | 12:057089026a20 | 143 | } |
gsfan | 12:057089026a20 | 144 | |
gsfan | 12:057089026a20 | 145 | #endif |