forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailMessage.cpp
More file actions
1040 lines (903 loc) · 26.3 KB
/
MailMessage.cpp
File metadata and controls
1040 lines (903 loc) · 26.3 KB
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// MailMessage.cpp
//
// Library: Net
// Package: Mail
// Module: MailMessage
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Net/MailMessage.h"
#include "Poco/Net/MediaType.h"
#include "Poco/Net/MultipartReader.h"
#include "Poco/Net/MultipartWriter.h"
#include "Poco/Net/PartSource.h"
#include "Poco/Net/PartHandler.h"
#include "Poco/Net/StringPartSource.h"
#include "Poco/Net/QuotedPrintableEncoder.h"
#include "Poco/Net/QuotedPrintableDecoder.h"
#include "Poco/Net/NameValueCollection.h"
#include "Poco/Base64Encoder.h"
#include "Poco/Base64Decoder.h"
#include "Poco/StreamCopier.h"
#include "Poco/DateTimeFormat.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/DateTimeParser.h"
#include "Poco/String.h"
#include "Poco/Format.h"
#include "Poco/StringTokenizer.h"
#include "Poco/StreamCopier.h"
#include "Poco/NumberFormatter.h"
#include "Poco/TextEncoding.h"
#include "Poco/TextConverter.h"
#include "Poco/NumberParser.h"
#include "Poco/Ascii.h"
#include <sstream>
using Poco::Base64Encoder;
using Poco::Base64Decoder;
using Poco::StreamCopier;
using Poco::DateTimeFormat;
using Poco::DateTimeFormatter;
using Poco::DateTimeParser;
using Poco::StringTokenizer;
using Poco::TextEncoding;
using Poco::TextEncodingRegistry;
using Poco::TextConverter;
using Poco::NumberParser;
using Poco::Ascii;
using Poco::icompare;
namespace Poco {
namespace Net {
namespace
{
class MultiPartHandler: public PartHandler
/// This is a default part handler for multipart messages, used when there
/// is no external handler provided to he MailMessage. This handler
/// will handle all types of message parts, including attachments.
{
public:
MultiPartHandler(MailMessage* pMsg): _pMsg(pMsg)
/// Creates multi part handler.
/// The pMsg pointer points to the calling MailMessage
/// and will be used to properly populate it, so the
/// message content could be written out unmodified
/// in its entirety, including attachments.
{
}
~MultiPartHandler()
/// Destroys string part handler.
{
}
void handlePart(const MessageHeader& header, std::istream& stream)
/// Handles a part. If message pointer was provided at construction time,
/// the message pointed to will be properly populated so it could be written
/// back out at a later point in time.
{
std::string tmp;
Poco::StreamCopier::copyToString(stream, tmp);
if (_pMsg)
{
MailMessage::ContentTransferEncoding cte = MailMessage::ENCODING_7BIT;
if (header.has(MailMessage::HEADER_CONTENT_TRANSFER_ENCODING))
{
std::string enc = header[MailMessage::HEADER_CONTENT_TRANSFER_ENCODING];
if (enc == MailMessage::CTE_8BIT)
cte = MailMessage::ENCODING_8BIT;
else if (enc == MailMessage::CTE_QUOTED_PRINTABLE)
cte = MailMessage::ENCODING_QUOTED_PRINTABLE;
else if (enc == MailMessage::CTE_BASE64)
cte = MailMessage::ENCODING_BASE64;
}
std::string contentType = header.get(MailMessage::HEADER_CONTENT_TYPE, "text/plain; charset=us-ascii");
std::string contentDisp = header.get(MailMessage::HEADER_CONTENT_DISPOSITION, "");
std::string filename;
if (!contentDisp.empty())
filename = getParamFromHeader(contentDisp, "filename");
if (filename.empty())
filename = getParamFromHeader(contentType, "name");
Poco::SharedPtr<PartSource> pPS = _pMsg->createPartStore(tmp, contentType, filename);
poco_check_ptr (pPS);
NameValueCollection::ConstIterator it = header.begin();
NameValueCollection::ConstIterator end = header.end();
bool added = false;
for (; it != end; ++it)
{
if (!added && MailMessage::HEADER_CONTENT_DISPOSITION == it->first)
{
if (it->second == "inline")
_pMsg->addContent(pPS, cte);
else
_pMsg->addAttachment(getPartName(header), pPS, cte);
added = true;
}
pPS->headers().set(it->first, it->second);
}
if (contentDisp.empty())
{
_pMsg->addContent(pPS, cte);
}
}
}
private:
std::string getParamFromHeader(const std::string& header, const std::string& param)
{
StringTokenizer st(header, ";=", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
StringTokenizer::Iterator it = st.begin();
StringTokenizer::Iterator end = st.end();
for (; it != end; ++it) { if (icompare(*it, param) == 0) break; }
if (it != end)
{
++it;
if (it == end) return "";
return *it;
}
return "";
}
std::string getPartName(const MessageHeader& header)
{
std::string ct = MailMessage::HEADER_CONTENT_TYPE;
if (header.has(ct))
return getParamFromHeader(header[ct], "name");
return "";
}
MailMessage* _pMsg;
};
class StringPartHandler: public PartHandler
/// This is a default part handler, used when there is no
/// external handler provided to the MailMessage. This handler
/// handles only single-part messages.
{
public:
StringPartHandler(std::string& content): _str(content)
/// Creates string part handler.
/// The content parameter represents the part content.
{
}
~StringPartHandler()
/// Destroys string part handler.
{
}
void handlePart(const MessageHeader& header, std::istream& stream)
/// Handles a part.
{
std::string tmp;
Poco::StreamCopier::copyToString(stream, tmp);
_str.append(tmp);
}
private:
std::string& _str;
};
}
const std::string MailMessage::HEADER_SUBJECT("Subject");
const std::string MailMessage::HEADER_FROM("From");
const std::string MailMessage::HEADER_TO("To");
const std::string MailMessage::HEADER_CC("CC");
const std::string MailMessage::HEADER_BCC("BCC");
const std::string MailMessage::HEADER_DATE("Date");
const std::string MailMessage::HEADER_CONTENT_TYPE("Content-Type");
const std::string MailMessage::HEADER_CONTENT_TRANSFER_ENCODING("Content-Transfer-Encoding");
const std::string MailMessage::HEADER_CONTENT_DISPOSITION("Content-Disposition");
const std::string MailMessage::HEADER_CONTENT_ID("Content-ID");
const std::string MailMessage::HEADER_MIME_VERSION("Mime-Version");
const std::string MailMessage::EMPTY_HEADER;
const std::string MailMessage::TEXT_PLAIN("text/plain");
const std::string MailMessage::CTE_7BIT("7bit");
const std::string MailMessage::CTE_8BIT("8bit");
const std::string MailMessage::CTE_QUOTED_PRINTABLE("quoted-printable");
const std::string MailMessage::CTE_BASE64("base64");
MailMessage::MailMessage(PartStoreFactory* pStoreFactory):
_encoding(),
_pStoreFactory(pStoreFactory)
{
Poco::Timestamp now;
setDate(now);
setContentType("text/plain");
}
MailMessage::MailMessage(MailMessage&& other):
_recipients(std::move(other._recipients)),
_content(std::move(other._content)),
_encoding(other._encoding),
_boundary(std::move(other._boundary)),
_pStoreFactory(other._pStoreFactory)
{
other._recipients.clear();
other._content.clear();
other._boundary.clear();
other._pStoreFactory = 0;
}
MailMessage& MailMessage::operator = (MailMessage&& other)
{
if (&other != this)
{
_recipients = std::move(other._recipients);
other._recipients.clear();
_content = std::move(other._content);
other._content.clear();
_encoding = other._encoding;
_boundary = std::move(other._boundary);
other._boundary.clear();
_pStoreFactory = other._pStoreFactory;
other._pStoreFactory = 0;
}
return *this;
}
MailMessage::~MailMessage()
{
}
void MailMessage::addRecipient(const MailRecipient& recipient)
{
_recipients.push_back(recipient);
}
void MailMessage::setRecipients(const Recipients& recipients)
{
_recipients.assign(recipients.begin(), recipients.end());
}
void MailMessage::setSender(const std::string& sender)
{
set(HEADER_FROM, sender);
}
const std::string& MailMessage::getSender() const
{
if (has(HEADER_FROM))
return get(HEADER_FROM);
else
return EMPTY_HEADER;
}
void MailMessage::setSubject(const std::string& subject)
{
set(HEADER_SUBJECT, subject);
}
const std::string& MailMessage::getSubject() const
{
if (has(HEADER_SUBJECT))
return get(HEADER_SUBJECT);
else
return EMPTY_HEADER;
}
void MailMessage::setContent(const std::string& content, ContentTransferEncoding encoding)
{
_content = content;
_encoding = encoding;
set(HEADER_CONTENT_TRANSFER_ENCODING, contentTransferEncodingToString(encoding));
}
void MailMessage::setContentType(const std::string& mediaType)
{
set(HEADER_CONTENT_TYPE, mediaType);
}
void MailMessage::setContentType(const MediaType& mediaType)
{
setContentType(mediaType.toString());
}
const std::string& MailMessage::getContentType() const
{
if (has(HEADER_CONTENT_TYPE))
return get(HEADER_CONTENT_TYPE);
else
return TEXT_PLAIN;
}
void MailMessage::setDate(const Poco::Timestamp& dateTime)
{
set(HEADER_DATE, DateTimeFormatter::format(dateTime, DateTimeFormat::RFC1123_FORMAT));
}
Poco::Timestamp MailMessage::getDate() const
{
const std::string& dateTime = get(HEADER_DATE);
int tzd;
return DateTimeParser::parse(dateTime, tzd).timestamp();
}
bool MailMessage::isMultipart() const
{
MediaType mediaType = getContentType();
return mediaType.matches("multipart");
}
void MailMessage::addPart(const std::string& name, const Poco::SharedPtr<PartSource>& pSource, ContentDisposition disposition, ContentTransferEncoding encoding)
{
poco_check_ptr (pSource);
makeMultipart();
Part part;
part.name = name;
part.pSource = pSource;
part.disposition = disposition;
part.encoding = encoding;
_parts.push_back(part);
}
void MailMessage::addContent(const Poco::SharedPtr<PartSource>& pSource, ContentTransferEncoding encoding)
{
addPart("", pSource, CONTENT_INLINE, encoding);
}
void MailMessage::addAttachment(const std::string& name, const Poco::SharedPtr<PartSource>& pSource, ContentTransferEncoding encoding)
{
addPart(name, pSource, CONTENT_ATTACHMENT, encoding);
}
void MailMessage::read(std::istream& istr, PartHandler& handler)
{
readHeader(istr);
if (isMultipart())
{
readMultipart(istr, handler);
}
else
{
StringPartHandler handler(_content);
readPart(istr, *this, handler);
}
}
void MailMessage::read(std::istream& istr)
{
readHeader(istr);
if (isMultipart())
{
MultiPartHandler handler(this);
readMultipart(istr, handler);
}
else
{
StringPartHandler handler(_content);
readPart(istr, *this, handler);
}
}
void MailMessage::write(std::ostream& ostr) const
{
MessageHeader header(*this);
setRecipientHeaders(header);
if (isMultipart())
{
writeMultipart(header, ostr);
}
else
{
writeHeader(header, ostr);
std::istringstream istr(_content);
writeEncoded(istr, ostr, _encoding);
}
}
void MailMessage::makeMultipart()
{
if (!isMultipart())
{
MediaType mediaType("multipart", "mixed");
setContentType(mediaType);
}
}
void MailMessage::writeHeader(const MessageHeader& header, std::ostream& ostr) const
{
header.write(ostr);
ostr << "\r\n";
}
void MailMessage::writeMultipart(MessageHeader& header, std::ostream& ostr) const
{
if (_boundary.empty()) _boundary = MultipartWriter::createBoundary();
MediaType mediaType(getContentType());
mediaType.setParameter("boundary", _boundary);
header.set(HEADER_CONTENT_TYPE, mediaType.toString());
header.set(HEADER_MIME_VERSION, "1.0");
writeHeader(header, ostr);
MultipartWriter writer(ostr, _boundary);
for (PartVec::const_iterator it = _parts.begin(); it != _parts.end(); ++it)
{
writePart(writer, *it);
}
writer.close();
}
void MailMessage::writePart(MultipartWriter& writer, const Part& part)
{
MessageHeader partHeader(part.pSource->headers());
MediaType mediaType(part.pSource->mediaType());
if (!part.name.empty())
mediaType.setParameter("name", part.name);
partHeader.set(HEADER_CONTENT_TYPE, mediaType.toString());
partHeader.set(HEADER_CONTENT_TRANSFER_ENCODING, contentTransferEncodingToString(part.encoding));
std::string disposition;
if (part.disposition == CONTENT_ATTACHMENT)
{
disposition = "attachment";
const std::string& filename = part.pSource->filename();
if (!filename.empty())
{
disposition.append("; filename=");
quote(filename, disposition);
}
}
else disposition = "inline";
partHeader.set(HEADER_CONTENT_DISPOSITION, disposition);
writer.nextPart(partHeader);
writeEncoded(part.pSource->stream(), writer.stream(), part.encoding);
}
void MailMessage::writeEncoded(std::istream& istr, std::ostream& ostr, ContentTransferEncoding encoding)
{
switch (encoding)
{
case ENCODING_7BIT:
case ENCODING_8BIT:
StreamCopier::copyStream(istr, ostr);
break;
case ENCODING_QUOTED_PRINTABLE:
{
QuotedPrintableEncoder encoder(ostr);
StreamCopier::copyStream(istr, encoder);
encoder.close();
}
break;
case ENCODING_BASE64:
{
Base64Encoder encoder(ostr);
StreamCopier::copyStream(istr, encoder);
encoder.close();
}
break;
}
}
void MailMessage::readHeader(std::istream& istr)
{
clear();
MessageHeader::RecipientList recipients;
MessageHeader::read(istr, &recipients);
istr.get(); // \r
if ('\n' == istr.peek()) istr.get(); // \n
for (const auto& r : recipients) addRecipient(r);
}
void MailMessage::readMultipart(std::istream& istr, PartHandler& handler)
{
MediaType contentType(getContentType());
_boundary = contentType.getParameter("boundary");
MultipartReader reader(istr, _boundary);
while (reader.hasNextPart())
{
MessageHeader partHeader;
reader.nextPart(partHeader);
readPart(reader.stream(), partHeader, handler);
}
}
void MailMessage::readPart(std::istream& istr, const MessageHeader& header, PartHandler& handler)
{
std::string encoding;
if (header.has(HEADER_CONTENT_TRANSFER_ENCODING))
{
encoding = header.get(HEADER_CONTENT_TRANSFER_ENCODING);
// get rid of a parameter if one is set
std::string::size_type pos = encoding.find(';');
if (pos != std::string::npos)
encoding.resize(pos);
}
if (icompare(encoding, CTE_QUOTED_PRINTABLE) == 0)
{
QuotedPrintableDecoder decoder(istr);
handlePart(decoder, header, handler);
_encoding = ENCODING_QUOTED_PRINTABLE;
}
else if (icompare(encoding, CTE_BASE64) == 0)
{
Base64Decoder decoder(istr);
handlePart(decoder, header, handler);
_encoding = ENCODING_BASE64;
}
else
{
if (icompare(encoding, CTE_7BIT) == 0)
_encoding = ENCODING_7BIT;
else if (icompare(encoding, CTE_8BIT) == 0)
_encoding = ENCODING_8BIT;
handlePart(istr, header, handler);
}
}
void MailMessage::handlePart(std::istream& istr, const MessageHeader& header, PartHandler& handler)
{
handler.handlePart(header, istr);
// Read remaining characters from stream in case
// the handler failed to read the complete stream.
while (istr.good()) istr.get();
}
void MailMessage::setRecipientHeaders(MessageHeader& headers) const
{
std::string to;
std::string cc;
std::string bcc;
for (Recipients::const_iterator it = _recipients.begin(); it != _recipients.end(); ++it)
{
switch (it->getType())
{
case MailRecipient::PRIMARY_RECIPIENT:
appendRecipient(*it, to);
break;
case MailRecipient::CC_RECIPIENT:
appendRecipient(*it, cc);
break;
case MailRecipient::BCC_RECIPIENT:
break;
}
}
if (!to.empty()) headers.set(HEADER_TO, to);
if (!cc.empty()) headers.set(HEADER_CC, cc);
}
const std::string& MailMessage::contentTransferEncodingToString(ContentTransferEncoding encoding)
{
switch (encoding)
{
case ENCODING_7BIT:
return CTE_7BIT;
case ENCODING_8BIT:
return CTE_8BIT;
case ENCODING_QUOTED_PRINTABLE:
return CTE_QUOTED_PRINTABLE;
case ENCODING_BASE64:
return CTE_BASE64;
default:
poco_bugcheck();
}
return CTE_7BIT;
}
int MailMessage::lineLength(const std::string& str)
{
int n = 0;
std::string::const_reverse_iterator it = str.rbegin();
std::string::const_reverse_iterator end = str.rend();
while (it != end && *it != '\n') { ++n; ++it; }
return n;
}
void MailMessage::appendRecipient(const MailRecipient& recipient, std::string& str)
{
if (!str.empty()) str.append(", ");
const std::string& realName = recipient.getRealName();
const std::string& address = recipient.getAddress();
std::string rec;
if (!realName.empty())
{
quote(realName, rec, true);
rec.append(" ");
}
rec.append("<");
rec.append(address);
rec.append(">");
if (lineLength(str) + rec.length() > 70) str.append("\r\n\t");
str.append(rec);
}
void encodeQ(std::string& encodedText, std::string::const_iterator it, std::string::size_type& lineLength)
{
switch (*it)
{
case ' ':
encodedText += '_';
lineLength++;
break;
case '=':
case '?':
case '_':
case '(':
case ')':
case '[':
case ']':
case '<':
case '>':
case ',':
case ';':
case ':':
case '.':
case '@':
encodedText += '=';
NumberFormatter::appendHex(encodedText, static_cast<unsigned>(static_cast<unsigned char>(*it)), 2);
lineLength += 3;
break;
default:
if (*it > 32 && *it < 127)
{
encodedText += *it;
lineLength++;
}
else
{
encodedText += '=';
NumberFormatter::appendHex(encodedText, static_cast<unsigned>(static_cast<unsigned char>(*it)), 2);
lineLength += 3;
}
}
}
void startEncoding(std::string& encodedText, const std::string& charset, char encoding)
{
encodedText += "=?";
encodedText += charset;
encodedText += '?';
encodedText += encoding;
encodedText += '?';
}
std::string MailMessage::encodeWord(const std::string& text, const std::string& charset, char encoding)
{
if (encoding == 'q' || encoding == 'Q')
{
bool containsNonASCII = false;
for (std::string::const_iterator it = text.begin(); it != text.end(); ++it)
{
if (static_cast<unsigned char>(*it) > 127)
{
containsNonASCII = true;
break;
}
}
if (!containsNonASCII) return text;
}
std::string encodedText;
std::string::size_type lineLength = 0;
if (encoding == 'q' || encoding == 'Q')
{
for (std::string::const_iterator it = text.begin(); it != text.end(); ++it)
{
if (lineLength == 0)
{
startEncoding(encodedText, charset, encoding);
lineLength += charset.length() + 5;
}
encodeQ(encodedText, it, lineLength);
if ((lineLength >= 64 &&
(*it == ' ' || *it == '\t' || *it == '\r' || *it == '\n')) ||
lineLength >= 72)
{
encodedText += "?=\r\n ";
lineLength = 0;
}
}
}
else if (encoding == 'b' || encoding == 'B')
{
// to ensure we're under 75 chars, 4 padding chars are always predicted
lineLength = 75 - (charset.length() + 5/*=??B?*/ + 2/*?=*/ + 4/*base64 padding*/);
std::string::size_type pos = 0;
size_t textLen = static_cast<size_t>(floor(lineLength * 3 / 4));
std::ostringstream ostr;
while (true)
{
Base64Encoder encoder(ostr);
encoder.rdbuf()->setLineLength(static_cast<int>(lineLength));
startEncoding(encodedText, charset, encoding);
std::string line = text.substr(pos, textLen);
encoder << line;
encoder.close();
encodedText.append(ostr.str());
encodedText.append("?=");
if (line.size() < textLen) break;
ostr.str("");
pos += textLen;
encodedText.append("\r\n");
}
lineLength = 0;;
}
else
{
throw InvalidArgumentException(Poco::format("MailMessage::encodeWord: "
"unknown encoding: %c", encoding));
}
if (lineLength > 0) encodedText += "?=";
return encodedText;
}
void MailMessage::advanceToEncoded(const std::string& encoded, std::string& decoded, std::string::size_type& pos1, bool& isComment)
{
bool spaceOnly = isComment; // flag to trim away spaces between encoded-word's
auto it = encoded.begin();
auto end = encoded.end();
for (; it != end; ++it)
{
if (*it == '=')
{
if (++it != end && *it == '?')
{
if (spaceOnly) trimRightInPlace(decoded);
return;
}
}
else if (*it == '(') isComment = true;
else if (*it == ')') isComment = false;
if ((isComment) && (!Ascii::isSpace(*it))) spaceOnly = false;
decoded.append(1, *it);
++pos1;
}
pos1 = std::string::npos;
}
std::string MailMessage::decodeWord(const std::string& encoded, std::string toCharset)
{
std::string encodedWord = replace(encoded, "?=\r\n=?", "?==?");
bool toCharsetGiven = !toCharset.empty();
std::string errMsg;
const std::size_t notFound = std::string::npos;
std::string decoded;
std::string::size_type pos1 = 0, pos2 = 0;
bool isComment = false;
advanceToEncoded(encodedWord, decoded, pos1, isComment);
if (pos1 != notFound)
{
getEncWordLimits(encodedWord, pos1, pos2, isComment);
while ((pos1 != notFound) && (pos2 != notFound) && pos2 > pos1 + 2)
{
pos1 += 2;
StringTokenizer st(encodedWord.substr(pos1, pos2 - pos1), "?");
if (st.count() == 3)
{
std::string charset = st[0];
if (!toCharsetGiven) toCharset = charset;
if (st[1].size() > 1)
{
throw InvalidArgumentException(Poco::format("MailMessage::decodeWord: "
"invalid encoding %s", st[1]));
}
char encoding = st[1][0];
std::string encodedText = st[2];
if (encodedText.find_first_of(" ?") != notFound)
{
throw InvalidArgumentException("MailMessage::decodeWord: "
"forbidden characters found in encoded-word");
}
else if (encoding == 'q' || encoding == 'Q')
{
// no incomplete encoded characters allowed on single line
std::string::size_type eqPos = encodedText.rfind('=');
if (eqPos != notFound)
{
if ((eqPos + 2) >= encodedText.size())
{
throw InvalidArgumentException("MailMessage::decodeWord: "
"incomplete encoded character found in encoded-word");
}
}
}
decoded.append(decodeWord(charset, encoding, encodedText, toCharset));
pos1 = pos2 + 2;
advanceToEncoded(encodedWord.substr(pos1), decoded, pos1, isComment);
if (pos1 != notFound) getEncWordLimits(encodedWord, pos1, pos2, isComment);
}
else
{
throw InvalidArgumentException(Poco::format("MailMessage::decodeWord: "
"invalid number of entries in encoded-word (expected 3, found %z)", st.count()));
}
}
}
else decoded = std::move(encodedWord);
return decoded;
}
void MailMessage::getEncWordLimits(const std::string& encodedWord, std::string::size_type& pos1, std::string::size_type& pos2, bool isComment)
{
const std::size_t notFound = std::string::npos;
pos1 = encodedWord.find("=?", pos1); // beginning of encoded-word
if (pos1 != notFound)
{
// must look sequentially for all '?' occurences because of a (valid) case like this:
// =?ISO-8859-1?q?=C4?=
// where end would be prematurely found if we search for ?= only
pos2 = encodedWord.find('?', pos1 + 2); // first '?'
if (pos2 == notFound) goto err;
pos2 = encodedWord.find('?', pos2 + 1); // second '?'
if (pos2 == notFound) goto err;
pos2 = encodedWord.find("?=", pos2 + 1); // end of encoded-word
if (pos2 == notFound) goto err;
// before we leave, double-check for the next encoded-word end, to make sure
// an illegal '?' was not sneaked in (eg. =?ISO-8859-1?q?=C4?=D6?=)
if (((encodedWord.find("?=", pos2 + 1) != notFound &&
encodedWord.find("=?", pos2 + 1) == notFound)) ||
((encodedWord.find("=?", pos2 + 1) != notFound &&
encodedWord.find("?=", pos2 + 1) == notFound))) goto err;
}
else goto err;
// if encoded word is in a comment, then '(' and ')' are forbidden inside it
if (isComment &&
(notFound != encodedWord.substr(pos1, pos2 - pos1).find_first_of("()"))) goto err;
return;
err:
throw InvalidArgumentException("MailMessage::encodedWordLimits: invalid encoded word");
}
std::string MailMessage::decodeWord(const std::string& charset, char encoding,
const std::string& text, const std::string& toCharset)
{
const TextEncodingRegistry& registry = TextEncoding::registry();
if (!registry.has(charset) || !registry.has(toCharset))
{
throw NotImplementedException(Poco::format("MailMessage::decodeWord: "
"charset not supported: %s", charset));
}
TextEncoding* fromEnc = registry.find(charset);
TextEncoding* toEnc = registry.find(toCharset);
std::string decoded;
switch (encoding)
{
case 'B': case 'b':
{
std::istringstream istr(text);
Base64Decoder decoder(istr);
int c = decoder.get();
while (c != -1) { decoded.append(1, char(c)); c = decoder.get(); }
break;
}
case 'Q': case 'q':
{
bool isWide = false;
std::vector<char> wideChar;
std::vector<unsigned char> wideCharSeq;
for (const auto& c : text)
{
if (!Ascii::isPrintable(c) || c == '?' || c == ' ')
{
throw InvalidArgumentException("MailMessage::decodeWord: encoded-word must not contain "
"non-printable characters, '? or SPACE");
}
if (c == '_') decoded.append(1, ' ');
else if (c == '=') isWide = true;
else if (isWide)
{
wideChar.push_back(c);
if (wideChar.size() % 2 == 0)
{
std::string wcStr(&wideChar[0], wideChar.size());
unsigned char chr = NumberParser::parseHex(wcStr);
wideCharSeq.push_back(chr);
if (fromEnc->sequenceLength(&wideCharSeq[0], static_cast<int>(wideCharSeq.size())) > 0)
{
auto it = wideCharSeq.begin();
auto end = wideCharSeq.end();
for (; it != end; ++it)
{
decoded.append(1, static_cast<char>(*it));
}
wideChar.clear();
wideCharSeq.clear();
isWide = false;
}
}
}
else decoded.append(1, c);
}
break;
}
default:
throw InvalidArgumentException(Poco::format("MailMessage::decodeWord: Unknown encoding: %c", encoding));
}
if (charset != toCharset)
{
TextConverter converter(*fromEnc, *toEnc);
std::string converted;
converter.convert(decoded, converted);
return converted;
}
return decoded;
}
PartSource* MailMessage::createPartStore(const std::string& content, const std::string& mediaType, const std::string& filename)
{
if (!_pStoreFactory) return new StringPartSource(content, mediaType, filename);
else return _pStoreFactory->createPartStore(content, mediaType, filename);
}
MultipartSource::MultipartSource(const std::string& contentType):
PartSource(contentTypeWithBoundary(contentType))
{
}
MultipartSource::~MultipartSource()
{
}