Warning, file /pim/kmime/src/kmime_content.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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() && !ct->isSubtype("related") && !ct->isSubtype("alternative")) {
0444         const QVector<Content*> contentsList = contents();
0445         result.reserve(contentsList.count());
0446         for (Content *child : contentsList) {
0447             if (isAttachment(child)) {
0448                 result.push_back(child);
0449             } else {
0450                 result += child->attachments();
0451             }
0452         }
0453     }
0454 
0455     return result;
0456 }
0457 
0458 QVector<Content*> Content::contents() const
0459 {
0460     return d_ptr->contents();
0461 }
0462 
0463 void Content::replaceContent(Content *oldContent, Content *newContent)
0464 {
0465     Q_D( Content );
0466     if ( d->multipartContents.isEmpty() || !d->multipartContents.contains( oldContent ) ) {
0467       return;
0468     }
0469 
0470     d->multipartContents.removeAll( oldContent );
0471     delete oldContent;
0472     d->multipartContents.append( newContent );
0473     if( newContent->parent() != this ) {
0474       // If the content was part of something else, this will remove it from there.
0475       newContent->setParent( this );
0476     }
0477 }
0478 
0479 
0480 void Content::addContent(Content *c, bool prepend)
0481 {
0482     Q_D(Content);
0483 
0484     // This method makes no sense for encapsulated messages
0485     Q_ASSERT(!bodyIsMessage());
0486 
0487     // If this message is single-part; make it multipart first.
0488     if (d->multipartContents.isEmpty() && !contentType()->isMultipart()) {
0489         // The current body will be our first sub-Content.
0490         auto *main = new Content(this);
0491 
0492         // Move the MIME headers to the newly created sub-Content.
0493         // NOTE: The other headers (RFC5322 headers like From:, To:, as well as X-headers
0494         // are not moved to the subcontent; they remain with the top-level content.
0495         for (auto it = d->headers.begin(); it != d->headers.end();) {
0496             if ((*it)->isMimeHeader()) {
0497                 // Add to new content.
0498                 main->setHeader(*it);
0499                 // Remove from this content.
0500                 it = d->headers.erase(it);
0501             } else {
0502                 ++it;
0503             }
0504         }
0505 
0506         // Adjust the Content-Type of the newly created sub-Content.
0507         main->contentType()->setCategory(Headers::CCmixedPart);
0508 
0509         // Move the body to the new subcontent.
0510         main->setBody(d->body);
0511         d->body.clear();
0512 
0513         // Add the subcontent.
0514         d->multipartContents.append(main);
0515 
0516         // Convert this content to "multipart/mixed".
0517         Headers::ContentType *ct = contentType();
0518         ct->setMimeType("multipart/mixed");
0519         ct->setBoundary(multiPartBoundary());
0520         ct->setCategory(Headers::CCcontainer);
0521         auto cte = contentTransferEncoding();
0522         cte->setEncoding(Headers::CE7Bit);
0523         cte->setDecoded(true);
0524     }
0525 
0526     // Add the new content.
0527     if (prepend) {
0528         d->multipartContents.prepend(c);
0529     } else {
0530         d->multipartContents.append(c);
0531     }
0532 
0533     if (c->parent() != this) {
0534         // If the content was part of something else, this will remove it from there.
0535         c->setParent(this);
0536     }
0537 }
0538 
0539 void Content::removeContent(Content *c, bool del)
0540 {
0541     Q_D(Content);
0542     if (d->multipartContents.isEmpty() || !d->multipartContents.contains(c)) {
0543         return;
0544     }
0545 
0546     // This method makes no sense for encapsulated messages.
0547     // Should be covered by the above assert already, though.
0548     Q_ASSERT(!bodyIsMessage());
0549 
0550     d->multipartContents.removeAll(c);
0551     if (del) {
0552         delete c;
0553     } else {
0554         c->d_ptr->parent = nullptr;
0555     }
0556 
0557     // If only one content is left, turn this content into a single-part.
0558     if (d->multipartContents.count() == 1) {
0559         Content *main = d->multipartContents.constFirst();
0560 
0561         // Move all headers from the old subcontent to ourselves.
0562         // NOTE: This also sets the new Content-Type.
0563         const auto headers = main->d_ptr->headers;
0564         for (Headers::Base *h : headers) {
0565             setHeader(h);   // Will remove the old one if present.
0566         }
0567         main->d_ptr->headers.clear();
0568 
0569         // Move the body.
0570         d->body = main->body();
0571 
0572         // Delete the old subcontent.
0573         delete main;
0574         d->multipartContents.clear();
0575     }
0576 }
0577 
0578 void Content::changeEncoding(Headers::contentEncoding e)
0579 {
0580     // This method makes no sense for encapsulated messages, they are always 7bit
0581     // encoded.
0582     Q_ASSERT(!bodyIsMessage());
0583 
0584     Headers::ContentTransferEncoding *enc = contentTransferEncoding();
0585     if (enc->encoding() == e) {
0586         // Nothing to do.
0587         return;
0588     }
0589 
0590     if (d_ptr->decodeText(this)) {
0591         // This is textual content.  Textual content is stored decoded.
0592         Q_ASSERT(enc->isDecoded());
0593         enc->setEncoding(e);
0594     } else {
0595         // This is non-textual content.  Re-encode it.
0596         if (e == Headers::CEbase64) {
0597             KCodecs::base64Encode(decodedContent(), d_ptr->body, true);
0598             enc->setEncoding(e);
0599             enc->setDecoded(false);
0600         } else {
0601             // It only makes sense to convert binary stuff to base64.
0602             Q_ASSERT(false);
0603         }
0604     }
0605 }
0606 
0607 QVector<Headers::Base*> Content::headers() const
0608 {
0609     return d_ptr->headers;
0610 }
0611 
0612 Headers::Base *Content::headerByType(const char *type) const
0613 {
0614     Q_ASSERT(type  && *type);
0615 
0616     for (Headers::Base *h : std::as_const(d_ptr->headers)) {
0617         if (h->is(type)) {
0618             return h; // Found.
0619         }
0620     }
0621 
0622     return nullptr; // Not found.
0623 }
0624 
0625 QVector<Headers::Base*> Content::headersByType(const char *type) const
0626 {
0627     Q_ASSERT(type && *type);
0628 
0629     QVector<Headers::Base*> result;
0630 
0631     for (Headers::Base *h : std::as_const(d_ptr->headers)) {
0632         if (h->is(type)) {
0633             result << h;
0634         }
0635     }
0636 
0637     return result;
0638 }
0639 
0640 void Content::setHeader(Headers::Base *h)
0641 {
0642     Q_ASSERT(h);
0643     removeHeader(h->type());
0644     appendHeader(h);
0645 }
0646 
0647 void Content::appendHeader(Headers::Base *h)
0648 {
0649     Q_D(Content);
0650     d->headers.append(h);
0651 }
0652 
0653 bool Content::removeHeader(const char *type)
0654 {
0655     Q_D(Content);
0656     const auto endIt = d->headers.end();
0657     for (auto it = d->headers.begin(); it != endIt; ++it) {
0658         if ((*it)->is(type)) {
0659             delete(*it);
0660             d->headers.erase(it);
0661             return true;
0662         }
0663     }
0664 
0665     return false;
0666 }
0667 
0668 bool Content::hasHeader(const char* type) const
0669 {
0670     return headerByType(type) != nullptr;
0671 }
0672 
0673 int Content::size()
0674 {
0675     int ret = d_ptr->body.length();
0676 
0677     if (contentTransferEncoding()->encoding() == Headers::CEbase64) {
0678         KCodecs::Codec *codec = KCodecs::Codec::codecForName("base64");
0679         return codec->maxEncodedSizeFor(ret);
0680     }
0681 
0682     // Not handling quoted-printable here since that requires actually
0683     // converting the content, and that is O(size_of_content).
0684     // For quoted-printable, this is only an approximate size.
0685 
0686     return ret;
0687 }
0688 
0689 int Content::storageSize() const
0690 {
0691     const Q_D(Content);
0692     int s = d->head.size();
0693 
0694     if (d->contents().isEmpty()) {
0695         s += d->body.size();
0696     } else {
0697 
0698         // FIXME: This should take into account the boundary headers that are added in
0699         //        encodedContent!
0700         const auto contents = d->contents();
0701         for (Content *c : contents) {
0702             s += c->storageSize();
0703         }
0704     }
0705 
0706     return s;
0707 }
0708 
0709 int Content::lineCount() const
0710 {
0711     const Q_D(Content);
0712     int ret = 0;
0713     if (!isTopLevel()) {
0714         ret += d->head.count('\n');
0715     }
0716     ret += d->body.count('\n');
0717 
0718     const auto contents = d->contents();
0719     for (Content *c : contents) {
0720         ret += c->lineCount();
0721     }
0722 
0723     return ret;
0724 }
0725 
0726 bool ContentPrivate::decodeText(Content *q)
0727 {
0728     Headers::ContentTransferEncoding *enc = q->contentTransferEncoding();
0729 
0730     if (!q->contentType()->isText()) {
0731         return false; //non textual data cannot be decoded here => use decodedContent() instead
0732     }
0733     if (enc->isDecoded()) {
0734         return true; //nothing to do
0735     }
0736 
0737     switch (enc->encoding()) {
0738     case Headers::CEbase64 :
0739         body = KCodecs::base64Decode(body);
0740         break;
0741     case Headers::CEquPr :
0742         body = KCodecs::quotedPrintableDecode(body);
0743         break;
0744     case Headers::CEuuenc :
0745         body = KCodecs::uudecode(body);
0746         break;
0747     case Headers::CEbinary :
0748         // nothing to decode
0749     default :
0750         break;
0751     }
0752     if (!body.endsWith("\n")) {
0753         body.append("\n");
0754     }
0755     enc->setDecoded(true);
0756     return true;
0757 }
0758 
0759 QByteArray Content::defaultCharset()
0760 {
0761     return KMime::cachedCharset(QByteArrayLiteral("ISO-8859-1"));
0762 }
0763 
0764 Content *KMime::Content::content(const ContentIndex &index) const
0765 {
0766     if (!index.isValid()) {
0767         return const_cast<KMime::Content *>(this);
0768     }
0769     ContentIndex idx = index;
0770     unsigned int i = idx.pop() - 1; // one-based -> zero-based index
0771     if (i < static_cast<unsigned int>(d_ptr->contents().size())) {
0772         return d_ptr->contents().at(i)->content(idx);
0773     } else {
0774         return nullptr;
0775     }
0776 }
0777 
0778 ContentIndex KMime::Content::indexForContent(Content *content) const
0779 {
0780     int i = d_ptr->contents().indexOf(content);
0781     if (i >= 0) {
0782         ContentIndex ci;
0783         ci.push(i + 1);   // zero-based -> one-based index
0784         return ci;
0785     }
0786     // not found, we need to search recursively
0787     for (int i = 0; i < d_ptr->contents().size(); ++i) {
0788         ContentIndex ci = d_ptr->contents().at(i)->indexForContent(content);
0789         if (ci.isValid()) {
0790             // found it
0791             ci.push(i + 1);   // zero-based -> one-based index
0792             return ci;
0793         }
0794     }
0795     return {}; // not found
0796 }
0797 
0798 bool Content::isTopLevel() const
0799 {
0800     return d_ptr->parent == nullptr;
0801 }
0802 
0803 void Content::setParent(Content *parent)
0804 {
0805     // Make sure the Content is only in the contents list of one parent object
0806     Content *oldParent = d_ptr->parent;
0807     if (oldParent) {
0808         if (!oldParent->contents().isEmpty() && oldParent->contents().contains(this)) {
0809             oldParent->removeContent(this);
0810         }
0811     }
0812 
0813     d_ptr->parent = parent;
0814     if (parent) {
0815         if (!parent->contents().isEmpty() && !parent->contents().contains(this)) {
0816             parent->addContent(this);
0817         }
0818     }
0819 }
0820 
0821 Content *Content::parent() const
0822 {
0823     return d_ptr->parent;
0824 }
0825 
0826 Content *Content::topLevel() const
0827 {
0828     auto *top = const_cast<Content *>(this);
0829     Content *c = parent();
0830     while (c) {
0831         top = c;
0832         c = c->parent();
0833     }
0834 
0835     return top;
0836 }
0837 
0838 ContentIndex Content::index() const
0839 {
0840     Content *top = topLevel();
0841     if (top) {
0842         return top->indexForContent(const_cast<Content *>(this));
0843     }
0844 
0845     return indexForContent(const_cast<Content *>(this));
0846 }
0847 
0848 Message::Ptr Content::bodyAsMessage() const
0849 {
0850     if (bodyIsMessage() && d_ptr->bodyAsMessage) {
0851         return d_ptr->bodyAsMessage;
0852     } else {
0853       return {};
0854     }
0855 }
0856 
0857 bool Content::bodyIsMessage() const
0858 {
0859     // Use const_case here to work around API issue that neither header() nor hasHeader() are
0860     // const, even though they should be
0861     return const_cast<Content *>(this)->header<Headers::ContentType>(false) &&
0862            const_cast<Content *>(this)->header<Headers::ContentType>(true)
0863            ->mimeType().toLower() == "message/rfc822";
0864 }
0865 
0866 // @cond PRIVATE
0867 #define kmime_mk_header_accessor( type, method ) \
0868     Headers::type *Content::method( bool create ) { \
0869         return header<Headers::type>( create ); \
0870     }
0871 
0872 kmime_mk_header_accessor(ContentType, contentType)
0873 kmime_mk_header_accessor(ContentTransferEncoding, contentTransferEncoding)
0874 kmime_mk_header_accessor(ContentDisposition, contentDisposition)
0875 kmime_mk_header_accessor(ContentDescription, contentDescription)
0876 kmime_mk_header_accessor(ContentLocation, contentLocation)
0877 kmime_mk_header_accessor(ContentID, contentID)
0878 
0879 #undef kmime_mk_header_accessor
0880 // @endcond
0881 
0882 void ContentPrivate::clearBodyMessage()
0883 {
0884     bodyAsMessage.reset();
0885 }
0886 
0887 QVector<Content*> ContentPrivate::contents() const
0888 {
0889     Q_ASSERT(multipartContents.isEmpty() || !bodyAsMessage);
0890     if (bodyAsMessage) {
0891         return QVector<Content*>() << bodyAsMessage.data();
0892     } else {
0893         return multipartContents;
0894     }
0895 }
0896 
0897 bool ContentPrivate::parseUuencoded(Content *q)
0898 {
0899     Parser::UUEncoded uup(body, KMime::extractHeader(head, "Subject"));
0900     if (!uup.parse()) {
0901         return false; // Parsing failed.
0902     }
0903 
0904     Headers::ContentType *ct = q->contentType();
0905     ct->clear();
0906 
0907     if (uup.isPartial()) {
0908         // This seems to be only a part of the message, so we treat it as "message/partial".
0909         ct->setMimeType("message/partial");
0910         //ct->setId( uniqueString() ); not needed yet
0911         ct->setPartialParams(uup.partialCount(), uup.partialNumber());
0912         q->contentTransferEncoding()->setEncoding(Headers::CE7Bit);
0913     } else {
0914         // This is a complete message, so treat it as "multipart/mixed".
0915         const auto prevBody = body;
0916         body.clear();
0917         ct->setMimeType("multipart/mixed");
0918         ct->setBoundary(multiPartBoundary());
0919         ct->setCategory(Headers::CCcontainer);
0920         auto cte = q->contentTransferEncoding();
0921         cte->setEncoding(Headers::CE7Bit);
0922         cte->setDecoded(true);
0923 
0924         // Add the plain text part first.
0925         Q_ASSERT(multipartContents.isEmpty());
0926         {
0927             auto *c = new Content(q);
0928             c->contentType()->setMimeType("text/plain");
0929             c->contentTransferEncoding()->setEncoding(Headers::CE7Bit);
0930             c->setBody(uup.textPart());
0931             multipartContents.append(c);
0932         }
0933 
0934         // Now add each of the binary parts as sub-Contents.
0935         for (int i = 0; i < uup.binaryParts().count(); ++i) {
0936             auto *c = new Content(q);
0937             c->contentType()->setMimeType(uup.mimeTypes().at(i));
0938             c->contentType()->setName(QLatin1String(uup.filenames().at(i)), QByteArray(/*charset*/));
0939             c->contentTransferEncoding()->setEncoding(Headers::CEuuenc);
0940             c->contentTransferEncoding()->setDecoded(false);
0941             c->contentDisposition()->setDisposition(Headers::CDattachment);
0942             c->contentDisposition()->setFilename(QLatin1String(uup.filenames().at(i)));
0943             // uup.binaryParts().at(i) does no longer have the uuencode header, which makes KCodecs fail since 5c66308c4786ef7fbf77b0e306e73f7d4ac3431b
0944             c->setBody(prevBody);
0945             c->changeEncoding(Headers::CEbase64);   // Convert to base64.
0946             multipartContents.append(c);
0947         }
0948     }
0949 
0950     return true; // Parsing successful.
0951 }
0952 
0953 bool ContentPrivate::parseYenc(Content *q)
0954 {
0955     Parser::YENCEncoded yenc(body);
0956     if (!yenc.parse()) {
0957         return false; // Parsing failed.
0958     }
0959 
0960     Headers::ContentType *ct = q->contentType();
0961     ct->clear();
0962 
0963     if (yenc.isPartial()) {
0964         // Assume there is exactly one decoded part.  Treat this as "message/partial".
0965         ct->setMimeType("message/partial");
0966         //ct->setId( uniqueString() ); not needed yet
0967         ct->setPartialParams(yenc.partialCount(), yenc.partialNumber());
0968         q->contentTransferEncoding()->setEncoding(Headers::CEbinary);
0969         q->changeEncoding(Headers::CEbase64);   // Convert to base64.
0970     } else {
0971         // This is a complete message, so treat it as "multipart/mixed".
0972         body.clear();
0973         ct->setMimeType("multipart/mixed");
0974         ct->setBoundary(multiPartBoundary());
0975         ct->setCategory(Headers::CCcontainer);
0976         auto cte = q->contentTransferEncoding();
0977         cte->setEncoding(Headers::CE7Bit);
0978         cte->setDecoded(true);
0979 
0980         // Add the plain text part first.
0981         Q_ASSERT(multipartContents.isEmpty());
0982         {
0983             auto *c = new Content(q);
0984             c->contentType()->setMimeType("text/plain");
0985             c->contentTransferEncoding()->setEncoding(Headers::CE7Bit);
0986             c->setBody(yenc.textPart());
0987             multipartContents.append(c);
0988         }
0989 
0990         // Now add each of the binary parts as sub-Contents.
0991         for (int i = 0; i < yenc.binaryParts().count(); i++) {
0992             auto *c = new Content(q);
0993             c->contentType()->setMimeType(yenc.mimeTypes().at(i));
0994             c->contentType()->setName(QLatin1String(yenc.filenames().at(i)), QByteArray(/*charset*/));
0995             c->contentTransferEncoding()->setEncoding(Headers::CEbinary);
0996             c->contentDisposition()->setDisposition(Headers::CDattachment);
0997             c->contentDisposition()->setFilename(QLatin1String(yenc.filenames().at(i)));
0998             c->setBody(yenc.binaryParts().at(i));     // Yenc bodies are binary.
0999             c->changeEncoding(Headers::CEbase64);   // Convert to base64.
1000             multipartContents.append(c);
1001         }
1002     }
1003 
1004     return true; // Parsing successful.
1005 }
1006 
1007 bool ContentPrivate::parseMultipart(Content *q)
1008 {
1009     const Headers::ContentType *ct = q->contentType();
1010     const QByteArray boundary = ct->boundary();
1011     if (boundary.isEmpty()) {
1012         return false; // Parsing failed; invalid multipart content.
1013     }
1014     Parser::MultiPart mpp(body, boundary);
1015     if (!mpp.parse()) {
1016         return false; // Parsing failed.
1017     }
1018 
1019     preamble = mpp.preamble();
1020     epilogue = mpp.epilouge();
1021 
1022     // Determine the category of the subparts (used in attachments()).
1023     Headers::contentCategory cat;
1024     if (ct->isSubtype("alternative")) {
1025         cat = Headers::CCalternativePart;
1026     } else {
1027         cat = Headers::CCmixedPart; // Default to "mixed".
1028     }
1029 
1030     // Create a sub-Content for every part.
1031     Q_ASSERT(multipartContents.isEmpty());
1032     body.clear();
1033     const auto parts = mpp.parts();
1034     for (const QByteArray &part : parts) {
1035         auto *c = new Content(q);
1036         c->setContent(part);
1037         c->setFrozen(frozen);
1038         c->parse();
1039         c->contentType()->setCategory(cat);
1040         multipartContents.append(c);
1041     }
1042 
1043     return true; // Parsing successful.
1044 }
1045 
1046 } // namespace KMime