-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmtp.hpp
180 lines (150 loc) · 4.29 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
#pragma once
#include <nall/stdint.hpp>
#include <nall/string.hpp>
#include <nall/file.hpp>
#include <nall/location.hpp>
#include <nall/random.hpp>
#include <nall/encode/base64.hpp>
#if !defined(PLATFORM_WINDOWS)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.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 = Location::file(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::message() -> string {
return info.message;
}
inline auto SMTP::response() -> string {
return info.response;
}
inline auto SMTP::boundary() -> string {
PRNG::LFSR random;
random.seed(time(0));
string boundary;
for(u32 n = 0; n < 16; n++) boundary.append(hex(random.random(), 2L));
return boundary;
}
inline auto SMTP::filename(const string& filename) -> string {
string result;
for(auto& n : filename) {
if(n <= 32 || n >= 127) result.append("%", hex(n, 2L));
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(NALL_HEADER_ONLY)
#include <nall/smtp.cpp>
#endif