aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/imap/IMAPUtils.cpp
blob: bf31041438c4405060161665c2ecf57f336c09f2 (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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002-2013 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 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_IMAP


#include "vmime/net/imap/IMAPUtils.hpp"
#include "vmime/net/imap/IMAPStore.hpp"

#include "vmime/net/message.hpp"
#include "vmime/net/folder.hpp"

#include <sstream>
#include <iterator>
#include <algorithm>


namespace vmime {
namespace net {
namespace imap {


// static
const string IMAPUtils::quoteString(const string& text)
{
	//
	// ATOM_CHAR       ::= <any CHAR except atom_specials>
	//
	// atom_specials   ::= "(" / ")" / "{" / SPACE / CTL /
	//                     list_wildcards / quoted_specials
	//
	// list_wildcards  ::= "%" / "*"
	//
	// quoted_specials ::= <"> / "\"
	//
	// CHAR            ::= <any 7-bit US-ASCII character except NUL,
	//                      0x01 - 0x7f>
	//
	// CTL             ::= <any ASCII control character and DEL,
	//                      0x00 - 0x1f, 0x7f>
	//

	bool needQuoting = text.empty();

	for (string::const_iterator it = text.begin() ;
	     !needQuoting && it != text.end() ; ++it)
	{
		const unsigned char c = *it;

		switch (c)
		{
		case '(':
		case ')':
		case '{':
		case 0x20:   // SPACE
		case '%':
		case '*':
		case '"':
		case '\\':

			needQuoting = true;
			break;

		default:

			if (c <= 0x1f || c >= 0x7f)
				needQuoting = true;
		}
	}

	if (needQuoting)
	{
		string quoted;
		quoted.reserve((text.length() * 3) / 2 + 2);

		quoted += '"';

		for (string::const_iterator it = text.begin() ; it != text.end() ; ++it)
		{
			const unsigned char c = *it;

			if (c == '\\' || c == '"')
				quoted += '\\';

			quoted += c;
		}

		quoted += '"';

		return (quoted);
	}
	else
	{
		return (text);
	}
}


const string IMAPUtils::pathToString
	(const char hierarchySeparator, const folder::path& path)
{
	string result;

	for (size_t i = 0 ; i < path.getSize() ; ++i)
	{
		if (i > 0) result += hierarchySeparator;
		result += toModifiedUTF7(hierarchySeparator, path[i]);
	}

	return (result);
}


const folder::path IMAPUtils::stringToPath
	(const char hierarchySeparator, const string& str)
{
	folder::path result;
	string::const_iterator begin = str.begin();

	for (string::const_iterator it = str.begin() ; it != str.end() ; ++it)
	{
		if (*it == hierarchySeparator)
		{
			result /= fromModifiedUTF7(string(begin, it));
			begin = it + 1;
		}
	}

	if (begin != str.end())
	{
		result /= fromModifiedUTF7(string(begin, str.end()));
	}

	return (result);
}


const string IMAPUtils::toModifiedUTF7
	(const char hierarchySeparator, const folder::path::component& text)
{
	// We will replace the hierarchy separator with an equivalent
	// UTF-7 sequence, so we compute it here...
	const char base64alphabet[] =
		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,=";

	const unsigned int hs = static_cast <unsigned int>(static_cast <unsigned char>(hierarchySeparator));

	string hsUTF7;
	hsUTF7.resize(3);

	hsUTF7[0] = base64alphabet[0];
	hsUTF7[1] = base64alphabet[(hs & 0xF0) >> 4];
	hsUTF7[2] = base64alphabet[(hs & 0x0F) << 2];

	// iconv() is buggy with UTF-8 to UTF-7 conversion, so we do it "by hand".
	// This code is largely inspired from "imap/utf7.c", in mutt 1.4.
	// Copyright (C) 2000 Edmund Grimley Evans <[email protected]>

	// WARNING: This may throw "exceptions::charset_conv_error"
	const string cvt = text.getConvertedText(charset(charsets::UTF_8));

	// In the worst case we convert 2 chars to 7 chars.
	// For example: "\x10&\x10&..." -> "&ABA-&-&ABA-&-...".
	string out;
	out.reserve((cvt.length() / 2) * 7 + 6);

	int b = 0, k = 0;
	bool base64 = false;

	string::size_type remaining = cvt.length();

	for (string::size_type i = 0, len = cvt.length() ; i < len ; )
	{
		const unsigned char c = cvt[i];

		// Replace hierarchy separator with an equivalent UTF-7 Base64 sequence
		if (!base64 && c == hierarchySeparator)
		{
			out += "&" + hsUTF7 + "-";

			++i;
			--remaining;
			continue;
		}

		string::size_type n = 0;
		int ch = 0;

		if (c < 0x80)
			ch = c, n = 0;
		else if (c < 0xc2)
			return "";
		else if (c < 0xe0)
			ch = c & 0x1f, n = 1;
		else if (c < 0xf0)
			ch = c & 0x0f, n = 2;
		else if (c < 0xf8)
			ch = c & 0x07, n = 3;
		else if (c < 0xfc)
			ch = c & 0x03, n = 4;
		else if (c < 0xfe)
			ch = c & 0x01, n = 5;
		else
			return "";

		if (n > remaining)
			return "";  // error

		++i;
		--remaining;

		for (string::size_type j = 0 ; j < n ; j++)
		{
			if ((cvt[i + j] & 0xc0) != 0x80)
				return "";  // error

			ch = (ch << 6) | (cvt[i + j] & 0x3f);
		}

		if (n > 1 && !(ch >> (n * 5 + 1)))
			return "";  // error

		i += n;
		remaining -= n;

		if (ch < 0x20 || ch >= 0x7f)
		{
			if (!base64)
			{
				out += '&';
				base64 = true;
				b = 0;
				k = 10;
			}

			if (ch & ~0xffff)
				ch = 0xfffe;

			out += base64alphabet[b | ch >> k];

			k -= 6;

			for ( ; k >= 0 ; k -= 6)
				out += base64alphabet[(ch >> k) & 0x3f];

			b = (ch << (-k)) & 0x3f;
			k += 16;
		}
		else
		{
			if (base64)
			{
				if (k > 10)
					out += base64alphabet[b];

				out += '-';
				base64 = false;
			}

			out += static_cast <string::value_type>(ch);

			if (ch == '&')
				out += '-';
		}
	}

	if (base64)
	{
		if (k > 10)
			out += base64alphabet[b];

		out += '-';
	}

	return (out);
}


const folder::path::component IMAPUtils::fromModifiedUTF7(const string& text)
{
	// Transcode from modified UTF-7 (RFC-2060).
	string out;
	out.reserve(text.length());

	bool inB64sequence = false;
	unsigned char prev = 0;

	for (string::const_iterator it = text.begin() ; it != text.end() ; ++it)
	{
		const unsigned char c = *it;

		switch (c)
		{
		// Start of Base64 sequence
		case '&':
		{
			if (!inB64sequence)
			{
				inB64sequence = true;
				out += '+';
			}
			else
			{
				out += '&';
			}

			break;
		}
		// End of Base64 sequence (or "&-" --> "&")
		case '-':
		{
			if (inB64sequence && prev == '&')
				out += '&';
			else
				out += '-';

			inB64sequence = false;
			break;
		}
		// ',' is used instead of '/' in modified Base64
		case ',':
		{
			out += (inB64sequence ? '/' : ',');
			break;
		}
		default:
		{
			out += c;
			break;
		}

		}

		prev = c;
	}

	// Store it as UTF-8 by default
	string cvt;
	charset::convert(out, cvt,
		charset(charsets::UTF_7), charset(charsets::UTF_8));

	return (folder::path::component(cvt, charset(charsets::UTF_8)));
}


int IMAPUtils::folderTypeFromFlags(const IMAPParser::mailbox_flag_list* list)
{
	// Get folder type
	int type = folder::TYPE_CONTAINS_MESSAGES | folder::TYPE_CONTAINS_FOLDERS;
	const std::vector <IMAPParser::mailbox_flag*>& flags = list->flags();

	for (std::vector <IMAPParser::mailbox_flag*>::const_iterator it = flags.begin() ;
	     it != flags.end() ; ++it)
	{
		if ((*it)->type() == IMAPParser::mailbox_flag::NOSELECT)
			type &= ~folder::TYPE_CONTAINS_MESSAGES;
	}

	if (type & folder::TYPE_CONTAINS_MESSAGES)
		type &= ~folder::TYPE_CONTAINS_FOLDERS;

	return (type);
}


int IMAPUtils::folderFlagsFromFlags(const IMAPParser::mailbox_flag_list* list)
{
	// Get folder flags
	int folderFlags = folder::FLAG_CHILDREN;
	const std::vector <IMAPParser::mailbox_flag*>& flags = list->flags();

	for (std::vector <IMAPParser::mailbox_flag*>::const_iterator it = flags.begin() ;
	     it != flags.end() ; ++it)
	{
		if ((*it)->type() == IMAPParser::mailbox_flag::NOSELECT)
			folderFlags |= folder::FLAG_NO_OPEN;
		else if ((*it)->type() == IMAPParser::mailbox_flag::NOINFERIORS)
			folderFlags &= ~folder::FLAG_CHILDREN;
	}

	return (folderFlags);
}


int IMAPUtils::messageFlagsFromFlags(const IMAPParser::flag_list* list)
{
	const std::vector <IMAPParser::flag*>& flagList = list->flags();
	int flags = 0;

	for (std::vector <IMAPParser::flag*>::const_iterator
	     it = flagList.begin() ; it != flagList.end() ; ++it)
	{
		switch ((*it)->type())
		{
		case IMAPParser::flag::ANSWERED:
			flags |= message::FLAG_REPLIED;
			break;
		case IMAPParser::flag::FLAGGED:
			flags |= message::FLAG_MARKED;
			break;
		case IMAPParser::flag::DELETED:
			flags |= message::FLAG_DELETED;
			break;
		case IMAPParser::flag::SEEN:
			flags |= message::FLAG_SEEN;
			break;
		case IMAPParser::flag::DRAFT:
			flags |= message::FLAG_DRAFT;
			break;

		default:
		//case IMAPParser::flag::UNKNOWN:
			break;
		}
	}

	return (flags);
}


const string IMAPUtils::messageFlagList(const int flags)
{
	std::vector <string> flagList;

	if (flags & message::FLAG_REPLIED) flagList.push_back("\\Answered");
	if (flags & message::FLAG_MARKED) flagList.push_back("\\Flagged");
	if (flags & message::FLAG_DELETED) flagList.push_back("\\Deleted");
	if (flags & message::FLAG_SEEN) flagList.push_back("\\Seen");
	if (flags & message::FLAG_DRAFT) flagList.push_back("\\Draft");

	if (!flagList.empty())
	{
		std::ostringstream res;
		res.imbue(std::locale::classic());

		res << "(";

		if (flagList.size() >= 2)
		{
			std::copy(flagList.begin(), flagList.end() - 1,
			          std::ostream_iterator <string>(res, " "));
		}

		res << *(flagList.end() - 1) << ")";

		return (res.str());
	}

	return "";
}


// static
const string IMAPUtils::dateTime(const vmime::datetime& date)
{
	std::ostringstream res;
	res.imbue(std::locale::classic());

	// date_time ::= <"> date_day_fixed "-" date_month "-" date_year
	//               SPACE time SPACE zone <">
	//
	// time      ::= 2digit ":" 2digit ":" 2digit
	//               ;; Hours minutes seconds
	// zone      ::= ("+" / "-") 4digit
	//               ;; Signed four-digit value of hhmm representing
	//               ;; hours and minutes west of Greenwich
	res << '"';

	// Date
	if (date.getDay() < 10) res << ' ';
	res << date.getDay();

	res << '-';

	static const char* monthNames[12] =
		{ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
		  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

	res << monthNames[std::min(std::max(date.getMonth() - 1, 0), 11)];

	res << '-';

	if (date.getYear() < 10) res << '0';
	if (date.getYear() < 100) res << '0';
	if (date.getYear() < 1000) res << '0';
	res << date.getYear();

	res << ' ';

	// Time
	if (date.getHour() < 10) res << '0';
	res << date.getHour() << ':';

	if (date.getMinute() < 10) res << '0';
	res << date.getMinute() << ':';

	if (date.getSecond() < 10) res << '0';
	res << date.getSecond();

	res << ' ';

	// Zone
	const int zs = (date.getZone() < 0 ? -1 : 1);
	const int zh = (date.getZone() * zs) / 60;
	const int zm = (date.getZone() * zs) % 60;

	res << (zs < 0 ? '-' : '+');

	if (zh < 10) res << '0';
	res << zh;

	if (zm < 10) res << '0';
	res << zm;

	res << '"';


	return (res.str());
}


// static
const string IMAPUtils::buildFetchRequest
	(shared_ptr <IMAPConnection> cnt, const messageSet& msgs, const fetchAttributes& options)
{
	// Example:
	//   C: A654 FETCH 2:4 (FLAGS BODY[HEADER.FIELDS (DATE FROM)])
	//   S: * 2 FETCH ....
	//   S: * 3 FETCH ....
	//   S: * 4 FETCH ....
	//   S: A654 OK FETCH completed

	std::vector <string> items;

	if (options.has(fetchAttributes::SIZE))
		items.push_back("RFC822.SIZE");

	if (options.has(fetchAttributes::FLAGS))
		items.push_back("FLAGS");

	if (options.has(fetchAttributes::STRUCTURE))
		items.push_back("BODYSTRUCTURE");

	if (options.has(fetchAttributes::UID))
	{
		items.push_back("UID");

		// Also fetch MODSEQ if CONDSTORE is supported
		if (cnt->hasCapability("CONDSTORE") && !cnt->isMODSEQDisabled())
			items.push_back("MODSEQ");
	}

	if (options.has(fetchAttributes::FULL_HEADER))
		items.push_back("RFC822.HEADER");
	else
	{
		if (options.has(fetchAttributes::ENVELOPE))
			items.push_back("ENVELOPE");

		std::vector <string> headerFields;

		if (options.has(fetchAttributes::CONTENT_INFO))
			headerFields.push_back("CONTENT_TYPE");

		if (options.has(fetchAttributes::IMPORTANCE))
		{
			headerFields.push_back("IMPORTANCE");
			headerFields.push_back("X-PRIORITY");
		}

		// Also add custom header fields to fetch, if any
		const std::vector <string> customHeaderFields = options.getHeaderFields();
		std::copy(customHeaderFields.begin(), customHeaderFields.end(), std::back_inserter(headerFields));

		if (!headerFields.empty())
		{
			string list;

			for (std::vector <string>::iterator it = headerFields.begin() ;
			     it != headerFields.end() ; ++it)
			{
				if (it != headerFields.begin())
					list += " ";

				list += *it;
			}

			items.push_back("BODY[HEADER.FIELDS (" + list + ")]");
		}
	}

	// Build the request text
	std::ostringstream command;
	command.imbue(std::locale::classic());

	if (msgs.isUIDSet())
		command << "UID FETCH " << messageSetToSequenceSet(msgs) << " (";
	else
		command << "FETCH " << messageSetToSequenceSet(msgs) << " (";

	for (std::vector <string>::const_iterator it = items.begin() ;
	     it != items.end() ; ++it)
	{
		if (it != items.begin()) command << " ";
		command << *it;
	}

	command << ")";

	return command.str();
}


// static
void IMAPUtils::convertAddressList
	(const IMAPParser::address_list& src, mailboxList& dest)
{
	for (std::vector <IMAPParser::address*>::const_iterator
	     it = src.addresses().begin() ; it != src.addresses().end() ; ++it)
	{
		const IMAPParser::address& addr = **it;

		text name;
		text::decodeAndUnfold(addr.addr_name()->value(), &name);

		string email = addr.addr_mailbox()->value()
			+ "@" + addr.addr_host()->value();

		dest.appendMailbox(make_shared <mailbox>(name, email));
	}
}



class IMAPUIDMessageSetEnumerator : public messageSetEnumerator
{
public:

	IMAPUIDMessageSetEnumerator()
		: m_first(true)
	{
	}

	void enumerateNumberMessageRange(const vmime::net::numberMessageRange& range)
	{
		if (!m_first)
			m_oss << ",";

		if (range.getFirst() == range.getLast())
			m_oss << range.getFirst();
		else
			m_oss << range.getFirst() << ":" << range.getLast();

		m_first = false;
	}

	void enumerateUIDMessageRange(const vmime::net::UIDMessageRange& range)
	{
		if (!m_first)
			m_oss << ",";

		if (range.getFirst() == range.getLast())
			m_oss << range.getFirst();
		else
			m_oss << range.getFirst() << ":" << range.getLast();

		m_first = false;
	}

	const std::string str() const
	{
		return m_oss.str();
	}

private:

	std::ostringstream m_oss;
	bool m_first;
};


class IMAPMessageSetEnumerator : public messageSetEnumerator
{
public:

	void enumerateNumberMessageRange(const vmime::net::numberMessageRange& range)
	{
		for (int i = range.getFirst(), last = range.getLast() ; i <= last ; ++i)
			m_list.push_back(i);
	}

	void enumerateUIDMessageRange(const vmime::net::UIDMessageRange& /* range */)
	{
		// Not used
	}

	const std::vector <int>& list() const
	{
		return m_list;
	}

public:

	std::vector <int> m_list;
};



// static
const string IMAPUtils::messageSetToSequenceSet(const messageSet& msgs)
{
	IMAPUIDMessageSetEnumerator en;
	msgs.enumerate(en);

	return en.str();
}


// static
const std::vector <int> IMAPUtils::messageSetToNumberList(const messageSet& msgs)
{
	IMAPMessageSetEnumerator en;
	msgs.enumerate(en);

	return en.list();
}


} // imap
} // net
} // vmime


#endif // VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_MESSAGING_PROTO_IMAP