2
// POP3ClientSession.cpp
4
// $Id: //poco/1.2/Net/src/POP3ClientSession.cpp#1 $
8
// Module: POP3ClientSession
10
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
13
// Permission is hereby granted, free of charge, to any person or organization
14
// obtaining a copy of the software and accompanying documentation covered by
15
// this license (the "Software") to use, reproduce, display, distribute,
16
// execute, and transmit the Software, and to prepare derivative works of the
17
// Software, and to permit third-parties to whom the Software is furnished to
18
// do so, all subject to the following:
20
// The copyright notices in the Software and this entire statement, including
21
// the above license grant, this restriction and the following disclaimer,
22
// must be included in all copies of the Software, in whole or in part, and
23
// all derivative works of the Software, unless such copies or derivative
24
// works are solely in the form of machine-executable object code generated by
25
// a source language processor.
27
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33
// DEALINGS IN THE SOFTWARE.
37
#include "Poco/Net/POP3ClientSession.h"
38
#include "Poco/Net/MailMessage.h"
39
#include "Poco/Net/MailStream.h"
40
#include "Poco/Net/SocketAddress.h"
41
#include "Poco/Net/NetException.h"
42
#include "Poco/StreamCopier.h"
43
#include "Poco/NumberFormatter.h"
44
#include "Poco/UnbufferedStreamBuf.h"
49
using Poco::NumberFormatter;
50
using Poco::StreamCopier;
57
class DialogStreamBuf: public Poco::UnbufferedStreamBuf
60
DialogStreamBuf(DialogSocket& socket):
75
DialogSocket& _socket;
79
class DialogIOS: public virtual std::ios
82
DialogIOS(DialogSocket& socket):
92
DialogStreamBuf* rdbuf()
102
class DialogInputStream: public DialogIOS, public std::istream
105
DialogInputStream(DialogSocket& socket):
117
POP3ClientSession::POP3ClientSession(const StreamSocket& socket):
124
POP3ClientSession::POP3ClientSession(const std::string& host, Poco::UInt16 port):
125
_socket(SocketAddress(host, port)),
131
POP3ClientSession::~POP3ClientSession()
143
void POP3ClientSession::setTimeout(const Poco::Timespan& timeout)
145
_socket.setReceiveTimeout(timeout);
149
Poco::Timespan POP3ClientSession::getTimeout() const
151
return _socket.getReceiveTimeout();
155
void POP3ClientSession::login(const std::string& username, const std::string& password)
157
std::string response;
158
_socket.receiveMessage(response);
159
if (!isPositive(response)) throw SMTPException("The POP3 service is unavailable", response);
160
sendCommand("USER", username, response);
161
if (!isPositive(response)) throw POP3Exception("Login rejected for user", response);
162
sendCommand("PASS", password, response);
163
if (!isPositive(response)) throw POP3Exception("Password rejected for user", response);
167
void POP3ClientSession::close()
171
std::string response;
172
sendCommand("QUIT", response);
179
int POP3ClientSession::messageCount()
181
std::string response;
182
sendCommand("STAT", response);
183
if (!isPositive(response)) throw POP3Exception("Cannot determine message count", response);
184
std::string::const_iterator it = response.begin();
185
std::string::const_iterator end = response.end();
187
while (it != end && !isspace(*it)) ++it;
188
while (it != end && isspace(*it)) ++it;
189
while (it != end && isdigit(*it)) count = count*10 + *it++ - '0';
194
void POP3ClientSession::listMessages(MessageInfoVec& messages)
197
std::string response;
198
sendCommand("LIST", response);
199
if (!isPositive(response)) throw POP3Exception("Cannot get message list", response);
200
_socket.receiveMessage(response);
201
while (response != ".")
203
MessageInfo info = {0, 0};
204
std::string::const_iterator it = response.begin();
205
std::string::const_iterator end = response.end();
206
while (it != end && isdigit(*it)) info.id = info.id*10 + *it++ - '0';
207
while (it != end && isspace(*it)) ++it;
208
while (it != end && isdigit(*it)) info.size = info.size*10 + *it++ - '0';
209
messages.push_back(info);
210
_socket.receiveMessage(response);
215
void POP3ClientSession::retrieveMessage(int id, MailMessage& message)
217
std::string response;
218
sendCommand("RETR", NumberFormatter::format(id), response);
219
if (!isPositive(response)) throw POP3Exception("Cannot get message list", response);
220
DialogInputStream sis(_socket);
221
MailInputStream mis(sis);
226
void POP3ClientSession::retrieveMessage(int id, MailMessage& message, PartHandler& handler)
228
std::string response;
229
sendCommand("RETR", NumberFormatter::format(id), response);
230
if (!isPositive(response)) throw POP3Exception("Cannot get message list", response);
231
DialogInputStream sis(_socket);
232
MailInputStream mis(sis);
233
message.read(mis, handler);
237
void POP3ClientSession::retrieveMessage(int id, std::ostream& ostr)
239
std::string response;
240
sendCommand("RETR", NumberFormatter::format(id), response);
241
if (!isPositive(response)) throw POP3Exception("Cannot get message list", response);
242
DialogInputStream sis(_socket);
243
MailInputStream mis(sis);
244
StreamCopier::copyStream(mis, ostr);
248
void POP3ClientSession::retrieveHeader(int id, MessageHeader& header)
250
std::string response;
251
sendCommand("TOP", NumberFormatter::format(id), "0", response);
252
if (!isPositive(response)) throw POP3Exception("Cannot get message list", response);
253
DialogInputStream sis(_socket);
254
MailInputStream mis(sis);
256
// skip stuff following header
262
void POP3ClientSession::deleteMessage(int id)
264
std::string response;
265
sendCommand("DELE", NumberFormatter::format(id), response);
266
if (!isPositive(response)) throw POP3Exception("Cannot mark message for deletion", response);
270
bool POP3ClientSession::sendCommand(const std::string& command, std::string& response)
272
_socket.sendMessage(command);
273
_socket.receiveMessage(response);
274
return isPositive(response);
278
bool POP3ClientSession::sendCommand(const std::string& command, const std::string& arg, std::string& response)
280
_socket.sendMessage(command, arg);
281
_socket.receiveMessage(response);
282
return isPositive(response);
286
bool POP3ClientSession::sendCommand(const std::string& command, const std::string& arg1, const std::string& arg2, std::string& response)
288
_socket.sendMessage(command, arg1, arg2);
289
_socket.receiveMessage(response);
290
return isPositive(response);
294
bool POP3ClientSession::isPositive(const std::string& response)
296
return response.length() > 0 && response[0] == '+';
300
} } // namespace Poco::Net