~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Net/src/POP3ClientSession.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// POP3ClientSession.cpp
 
3
//
 
4
// $Id: //poco/1.2/Net/src/POP3ClientSession.cpp#1 $
 
5
//
 
6
// Library: Net
 
7
// Package: Mail
 
8
// Module:  POP3ClientSession
 
9
//
 
10
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
 
11
// and Contributors.
 
12
//
 
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:
 
19
// 
 
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.
 
26
// 
 
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.
 
34
//
 
35
 
 
36
 
 
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"
 
45
#include <istream>
 
46
#include <ctype.h>
 
47
 
 
48
 
 
49
using Poco::NumberFormatter;
 
50
using Poco::StreamCopier;
 
51
 
 
52
 
 
53
namespace Poco {
 
54
namespace Net {
 
55
 
 
56
 
 
57
class DialogStreamBuf: public Poco::UnbufferedStreamBuf
 
58
{
 
59
public:
 
60
        DialogStreamBuf(DialogSocket& socket):
 
61
                _socket(socket)
 
62
        {
 
63
        }
 
64
        
 
65
        ~DialogStreamBuf()
 
66
        {
 
67
        }
 
68
                
 
69
private:
 
70
        int readFromDevice()
 
71
        {
 
72
                return _socket.get();
 
73
        }
 
74
        
 
75
        DialogSocket& _socket;
 
76
};
 
77
 
 
78
 
 
79
class DialogIOS: public virtual std::ios
 
80
{
 
81
public:
 
82
        DialogIOS(DialogSocket& socket):
 
83
                _buf(socket)
 
84
        {
 
85
                poco_ios_init(&_buf);
 
86
        }
 
87
        
 
88
        ~DialogIOS()
 
89
        {
 
90
        }
 
91
        
 
92
        DialogStreamBuf* rdbuf()
 
93
        {
 
94
                return &_buf;
 
95
        }
 
96
 
 
97
protected:
 
98
        DialogStreamBuf _buf;
 
99
};
 
100
 
 
101
 
 
102
class DialogInputStream: public DialogIOS, public std::istream
 
103
{
 
104
public:
 
105
        DialogInputStream(DialogSocket& socket):
 
106
                DialogIOS(socket),
 
107
                std::istream(&_buf)
 
108
        {
 
109
        }
 
110
                
 
111
        ~DialogInputStream()
 
112
        {
 
113
        }
 
114
};
 
115
 
 
116
 
 
117
POP3ClientSession::POP3ClientSession(const StreamSocket& socket):
 
118
        _socket(socket),
 
119
        _isOpen(true)
 
120
{
 
121
}
 
122
 
 
123
 
 
124
POP3ClientSession::POP3ClientSession(const std::string& host, Poco::UInt16 port):       
 
125
        _socket(SocketAddress(host, port)),
 
126
        _isOpen(true)
 
127
{
 
128
}
 
129
 
 
130
 
 
131
POP3ClientSession::~POP3ClientSession()
 
132
{
 
133
        try
 
134
        {
 
135
                close();
 
136
        }
 
137
        catch (...)
 
138
        {
 
139
        }
 
140
}
 
141
 
 
142
 
 
143
void POP3ClientSession::setTimeout(const Poco::Timespan& timeout)
 
144
{
 
145
        _socket.setReceiveTimeout(timeout);
 
146
}
 
147
 
 
148
        
 
149
Poco::Timespan POP3ClientSession::getTimeout() const
 
150
{
 
151
        return _socket.getReceiveTimeout();
 
152
}
 
153
 
 
154
 
 
155
void POP3ClientSession::login(const std::string& username, const std::string& password)
 
156
{
 
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);
 
164
}
 
165
 
 
166
 
 
167
void POP3ClientSession::close()
 
168
{
 
169
        if (_isOpen)
 
170
        {
 
171
                std::string response;
 
172
                sendCommand("QUIT", response);
 
173
                _socket.close();
 
174
                _isOpen = false;
 
175
        }
 
176
}
 
177
 
 
178
 
 
179
int POP3ClientSession::messageCount()
 
180
{
 
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();
 
186
        int count = 0;
 
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';
 
190
        return count;
 
191
}
 
192
 
 
193
 
 
194
void POP3ClientSession::listMessages(MessageInfoVec& messages)
 
195
{
 
196
        messages.clear();
 
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 != ".")
 
202
        {
 
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);
 
211
        }
 
212
}
 
213
 
 
214
 
 
215
void POP3ClientSession::retrieveMessage(int id, MailMessage& message)
 
216
{
 
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);
 
222
        message.read(mis);
 
223
}
 
224
 
 
225
 
 
226
void POP3ClientSession::retrieveMessage(int id, MailMessage& message, PartHandler& handler)
 
227
{
 
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);
 
234
}
 
235
 
 
236
 
 
237
void POP3ClientSession::retrieveMessage(int id, std::ostream& ostr)
 
238
{
 
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);
 
245
}
 
246
 
 
247
 
 
248
void POP3ClientSession::retrieveHeader(int id, MessageHeader& header)
 
249
{
 
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);
 
255
        header.read(mis);
 
256
        // skip stuff following header
 
257
        mis.get(); // \r
 
258
        mis.get(); // \n
 
259
}
 
260
 
 
261
 
 
262
void POP3ClientSession::deleteMessage(int id)
 
263
{
 
264
        std::string response;
 
265
        sendCommand("DELE", NumberFormatter::format(id), response);
 
266
        if (!isPositive(response)) throw POP3Exception("Cannot mark message for deletion", response);
 
267
}
 
268
 
 
269
 
 
270
bool POP3ClientSession::sendCommand(const std::string& command, std::string& response)
 
271
{
 
272
        _socket.sendMessage(command);
 
273
        _socket.receiveMessage(response);
 
274
        return isPositive(response);
 
275
}
 
276
 
 
277
 
 
278
bool POP3ClientSession::sendCommand(const std::string& command, const std::string& arg, std::string& response)
 
279
{
 
280
        _socket.sendMessage(command, arg);
 
281
        _socket.receiveMessage(response);
 
282
        return isPositive(response);
 
283
}
 
284
 
 
285
 
 
286
bool POP3ClientSession::sendCommand(const std::string& command, const std::string& arg1, const std::string& arg2, std::string& response)
 
287
{
 
288
        _socket.sendMessage(command, arg1, arg2);
 
289
        _socket.receiveMessage(response);
 
290
        return isPositive(response);
 
291
}
 
292
 
 
293
 
 
294
bool POP3ClientSession::isPositive(const std::string& response)
 
295
{
 
296
        return response.length() > 0 && response[0] == '+';
 
297
}
 
298
 
 
299
 
 
300
} } // namespace Poco::Net