-
Notifications
You must be signed in to change notification settings - Fork 0
/
smtpsend.cpp
301 lines (246 loc) · 8.02 KB
/
smtpsend.cpp
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
#include "smtpsend.hpp"
#include <fstream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <algorithm>
#include <cctype>
#include <functional>
#include <string>
//#include <boost/algorithm/string/replace.hpp>
//#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string.hpp>
#include <ace/SOCK_Connector.h>
#include <bk/debug.hpp>
namespace transport
{
boost::uint32_t SmtpTransport::connect_count = 0;
int SmtpTransport::get_connection_count()
{
return connect_count;
}
SmtpTransport::~SmtpTransport()
{
if(connected_)
{
stream_.close();
boost::interprocess::ipcdetail::atomic_dec32(&connect_count);
}
}
SmtpTransport::SmtpTransport(int timeout) : timeout_(timeout)
{
connected_ = false;
}
std::string SmtpTransport::normalize(const std::string& email)
{
if(email[0] == '<' && email[email.length() -1] == '>')
{
return email;
}
else
{
return std::string("<") + email + std::string(">");
}
}
void SmtpTransport::connect(const std::string& host, int port, std::string helo)
{
ACE_SOCK_Connector connector;
ACE_INET_Addr addr;
ACE_Time_Value tm(timeout_);
if(addr.set(port, host.c_str()))
throw resolve_error(host, port);
else if(connector.connect(stream_, addr, &tm) )
throw connect_exception(host, port);
host_ = host;
port_ = port;
boost::interprocess::ipcdetail::atomic_inc32(&connect_count);
connected_ = true;
std::vector<std::string> lines;
unsigned int retcode = get(lines, 1024);
if(retcode >= 400 && retcode < 500)
{
throw transient_error(std::accumulate(lines.begin(), lines.end(), std::string("")) );
}
else if(retcode > 500)
{
throw permanent_error(std::accumulate(lines.begin(), lines.end(), std::string("")) );
}
if(helo.empty())
{
helo = "me";
}
try
{
say("EHLO", helo);
}
catch(const permanent_error& e)
{
say("HELO", helo);
}
}
void SmtpTransport::send_message(std::istream& is, const std::string& from, const RecipientList& recipient_list)
{
if(connected_ == false)
{
throw connect_exception();
}
//std::cout << "MAIL FROM: "<< from << "\n";
say("MAIL FROM:", normalize(from));
RecipientList::const_iterator pos;
for(pos = recipient_list.begin(); pos != recipient_list.end(); ++pos)
{
//std::cout << "RCPT TO: "<< (*pos) << "\n";
say("RCPT TO:", normalize(*pos));
}
//std::cout << "DATA\n";
say("DATA");
//std::cout << "MESSAGE\n";
std::istreambuf_iterator<char> eos;
std::string buf(std::istreambuf_iterator<char>(is), eos);
say_data(buf);
//std::cout << "QUIT\n";
say("QUIT");
}
unsigned int SmtpTransport::say(const std::string& cmd, const std::string& arg, unsigned int expected_retcode)
{
iovec iov[4];
iov[0].iov_base = const_cast<char*> (cmd.c_str());
iov[0].iov_len = cmd.length();
iov[1].iov_base = const_cast<char*> (" ");
iov[1].iov_len = 1;
iov[2].iov_base = const_cast<char*> (arg.c_str());
iov[2].iov_len = arg.length();
iov[3].iov_base = const_cast<char*> ("\r\n");
iov[3].iov_len = 2;
unsigned int buff_len = cmd.length() + 1 + arg.length() + 2;
unsigned int res = stream_.sendv_n(iov, 4, &timeout_);
if (res != buff_len)
{
throw transport_error("Can't send data", host_, port_);
}
std::vector<std::string> lines;
unsigned int retcode = get(lines, 1024);
if(retcode != expected_retcode && expected_retcode != 0)
{
throw command_unexpected(cmd, arg, std::accumulate(lines.begin(), lines.end(), std::string("")), expected_retcode);
}
if(retcode >= 400 && retcode < 500)
{
throw transient_error(std::accumulate(lines.begin(), lines.end(), std::string("")) );
}
else if(retcode > 500)
{
throw permanent_error(std::accumulate(lines.begin(), lines.end(), std::string("")) );
}
return retcode;
}
unsigned int SmtpTransport::say(const std::string& cmd, unsigned int expected_retcode)
{
iovec iov[2];
iov[0].iov_base = const_cast<char*> (cmd.c_str());
iov[0].iov_len = cmd.length();
iov[1].iov_base = const_cast<char*> ("\r\n");
iov[1].iov_len = 2;
unsigned int buff_len = cmd.length() + 2;
unsigned int res = stream_.sendv_n(iov, 2, &timeout_);
if (res != buff_len)
{
throw transport_error("Can't send data", host_, port_);
}
std::vector<std::string> lines;
unsigned int retcode = get(lines, 1024);
if(retcode != expected_retcode && expected_retcode != 0)
{
throw command_unexpected(cmd, std::accumulate(lines.begin(), lines.end(), std::string("")), expected_retcode);
}
if(retcode >= 400 && retcode < 500)
{
throw transient_error(std::accumulate(lines.begin(), lines.end(), std::string("")) );
}
else if(retcode > 500)
{
throw permanent_error(std::accumulate(lines.begin(), lines.end(), std::string("")) );
}
return retcode;
}
unsigned int SmtpTransport::say_data(std::string& buf, unsigned int expected_retcode)
{
boost::algorithm::replace_all(buf, "\r\n.\r\n", "\r\n..\r\n");
buf.append("\r\n.\r\n");
unsigned int res = stream_.send_n(&buf[0], buf.size(), &timeout_);
if (res != buf.size())
{
throw transport_error("Can't send data", host_, port_);
}
std::vector<std::string> lines;
unsigned int retcode = get(lines, 1024);
if(retcode != expected_retcode && expected_retcode != 0)
{
throw command_unexpected(std::accumulate(lines.begin(), lines.end(), std::string("")), expected_retcode);
}
if(retcode >= 400 && retcode < 500)
{
throw transient_error(std::accumulate(lines.begin(), lines.end(), std::string("")) );
}
else if(retcode > 500)
{
throw permanent_error(std::accumulate(lines.begin(), lines.end(), std::string("")) );
}
return retcode;
}
unsigned int SmtpTransport::get(std::vector<std::string>& response, unsigned int max_len)
{
std::vector<char> buff(max_len);
unsigned int read_bytes = 0;
unsigned int match_from = 0;
while( read_bytes < max_len)
{
int received = stream_.recv(&buff[read_bytes], max_len - read_bytes, &timeout_);
if(received == -1)
{
throw transport_error("Can't receive data", host_, port_);
}
else if(received == 0)
{
throw transport_error("Can't receive data", host_, port_);
}
unsigned int line_start = read_bytes;
for(unsigned int i = read_bytes; i < read_bytes + received; ++i)
{
if(match_str(&buff[i], &buff[received], "\r\n", 2, &match_from ))
{
response.push_back( std::string(&buff[line_start], i - line_start) );
if((i - line_start) > 3)
{
if(buff[line_start + 3] == ' ')
{
std::string retcode_str = std::string(&buff[line_start],3);
unsigned int retcode;
try
{
retcode = boost::lexical_cast<unsigned int>(retcode_str);
}
catch(boost::bad_lexical_cast& e)
{
throw protocol_error("Broken reply code", retcode_str);
}
if(retcode < 200 || retcode > 599)
{
throw protocol_error("Unknown reply code", retcode_str);
}
return retcode;
}
}
else
{
throw protocol_error("Broken reply", std::string(&buff[0], read_bytes + received));
}
line_start = i + 2;
match_from = 0;
}
}
read_bytes += received;
}
throw protocol_error("Broken reply");
}
}