File indexing completed on 2022-10-04 16:37:47

0001 /*
0002     kmime_message.cpp
0003 
0004     KMime, the KDE Internet mail/usenet news message library.
0005     SPDX-FileCopyrightText: 2001 the KMime authors.
0006     See file AUTHORS for details
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 
0011 #include "kmime_message.h"
0012 #include "kmime_util_p.h"
0013 
0014 using namespace KMime;
0015 
0016 namespace KMime
0017 {
0018 
0019 Message::Message() : Content()
0020 {
0021 }
0022 
0023 Message::~Message() = default;
0024 
0025 QByteArray Message::assembleHeaders()
0026 {
0027     // Create the mandatory fields (RFC5322) if they do not exist already.
0028     date(true);
0029     from(true);
0030 
0031     // Make sure the mandatory MIME-Version field (RFC2045) is present and valid.
0032     auto *mimeVersion = header<Headers::MIMEVersion>(true);
0033     mimeVersion->from7BitString("1.0");
0034 
0035     // Assemble all header fields.
0036     return Content::assembleHeaders();
0037 }
0038 
0039 Content *Message::mainBodyPart(const QByteArray &type)
0040 {
0041     KMime::Content *c = this;
0042     while (c) {
0043         // not a multipart message
0044         const KMime::Headers::ContentType *const contentType = c->contentType();
0045         if (!contentType->isMultipart()) {
0046             if (contentType->mimeType() == type || type.isEmpty()) {
0047                 return c;
0048             }
0049             return nullptr;
0050         }
0051 
0052         // empty multipart
0053         if (c->contents().isEmpty()) {
0054             return nullptr;
0055         }
0056 
0057         // multipart/alternative
0058         if (contentType->subType() == "alternative") {
0059             if (type.isEmpty()) {
0060                 return c->contents().at(0);
0061             }
0062             const auto contents = c->contents();
0063             for (Content *c1 : contents) {
0064                 if (c1->contentType()->mimeType() == type) {
0065                     return c1;
0066                 }
0067             }
0068             return nullptr;
0069         }
0070 
0071         c = c->contents().at(0);
0072     }
0073 
0074     return nullptr;
0075 }
0076 
0077 QString Message::mimeType()
0078 {
0079     return QStringLiteral("message/rfc822");
0080 }
0081 
0082 // @cond PRIVATE
0083 #define kmime_mk_header_accessor( type, method ) \
0084     Headers::type *Message::method( bool create ) { \
0085         return header<Headers::type>( create ); \
0086     }
0087 
0088 kmime_mk_header_accessor(MessageID, messageID)
0089 kmime_mk_header_accessor(Subject, subject)
0090 kmime_mk_header_accessor(Date, date)
0091 kmime_mk_header_accessor(Organization, organization)
0092 kmime_mk_header_accessor(From, from)
0093 kmime_mk_header_accessor(ReplyTo, replyTo)
0094 kmime_mk_header_accessor(To, to)
0095 kmime_mk_header_accessor(Cc, cc)
0096 kmime_mk_header_accessor(Bcc, bcc)
0097 kmime_mk_header_accessor(References, references)
0098 kmime_mk_header_accessor(UserAgent, userAgent)
0099 kmime_mk_header_accessor(InReplyTo, inReplyTo)
0100 kmime_mk_header_accessor(Sender, sender)
0101 
0102 #undef kmime_mk_header_accessor
0103 // @endcond
0104 
0105 }
0106