File indexing completed on 2022-11-29 19:53:37

0001 /*
0002     kmime_content.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     SPDX-FileCopyrightText: 2006 Volker Krause <vkrause@kde.org>
0008     SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
0009 
0010     SPDX-License-Identifier: LGPL-2.0-or-later
0011 */
0012 /**
0013   @file
0014   This file is part of the API for handling @ref MIME data and
0015   defines the Content class.
0016 
0017   @brief
0018   Defines the Content class.
0019 
0020   @authors the KMime authors (see AUTHORS file),
0021   Volker Krause \<vkrause@kde.org\>
0022 */
0023 #include "kmime_content.h"
0024 #include "kmime_content_p.h"
0025 #include "kmime_message.h"
0026 #include "kmime_header_parsing.h"
0027 #include "kmime_header_parsing_p.h"
0028 #include "kmime_parsers.h"
0029 #include "kmime_util_p.h"
0030 
0031 #include <KCharsets>
0032 #include <KCodecs>
0033 
0034 
0035 #include <QTextCodec>
0036 
0037 using namespace KMime;
0038 
0039 namespace KMime
0040 {
0041 
0042 Content::Content(Content *parent)
0043     : d_ptr(new ContentPrivate)
0044 {
0045     d_ptr->parent = parent;
0046 }
0047 
0048 Content::~Content()
0049 {
0050     Q_D(Content);
0051     qDeleteAll(d->headers);
0052     d->headers.clear();
0053     delete d_ptr;
0054     d_ptr = nullptr;
0055 }
0056 
0057 bool Content::hasContent() const
0058 {
0059     return !d_ptr->head.isEmpty() || !d_ptr->body.isEmpty() || !d_ptr->contents().isEmpty();
0060 }
0061 
0062 void Content::setContent(const QByteArray &s)
0063 {
0064     Q_D(Content);
0065     KMime::HeaderParsing::extractHeaderAndBody(s, d->head, d->body);
0066 }
0067 
0068 QByteArray Content::head() const
0069 {
0070     return d_ptr->head;
0071 }
0072 
0073 void Content::setHead(const QByteArray &head)
0074 {
0075     d_ptr->head = head;
0076     if (!head.endsWith('\n')) {
0077         d_ptr->head += '\n';
0078     }
0079 }
0080 
0081 QByteArray Content::body() const
0082 {
0083     return d_ptr->body;
0084 }
0085 
0086 void Content::setBody(const QByteArray &body)
0087 {
0088     d_ptr->body = body;
0089 }
0090 
0091 QByteArray Content::preamble() const
0092 {
0093     return d_ptr->preamble;
0094 }
0095 
0096 void Content::setPreamble(const QByteArray &preamble)
0097 {
0098     d_ptr->preamble = preamble;
0099 }
0100 
0101 QByteArray Content::epilogue() const
0102 {
0103     return d_ptr->epilogue;
0104 }
0105 
0106 void Content::setEpilogue(const QByteArray &epilogue)
0107 {
0108     d_ptr->epilogue = epilogue;
0109 }
0110 
0111 void Content::parse()
0112 {
0113     Q_D(Content);
0114 
0115     // Clean up old headers and parse them again.
0116     qDeleteAll(d->headers);
0117     d->headers.clear();
0118     d->headers = HeaderParsing::parseHeaders(d->head);
0119 
0120     // If we are frozen, save the body as-is. This is done because parsing
0121     // changes the content (it loses preambles and epilogues, converts uuencode->mime, etc.)
0122     if (d->frozen) {
0123         d->frozenBody = d->body;
0124     }
0125 
0126     // Clean up old sub-Contents and parse them again.
0127     qDeleteAll(d->multipartContents);
0128     d->multipartContents.clear();
0129     d->clearBodyMessage();
0130     Headers::ContentType *ct = contentType();
0131     if (ct->isEmpty()) { //Set default content-type as defined in https://tools.ietf.org/html/rfc2045#page-10 (5.2.  Content-Type Defaults)
0132         ct->setMimeType("text/plain");
0133         ct->setCharset("us-ascii");
0134     }
0135     if (ct->isText()) {
0136         // This content is either text, or of unknown type.
0137 
0138         if (d->parseUuencoded(this)) {
0139             // This is actually uuencoded content generated by broken software.
0140         } else if (d->parseYenc(this)) {
0141             // This is actually yenc content generated by broken software.
0142         } else {
0143             // This is just plain text.
0144         }
0145     } else if (ct->isMultipart()) {
0146         // This content claims to be MIME multipart.
0147 
0148         if (d->parseMultipart(this)) {
0149             // This is actual MIME multipart content.
0150         } else {
0151             // Parsing failed; treat this content as "text/plain".
0152             ct->setMimeType("text/plain");
0153             ct->setCharset("US-ASCII");
0154         }
0155     } else {
0156         // This content is something else, like an encapsulated message or a binary attachment
0157         // or something like that
0158         if (bodyIsMessage()) {
0159             d->bodyAsMessage = Message::Ptr(new Message);
0160             d->bodyAsMessage->setContent(d->body);
0161             d->bodyAsMessage->setFrozen(d->frozen);
0162             d->bodyAsMessage->parse();
0163             d->bodyAsMessage->d_ptr->parent = this;
0164 
0165             // Clear the body, as it is now represented by d->bodyAsMessage. This is the same behavior
0166             // as with multipart contents, since parseMultipart() clears the body as well
0167             d->body.clear();
0168         }
0169     }
0170 }
0171 
0172 bool Content::isFrozen() const
0173 {
0174     return d_ptr->frozen;
0175 }
0176 
0177 void Content::setFrozen(bool frozen)
0178 {
0179     d_ptr->frozen = frozen;
0180 }
0181 
0182 void Content::assemble()
0183 {
0184     Q_D(Content);
0185     if (d->frozen) {
0186         return;
0187     }
0188 
0189     d->head = assembleHeaders();
0190     const auto contentsList = contents();
0191     for (Content *c : contentsList) {
0192         c->assemble();
0193     }
0194 }
0195 
0196 QByteArray Content::assembleHeaders()
0197 {
0198     Q_D(Content);
0199     QByteArray newHead;
0200     for (const Headers::Base *h : std::as_const(d->headers)) {
0201         if (!h->isEmpty()) {
0202             newHead += foldHeader(h->as7BitString()) + '\n';
0203         }
0204     }
0205 
0206     return newHead;
0207 }
0208 
0209 void Content::clear()
0210 {
0211     Q_D(Content);
0212     qDeleteAll(d->headers);
0213     d->headers.clear();
0214     clearContents();
0215     d->head.clear();
0216     d->body.clear();
0217 }
0218 
0219 void Content::clearContents(bool del)
0220 {
0221     Q_D(Content);
0222     if (del) {
0223         qDeleteAll(d->multipartContents);
0224     }
0225     d->multipartContents.clear();
0226     d->clearBodyMessage();
0227 }
0228 
0229 QByteArray Content::encodedContent(bool useCrLf)
0230 {
0231     QByteArray encodedContentData = head();           // return value; initialise with the head data
0232     const QByteArray encodedBodyData = encodedBody();
0233 
0234     /* Make sure that head and body have at least two newlines as separator, otherwise add one.
0235      * If we have enough newlines as sperator, then we should not change the number of newlines
0236      * to not break digital signatures
0237      */
0238     if (!encodedContentData.endsWith("\n\n") &&
0239         !encodedBodyData.startsWith("\n\n") &&
0240         !(encodedContentData.endsWith("\n") && encodedBodyData.startsWith("\n"))){
0241         encodedContentData += '\n';
0242     }
0243     encodedContentData += encodedBodyData;
0244 
0245     if (useCrLf) {
0246         return LFtoCRLF(encodedContentData);
0247     } else {
0248         return encodedContentData;
0249     }
0250 }
0251 
0252 QByteArray Content::encodedBody()
0253 {
0254     Q_D(Content);
0255     QByteArray e;
0256     // Body.
0257     if (d->frozen) {
0258         // This Content is frozen.
0259         if (d->frozenBody.isEmpty()) {
0260             // This Content has never been parsed.
0261             e += d->body;
0262         } else {
0263             // Use the body as it was before parsing.
0264             e += d->frozenBody;
0265         }
0266     } else if (bodyIsMessage() && d->bodyAsMessage) {
0267         // This is an encapsulated message
0268         // No encoding needed, as the ContentTransferEncoding can only be 7bit
0269         // for encapsulated messages
0270         e += d->bodyAsMessage->encodedContent();
0271     } else if (!d->body.isEmpty()) {
0272         // This is a single-part Content.
0273         Headers::ContentTransferEncoding *enc = contentTransferEncoding();
0274 
0275         if (enc->needToEncode()) {
0276             if (enc->encoding() == Headers::CEquPr) {
0277                 e += KCodecs::quotedPrintableEncode(d->body, false);
0278             } else {
0279                 QByteArray encoded;
0280                 KCodecs::base64Encode(d->body, encoded, true);
0281                 e += encoded;
0282                 e += '\n';
0283             }
0284         } else {
0285             e += d->body;
0286         }
0287     }
0288 
0289     if (!d->frozen && !d->multipartContents.isEmpty()) {
0290         // This is a multipart Content.
0291         Headers::ContentType *ct = contentType();
0292         QByteArray boundary = "\n--" + ct->boundary();
0293 
0294         if (!d->preamble.isEmpty()) {
0295             e += d->preamble;
0296         }
0297 
0298         //add all (encoded) contents separated by boundaries
0299         for (Content *c : std::as_const(d->multipartContents)) {
0300             e += boundary + '\n';
0301             e += c->encodedContent(false);    // don't convert LFs here, we do that later!!!!!
0302         }
0303         //finally append the closing boundary
0304         e += boundary + "--\n";
0305 
0306         if (!d->epilogue.isEmpty()) {
0307             e += d->epilogue;
0308         }
0309     }
0310     return e;
0311 }
0312 
0313 QByteArray Content::decodedContent()
0314 {
0315     QByteArray ret;
0316     Headers::ContentTransferEncoding *ec = contentTransferEncoding();
0317     bool removeTrailingNewline = false;
0318 
0319     if (d_ptr->body.isEmpty()) {
0320         return ret;
0321     }
0322 
0323     if (ec->isDecoded()) {
0324         ret = d_ptr->body;
0325         //Laurent Fix bug #311267
0326         //removeTrailingNewline = true;
0327     } else {
0328         switch (ec->encoding()) {
0329         case Headers::CEbase64 : {
0330             KCodecs::Codec *codec = KCodecs::Codec::codecForName("base64");
0331             Q_ASSERT(codec);
0332             ret.resize(codec->maxDecodedSizeFor(d_ptr->body.size()));
0333             QScopedPointer<KCodecs::Decoder> decoder(codec->makeDecoder());
0334             QByteArray::const_iterator inputIt = d_ptr->body.constBegin();
0335             QByteArray::iterator resultIt = ret.begin();
0336             decoder->decode(inputIt, d_ptr->body.constEnd(), resultIt, ret.constEnd());
0337             ret.truncate(resultIt - ret.begin());
0338             break;
0339         }
0340         case Headers::CEquPr :
0341             ret = KCodecs::quotedPrintableDecode(d_ptr->body);
0342             removeTrailingNewline = true;
0343             break;
0344         case Headers::CEuuenc :
0345             KCodecs::uudecode(d_ptr->body, ret);
0346             break;
0347         case Headers::CEbinary :
0348             ret = d_ptr->body;
0349             removeTrailingNewline = false;
0350             break;
0351         default :
0352             ret = d_ptr->body;
0353             removeTrailingNewline = true;
0354         }
0355     }
0356 
0357     if (removeTrailingNewline && (ret.size() > 0) && (ret[ret.size() - 1] == '\n')) {
0358         ret.resize(ret.size() - 1);
0359     }
0360 
0361     return ret;
0362 }
0363 
0364 QString Content::decodedText(bool trimText, bool removeTrailingNewlines)
0365 {
0366     if (!d_ptr->decodeText(this)) {   //this is not a text content !!
0367       return {};
0368     }
0369 
0370     bool ok = true;
0371     QTextCodec *codec =
0372         KCharsets::charsets()->codecForName(QLatin1String(contentType()->charset()), ok);
0373     if (!ok  || codec == nullptr) {   // no suitable codec found => try local settings and hope the best ;-)
0374         codec = QTextCodec::codecForLocale();
0375         QByteArray chset = codec->name();
0376         contentType()->setCharset(chset);
0377     }
0378 
0379     QString s = codec->toUnicode(d_ptr->body.data(), d_ptr->body.length());
0380 
0381     if (trimText || removeTrailingNewlines) {
0382         int i;
0383         for (i = s.length() - 1; i >= 0; --i) {
0384             if (trimText) {
0385                 if (!s[i].isSpace()) {
0386                     break;
0387                 }
0388             } else {
0389                 if (s[i] != QLatin1Char('\n')) {
0390                     break;
0391                 }
0392             }
0393         }
0394         s.truncate(i + 1);
0395     } else {
0396         if (s.right(1) == QLatin1Char('\n')) {
0397             s.chop(1);   // remove trailing new-line
0398         }
0399     }
0400 
0401     return s;
0402 }
0403 
0404 void Content::fromUnicodeString(const QString &s)
0405 {
0406     bool ok = true;
0407     QTextCodec *codec =
0408         KCharsets::charsets()->codecForName(QLatin1String(contentType()->charset()), ok);
0409 
0410     if (!ok) {   // no suitable codec found => try local settings and hope the best ;-)
0411         codec = QTextCodec::codecForLocale();
0412         QByteArray chset = codec->name();
0413         contentType()->setCharset(chset);
0414     }
0415 
0416     d_ptr->body = codec->fromUnicode(s);
0417     contentTransferEncoding()->setDecoded(true);   //text is always decoded
0418 }
0419 
0420 Content *Content::textContent()
0421 {
0422     Content *ret = nullptr;
0423 
0424     //return the first content with mimetype=text/*
0425     if (contentType()->isText()) {
0426         ret = this;
0427     } else {
0428         const auto contents = d_ptr->contents();
0429         for (Content *c : contents) {
0430             if ((ret = c->textContent()) != nullptr) {
0431                 break;
0432             }
0433         }
0434     }
0435     return ret;
0436 }
0437 
0438 QVector<Content*> Content::attachments()
0439 {
0440     QVector<Content*> result;
0441 
0442     auto ct = contentType(false);
0443     if (ct && ct->isMultipart() &&
0444         !ct->isSubtype("related") /* && !ct->isSubtype("alternative")*/) {
0445       const QVector<Content *> contentsList = contents();
0446       result.reserve(contentsList.count());
0447       for (Content *child : contentsList) {
0448         if (isAttachment(child)) {
0449           result.push_back(child);
0450         } else {
0451           result += child->attachments();
0452         }
0453       }
0454     }
0455 
0456     return result;
0457 }
0458 
0459 QVector<Content*> Content::contents() const
0460 {
0461     return d_ptr->contents();
0462 }
0463 
0464 void Content::replaceContent(Content *oldContent, Content *newContent)
0465 {
0466     Q_D( Content );
0467     if ( d->multipartContents.isEmpty() || !d->multipartContents.contains( oldContent ) ) {
0468       return;
0469     }
0470 
0471     d->multipartContents.removeAll( oldContent );
0472     delete oldContent;
0473     d->multipartContents.append( newContent );
0474     if( newContent->parent() != this ) {
0475       // If the content was part of something else, this will remove it from there.
0476       newContent->setParent( this );
0477     }
0478 }
0479 
0480 
0481 void Content::addContent(Content *c, bool prepend)
0482 {
0483     Q_D(Content);
0484 
0485     // This method makes no sense for encapsulated messages
0486     Q_ASSERT(!bodyIsMessage());
0487 
0488     // If this message is single-part; make it multipart first.
0489     if (d->multipartContents.isEmpty() && !contentType()->isMultipart()) {
0490         // The current body will be our first sub-Content.
0491         auto *main = new Content(this);
0492 
0493         // Move the MIME headers to the newly created sub-Content.
0494         // NOTE: The other headers (RFC5322 headers like From:, To:, as well as X-headers
0495         // are not moved to the subcontent; they remain with the top-level content.
0496         for (auto it = d->headers.begin(); it != d->headers.end();) {
0497             if ((*it)->isMimeHeader()) {
0498                 // Add to new content.
0499                 main->setHeader(*it);
0500                 // Remove from this content.
0501                 it = d->headers.erase(it);
0502             } else {
0503                 ++it;
0504             }
0505         }
0506 
0507         // Adjust the Content-Type of the newly created sub-Content.
0508         main->contentType()->setCategory(Headers::CCmixedPart);
0509 
0510         // Move the body to the new subcontent.
0511         main->setBody(d->body);
0512         d->body.clear();
0513 
0514         // Add the subcontent.
0515         d->multipartContents.append(main);
0516 
0517         // Convert this content to "multipart/mixed".
0518         Headers::ContentType *ct = contentType();
0519         ct->setMimeType("multipart/mixed");
0520         ct->setBoundary(multiPartBoundary());
0521         ct->setCategory(Headers::CCcontainer);
0522         auto cte = contentTransferEncoding();
0523         cte->setEncoding(Headers::CE7Bit);
0524         cte->setDecoded(true);
0525     }
0526 
0527     // Add the new content.
0528     if (prepend) {
0529         d->multipartContents.prepend(c);
0530     } else {
0531         d->multipartContents.append(c);
0532     }
0533 
0534     if (c->parent() != this) {
0535         // If the content was part of something else, this will remove it from there.
0536         c->setParent(this);
0537     }
0538 }
0539 
0540 void Content::removeContent(Content *c, bool del)
0541 {
0542     Q_D(Content);
0543     if (d->multipartContents.isEmpty() || !d->multipartContents.contains(c)) {
0544         return;
0545     }
0546 
0547     // This method makes no sense for encapsulated messages.
0548     // Should be covered by the above assert already, though.
0549     Q_ASSERT(!bodyIsMessage());
0550 
0551     d->multipartContents.removeAll(c);
0552     if (del) {
0553         delete c;
0554     } else {
0555         c->d_ptr->parent = nullptr;
0556     }
0557 
0558     // If only one content is left, turn this content into a single-part.
0559     if (d->multipartContents.count() == 1) {
0560         Content *main = d->multipartContents.constFirst();
0561 
0562         // Move all headers from the old subcontent to ourselves.
0563         // NOTE: This also sets the new Content-Type.
0564         const auto headers = main->d_ptr->headers;
0565         for (Headers::Base *h : headers) {
0566             setHeader(h);   // Will remove the old one if present.
0567         }
0568         main->d_ptr->headers.clear();
0569 
0570         // Move the body.
0571         d->body = main->body();
0572 
0573         // Delete the old subcontent.
0574         delete main;
0575         d->multipartContents.clear();
0576     }
0577 }
0578 
0579 void Content::changeEncoding(Headers::contentEncoding e)
0580 {
0581     // This method makes no sense for encapsulated messages, they are always 7bit
0582     // encoded.
0583     Q_ASSERT(!bodyIsMessage());
0584 
0585     Headers::ContentTransferEncoding *enc = contentTransferEncoding();
0586     if (enc->encoding() == e) {
0587         // Nothing to do.
0588         return;
0589     }
0590 
0591     if (d_ptr->decodeText(this)) {
0592         // This is textual content.  Textual content is stored decoded.
0593         Q_ASSERT(enc->isDecoded());
0594         enc->setEncoding(e);
0595     } else {
0596         // This is non-textual content.  Re-encode it.
0597         if (e == Headers::CEbase64) {
0598             KCodecs::base64Encode(decodedContent(), d_ptr->body, true);
0599             enc->setEncoding(e);
0600             enc->setDecoded(false);
0601         } else {
0602             // It only makes sense to convert binary stuff to base64.
0603             Q_ASSERT(false);
0604         }
0605     }
0606 }
0607 
0608 QVector<Headers::Base*> Content::headers() const
0609 {
0610     return d_ptr->headers;
0611 }
0612 
0613 Headers::Base *Content::headerByType(const char *type) const
0614 {
0615     Q_ASSERT(type  && *type);
0616 
0617     for (Headers::Base *h : std::as_const(d_ptr->headers)) {
0618         if (h->is(type)) {
0619             return h; // Found.
0620         }
0621     }
0622 
0623     return nullptr; // Not found.
0624 }
0625 
0626 QVector<Headers::Base*> Content::headersByType(const char *type) const
0627 {
0628     Q_ASSERT(type && *type);
0629 
0630     QVector<Headers::Base*> result;
0631 
0632     for (Headers::Base *h : std::as_const(d_ptr->headers)) {
0633         if (h->is(type)) {
0634             result << h;
0635         }
0636     }
0637 
0638     return result;
0639 }
0640 
0641 void Content::setHeader(Headers::Base *h)
0642 {
0643     Q_ASSERT(h);
0644     removeHeader(h->type());
0645     appendHeader(h);
0646 }
0647 
0648 void Content::appendHeader(Headers::Base *h)
0649 {
0650     Q_D(Content);
0651     d->headers.append(h);
0652 }
0653 
0654 bool Content::removeHeader(const char *type)
0655 {
0656     Q_D(Content);
0657     const auto endIt = d->headers.end();
0658     for (auto it = d->headers.begin(); it != endIt; ++it) {
0659         if ((*it)->is(type)) {
0660             delete(*it);
0661             d->headers.erase(it);
0662             return true;
0663         }
0664     }
0665 
0666     return false;
0667 }
0668 
0669 bool Content::hasHeader(const char* type) const
0670 {
0671     return headerByType(type) != nullptr;
0672 }
0673 
0674 int Content::size()
0675 {
0676     int ret = d_ptr->body.length();
0677 
0678     if (contentTransferEncoding()->encoding() == Headers::CEbase64) {
0679         KCodecs::Codec *codec = KCodecs::Codec::codecForName("base64");
0680         return codec->maxEncodedSizeFor(ret);
0681     }
0682 
0683     // Not handling quoted-printable here since that requires actually
0684     // converting the content, and that is O(size_of_content).
0685     // For quoted-printable, this is only an approximate size.
0686 
0687     return ret;
0688 }
0689 
0690 int Content::storageSize() const
0691 {
0692     const Q_D(Content);
0693     int s = d->head.size();
0694 
0695     if (d->contents().isEmpty()) {
0696         s += d->body.size();
0697     } else {
0698 
0699         // FIXME: This should take into account the boundary headers that are added in
0700         //        encodedContent!
0701         const auto contents = d->contents();
0702         for (Content *c : contents) {
0703             s += c->storageSize();
0704         }
0705     }
0706 
0707     return s;
0708 }
0709 
0710 int Content::lineCount() const
0711 {
0712     const Q_D(Content);
0713     int ret = 0;
0714     if (!isTopLevel()) {
0715         ret += d->head.count('\n');
0716     }
0717     ret += d->body.count('\n');
0718 
0719     const auto contents = d->contents();
0720     for (Content *c : contents) {
0721         ret += c->lineCount();
0722     }
0723 
0724     return ret;
0725 }
0726 
0727 bool ContentPrivate::decodeText(Content *q)
0728 {
0729     Headers::ContentTransferEncoding *enc = q->contentTransferEncoding();
0730 
0731     if (!q->contentType()->isText()) {
0732         return false; //non textual data cannot be decoded here => use decodedContent() instead
0733     }
0734     if (enc->isDecoded()) {
0735         return true; //nothing to do
0736     }
0737 
0738     switch (enc->encoding()) {
0739     case Headers::CEbase64 :
0740         body = KCodecs::base64Decode(body);
0741         break;
0742     case Headers::CEquPr :
0743         body = KCodecs::quotedPrintableDecode(body);
0744         break;
0745     case Headers::CEuuenc :
0746         body = KCodecs::uudecode(body);
0747         break;
0748     case Headers::CEbinary :
0749         // nothing to decode
0750     default :
0751         break;
0752     }
0753     if (!body.endsWith("\n")) {
0754         body.append("\n");
0755     }
0756     enc->setDecoded(true);
0757     return true;
0758 }
0759 
0760 QByteArray Content::defaultCharset()
0761 {
0762     return KMime::cachedCharset(QByteArrayLiteral("ISO-8859-1"));
0763 }
0764 
0765 Content *KMime::Content::content(const ContentIndex &index) const
0766 {
0767     if (!index.isValid()) {
0768         return const_cast<KMime::Content *>(this);
0769     }
0770     ContentIndex idx = index;
0771     unsigned int i = idx.pop() - 1; // one-based -> zero-based index
0772     if (i < static_cast<unsigned int>(d_ptr->contents().size())) {
0773         return d_ptr->contents().at(i)->content(idx);
0774     } else {
0775         return nullptr;
0776     }
0777 }
0778 
0779 ContentIndex KMime::Content::indexForContent(Content *content) const
0780 {
0781     int i = d_ptr->contents().indexOf(content);
0782     if (i >= 0) {
0783         ContentIndex ci;
0784         ci.push(i + 1);   // zero-based -> one-based index
0785         return ci;
0786     }
0787     // not found, we need to search recursively
0788     for (int i = 0; i < d_ptr->contents().size(); ++i) {
0789         ContentIndex ci = d_ptr->contents().at(i)->indexForContent(content);
0790         if (ci.isValid()) {
0791             // found it
0792             ci.push(i + 1);   // zero-based -> one-based index
0793             return ci;
0794         }
0795     }
0796     return {}; // not found
0797 }
0798 
0799 bool Content::isTopLevel() const
0800 {
0801     return d_ptr->parent == nullptr;
0802 }
0803 
0804 void Content::setParent(Content *parent)
0805 {
0806     // Make sure the Content is only in the contents list of one parent object
0807     Content *oldParent = d_ptr->parent;
0808     if (oldParent) {
0809         if (!oldParent->contents().isEmpty() && oldParent->contents().contains(this)) {
0810             oldParent->removeContent(this);
0811         }
0812     }
0813 
0814     d_ptr->parent = parent;
0815     if (parent) {
0816         if (!parent->contents().isEmpty() && !parent->contents().contains(this)) {
0817             parent->addContent(this);
0818         }
0819     }
0820 }
0821 
0822 Content *Content::parent() const
0823 {
0824     return d_ptr->parent;
0825 }
0826 
0827 Content *Content::topLevel() const
0828 {
0829     auto *top = const_cast<Content *>(this);
0830     Content *c = parent();
0831     while (c) {
0832         top = c;
0833         c = c->parent();
0834     }
0835 
0836     return top;
0837 }
0838 
0839 ContentIndex Content::index() const
0840 {
0841     Content *top = topLevel();
0842     if (top) {
0843         return top->indexForContent(const_cast<Content *>(this));
0844     }
0845 
0846     return indexForContent(const_cast<Content *>(this));
0847 }
0848 
0849 Message::Ptr Content::bodyAsMessage() const
0850 {
0851     if (bodyIsMessage() && d_ptr->bodyAsMessage) {
0852         return d_ptr->bodyAsMessage;
0853     } else {
0854       return {};
0855     }
0856 }
0857 
0858 bool Content::bodyIsMessage() const
0859 {
0860     // Use const_case here to work around API issue that neither header() nor hasHeader() are
0861     // const, even though they should be
0862     return const_cast<Content *>(this)->header<Headers::ContentType>(false) &&
0863            const_cast<Content *>(this)->header<Headers::ContentType>(true)
0864            ->mimeType().toLower() == "message/rfc822";
0865 }
0866 
0867 // @cond PRIVATE
0868 #define kmime_mk_header_accessor( type, method ) \
0869     Headers::type *Content::method( bool create ) { \
0870         return header<Headers::type>( create ); \
0871     }
0872 
0873 kmime_mk_header_accessor(ContentType, contentType)
0874 kmime_mk_header_accessor(ContentTransferEncoding, contentTransferEncoding)
0875 kmime_mk_header_accessor(ContentDisposition, contentDisposition)
0876 kmime_mk_header_accessor(ContentDescription, contentDescription)
0877 kmime_mk_header_accessor(ContentLocation, contentLocation)
0878 kmime_mk_header_accessor(ContentID, contentID)
0879 
0880 #undef kmime_mk_header_accessor
0881 // @endcond
0882 
0883 void ContentPrivate::clearBodyMessage()
0884 {
0885     bodyAsMessage.reset();
0886 }
0887 
0888 QVector<Content*> ContentPrivate::contents() const
0889 {
0890     Q_ASSERT(multipartContents.isEmpty() || !bodyAsMessage);
0891     if (bodyAsMessage) {
0892         return QVector<Content*>() << bodyAsMessage.data();
0893     } else {
0894         return multipartContents;
0895     }
0896 }
0897 
0898 bool ContentPrivate::parseUuencoded(Content *q)
0899 {
0900     Parser::UUEncoded uup(body, KMime::extractHeader(head, "Subject"));
0901     if (!uup.parse()) {
0902         return false; // Parsing failed.
0903     }
0904 
0905     Headers::ContentType *ct = q->contentType();
0906     ct->clear();
0907 
0908     if (uup.isPartial()) {
0909         // This seems to be only a part of the message, so we treat it as "message/partial".
0910         ct->setMimeType("message/partial");
0911         //ct->setId( uniqueString() ); not needed yet
0912         ct->setPartialParams(uup.partialCount(), uup.partialNumber());
0913         q->contentTransferEncoding()->setEncoding(Headers::CE7Bit);
0914     } else {
0915         // This is a complete message, so treat it as "multipart/mixed".
0916         const auto prevBody = body;
0917         body.clear();
0918         ct->setMimeType("multipart/mixed");
0919         ct->setBoundary(multiPartBoundary());
0920         ct->setCategory(Headers::CCcontainer);
0921         auto cte = q->contentTransferEncoding();
0922         cte->setEncoding(Headers::CE7Bit);
0923         cte->setDecoded(true);
0924 
0925         // Add the plain text part first.
0926         Q_ASSERT(multipartContents.isEmpty());
0927         {
0928             auto *c = new Content(q);
0929             c->contentType()->setMimeType("text/plain");
0930             c->contentTransferEncoding()->setEncoding(Headers::CE7Bit);
0931             c->setBody(uup.textPart());
0932             multipartContents.append(c);
0933         }
0934 
0935         // Now add each of the binary parts as sub-Contents.
0936         for (int i = 0; i < uup.binaryParts().count(); ++i) {
0937             auto *c = new Content(q);
0938             c->contentType()->setMimeType(uup.mimeTypes().at(i));
0939             c->contentType()->setName(QLatin1String(uup.filenames().at(i)), QByteArray(/*charset*/));
0940             c->contentTransferEncoding()->setEncoding(Headers::CEuuenc);
0941             c->contentTransferEncoding()->setDecoded(false);
0942             c->contentDisposition()->setDisposition(Headers::CDattachment);
0943             c->contentDisposition()->setFilename(QLatin1String(uup.filenames().at(i)));
0944             // uup.binaryParts().at(i) does no longer have the uuencode header, which makes KCodecs fail since 5c66308c4786ef7fbf77b0e306e73f7d4ac3431b
0945             c->setBody(prevBody);
0946             c->changeEncoding(Headers::CEbase64);   // Convert to base64.
0947             multipartContents.append(c);
0948         }
0949     }
0950 
0951     return true; // Parsing successful.
0952 }
0953 
0954 bool ContentPrivate::parseYenc(Content *q)
0955 {
0956     Parser::YENCEncoded yenc(body);
0957     if (!yenc.parse()) {
0958         return false; // Parsing failed.
0959     }
0960 
0961     Headers::ContentType *ct = q->contentType();
0962     ct->clear();
0963 
0964     if (yenc.isPartial()) {
0965         // Assume there is exactly one decoded part.  Treat this as "message/partial".
0966         ct->setMimeType("message/partial");
0967         //ct->setId( uniqueString() ); not needed yet
0968         ct->setPartialParams(yenc.partialCount(), yenc.partialNumber());
0969         q->contentTransferEncoding()->setEncoding(Headers::CEbinary);
0970         q->changeEncoding(Headers::CEbase64);   // Convert to base64.
0971     } else {
0972         // This is a complete message, so treat it as "multipart/mixed".
0973         body.clear();
0974         ct->setMimeType("multipart/mixed");
0975         ct->setBoundary(multiPartBoundary());
0976         ct->setCategory(Headers::CCcontainer);
0977         auto cte = q->contentTransferEncoding();
0978         cte->setEncoding(Headers::CE7Bit);
0979         cte->setDecoded(true);
0980 
0981         // Add the plain text part first.
0982         Q_ASSERT(multipartContents.isEmpty());
0983         {
0984             auto *c = new Content(q);
0985             c->contentType()->setMimeType("text/plain");
0986             c->contentTransferEncoding()->setEncoding(Headers::CE7Bit);
0987             c->setBody(yenc.textPart());
0988             multipartContents.append(c);
0989         }
0990 
0991         // Now add each of the binary parts as sub-Contents.
0992         for (int i = 0; i < yenc.binaryParts().count(); i++) {
0993             auto *c = new Content(q);
0994             c->contentType()->setMimeType(yenc.mimeTypes().at(i));
0995             c->contentType()->setName(QLatin1String(yenc.filenames().at(i)), QByteArray(/*charset*/));
0996             c->contentTransferEncoding()->setEncoding(Headers::CEbinary);
0997             c->contentDisposition()->setDisposition(Headers::CDattachment);
0998             c->contentDisposition()->setFilename(QLatin1String(yenc.filenames().at(i)));
0999             c->setBody(yenc.binaryParts().at(i));     // Yenc bodies are binary.
1000             c->changeEncoding(Headers::CEbase64);   // Convert to base64.
1001             multipartContents.append(c);
1002         }
1003     }
1004 
1005     return true; // Parsing successful.
1006 }
1007 
1008 bool ContentPrivate::parseMultipart(Content *q)
1009 {
1010     const Headers::ContentType *ct = q->contentType();
1011     const QByteArray boundary = ct->boundary();
1012     if (boundary.isEmpty()) {
1013         return false; // Parsing failed; invalid multipart content.
1014     }
1015     Parser::MultiPart mpp(body, boundary);
1016     if (!mpp.parse()) {
1017         return false; // Parsing failed.
1018     }
1019 
1020     preamble = mpp.preamble();
1021     epilogue = mpp.epilouge();
1022 
1023     // Determine the category of the subparts (used in attachments()).
1024     Headers::contentCategory cat;
1025     if (ct->isSubtype("alternative")) {
1026         cat = Headers::CCalternativePart;
1027     } else {
1028         cat = Headers::CCmixedPart; // Default to "mixed".
1029     }
1030 
1031     // Create a sub-Content for every part.
1032     Q_ASSERT(multipartContents.isEmpty());
1033     body.clear();
1034     const auto parts = mpp.parts();
1035     for (const QByteArray &part : parts) {
1036         auto *c = new Content(q);
1037         c->setContent(part);
1038         c->setFrozen(frozen);
1039         c->parse();
1040         c->contentType()->setCategory(cat);
1041         multipartContents.append(c);
1042     }
1043 
1044     return true; // Parsing successful.
1045 }
1046 
1047 } // namespace KMime