aboutsummaryrefslogtreecommitdiffstats
path: root/src/messageParser.cpp
blob: aedbecaeee043fa25fadedbd5645c10ec5fce4aa (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
//
// VMime library (http://vmime.sourceforge.net)
// Copyright (C) 2002-2005 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 "vmime/messageParser.hpp"

#include "vmime/defaultAttachment.hpp"
#include "vmime/textPartFactory.hpp"


namespace vmime
{


messageParser::messageParser(const string& buffer)
{
	vmime::message msg;
	msg.parse(buffer);

	parse(msg);
}


messageParser::messageParser(const message& msg)
{
	parse(msg);
}


messageParser::~messageParser()
{
	free_container(m_attach);
	free_container(m_textParts);

	for (std::map <attachment*, contentDispositionField*>::iterator
	     it = m_attachInfo.begin() ; it != m_attachInfo.end() ; ++it)
	{
		delete ((*it).second);
	}
}


void messageParser::parse(const message& msg)
{
	// Header fields (if field is present, copy its value, else do nothing)
#ifndef VMIME_BUILDING_DOC

#define TRY_FIELD(var, type, name) \
	try { var = dynamic_cast<type&>(*msg.getHeader()->findField(name)).getValue(); } \
	catch (exceptions::no_such_field) { }

	TRY_FIELD(m_from, mailboxField, fields::FROM);

	TRY_FIELD(m_to, addressListField, fields::TO);
	TRY_FIELD(m_cc, addressListField, fields::CC);
	TRY_FIELD(m_bcc, addressListField, fields::BCC);

	TRY_FIELD(m_subject, textField, fields::SUBJECT);

#undef TRY_FIELD

#endif // VMIME_BUILDING_DOC

	// Date
	try
	{
		vmime::relayField& recv = dynamic_cast <vmime::relayField&>
			(*msg.getHeader()->findField(fields::RECEIVED));

		m_date = recv.getValue().getDate();
	}
	catch (vmime::exceptions::no_such_field&)
	{
		try
		{
			vmime::dateField& date = dynamic_cast <vmime::dateField&>
				(*msg.getHeader()->findField(fields::DATE));

			m_date = date.getValue();
		}
		catch (vmime::exceptions::no_such_field&)
		{
			m_date = datetime::now();
		}
	}

	// Attachments
	findAttachments(msg);

	// Text parts
	findTextParts(msg, msg);
}


void messageParser::findAttachments(const bodyPart& part)
{
	// We simply search for parts that are not "Content-disposition: inline".
	for (int i = 0 ; i < part.getBody()->getPartCount() ; ++i)
	{
		const bodyPart& p = *part.getBody()->getPartAt(i);
		const header& hdr = *p.getHeader();
		const body& bdy = *p.getBody();

		// Is this part an attachment?
		bool isAttachment = false;
		const contentDispositionField* contentDispField = NULL;

		try
		{
			const contentDispositionField& cdf = dynamic_cast<contentDispositionField&>
				(*hdr.findField(fields::CONTENT_DISPOSITION));

			if (cdf.getValue().getName() != contentDispositionTypes::INLINE)
			{
				contentDispField = &cdf;
				isAttachment = true;
			}
		}
		catch (exceptions::no_such_field)
		{
			// No "Content-disposition" field: assume "attachment" if
			// type is not "text/..." or "multipart/...".
			mediaType type;

			try
			{
				const contentTypeField& ctf = dynamic_cast<contentTypeField&>
					(*hdr.findField(fields::CONTENT_TYPE));

				type = ctf.getValue();
			}
			catch (exceptions::no_such_field)
			{
				// No "Content-type" field: assume "application/octet-stream".
				type = mediaType(mediaTypes::APPLICATION,
				                 mediaTypes::APPLICATION_OCTET_STREAM);
			}

			if (type.getType() != mediaTypes::TEXT &&
			    type.getType() != mediaTypes::MULTIPART)
			{
				isAttachment = true;
			}
		}

		if (isAttachment)
		{
			// Determine the media type of this attachment
			mediaType type;

			try
			{
				const contentTypeField& ctf = dynamic_cast<contentTypeField&>
					(*hdr.findField(fields::CONTENT_TYPE));

				type = ctf.getValue();
			}
			catch (exceptions::no_such_field)
			{
				// No "Content-type" field: assume "application/octet-stream".
				type = mediaType(mediaTypes::APPLICATION,
				                 mediaTypes::APPLICATION_OCTET_STREAM);
			}

			// Get the description (if available)
			text description;

			try
			{
				const textField& cd = dynamic_cast<textField&>
					(*hdr.findField(fields::CONTENT_DESCRIPTION));

				description = cd.getValue();
			}
			catch (exceptions::no_such_field)
			{
				// No description available.
			}

			// Construct the attachment object
			attachment* attach = new defaultAttachment
				(bdy.getContents(), bdy.getEncoding(), type, description);

			if (contentDispField != NULL)
			{
				m_attachInfo.insert(std::map <attachment*, contentDispositionField*>::
					value_type(attach, dynamic_cast <contentDispositionField*>
						(contentDispField->clone())));
			}

			// Add the attachment to the list
			m_attach.push_back(attach);
		}

		// Try to find attachments in sub-parts
		if (bdy.getPartCount())
			findAttachments(p);
	}
}


void messageParser::findTextParts(const bodyPart& msg, const bodyPart& part)
{
	// Handle the case in which the message is not multipart: if the body part is
	// "text/*", take this part.
	if (part.getBody()->getPartCount() == 0)
	{
		mediaType type(mediaTypes::TEXT, mediaTypes::TEXT_PLAIN);
		bool accept = false;

		try
		{
			const contentTypeField& ctf = dynamic_cast<contentTypeField&>
				(*msg.getHeader()->findField(fields::CONTENT_TYPE));

			if (ctf.getValue().getType() == mediaTypes::TEXT)
			{
				type = ctf.getValue();
				accept = true;
			}
		}
		catch (exceptions::no_such_field)
		{
			// No "Content-type" field: assume "text/plain".
			accept = true;
		}

		if (accept)
		{
			textPart* textPart = textPartFactory::getInstance()->create(type);
			textPart->parse(msg, msg, msg);

			m_textParts.push_back(textPart);
		}
	}
	// Multipart message
	else
	{
		findSubTextParts(msg, part);
	}
}


bool messageParser::findSubTextParts(const bodyPart& msg, const bodyPart& part)
{
	// In general, all the text parts are contained in parallel in the same
	// parent part (or message).
	// So, wherever the text parts are, all we have to do is to find the first
	// MIME part which is a text part.

	std::vector <const bodyPart*> textParts;

	for (int i = 0 ; i < part.getBody()->getPartCount() ; ++i)
	{
		const bodyPart& p = *part.getBody()->getPartAt(i);

		try
		{
			const contentTypeField& ctf = dynamic_cast<contentTypeField&>
				(*p.getHeader()->findField(fields::CONTENT_TYPE));

			if (ctf.getValue().getType() == mediaTypes::TEXT)
			{
				textParts.push_back(&p);
			}
		}
		catch (exceptions::no_such_field)
		{
			// No "Content-type" field.
		}
	}

	if (textParts.size())
	{
		// Okay. So we have found at least one text part
		for (std::vector <const bodyPart*>::const_iterator p = textParts.begin() ;
		     p != textParts.end() ; ++p)
		{
			const contentTypeField& ctf = dynamic_cast<contentTypeField&>
				(*(*p)->getHeader()->findField(fields::CONTENT_TYPE));

			try
			{
				textPart* textPart = textPartFactory::getInstance()->create(ctf.getValue());
				textPart->parse(msg, part, **p);

				m_textParts.push_back(textPart);
			}
			catch (exceptions::no_factory_available& e)
			{
				// Content-type not recognized.
			}
		}

		//return true;
	}

	//else
	{
		bool found = false;

		for (int i = 0 ; !found && (i < part.getBody()->getPartCount()) ; ++i)
		{
			found = findSubTextParts(msg, *part.getBody()->getPartAt(i));
		}

		return found;
	}
}


const contentDispositionField* messageParser::getAttachmentInfo(const attachment* a) const
{
	std::map <attachment*, contentDispositionField*>::const_iterator
		it = m_attachInfo.find(const_cast <attachment*>(a));

	return (it != m_attachInfo.end() ? (*it).second : NULL);
}


const mailbox& messageParser::getExpeditor() const
{
	return (m_from);
}


const addressList& messageParser::getRecipients() const
{
	return (m_to);
}


const addressList& messageParser::getCopyRecipients() const
{
	return (m_cc);
}


const addressList& messageParser::getBlindCopyRecipients() const
{
	return (m_bcc);
}


const text& messageParser::getSubject() const
{
	return (m_subject);
}


const datetime& messageParser::getDate() const
{
	return (m_date);
}


const std::vector <const attachment*> messageParser::getAttachmentList() const
{
	std::vector <const attachment*> res;

	res.reserve(m_attach.size());

	for (std::vector <attachment*>::const_iterator it = m_attach.begin() ;
	     it != m_attach.end() ; ++it)
	{
		res.push_back(*it);
	}

	return (res);
}


const int messageParser::getAttachmentCount() const
{
	return (m_attach.size());
}


const attachment* messageParser::getAttachmentAt(const int pos) const
{
	return (m_attach[pos]);
}


const std::vector <const textPart*> messageParser::getTextPartList() const
{
	std::vector <const textPart*> res;

	res.reserve(m_textParts.size());

	for (std::vector <textPart*>::const_iterator it = m_textParts.begin() ;
	     it != m_textParts.end() ; ++it)
	{
		res.push_back(*it);
	}

	return (res);
}


const int messageParser::getTextPartCount() const
{
	return (m_textParts.size());
}


const textPart* messageParser::getTextPartAt(const int pos) const
{
	return (m_textParts[pos]);
}


} // vmime