aboutsummaryrefslogtreecommitdiffstats
path: root/src/messaging/maildirMessage.cpp
blob: 2322dfd0a2065009abf15888388d1fd2e6da8bd5 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//
// VMime library (http://vmime.sourceforge.net)
// Copyright (C) 2002-2004 Vincent Richard <[email protected]>
//
// 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 2 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., 675 Mass Ave, Cambridge, MA 02139, USA.
//

#include "maildirMessage.hpp"
#include "maildirFolder.hpp"
#include "maildirUtils.hpp"

#include "../exception.hpp"
#include "../platformDependant.hpp"


namespace vmime {
namespace messaging {


//
// maildirPart
//

class maildirStructure;

class maildirPart : public part
{
public:

	maildirPart(maildirPart* parent, const int number, const bodyPart& part);
	~maildirPart();


	const structure& getStructure() const;
	structure& getStructure();

	const maildirPart* getParent() const { return (m_parent); }

	const mediaType& getType() const { return (m_mediaType); }
	const int getSize() const { return (m_size); }
	const int getNumber() const { return (m_number); }

	const header& getHeader() const
	{
		if (m_header == NULL)
			throw exceptions::unfetched_object();
		else
			return (*m_header);
	}

	header& getOrCreateHeader()
	{
		if (m_header != NULL)
			return (*m_header);
		else
			return (*(m_header = new header()));
	}

	const int getHeaderParsedOffset() const { return (m_headerParsedOffset); }
	const int getHeaderParsedLength() const { return (m_headerParsedLength); }

	const int getBodyParsedOffset() const { return (m_bodyParsedOffset); }
	const int getBodyParsedLength() const { return (m_bodyParsedLength); }

private:

	maildirStructure* m_structure;
	maildirPart* m_parent;
	header* m_header;

	int m_number;
	int m_size;
	mediaType m_mediaType;

	int m_headerParsedOffset;
	int m_headerParsedLength;

	int m_bodyParsedOffset;
	int m_bodyParsedLength;
};



//
// maildirStructure
//

class maildirStructure : public structure
{
private:

	maildirStructure()
	{
	}

public:

	maildirStructure(maildirPart* parent, const bodyPart& part)
	{
		m_parts.push_back(new maildirPart(parent, 1, part));
	}

	maildirStructure(maildirPart* parent, const std::vector <const vmime::bodyPart*>& list)
	{
		int number = 1;

		for (unsigned int i = 0 ; i < list.size() ; ++i)
			m_parts.push_back(new maildirPart(parent, number, *list[i]));
	}

	~maildirStructure()
	{
		free_container(m_parts);
	}


	const part& operator[](const int x) const
	{
		return (*m_parts[x - 1]);
	}

	part& operator[](const int x)
	{
		return (*m_parts[x - 1]);
	}

	const int getCount() const
	{
		return (m_parts.size());
	}


	static maildirStructure* emptyStructure()
	{
		return (&m_emptyStructure);
	}

private:

	static maildirStructure m_emptyStructure;

	std::vector <maildirPart*> m_parts;
};


maildirStructure maildirStructure::m_emptyStructure;



maildirPart::maildirPart(maildirPart* parent, const int number, const bodyPart& part)
	: m_parent(parent), m_header(NULL), m_number(number)
{
	if (part.getBody()->getPartList().size() == 0)
		m_structure = NULL;
	else
		m_structure = new maildirStructure(this, part.getBody()->getPartList());

	m_headerParsedOffset = part.getHeader()->getParsedOffset();
	m_headerParsedLength = part.getHeader()->getParsedLength();

	m_bodyParsedOffset = part.getBody()->getParsedOffset();
	m_bodyParsedLength = part.getBody()->getParsedLength();

	m_size = part.getBody()->getContents().getLength();

	m_mediaType = part.getBody()->getContentType();
}


maildirPart::~maildirPart()
{
	delete (m_structure);
	delete (m_header);
}


const structure& maildirPart::getStructure() const
{
	if (m_structure != NULL)
		return (*m_structure);
	else
		return (*maildirStructure::emptyStructure());
}


structure& maildirPart::getStructure()
{
	if (m_structure != NULL)
		return (*m_structure);
	else
		return (*maildirStructure::emptyStructure());
}



//
// maildirMessage
//

maildirMessage::maildirMessage(maildirFolder* folder, const int num)
	: m_folder(folder), m_num(num), m_size(-1), m_flags(FLAG_UNDEFINED),
	  m_expunged(false), m_header(NULL), m_structure(NULL)
{
	m_folder->registerMessage(this);
}


maildirMessage::~maildirMessage()
{
	if (m_folder)
		m_folder->unregisterMessage(this);

	delete (m_header);
	delete (m_structure);
}


void maildirMessage::onFolderClosed()
{
	m_folder = NULL;
}


const int maildirMessage::getNumber() const
{
	return (m_num);
}


const message::uid maildirMessage::getUniqueId() const
{
	return (m_uid);
}


const int maildirMessage::getSize() const
{
	if (m_size == -1)
		throw exceptions::unfetched_object();

	return (m_size);
}


const bool maildirMessage::isExpunged() const
{
	return (m_expunged);
}


const structure& maildirMessage::getStructure() const
{
	if (m_structure == NULL)
		throw exceptions::unfetched_object();

	return (*m_structure);
}


structure& maildirMessage::getStructure()
{
	if (m_structure == NULL)
		throw exceptions::unfetched_object();

	return (*m_structure);
}


const header& maildirMessage::getHeader() const
{
	if (m_header == NULL)
		throw exceptions::unfetched_object();

	return (*m_header);
}


const int maildirMessage::getFlags() const
{
	if (m_flags == FLAG_UNDEFINED)
		throw exceptions::unfetched_object();

	return (m_flags);
}


void maildirMessage::setFlags(const int flags, const int mode)
{
	if (!m_folder)
		throw exceptions::folder_not_found();

	m_folder->setMessageFlags(m_num, m_num, flags, mode);
}


void maildirMessage::extract(utility::outputStream& os,
	progressionListener* progress, const int start, const int length) const
{
	extractImpl(os, progress, 0, m_size, start, length);
}


void maildirMessage::extractPart(const part& p, utility::outputStream& os,
	progressionListener* progress, const int start, const int length) const
{
	const maildirPart& mp = dynamic_cast <const maildirPart&>(p);

	extractImpl(os, progress, mp.getBodyParsedOffset(), mp.getBodyParsedLength(), start, length);
}


void maildirMessage::extractImpl(utility::outputStream& os, progressionListener* progress,
	const int start, const int length, const int partialStart, const int partialLength) const
{
	utility::fileSystemFactory* fsf = platformDependant::getHandler()->getFileSystemFactory();

	const utility::file::path path = m_folder->getMessageFSPath(m_num);
	utility::auto_ptr <utility::file> file = fsf->create(path);

	utility::auto_ptr <utility::fileReader> reader = file->getFileReader();
	utility::auto_ptr <utility::inputStream> is = reader->getInputStream();

	is->skip(start + partialStart);

	utility::stream::value_type buffer[8192];
	utility::stream::size_type remaining = (partialLength == -1 ? length
		: std::min(partialLength, length));

	const int total = remaining;
	int current = 0;

	if (progress)
		progress->start(total);

	while (!is->eof() && remaining > 0)
	{
		const utility::stream::size_type read =
			is->read(buffer, std::min(remaining, sizeof(buffer)));

		remaining -= read;
		current += read;

		os.write(buffer, read);

		if (progress)
			progress->progress(current, total);
	}

	if (progress)
		progress->stop(total);
}


void maildirMessage::fetchPartHeader(part& p)
{
	maildirPart& mp = dynamic_cast <maildirPart&>(p);

	utility::fileSystemFactory* fsf = platformDependant::getHandler()->getFileSystemFactory();

	const utility::file::path path = m_folder->getMessageFSPath(m_num);
	utility::auto_ptr <utility::file> file = fsf->create(path);

	utility::auto_ptr <utility::fileReader> reader = file->getFileReader();
	utility::auto_ptr <utility::inputStream> is = reader->getInputStream();

	is->skip(mp.getHeaderParsedOffset());

	utility::stream::value_type buffer[1024];
	utility::stream::size_type remaining = mp.getHeaderParsedLength();

	string contents;
	contents.reserve(remaining);

	while (!is->eof() && remaining > 0)
	{
		const utility::stream::size_type read =
			is->read(buffer, std::min(remaining, sizeof(buffer)));

		remaining -= read;

		contents.append(buffer, read);
	}

	mp.getOrCreateHeader().parse(contents);
}


void maildirMessage::fetch(maildirFolder* folder, const int options)
{
	if (m_folder != folder)
		throw exceptions::folder_not_found();

	utility::fileSystemFactory* fsf = platformDependant::getHandler()->getFileSystemFactory();

	const utility::file::path path = folder->getMessageFSPath(m_num);
	utility::auto_ptr <utility::file> file = fsf->create(path);

	if (options & folder::FETCH_FLAGS)
		m_flags = maildirUtils::extractFlags(path.getLastComponent());

	if (options & folder::FETCH_SIZE)
		m_size = file->getLength();

	if (options & folder::FETCH_UID)
		m_uid = maildirUtils::extractId(path.getLastComponent()).getBuffer();

	if (options & (folder::FETCH_ENVELOPE | folder::FETCH_CONTENT_INFO |
	               folder::FETCH_FULL_HEADER | folder::FETCH_STRUCTURE))
	{
		string contents;

		utility::auto_ptr <utility::fileReader> reader = file->getFileReader();
		utility::auto_ptr <utility::inputStream> is = reader->getInputStream();

		// Need whole message contents for structure
		if (options & folder::FETCH_STRUCTURE)
		{
			utility::stream::value_type buffer[16384];

			contents.reserve(file->getLength());

			while (!is->eof())
			{
				const utility::stream::size_type read = is->read(buffer, sizeof(buffer));
				contents.append(buffer, read);
			}
		}
		// Need only header
		else
		{
			utility::stream::value_type buffer[1024];

			contents.reserve(4096);

			while (!is->eof())
			{
				const utility::stream::size_type read = is->read(buffer, sizeof(buffer));
				contents.append(buffer, read);

				const string::size_type sep1 = contents.rfind("\r\n\r\n");
				const string::size_type sep2 = contents.rfind("\n\n");

				if (sep1 != string::npos)
				{
					contents.erase(contents.begin() + sep1 + 4, contents.end());
					break;
				}
				else if (sep2 != string::npos)
				{
					contents.erase(contents.begin() + sep2 + 2, contents.end());
					break;
				}
			}
		}

		vmime::message msg;
		msg.parse(contents);

		// Extract structure
		if (options & folder::FETCH_STRUCTURE)
		{
			if (m_structure)
				delete (m_structure);

			m_structure = new maildirStructure(NULL, msg);
		}

		// Extract some header fields or whole header
		if (options & (folder::FETCH_ENVELOPE |
		               folder::FETCH_CONTENT_INFO |
		               folder::FETCH_FULL_HEADER))
		{
			getOrCreateHeader().copyFrom(*(msg.getHeader()));
		}
	}
}


header& maildirMessage::getOrCreateHeader()
{
	if (m_header != NULL)
		return (*m_header);
	else
		return (*(m_header = new header()));
}


} // messaging
} // vmime