forked from ares-emulator/ares
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smtp.hpp
314 lines (260 loc) · 8.66 KB
/
smtp.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#pragma once
#include <nall/base64.hpp>
#include <nall/stdint.hpp>
#include <nall/string.hpp>
#if !defined(PLATFORM_WINDOWS)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
namespace nall {
struct SMTP {
enum class Format : u32 { Plain, HTML };
auto server(string server, u16 port = 25) -> void;
auto from(string mail, string name = "") -> void;
auto to(string mail, string name = "") -> void;
auto cc(string mail, string name = "") -> void;
auto bcc(string mail, string name = "") -> void;
auto attachment(const u8* data, u32 size, string name) -> void;
auto attachment(string filename, string name = "") -> bool;
auto subject(string subject) -> void;
auto body(string body, Format format = Format::Plain) -> void;
auto send() -> bool;
auto message() -> string;
auto response() -> string;
#if defined(API_WINDOWS)
auto close(s32) -> s32;
SMTP();
#endif
private:
struct Information {
string server;
u16 port;
struct Contact {
string mail;
string name;
};
Contact from;
vector<Contact> to;
vector<Contact> cc;
vector<Contact> bcc;
struct Attachment {
vector<u8> buffer;
string name;
};
string subject;
string body;
Format format = Format::Plain;
vector<Attachment> attachments;
string message;
string response;
} info;
auto send(s32 sock, const string& text) -> bool;
auto recv(s32 sock) -> string;
auto boundary() -> string;
auto filename(const string& filename) -> string;
auto contact(const Information::Contact& contact) -> string;
auto contacts(const vector<Information::Contact>& contacts) -> string;
auto split(const string& text) -> string;
};
inline auto SMTP::server(string server, u16 port) -> void {
info.server = server;
info.port = port;
}
inline auto SMTP::from(string mail, string name) -> void {
info.from = {mail, name};
}
inline auto SMTP::to(string mail, string name) -> void {
info.to.append({mail, name});
}
inline auto SMTP::cc(string mail, string name) -> void {
info.cc.append({mail, name});
}
inline auto SMTP::bcc(string mail, string name) -> void {
info.bcc.append({mail, name});
}
inline auto SMTP::attachment(const u8* data, u32 size, string name) -> void {
vector<u8> buffer;
buffer.resize(size);
memcpy(buffer.data(), data, size);
info.attachments.append({std::move(buffer), name});
}
inline auto SMTP::attachment(string filename, string name) -> bool {
if(!file::exists(filename)) return false;
if(name == "") name = notdir(filename);
auto buffer = file::read(filename);
info.attachments.append({std::move(buffer), name});
return true;
}
inline auto SMTP::subject(string subject) -> void {
info.subject = subject;
}
inline auto SMTP::body(string body, Format format) -> void {
info.body = body;
info.format = format;
}
inline auto SMTP::send() -> bool {
info.message.append("From: =?UTF-8?B?", Base64::encode(contact(info.from)), "?=\r\n");
info.message.append("To: =?UTF-8?B?", Base64::encode(contacts(info.to)), "?=\r\n");
info.message.append("Cc: =?UTF-8?B?", Base64::encode(contacts(info.cc)), "?=\r\n");
info.message.append("Subject: =?UTF-8?B?", Base64::encode(info.subject), "?=\r\n");
string uniqueID = boundary();
info.message.append("MIME-Version: 1.0\r\n");
info.message.append("Content-Type: multipart/mixed; boundary=", uniqueID, "\r\n");
info.message.append("\r\n");
string format = (info.format == Format::Plain ? "text/plain" : "text/html");
info.message.append("--", uniqueID, "\r\n");
info.message.append("Content-Type: ", format, "; charset=UTF-8\r\n");
info.message.append("Content-Transfer-Encoding: base64\r\n");
info.message.append("\r\n");
info.message.append(split(Base64::encode(info.body)), "\r\n");
info.message.append("\r\n");
for(auto& attachment : info.attachments) {
info.message.append("--", uniqueID, "\r\n");
info.message.append("Content-Type: application/octet-stream\r\n");
info.message.append("Content-Transfer-Encoding: base64\r\n");
info.message.append("Content-Disposition: attachment; size=", attachment.buffer.size(), "; filename*=UTF-8''", filename(attachment.name), "\r\n");
info.message.append("\r\n");
info.message.append(split(Base64::encode(attachment.buffer)), "\r\n");
info.message.append("\r\n");
}
info.message.append("--", uniqueID, "--\r\n");
addrinfo hints;
memset(&hints, 0, sizeof(addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
addrinfo* serverinfo;
s32 status = getaddrinfo(info.server, string(info.port), &hints, &serverinfo);
if(status != 0) return false;
s32 sock = socket(serverinfo->ai_family, serverinfo->ai_socktype, serverinfo->ai_protocol);
if(sock == -1) return false;
s32 result = connect(sock, serverinfo->ai_addr, serverinfo->ai_addrlen);
if(result == -1) return false;
string response;
info.response.append(response = recv(sock));
if(!response.beginswith("220 ")) { close(sock); return false; }
send(sock, {"HELO ", info.server, "\r\n"});
info.response.append(response = recv(sock));
if(!response.beginswith("250 ")) { close(sock); return false; }
send(sock, {"MAIL FROM: <", info.from.mail, ">\r\n"});
info.response.append(response = recv(sock));
if(!response.beginswith("250 ")) { close(sock); return false; }
for(auto& contact : info.to) {
send(sock, {"RCPT TO: <", contact.mail, ">\r\n"});
info.response.append(response = recv(sock));
if(!response.beginswith("250 ")) { close(sock); return false; }
}
for(auto& contact : info.cc) {
send(sock, {"RCPT TO: <", contact.mail, ">\r\n"});
info.response.append(response = recv(sock));
if(!response.beginswith("250 ")) { close(sock); return false; }
}
for(auto& contact : info.bcc) {
send(sock, {"RCPT TO: <", contact.mail, ">\r\n"});
info.response.append(response = recv(sock));
if(!response.beginswith("250 ")) { close(sock); return false; }
}
send(sock, {"DATA\r\n"});
info.response.append(response = recv(sock));
if(!response.beginswith("354 ")) { close(sock); return false; }
send(sock, {info.message, "\r\n", ".\r\n"});
info.response.append(response = recv(sock));
if(!response.beginswith("250 ")) { close(sock); return false; }
send(sock, {"QUIT\r\n"});
info.response.append(response = recv(sock));
//if(!response.beginswith("221 ")) { close(sock); return false; }
close(sock);
return true;
}
inline auto SMTP::message() -> string {
return info.message;
}
inline auto SMTP::response() -> string {
return info.response;
}
inline auto SMTP::send(s32 sock, const string& text) -> bool {
const char* data = text.data();
u32 size = text.size();
while(size) {
s32 length = ::send(sock, (const char*)data, size, 0);
if(length == -1) return false;
data += length;
size -= length;
}
return true;
}
inline auto SMTP::recv(s32 sock) -> string {
vector<u8> buffer;
while(true) {
char c;
if(::recv(sock, &c, sizeof(char), 0) < 1) break;
buffer.append(c);
if(c == '\n') break;
}
buffer.append(0);
return buffer;
}
inline auto SMTP::boundary() -> string {
random_lfsr random;
random.seed(time(0));
string boundary;
for(u32 n = 0; n < 16; n++) boundary.append(hex<2>(random()));
return boundary;
}
inline auto SMTP::filename(const string& filename) -> string {
string result;
for(auto& n : filename) {
if(n <= 32 || n >= 127) result.append("%", hex<2>(n));
else result.append(n);
}
return result;
}
inline auto SMTP::contact(const Information::Contact& contact) -> string {
if(!contact.name) return contact.mail;
return {"\"", contact.name, "\" <", contact.mail, ">"};
}
inline auto SMTP::contacts(const vector<Information::Contact>& contacts) -> string {
string result;
for(auto& contact : contacts) {
result.append(this->contact(contact), "; ");
}
result.trimRight("; ", 1L);
return result;
}
inline auto SMTP::split(const string& text) -> string {
string result;
u32 offset = 0;
while(offset < text.size()) {
u32 length = min(76, text.size() - offset);
if(length < 76) {
result.append(text.slice(offset));
} else {
result.append(text.slice(offset, 76), "\r\n");
}
offset += length;
}
return result;
}
#if defined(API_WINDOWS)
inline auto SMTP::close(s32 sock) -> s32 {
return closesocket(sock);
}
inline SMTP::SMTP() {
s32 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == INVALID_SOCKET && WSAGetLastError() == WSANOTINITIALISED) {
WSADATA wsaData;
if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
WSACleanup();
return;
}
} else {
close(sock);
}
}
#endif
}