-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpop3.cpp
229 lines (191 loc) · 7.43 KB
/
pop3.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
/**
Copyright 2009-2019 Red Anchor Trading Co. Ltd.
Distributed under the Boost Software License, Version 1.0.
See <http://www.boost.org/LICENSE_1_0.txt>
*/
#include "fost-inet.hpp"
#include <fost/pop3.hpp>
#include <fost/exception/out_of_range.hpp>
#include <fost/exception/unicode_encoding.hpp>
#include <fost/log>
using namespace fostlib;
using namespace fostlib::pop3;
namespace {
mime::mime_headers read_headers(network_connection &the_network_connection) {
try {
mime::mime_headers headers;
std::string line;
the_network_connection >> line;
while (!line.empty()) {
std::string header(line);
line = "";
the_network_connection >> line;
while (line.substr(0, 1) == " " || line.substr(0, 1) == "\t") {
header += line;
line = "";
the_network_connection >> line;
}
string safe;
for (std::string::const_iterator c = header.begin();
c != header.end(); ++c)
if (*c > 127 || *c < 0)
safe += '?';
else
safe += *c;
headers.parse(safe);
}
return headers;
} catch (exceptions::exception &e) {
insert(e.data(), "whilst", "reading MIME headers");
throw;
}
}
std::string read_body(network_connection &the_network_connection) {
try {
std::string content;
std::string line;
the_network_connection >> line;
while (true) {
if ((line.length() > 0) && (line[0] == '.')) {
if ((line.length() > 1) && (line[1] == '.')) {
line = line.substr(1);
} else
break;
} else
content += line + "\n";
line = "";
the_network_connection >> line;
}
return content;
} catch (fostlib::exceptions::exception &e) {
insert(e.data(), "whilst", "reading message body");
throw;
}
}
void check_OK(network_connection &the_network_connection, string command) {
try {
utf8_string server_response;
the_network_connection >> server_response;
if (server_response.underlying().substr(0, 3) != "+OK") {
throw exceptions::not_implemented(
command, coerce<string>(server_response));
}
} catch (fostlib::exceptions::exception &e) {
insert(e.data(), "whilst", "waiting for OK after command");
insert(e.data(), "command", command);
throw;
}
}
void
send(network_connection &the_network_connection,
const string command,
const nullable<string> parameter = null) {
try {
the_network_connection << coerce<utf8_string>(command);
if (parameter) {
the_network_connection << " ";
the_network_connection << coerce<utf8_string>(parameter);
}
the_network_connection << "\r\n";
} catch (fostlib::exceptions::exception &e) {
insert(e.data(), "whilst", "sending command");
insert(e.data(), "command", command);
throw;
}
}
void
send(network_connection &the_network_connection,
const string command,
const size_t parameter) {
std::stringstream i_stream;
i_stream << parameter;
string value(coerce<string>(utf8_string(i_stream.str())));
send(the_network_connection, command, value);
}
void send_and_check_OK(
network_connection &the_network_connection,
const string command,
const string parameter) {
send(the_network_connection, command, parameter);
check_OK(the_network_connection, command);
}
void send_and_check_OK(
network_connection &the_network_connection,
const string command,
const size_t parameter) {
send(the_network_connection, command, parameter);
check_OK(the_network_connection, command);
}
}
namespace {
class pop3cnx {
network_connection m_cnx;
public:
size_t message_count;
pop3cnx(const host &h, const string &username, const string &password)
: m_cnx(h, 110) {
try {
utf8_string server_status;
m_cnx >> server_status;
send_and_check_OK(m_cnx, "user", username);
send_and_check_OK(m_cnx, "pass", password);
send(m_cnx, "stat");
utf8_string server_response;
m_cnx >> server_response;
std::stringstream server_response_stringstream{
static_cast<std::string>(
server_response.underlying().substr(3))};
server_response_stringstream >> message_count;
size_t octets;
server_response_stringstream >> octets;
} catch (exceptions::exception &e) {
insert(e.data(), "whilst", "establishing POP3 connection");
throw;
}
}
~pop3cnx() try { send(m_cnx, "quit"); } catch (...) {
absorb_exception();
}
std::unique_ptr<text_body> message(size_t i) {
try {
send_and_check_OK(m_cnx, "retr", i);
mime::mime_headers h = read_headers(m_cnx);
utf8_string content;
try {
content = read_body(m_cnx);
} catch (fostlib::exceptions::unicode_encoding &) {
return std::unique_ptr<text_body>();
}
return std::unique_ptr<text_body>(new text_body(
coerce<string>(content), h, "text/plain"));
} catch (fostlib::exceptions::exception &e) {
insert(e.data(), "whilst", "retrieving message");
throw;
}
}
void remove(size_t i) { send_and_check_OK(m_cnx, "dele", i); }
};
}
void fostlib::pop3::iterate_mailbox(
const host &host,
std::function<bool(const text_body &)> destroy_message,
const string &username,
const string &password) {
boost::scoped_ptr<pop3cnx> mailbox(new pop3cnx(host, username, password));
const size_t messages = mailbox->message_count;
log::info(c_fost_inet, "Number of messages found", messages);
// Loop from the end so we always process the latest bounce messages first
for (std::size_t i = messages; i; --i) {
std::unique_ptr<text_body> message = mailbox->message(i);
if (message.get() && destroy_message(*message)) mailbox->remove(i);
if (i % 20 == 0) {
log::info(c_fost_inet, "Resetting mailbox connection", i);
mailbox.reset(new pop3cnx(host, username, password));
if (mailbox->message_count < i)
throw fostlib::exceptions::out_of_range<size_t>(
"The number of messages on the server can't go down "
"below the ones we've processed!",
i, messages, mailbox->message_count);
}
}
}