aboutsummaryrefslogtreecommitdiff
path: root/vmime-master/src/vmime/net/pop3/POP3Message.cpp
blob: 8d6b7f536b63aa876ebfc51be0a23d1148561b5b (plain)
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
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002 Vincent Richard <vincent@vmime.org>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 3 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Linking this library statically or dynamically with other modules is making
// a combined work based on this library.  Thus, the terms and conditions of
// the GNU General Public License cover the whole combination.
//

#include "vmime/config.hpp"


#if VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_MESSAGING_PROTO_POP3


#include "vmime/net/pop3/POP3Message.hpp"
#include "vmime/net/pop3/POP3Command.hpp"
#include "vmime/net/pop3/POP3Response.hpp"
#include "vmime/net/pop3/POP3Folder.hpp"
#include "vmime/net/pop3/POP3Store.hpp"

#include "vmime/utility/outputStreamAdapter.hpp"
#include "vmime/utility/outputStreamStringAdapter.hpp"

#include <sstream>


namespace vmime {
namespace net {
namespace pop3 {


POP3Message::POP3Message(
	const shared_ptr <POP3Folder>& folder,
	const size_t num
)
	: m_folder(folder),
	  m_num(num),
	  m_size(-1),
	  m_deleted(false) {

	folder->registerMessage(this);
}


POP3Message::~POP3Message() {

	try {

		shared_ptr <POP3Folder> folder = m_folder.lock();

		if (folder) {
			folder->unregisterMessage(this);
		}

	} catch (...) {

		// Don't throw in destructor
	}
}


void POP3Message::onFolderClosed() {

	m_folder.reset();
}


size_t POP3Message::getNumber() const {

	return m_num;
}


const message::uid POP3Message::getUID() const {

	return m_uid;
}


size_t POP3Message::getSize() const {

	if (m_size == static_cast <size_t>(-1)) {
		throw exceptions::unfetched_object();
	}

	return m_size;
}


bool POP3Message::isExpunged() const {

	return false;
}


int POP3Message::getFlags() const {

	int flags = 0;

	if (m_deleted) {
		flags |= FLAG_DELETED;
	}

	return flags;
}


shared_ptr <const messageStructure> POP3Message::getStructure() const {

	throw exceptions::operation_not_supported();
}


shared_ptr <messageStructure> POP3Message::getStructure() {

	throw exceptions::operation_not_supported();
}


shared_ptr <const header> POP3Message::getHeader() const {

	if (!m_header) {
		throw exceptions::unfetched_object();
	}

	return m_header;
}


void POP3Message::extract(
	utility::outputStream& os,
	utility::progressListener* progress,
	const size_t start,
	const size_t length,
	const bool /* peek */
) const {

	shared_ptr <const POP3Folder> folder = m_folder.lock();

	if (!folder) {
		throw exceptions::illegal_state("Folder closed");
	} else if (!folder->getStore()) {
		throw exceptions::illegal_state("Store disconnected");
	}

	if (start != 0 && length != static_cast <size_t>(-1)) {
		throw exceptions::partial_fetch_not_supported();
	}

	// Emit the "RETR" command
	shared_ptr <POP3Store> store = folder->m_store.lock();

	POP3Command::RETR(m_num)->send(store->getConnection());

	try {

		POP3Response::readLargeResponse(
			store->getConnection(), os, progress,
			m_size == static_cast <size_t>(-1) ? 0 : m_size
		);

	} catch (exceptions::command_error& e) {

		throw exceptions::command_error("RETR", e.response());
	}
}


void POP3Message::extractPart(
	const shared_ptr <const messagePart>& /* p */,
	utility::outputStream& /* os */,
	utility::progressListener* /* progress */,
	const size_t /* start */,
	const size_t /* length */,
	const bool /* peek */
) const {

	throw exceptions::operation_not_supported();
}


void POP3Message::fetchPartHeader(const shared_ptr <messagePart>& /* p */) {

	throw exceptions::operation_not_supported();
}


void POP3Message::fetch(
	const shared_ptr <POP3Folder>& msgFolder,
	const fetchAttributes& options
) {

	shared_ptr <POP3Folder> folder = m_folder.lock();

	if (folder != msgFolder) {
		throw exceptions::folder_not_found();
	}

	// STRUCTURE and FLAGS attributes are not supported by POP3
	if (options.has(fetchAttributes::STRUCTURE | fetchAttributes::FLAGS)) {
		throw exceptions::operation_not_supported();
	}

	// Check for the real need to fetch the full header
	static const int optionsRequiringHeader =
		fetchAttributes::ENVELOPE | fetchAttributes::CONTENT_INFO |
		fetchAttributes::FULL_HEADER | fetchAttributes::IMPORTANCE;

	if (!options.has(optionsRequiringHeader)) {
		return;
	}

	// No need to differenciate between ENVELOPE, CONTENT_INFO, ...
	// since POP3 only permits to retrieve the whole header and not
	// fields in particular.

	// Emit the "TOP" command
	shared_ptr <POP3Store> store = folder->m_store.lock();

	POP3Command::TOP(m_num, 0)->send(store->getConnection());

	try {

		string buffer;
		utility::outputStreamStringAdapter bufferStream(buffer);

		POP3Response::readLargeResponse(
			store->getConnection(),
			bufferStream, /* progress */ NULL, /* predictedSize */ 0
		);

		m_header = make_shared <header>();
		m_header->parse(buffer);

	} catch (exceptions::command_error& e) {

		throw exceptions::command_error("TOP", e.response());
	}
}


void POP3Message::setFlags(const int /* flags */, const int /* mode */) {

	throw exceptions::operation_not_supported();
}


shared_ptr <vmime::message> POP3Message::getParsedMessage() {

	std::ostringstream oss;
	utility::outputStreamAdapter os(oss);

	extract(os);

	shared_ptr <vmime::message> msg = make_shared <vmime::message>();
	msg->parse(oss.str());

	return msg;
}


} // pop3
} // net
} // vmime


#endif // VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_MESSAGING_PROTO_POP3