messagepart.cpp 80.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
   Copyright (c) 2015 Sandro Knauß <sknauss@kde.org>

   This library is free software; you can redistribute it and/or modify it
   under the terms of the GNU Library General Public License as published by
   the Free Software Foundation; either version 2 of the License, or (at your
   option) any later version.

   This library is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
   License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.
*/

#include "messagepart.h"
21
#include "mimetreeparser_debug.h"
22
#include "objecttreeparser.h"
23
#include "converthtmltoplaintext.h"
24
#include "csshelperbase.h"
25
#include "cryptohelper.h"
Sandro Knauß's avatar
Sandro Knauß committed
26 27
#include "interfaces/htmlwriter.h"
#include "job/kleojobexecutor.h"
28
#include "utils/iconnamecache.h"
29

30
#include "memento/decryptverifybodypartmemento.h"
31 32
#include "memento/verifydetachedbodypartmemento.h"
#include "memento/verifyopaquebodypartmemento.h"
33

34 35
#include <MessageCore/StringUtil>

36
#include <Libkleo/Dn>
37 38
#include <Libkleo/ImportJob>
#include <Libkleo/CryptoBackendFactory>
39
#include <Libkleo/KeyListJob>
40 41
#include <Libkleo/VerifyDetachedJob>
#include <Libkleo/VerifyOpaqueJob>
42

43
#include <KMime/Content>
Sandro Knauß's avatar
Sandro Knauß committed
44

45
#include <gpgme++/key.h>
46
#include <gpgme++/keylistresult.h>
47 48 49 50
#include <gpgme.h>

#include <QTextCodec>
#include <QApplication>
51 52 53
#include <QWebPage>
#include <QWebElement>
#include <QWebFrame>
54 55

#include <KLocalizedString>
56
#include <KEmailAddress>
57

58 59
#include <sstream>

60
using namespace MimeTreeParser;
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
/** Checks whether @p str contains external references. To be precise,
    we only check whether @p str contains 'xxx="http[s]:' where xxx is
    not href. Obfuscated external references are ignored on purpose.
*/

bool containsExternalReferences(const QString &str, const QString &extraHead)
{
    const bool hasBaseInHeader = extraHead.contains(QStringLiteral("<base href=\""), Qt::CaseInsensitive);
    if (hasBaseInHeader && (str.contains(QStringLiteral("href=\"/"), Qt::CaseInsensitive) ||
                            str.contains(QStringLiteral("<img src=\"/"), Qt::CaseInsensitive))) {
        return true;
    }
    /*
    //Laurent: workaround for local ref cid
    if(str.contains(QStringLiteral("<img src=\"cid:"),Qt::CaseInsensitive)) {
    return true;
    }
    */
    int httpPos = str.indexOf(QLatin1String("\"http:"), Qt::CaseInsensitive);
    int httpsPos = str.indexOf(QLatin1String("\"https:"), Qt::CaseInsensitive);

    while (httpPos >= 0 || httpsPos >= 0) {
        // pos = index of next occurrence of "http: or "https: whichever comes first
        int pos = (httpPos < httpsPos)
                  ? ((httpPos >= 0) ? httpPos : httpsPos)
                  : ((httpsPos >= 0) ? httpsPos : httpPos);
        // look backwards for "href"
        if (pos > 5) {
            int hrefPos = str.lastIndexOf(QLatin1String("href"), pos - 5, Qt::CaseInsensitive);
            // if no 'href' is found or the distance between 'href' and '"http[s]:'
            // is larger than 7 (7 is the distance in 'href = "http[s]:') then
            // we assume that we have found an external reference
            if ((hrefPos == -1) || (pos - hrefPos > 7)) {

                // HTML messages created by KMail itself for now contain the following:
                // <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
                // Make sure not to show an external references warning for this string
                int dtdPos = str.indexOf(QLatin1String("http://www.w3.org/TR/html4/loose.dtd"), pos + 1);
                if (dtdPos != (pos + 1)) {
                    return true;
                }
            }
        }
        // find next occurrence of "http: or "https:
        if (pos == httpPos) {
            httpPos = str.indexOf(QLatin1String("\"http:"), httpPos + 6, Qt::CaseInsensitive);
        } else {
            httpsPos = str.indexOf(QLatin1String("\"https:"), httpsPos + 7, Qt::CaseInsensitive);
        }
    }
    return false;
}

115 116 117 118 119
QString HTMLBlock::dir() const
{
    return QApplication::isRightToLeft() ? QStringLiteral("rtl") : QStringLiteral("ltr");
}

120
//--------CryptoBlock-------------------
121
CryptoBlock::CryptoBlock(HtmlWriter *writer,
122
                         PartMetaData *block,
123
                         const NodeHelper *nodeHelper,
124
                         const Kleo::CryptoBackend::Protocol *cryptoProto,
125
                         ObjectTreeSourceIf *source,
126 127 128
                         const QString &fromAddress,
                         KMime::Content *node)
    : HTMLBlock()
129
    , mWriter(writer)
130
    , mMetaData(block)
131
    , mNodeHelper(nodeHelper)
132
    , mCryptoProto(cryptoProto)
133
    , mSource(source)
134 135 136 137 138 139 140 141 142 143 144 145 146
    , mFromAddress(fromAddress)
    , mNode(node)
{
    internalEnter();
}

CryptoBlock::~CryptoBlock()
{
    internalExit();
}

void CryptoBlock::internalEnter()
{
147
    if (mWriter && !entered) {
148
        entered = true;
149
        if (mMetaData->isEncapsulatedRfc822Message) {
150
            mInteralBlocks.append(HTMLBlock::Ptr(new EncapsulatedRFC822Block(mWriter, mNodeHelper, mNode)));
151 152
        }
        if (mMetaData->isEncrypted) {
153
            mInteralBlocks.append(HTMLBlock::Ptr(new EncryptedBlock(mWriter, *mMetaData)));
154
        }
155
        if (mMetaData->isSigned) {
156
            mInteralBlocks.append(HTMLBlock::Ptr(new SignedBlock(mWriter, *mMetaData, mCryptoProto, mSource, mFromAddress, false)));
157
        }
158 159 160 161 162 163 164 165
    }
}

void CryptoBlock::internalExit()
{
    if (!entered) {
        return;
    }
166

167 168 169 170 171 172
    while (!mInteralBlocks.isEmpty()) {
        mInteralBlocks.removeLast();
    }
    entered = false;
}

Sandro Knauß's avatar
Sandro Knauß committed
173
EncapsulatedRFC822Block::EncapsulatedRFC822Block(MimeTreeParser::HtmlWriter *writer, const MimeTreeParser::NodeHelper *nodeHelper, KMime::Content *node)
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    : mWriter(writer)
    , mNodeHelper(nodeHelper)
    , mNode(node)
{
    internalEnter();
}

EncapsulatedRFC822Block::~EncapsulatedRFC822Block()
{
    internalExit();
}

void EncapsulatedRFC822Block::internalEnter()
{
    if (mWriter && !entered) {
189
        QString text;
190 191
        if (mNode) {
            const QString href = mNodeHelper->asHREF(mNode, QStringLiteral("body"));
192
            text = QStringLiteral("<a href=\"%1\">%2</a>").arg(href, i18n("Encapsulated message"));
193
        } else {
194
            text = i18n("Encapsulated message");
195
        }
196
        mWriter->queue(QStringLiteral("<table cellspacing=\"1\" cellpadding=\"1\" class=\"rfc822\">") +
197
                       QStringLiteral("<tr class=\"rfc822H\"><td dir=\"%1\">").arg(dir()) +
198 199
                       text +
                       QStringLiteral("</td></tr><tr class=\"rfc822B\"><td>"));
200 201 202 203 204 205 206 207 208 209 210

        entered = true;
    }
}

void EncapsulatedRFC822Block::internalExit()
{
    if (!entered) {
        return;
    }

211
    mWriter->queue(QStringLiteral("</td></tr>"
Laurent Montel's avatar
Laurent Montel committed
212 213
                                  "<tr class=\"rfc822H\"><td dir=\"%1\">%2</td></tr>"
                                  "</table>").arg(dir(), i18n("End of encapsulated message")));
214 215 216
    entered = false;
}

217
EncryptedBlock::EncryptedBlock(MimeTreeParser::HtmlWriter *writer, const PartMetaData &block)
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    : mWriter(writer)
    , mBlock(block)
{
    internalEnter();
}

EncryptedBlock::~EncryptedBlock()
{
    internalExit();
}

void EncryptedBlock::internalEnter()
{
    if (mWriter && !entered) {
        entered = true;
Sandro Knauß's avatar
Sandro Knauß committed
233
        QString text;
234
        if (mBlock.inProgress) {
Sandro Knauß's avatar
Sandro Knauß committed
235
            text = i18n("Please wait while the message is being decrypted...");
236
        } else if (mBlock.isDecryptable) {
Sandro Knauß's avatar
Sandro Knauß committed
237
            text = i18n("Encrypted message");
238
        } else {
Sandro Knauß's avatar
Sandro Knauß committed
239
            text = i18n("Encrypted message (decryption not possible)");
240
            if (!mBlock.errorText.isEmpty()) {
Sandro Knauß's avatar
Sandro Knauß committed
241
                text += QStringLiteral("<br />") + i18n("Reason: %1", mBlock.errorText);
242 243
            }
        }
Sandro Knauß's avatar
Sandro Knauß committed
244
        mWriter->queue(QStringLiteral("<table cellspacing=\"1\" cellpadding=\"1\" class=\"encr\">") +
245
                       QStringLiteral("<tr class=\"encrH\"><td dir=\"%1\">").arg(dir()) +
Sandro Knauß's avatar
Sandro Knauß committed
246 247
                       text +
                       QStringLiteral("</td></tr><tr class=\"encrB\"><td>"));
248 249 250 251 252 253 254 255
    }
}

void EncryptedBlock::internalExit()
{
    if (!entered) {
        return;
    }
Sandro Knauß's avatar
Sandro Knauß committed
256
    mWriter->queue(QStringLiteral("</td></tr>"
Laurent Montel's avatar
Laurent Montel committed
257 258
                                  "<tr class=\"encrH\"><td dir=\"%1\">%2</td></tr>"
                                  "</table>").arg(dir(), i18n("End of encrypted message")));
259 260 261
    entered = false;
}

262
SignedBlock::SignedBlock(MimeTreeParser::HtmlWriter *writer, const PartMetaData &block,
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
                         const Kleo::CryptoBackend::Protocol *cryptoProto,
                         ObjectTreeSourceIf *source,
                         QString fromAddress, bool printing)
    : HTMLBlock()
    , mBlock(block)
    , mWriter(writer)
    , mCryptoProto(cryptoProto)
    , mSource(source)
    , mFromAddress(fromAddress)
    , mPrinting(printing)
{
    internalEnter();
}

SignedBlock::~SignedBlock()
{
    internalExit();
}

static const int SIG_FRAME_COL_UNDEF = 99;
#define SIG_FRAME_COL_RED    -1
#define SIG_FRAME_COL_YELLOW  0
#define SIG_FRAME_COL_GREEN   1
QString sigStatusToString(const Kleo::CryptoBackend::Protocol *cryptProto,
Laurent Montel's avatar
Laurent Montel committed
287 288 289 290
                          int status_code,
                          GpgME::Signature::Summary summary,
                          int &frameColor,
                          bool &showKeyInfos)
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
{
    // note: At the moment frameColor and showKeyInfos are
    //       used for CMS only but not for PGP signatures
    // pending(khz): Implement usage of these for PGP sigs as well.
    showKeyInfos = true;
    QString result;
    if (cryptProto) {
        if (cryptProto == Kleo::CryptoBackendFactory::instance()->openpgp()) {
            // process enum according to it's definition to be read in
            // GNU Privacy Guard CVS repository /gpgme/gpgme/gpgme.h
            switch (status_code) {
            case 0: // GPGME_SIG_STAT_NONE
                result = i18n("Error: Signature not verified");
                break;
            case 1: // GPGME_SIG_STAT_GOOD
                result = i18n("Good signature");
                break;
            case 2: // GPGME_SIG_STAT_BAD
                result = i18n("<b>Bad</b> signature");
                break;
            case 3: // GPGME_SIG_STAT_NOKEY
                result = i18n("No public key to verify the signature");
                break;
            case 4: // GPGME_SIG_STAT_NOSIG
                result = i18n("No signature found");
                break;
            case 5: // GPGME_SIG_STAT_ERROR
                result = i18n("Error verifying the signature");
                break;
            case 6: // GPGME_SIG_STAT_DIFF
                result = i18n("Different results for signatures");
                break;
            /* PENDING(khz) Verify exact meaning of the following values:
            case 7: // GPGME_SIG_STAT_GOOD_EXP
            return i18n("Signature certificate is expired");
            break;
            case 8: // GPGME_SIG_STAT_GOOD_EXPKEY
            return i18n("One of the certificate's keys is expired");
            break;
            */
            default:
                result.clear();   // do *not* return a default text here !
                break;
            }
        } else if (cryptProto == Kleo::CryptoBackendFactory::instance()->smime()) {
            // process status bits according to SigStatus_...
            // definitions in kdenetwork/libkdenetwork/cryptplug.h

            if (summary == GpgME::Signature::None) {
                result = i18n("No status information available.");
                frameColor = SIG_FRAME_COL_YELLOW;
                showKeyInfos = false;
                return result;
            }

            if (summary & GpgME::Signature::Valid) {
                result = i18n("Good signature.");
                // Note:
                // Here we are work differently than KMail did before!
                //
                // The GOOD case ( == sig matching and the complete
                // certificate chain was verified and is valid today )
                // by definition does *not* show any key
                // information but just states that things are OK.
                //           (khz, according to LinuxTag 2002 meeting)
                frameColor = SIG_FRAME_COL_GREEN;
                showKeyInfos = false;
                return result;
            }

            // we are still there?  OK, let's test the different cases:

            // we assume green, test for yellow or red (in this order!)
            frameColor = SIG_FRAME_COL_GREEN;
            QString result2;
            if (summary & GpgME::Signature::KeyExpired) {
                // still is green!
                result2 += i18n("One key has expired.");
            }
            if (summary & GpgME::Signature::SigExpired) {
                // and still is green!
                result2 += i18n("The signature has expired.");
            }

            // test for yellow:
            if (summary & GpgME::Signature::KeyMissing) {
                result2 += i18n("Unable to verify: key missing.");
                // if the signature certificate is missing
                // we cannot show information on it
                showKeyInfos = false;
                frameColor = SIG_FRAME_COL_YELLOW;
            }
            if (summary & GpgME::Signature::CrlMissing) {
                result2 += i18n("CRL not available.");
                frameColor = SIG_FRAME_COL_YELLOW;
            }
            if (summary & GpgME::Signature::CrlTooOld) {
                result2 += i18n("Available CRL is too old.");
                frameColor = SIG_FRAME_COL_YELLOW;
            }
            if (summary & GpgME::Signature::BadPolicy) {
                result2 += i18n("A policy was not met.");
                frameColor = SIG_FRAME_COL_YELLOW;
            }
            if (summary & GpgME::Signature::SysError) {
                result2 += i18n("A system error occurred.");
                // if a system error occurred
                // we cannot trust any information
                // that was given back by the plug-in
                showKeyInfos = false;
                frameColor = SIG_FRAME_COL_YELLOW;
            }

            // test for red:
            if (summary & GpgME::Signature::KeyRevoked) {
                // this is red!
                result2 += i18n("One key has been revoked.");
                frameColor = SIG_FRAME_COL_RED;
            }
            if (summary & GpgME::Signature::Red) {
                if (result2.isEmpty())
                    // Note:
                    // Here we are work differently than KMail did before!
                    //
                    // The BAD case ( == sig *not* matching )
                    // by definition does *not* show any key
                    // information but just states that things are BAD.
                    //
                    // The reason for this: In this case ALL information
                    // might be falsificated, we can NOT trust the data
                    // in the body NOT the signature - so we don't show
                    // any key/signature information at all!
                    //         (khz, according to LinuxTag 2002 meeting)
                {
                    showKeyInfos = false;
                }
                frameColor = SIG_FRAME_COL_RED;
            } else {
                result.clear();
            }

            if (SIG_FRAME_COL_GREEN == frameColor) {
                result = i18n("Good signature.");
            } else if (SIG_FRAME_COL_RED == frameColor) {
                result = i18n("<b>Bad</b> signature.");
            } else {
                result.clear();
            }

            if (!result2.isEmpty()) {
                if (!result.isEmpty()) {
                    result.append(QLatin1String("<br />"));
                }
                result.append(result2);
            }
        }
        /*
        // add i18n support for 3rd party plug-ins here:
        else if ( cryptPlug->libName().contains( "yetanotherpluginname", Qt::CaseInsensitive )) {

        }
        */
    }
    return result;
}

QString SignedBlock::simpleHeader()
{
    QString text;
    if (mClass == QStringLiteral("signErr")) {
        text = i18n("Invalid signature.");
    } else if (mClass == QStringLiteral("signOkKeyBad")
               || mClass == QStringLiteral("signWarn")) {
Laurent Montel's avatar
Laurent Montel committed
464
        text = i18n("Not enough information to check signature validity.");
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
    } else if (mClass == QStringLiteral("signOkKeyOk")) {

        QString addr;
        if (!mBlock.signerMailAddresses.isEmpty()) {
            addr = mBlock.signerMailAddresses.first();
        }

        QString name = addr;
        if (name.isEmpty()) {
            name = mBlock.signer;
        }

        if (addr.isEmpty()) {
            text = i18n("Signature is valid.");
        } else {
Laurent Montel's avatar
Laurent Montel committed
480
            text = i18n("Signed by <a href=\"mailto:%1\">%2</a>.", addr, name);
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
        }

    } else {
        // should not happen
        text = i18n("Unknown signature state");
    }

    QString html;
    html += QStringLiteral("<table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><tr>");
    html += QStringLiteral("<td>%1</td>").arg(text);
    if (!mPrinting) {
        html += QStringLiteral("<td align=\"right\">");
        html += QStringLiteral("<a href=\"kmail:showSignatureDetails\">%1</a>").arg(i18n("Show Details"));
        html += QStringLiteral("</td>");
    }
    html += QStringLiteral("</tr></table>");
    return html;
}

static QString beginVerboseSigstatHeader()
{
    return QStringLiteral("<table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><tr><td rowspan=\"2\">");
}

static QString makeShowAuditLogLink(const GpgME::Error &err, const QString &auditLog)
{
    // more or less the same as
    // kleopatra/utils/auditlog.cpp:formatLink(), so any bug fixed here
    // equally applies there:
    if (const unsigned int code = err.code()) {
        if (code == GPG_ERR_NOT_IMPLEMENTED) {
512
            qCDebug(MIMETREEPARSER_LOG) << "not showing link (not implemented)";
513 514
            return QString();
        } else if (code == GPG_ERR_NO_DATA) {
515
            qCDebug(MIMETREEPARSER_LOG) << "not showing link (not available)";
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
            return i18n("No Audit Log available");
        } else {
            return i18n("Error Retrieving Audit Log: %1", QString::fromLocal8Bit(err.asString()));
        }
    }

    if (!auditLog.isEmpty()) {
        QUrl url;
        url.setScheme(QStringLiteral("kmail"));
        url.setPath(QStringLiteral("showAuditLog"));
        QUrlQuery urlquery(url);
        urlquery.addQueryItem(QStringLiteral("log"), auditLog);
        url.setQuery(urlquery);
        return QStringLiteral("<a href=\"%1\">%2</a>").arg(url.toDisplayString(), i18nc("The Audit Log is a detailed error log from the gnupg backend", "Show Audit Log"));
    }

    return QString();
}

static QString endVerboseSigstatHeader(const PartMetaData &pmd)
{
    QString html;
    html += QStringLiteral("</td><td align=\"right\" valign=\"top\" nowrap=\"nowrap\">");
    html += QStringLiteral("<a href=\"kmail:hideSignatureDetails\">");
    html += i18n("Hide Details");
    html += QStringLiteral("</a></td></tr>");
    html += QStringLiteral("<tr><td align=\"right\" valign=\"bottom\" nowrap=\"nowrap\">");
    html += makeShowAuditLogLink(pmd.auditLogError, pmd.auditLog);
    html += QStringLiteral("</td></tr></table>");
    return html;
}

void SignedBlock::internalEnter()
{
    if (mWriter && !entered) {
        entered = true;
        const bool isSMIME = mCryptoProto && (mCryptoProto == Kleo::CryptoBackendFactory::instance()->smime());
        QString signer = mBlock.signer;

        QString htmlStr, simpleHtmlStr;

        if (mBlock.inProgress) {
            mClass = QStringLiteral("signInProgress");
            mWriter->queue(QStringLiteral("<table cellspacing=\"1\" cellpadding=\"1\" class=\"signInProgress\">") +
Laurent Montel's avatar
Laurent Montel committed
560
                           QStringLiteral("<tr class=\"signInProgressH\"><td dir=\"%1\">").arg(dir()) +
561 562 563
                           i18n("Please wait while the signature is being verified...") +
                           QStringLiteral("</td></tr><tr class=\"signInProgressB\"><td>"));
            return;
Sandro Knauß's avatar
Sandro Knauß committed
564
        } else {
565 566 567 568 569 570 571 572 573
            const QStringList &blockAddrs(mBlock.signerMailAddresses);
            // note: At the moment frameColor and showKeyInfos are
            //       used for CMS only but not for PGP signatures
            // pending(khz): Implement usage of these for PGP sigs as well.
            int frameColor = SIG_FRAME_COL_UNDEF;
            bool showKeyInfos;
            bool onlyShowKeyURL = false;
            bool cannotCheckSignature = true;
            QString statusStr = sigStatusToString(mCryptoProto,
Laurent Montel's avatar
Laurent Montel committed
574 575 576 577
                                                  mBlock.status_code,
                                                  mBlock.sigSummary,
                                                  frameColor,
                                                  showKeyInfos);
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
            // if needed fallback to english status text
            // that was reported by the plugin
            if (statusStr.isEmpty()) {
                statusStr = mBlock.status;
            }
            if (mBlock.technicalProblem) {
                frameColor = SIG_FRAME_COL_YELLOW;
            }

            switch (frameColor) {
            case SIG_FRAME_COL_RED:
                cannotCheckSignature = false;
                break;
            case SIG_FRAME_COL_YELLOW:
                cannotCheckSignature = true;
                break;
            case SIG_FRAME_COL_GREEN:
                cannotCheckSignature = false;
                break;
            }

            // compose the string for displaying the key ID
            // either as URL or not linked (for unknown crypto)
            // note: Once we can start PGP key manager programs
            //       from within KMail we could change this and
            //       always show the URL.    (khz, 2002/06/27)
            QString startKeyHREF;
            QString keyWithWithoutURL;
            if (mCryptoProto) {
                startKeyHREF =
                    QStringLiteral("<a href=\"kmail:showCertificate#%1 ### %2 ### %3\">")
                    .arg(mCryptoProto->displayName(),
Laurent Montel's avatar
Laurent Montel committed
610 611
                         mCryptoProto->name(),
                         QString::fromLatin1(mBlock.keyId));
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

                keyWithWithoutURL =
                    QStringLiteral("%1%2</a>").arg(startKeyHREF, QString::fromLatin1(QByteArray(QByteArray("0x") + mBlock.keyId)));
            } else {
                keyWithWithoutURL = QStringLiteral("0x") + QString::fromUtf8(mBlock.keyId);
            }

            // temporary hack: always show key information!
            showKeyInfos = true;

            if (isSMIME && (SIG_FRAME_COL_UNDEF != frameColor)) {

                // new frame settings for CMS:
                // beautify the status string
                if (!statusStr.isEmpty()) {
                    statusStr.prepend(QStringLiteral("<i>"));
                    statusStr.append(QStringLiteral("</i>"));
                }

                // special color handling: S/MIME uses only green/yellow/red.
                switch (frameColor) {
                case SIG_FRAME_COL_RED:
                    mClass = QStringLiteral("signErr");//"signCMSRed";
                    onlyShowKeyURL = true;
                    break;
                case SIG_FRAME_COL_YELLOW:
                    if (mBlock.technicalProblem) {
                        mClass = QStringLiteral("signWarn");
                    } else {
                        mClass = QStringLiteral("signOkKeyBad");    //"signCMSYellow";
                    }
                    break;
                case SIG_FRAME_COL_GREEN:
                    mClass = QStringLiteral("signOkKeyOk");//"signCMSGreen";
                    // extra hint for green case
                    // that email addresses in DN do not match fromAddress
                    QString greenCaseWarning;
                    QString msgFrom(KEmailAddress::extractEmailAddress(mFromAddress));
                    QString certificate;
                    if (mBlock.keyId.isEmpty()) {
                        certificate = i18n("certificate");
                    } else {
                        certificate = startKeyHREF + i18n("certificate") + QStringLiteral("</a>");
                    }
                    if (!blockAddrs.empty()) {
                        if (!blockAddrs.contains(msgFrom, Qt::CaseInsensitive)) {
                            greenCaseWarning =
                                QStringLiteral("<u>") +
                                i18nc("Start of warning message.", "Warning:") +
                                QStringLiteral("</u> ") +
                                i18n("Sender's mail address is not stored in the %1 used for signing.",
                                     certificate) +
                                QStringLiteral("<br />") +
                                i18n("sender: ") +
                                msgFrom +
                                QStringLiteral("<br />") +
                                i18n("stored: ");
                            // We cannot use Qt's join() function here but
                            // have to join the addresses manually to
                            // extract the mail addresses (without '<''>')
                            // before including it into our string:
                            bool bStart = true;
                            for (QStringList::ConstIterator it = blockAddrs.constBegin();
                                    it != blockAddrs.constEnd(); ++it) {
                                if (!bStart) {
                                    greenCaseWarning.append(QStringLiteral(", <br />&nbsp; &nbsp;"));
                                }
                                bStart = false;
                                greenCaseWarning.append(KEmailAddress::extractEmailAddress(*it));
                            }
                        }
                    } else {
                        greenCaseWarning =
                            QStringLiteral("<u>") +
                            i18nc("Start of warning message.", "Warning:") +
                            QStringLiteral("</u> ") +
                            i18n("No mail address is stored in the %1 used for signing, "
Laurent Montel's avatar
Laurent Montel committed
689 690 691
                                 "so we cannot compare it to the sender's address %2.",
                                 certificate,
                                 msgFrom);
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
                    }
                    if (!greenCaseWarning.isEmpty()) {
                        if (!statusStr.isEmpty()) {
                            statusStr.append(QStringLiteral("<br />&nbsp;<br />"));
                        }
                        statusStr.append(greenCaseWarning);
                    }
                    break;
                }

                QString frame = QStringLiteral("<table cellspacing=\"1\" cellpadding=\"1\" class=\"%1\">").arg(mClass) +
                                QStringLiteral("<tr class=\"%1H\"><td dir=\"%2\">").arg(mClass, dir());
                htmlStr += frame + beginVerboseSigstatHeader();
                simpleHtmlStr += frame;
                simpleHtmlStr += simpleHeader();
                if (mBlock.technicalProblem) {
                    htmlStr += mBlock.errorText;
                } else if (showKeyInfos) {
                    if (cannotCheckSignature) {
                        htmlStr += i18n("Not enough information to check "
                                        "signature. %1",
                                        keyWithWithoutURL);
                    } else {

                        if (mBlock.signer.isEmpty()) {
                            signer.clear();
                        } else {
                            if (!blockAddrs.empty()) {
                                const QUrl address = KEmailAddress::encodeMailtoUrl(blockAddrs.first());
Laurent Montel's avatar
Laurent Montel committed
721
                                signer = QStringLiteral("<a href=\"mailto:%1\">%2</a>").arg(QLatin1String(QUrl::toPercentEncoding(address.path())), signer);
722 723 724 725 726 727
                            }
                        }

                        if (mBlock.keyId.isEmpty()) {
                            if (signer.isEmpty() || onlyShowKeyURL) {
                                htmlStr += i18n("Message was signed with unknown key.");
Laurent Montel's avatar
Laurent Montel committed
728
                            } else {
729
                                htmlStr += i18n("Message was signed by %1.", signer);
Laurent Montel's avatar
Laurent Montel committed
730
                            }
731 732 733 734
                        } else {
                            QDateTime created = mBlock.creationTime;
                            if (created.isValid()) {
                                if (signer.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
735
                                    if (onlyShowKeyURL) {
736
                                        htmlStr += i18n("Message was signed with key %1.", keyWithWithoutURL);
Laurent Montel's avatar
Laurent Montel committed
737
                                    } else
738 739 740 741
                                        htmlStr += i18n("Message was signed on %1 with key %2.",
                                                        QLocale::system().toString(created, QLocale::ShortFormat),
                                                        keyWithWithoutURL);
                                } else {
Laurent Montel's avatar
Laurent Montel committed
742
                                    if (onlyShowKeyURL) {
743
                                        htmlStr += i18n("Message was signed with key %1.", keyWithWithoutURL);
Laurent Montel's avatar
Laurent Montel committed
744
                                    } else
745 746 747 748 749 750
                                        htmlStr += i18n("Message was signed by %3 on %1 with key %2",
                                                        QLocale::system().toString(created, QLocale::ShortFormat),
                                                        keyWithWithoutURL,
                                                        signer);
                                }
                            } else {
Laurent Montel's avatar
Laurent Montel committed
751
                                if (signer.isEmpty() || onlyShowKeyURL) {
752
                                    htmlStr += i18n("Message was signed with key %1.", keyWithWithoutURL);
Laurent Montel's avatar
Laurent Montel committed
753
                                } else
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
                                    htmlStr += i18n("Message was signed by %2 with key %1.",
                                                    keyWithWithoutURL,
                                                    signer);
                            }
                        }
                    }
                    htmlStr += QStringLiteral("<br />");
                    if (!statusStr.isEmpty()) {
                        htmlStr += QStringLiteral("&nbsp;<br />");
                        htmlStr += i18n("Status: ");
                        htmlStr += statusStr;
                    }
                } else {
                    htmlStr += statusStr;
                }
                frame = QStringLiteral("</td></tr><tr class=\"%1B\"><td>").arg(mClass);
                htmlStr += endVerboseSigstatHeader(mBlock) + frame;
                simpleHtmlStr += frame;

            } else {

Sandro Knauß's avatar
Sandro Knauß committed
775
                QString content;
776 777 778 779
                // old frame settings for PGP:

                if (mBlock.signer.isEmpty() || mBlock.technicalProblem) {
                    mClass = QStringLiteral("signWarn");
Sandro Knauß's avatar
Sandro Knauß committed
780

781
                    if (mBlock.technicalProblem) {
Sandro Knauß's avatar
Sandro Knauß committed
782
                        content = mBlock.errorText;
783 784 785 786
                    } else {
                        if (!mBlock.keyId.isEmpty()) {
                            QDateTime created = mBlock.creationTime;
                            if (created.isValid())
Sandro Knauß's avatar
Sandro Knauß committed
787 788 789
                                content = i18n("Message was signed on %1 with unknown key %2.",
                                               QLocale::system().toString(created, QLocale::ShortFormat),
                                               keyWithWithoutURL);
790
                            else
Sandro Knauß's avatar
Sandro Knauß committed
791 792
                                content = i18n("Message was signed with unknown key %1.",
                                               keyWithWithoutURL);
793
                        } else {
Sandro Knauß's avatar
Sandro Knauß committed
794
                            content = i18n("Message was signed with unknown key.");
795
                        }
Sandro Knauß's avatar
Sandro Knauß committed
796 797
                        content += QStringLiteral("<br />");
                        content += i18n("The validity of the signature cannot be "
798 799
                                        "verified.");
                        if (!statusStr.isEmpty()) {
Sandro Knauß's avatar
Sandro Knauß committed
800 801 802 803 804
                            content += QStringLiteral("<br />");
                            content += i18n("Status: ");
                            content += QStringLiteral("<i>");
                            content += statusStr;
                            content += QStringLiteral("</i>");
805 806 807 808 809
                        }
                    }
                } else {
                    // HTMLize the signer's user id and create mailto: link
                    signer = MessageCore::StringUtil::quoteHtmlChars(signer, true);
Sandro Knauß's avatar
Sandro Knauß committed
810
                    signer = QStringLiteral("<a href=\"mailto:%1\">%1</a>").arg(signer);
811 812 813 814 815 816 817 818 819

                    if (mBlock.isGoodSignature) {
                        if (mBlock.keyTrust < GpgME::Signature::Marginal) {
                            mClass = QStringLiteral("signOkKeyBad");
                        } else {
                            mClass = QStringLiteral("signOkKeyOk");
                        }

                        if (!mBlock.keyId.isEmpty())
Sandro Knauß's avatar
Sandro Knauß committed
820
                            content = i18n("Message was signed by %2 (Key ID: %1).",
Laurent Montel's avatar
Laurent Montel committed
821 822
                                           keyWithWithoutURL,
                                           signer);
823
                        else {
Sandro Knauß's avatar
Sandro Knauß committed
824
                            content = i18n("Message was signed by %1.", signer);
825
                        }
Sandro Knauß's avatar
Sandro Knauß committed
826
                        content += QStringLiteral("<br />");
827 828 829

                        switch (mBlock.keyTrust) {
                        case GpgME::Signature::Unknown:
Sandro Knauß's avatar
Sandro Knauß committed
830
                            content += i18n("The signature is valid, but the key's "
831 832 833
                                            "validity is unknown.");
                            break;
                        case GpgME::Signature::Marginal:
Sandro Knauß's avatar
Sandro Knauß committed
834
                            content += i18n("The signature is valid and the key is "
835 836 837
                                            "marginally trusted.");
                            break;
                        case GpgME::Signature::Full:
Sandro Knauß's avatar
Sandro Knauß committed
838
                            content += i18n("The signature is valid and the key is "
839 840 841
                                            "fully trusted.");
                            break;
                        case GpgME::Signature::Ultimate:
Sandro Knauß's avatar
Sandro Knauß committed
842
                            content += i18n("The signature is valid and the key is "
843 844 845
                                            "ultimately trusted.");
                            break;
                        default:
Sandro Knauß's avatar
Sandro Knauß committed
846
                            content += i18n("The signature is valid, but the key is "
847 848 849 850 851 852
                                            "untrusted.");
                        }
                    } else {
                        mClass = QStringLiteral("signErr");

                        if (!mBlock.keyId.isEmpty())
Sandro Knauß's avatar
Sandro Knauß committed
853
                            content = i18n("Message was signed by %2 (Key ID: %1).",
Laurent Montel's avatar
Laurent Montel committed
854 855
                                           keyWithWithoutURL,
                                           signer);
856
                        else {
Sandro Knauß's avatar
Sandro Knauß committed
857
                            content = i18n("Message was signed by %1.", signer);
858
                        }
Sandro Knauß's avatar
Sandro Knauß committed
859 860
                        content += QStringLiteral("<br />");
                        content += i18n("Warning: The signature is bad.");
861 862
                    }
                }
Sandro Knauß's avatar
Sandro Knauß committed
863 864 865 866 867 868 869 870 871 872 873
                const QString beginFrame = QStringLiteral("<table cellspacing=\"1\" cellpadding=\"1\" class=\"%1\">").arg(mClass) +
                                           QStringLiteral("<tr class=\"%1H\"><td dir=\"%2\">").arg(mClass, dir());
                const QString endFrame = QStringLiteral("</td></tr><tr class=\"%1B\"><td>").arg(mClass);

                htmlStr += beginFrame + beginVerboseSigstatHeader();
                htmlStr += content;
                htmlStr += endVerboseSigstatHeader(mBlock) + endFrame;

                simpleHtmlStr += beginFrame;
                simpleHtmlStr += simpleHeader();
                simpleHtmlStr += endFrame;
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
            }
        }

        if (mSource->showSignatureDetails()) {
            mWriter->queue(htmlStr);
        }
        mWriter->queue(simpleHtmlStr);
    }
}

void SignedBlock::internalExit()
{
    if (!entered) {
        return;
    }

    mWriter->queue(QStringLiteral("</td></tr>"
Laurent Montel's avatar
Laurent Montel committed
891 892
                                  "<tr class=\"%1H\"><td dir=\"%2\">%3</td></tr>"
                                  "</table>").arg(mClass, dir(), i18n("End of signed message")));
893 894 895
    entered = false;
}

896
AttachmentMarkBlock::AttachmentMarkBlock(MimeTreeParser::HtmlWriter *writer, KMime::Content *node)
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
    : mNode(node)
    , mWriter(writer)
{
    internalEnter();
}

AttachmentMarkBlock::~AttachmentMarkBlock()
{
    internalExit();
}

void AttachmentMarkBlock::internalEnter()
{
    if (mWriter && !entered) {
        const QString index = mNode->index().toString();
        mWriter->queue(QStringLiteral("<a name=\"att%1\"></a>").arg(index));
        mWriter->queue(QStringLiteral("<div id=\"attachmentDiv%1\">\n").arg(index));
        entered = true;
    }
}

void AttachmentMarkBlock::internalExit()
{
    if (!entered) {
        return;
    }

    mWriter->queue(QStringLiteral("</div>"));
    entered = false;
}

928
TextBlock::TextBlock(MimeTreeParser::HtmlWriter *writer, MimeTreeParser::NodeHelper *nodeHelper, KMime::Content *node, bool link)
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
    : mWriter(writer)
    , mNodeHelper(nodeHelper)
    , mNode(node)
    , mLink(link)
{
    internalEnter();
}

TextBlock::~TextBlock()
{
    internalExit();
}

void TextBlock::internalEnter()
{
    if (!mWriter || entered) {
        return;
    }
    entered = true;

    const QString label = MessageCore::StringUtil::quoteHtmlChars(NodeHelper::fileName(mNode), true);

    const QString comment =
        MessageCore::StringUtil::quoteHtmlChars(mNode->contentDescription()->asUnicodeString(), true);

    mWriter->queue(QLatin1String("<table cellspacing=\"1\" class=\"textAtm\">"
955
                                 "<tr class=\"textAtmH\"><td dir=\"") + dir() + QLatin1String("\">"));
956 957 958 959 960 961 962 963 964
    if (!mLink)
        mWriter->queue(QLatin1String("<a href=\"") + mNodeHelper->asHREF(mNode, QStringLiteral("body")) + QLatin1String("\">")
                       + label + QLatin1String("</a>"));
    else {
        mWriter->queue(label);
    }
    if (!comment.isEmpty()) {
        mWriter->queue(QLatin1String("<br/>") + comment);
    }
Laurent Montel's avatar
Laurent Montel committed
965
    mWriter->queue(QStringLiteral("</td></tr><tr class=\"textAtmB\"><td>"));
966 967 968 969 970 971 972 973 974 975 976 977 978
}

void TextBlock::internalExit()
{
    if (!entered) {
        return;
    }

    entered = false;

    mWriter->queue(QStringLiteral("</td></tr></table>"));
}

Laurent Montel's avatar
Laurent Montel committed
979
HTMLWarnBlock::HTMLWarnBlock(HtmlWriter *writer, const QString &msg)
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
    : mWriter(writer)
    , mMsg(msg)
{
    internalEnter();
}

HTMLWarnBlock::~HTMLWarnBlock()
{
    internalExit();
}

void HTMLWarnBlock::internalEnter()
{
    if (!mWriter || entered) {
        return;
    }
    entered = true;

    if (!mMsg.isEmpty()) {
        mWriter->queue(QStringLiteral("<div class=\"htmlWarn\">\n"));
        mWriter->queue(mMsg);
        mWriter->queue(QStringLiteral("</div><br/><br/>"));
    }

    mWriter->queue(QStringLiteral("<div style=\"position: relative\">\n"));
}

void HTMLWarnBlock::internalExit()
{
    if (!entered) {
        return;
    }

    entered = false;

    mWriter->queue(QStringLiteral("</div>\n"));
}

Laurent Montel's avatar
Laurent Montel committed
1018
RootBlock::RootBlock(HtmlWriter *writer)
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
    : HTMLBlock()
    , mWriter(writer)
{
    internalEnter();
}

RootBlock::~RootBlock()
{
    internalExit();
}

void RootBlock::internalEnter()
{
    if (!mWriter || entered) {
        return;
    }
    entered = true;

    mWriter->queue(QStringLiteral("<div style=\"position: relative; word-wrap: break-word\">\n"));
}

void RootBlock::internalExit()
{
    if (!entered) {
        return;
    }

    entered = false;

    mWriter->queue(QStringLiteral("</div>\n"));
}

1051 1052 1053 1054 1055 1056
//------MessagePart-----------------------
MessagePart::MessagePart(ObjectTreeParser *otp,
                         const QString &text)
    : mText(text)
    , mOtp(otp)
    , mSubOtp(Q_NULLPTR)
1057
    , mAttachmentNode(Q_NULLPTR)
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
{

}

MessagePart::~MessagePart()
{
    if (mSubOtp) {
        delete mSubOtp;
        mSubOtp = Q_NULLPTR;
    }
}

PartMetaData *MessagePart::partMetaData()
{
    return &mMetaData;
}

1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
void MessagePart::setAttachmentFlag(KMime::Content *node)
{
    mAttachmentNode = node;
}

bool MessagePart::isAttachment() const
{
    return mAttachmentNode;
}

HTMLBlock::Ptr MessagePart::attachmentBlock() const
{
    if (mOtp->htmlWriter() && isAttachment()) {
        return HTMLBlock::Ptr(new AttachmentMarkBlock(mOtp->htmlWriter(), mAttachmentNode));
    }
    return HTMLBlock::Ptr();
}

1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
QString MessagePart::text() const
{
    return mText;
}

void MessagePart::setText(const QString &text)
{
    mText = text;
}

void MessagePart::html(bool decorate)
{
1105
    MimeTreeParser::HtmlWriter *writer = mOtp->htmlWriter();
1106 1107 1108 1109 1110

    if (!writer) {
        return;
    }

1111 1112
    const HTMLBlock::Ptr aBlock(attachmentBlock());

1113
    const CryptoBlock block(mOtp->htmlWriter(), &mMetaData, mOtp->nodeHelper(), Q_NULLPTR, mOtp->mSource, QString(), Q_NULLPTR);
1114 1115 1116 1117 1118 1119 1120
    writer->queue(mOtp->quotedHTML(text(), decorate));
}

void MessagePart::parseInternal(KMime::Content *node, bool onlyOneMimePart)
{
    mSubOtp = new ObjectTreeParser(mOtp, onlyOneMimePart);
    mSubOtp->setAllowAsync(mOtp->allowAsync());
1121
    mSubMessagePart = mSubOtp->parseObjectTreeInternal(node);
1122 1123
}

1124
void MessagePart::renderInternalHtml(bool decorate) const
1125
{
1126 1127
    if (mSubMessagePart) {
        mSubMessagePart->html(decorate);
1128 1129 1130 1131 1132
    }
}

void MessagePart::copyContentFrom() const
{
1133 1134 1135 1136 1137
    if (mSubMessagePart) {
        mSubMessagePart->copyContentFrom();
        if (mSubOtp) {
            mOtp->copyContentFrom(mSubOtp);
        }
1138 1139 1140 1141 1142
    }
}

QString MessagePart::renderInternalText() const
{
1143
    if (!mSubMessagePart) {
1144 1145
        return QString();
    }
1146 1147 1148 1149 1150 1151 1152 1153
    return mSubMessagePart->text();
}

void MessagePart::fix() const
{
    if (mSubMessagePart) {
        mSubMessagePart->fix();
    }
1154 1155
}

1156
//-----MessagePartList----------------------
Laurent Montel's avatar
Laurent Montel committed
1157
MessagePartList::MessagePartList(ObjectTreeParser *otp)
1158
    : MessagePart(otp, QString())
1159
    , mRoot(false)
1160 1161 1162 1163 1164 1165 1166 1167
{
}

MessagePartList::~MessagePartList()
{

}

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
void MessagePartList::setIsRoot(bool root)
{
    mRoot = root;
}

bool MessagePartList::isRoot() const
{
    return mRoot;
}

HTMLBlock::Ptr MessagePartList::rootBlock() const
{
    if (mOtp->htmlWriter() && isRoot()) {
        return HTMLBlock::Ptr(new RootBlock(mOtp->htmlWriter()));
    }
    return HTMLBlock::Ptr();
}

Laurent Montel's avatar
Laurent Montel committed
1186
void MessagePartList::appendMessagePart(const Interface::MessagePart::Ptr &messagePart)
1187 1188 1189 1190
{
    mBlocks.append(messagePart);
}

Laurent Montel's avatar
Laurent Montel committed
1191
const QVector<Interface::MessagePart::Ptr> &MessagePartList::messageParts() const
1192 1193 1194 1195 1196 1197
{
    return mBlocks;
}

void MessagePartList::html(bool decorate)
{
1198
    const HTMLBlock::Ptr rBlock(rootBlock());
1199 1200 1201 1202 1203 1204 1205
    const HTMLBlock::Ptr aBlock(attachmentBlock());

    htmlInternal(decorate);
}

void MessagePartList::htmlInternal(bool decorate)
{
1206
    foreach (const auto &mp, mBlocks) {
1207 1208 1209 1210 1211 1212 1213
        mp->html(decorate);
    }
}

QString MessagePartList::text() const
{
    QString text;
1214
    foreach (const auto &mp, mBlocks) {
1215 1216 1217 1218 1219
        text += mp->text();
    }
    return text;
}

1220 1221
void MessagePartList::fix() const
{
Laurent Montel's avatar
Laurent Montel committed
1222 1223 1224 1225 1226 1227
    foreach (const auto &mp, mBlocks) {
        const auto m = mp.dynamicCast<MessagePart>();
        if (m) {
            m->fix();
        }
    }
1228 1229 1230 1231
}

void MessagePartList::copyContentFrom() const
{
Laurent Montel's avatar
Laurent Montel committed
1232 1233 1234 1235 1236 1237
    foreach (const auto &mp, mBlocks) {
        const auto m = mp.dynamicCast<MessagePart>();
        if (m) {
            m->copyContentFrom();
        }
    }
1238 1239
}

1240 1241
//-----TextMessageBlock----------------------

1242
TextMessagePart::TextMessagePart(ObjectTreeParser *otp, KMime::Content *node, bool drawFrame, bool showLink, bool decryptMessage, IconType asIcon)
1243
    : MessagePartList(otp)
1244 1245 1246
    , mNode(node)
    , mDrawFrame(drawFrame)
    , mShowLink(showLink)
1247
    , mDecryptMessage(decryptMessage)
1248
    , mAsIcon(asIcon)
1249 1250
{
    if (!mNode) {
1251
        qCWarning(MIMETREEPARSER_LOG) << "not a valid node";
1252 1253 1254
        return;
    }

1255
    if (mAsIcon == MimeTreeParser::NoIcon) {
1256 1257
        parseContent();
    }
1258 1259 1260 1261 1262 1263 1264
}

TextMessagePart::~TextMessagePart()
{

}

1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
bool TextMessagePart::decryptMessage() const
{
    return mDecryptMessage;
}

void TextMessagePart::parseContent()
{
    const auto aCodec = mOtp->codecFor(mNode);
    const QString &fromAddress = NodeHelper::fromAsString(mNode);
    mSignatureState  = KMMsgNotSigned;
    mEncryptionState = KMMsgNotEncrypted;
    const auto blocks = prepareMessageForDecryption(mNode->decodedContent());

    const auto cryptProto = Kleo::CryptoBackendFactory::instance()->openpgp();

    if (!blocks.isEmpty()) {

        /* The (overall) signature/encrypted status is broken
         * if one unencrypted part is at the beginning or in the middle
         * because mailmain adds an unencrypted part at the end this should not break the overall status
         *
         * That's why we first set the tmp status and if one crypted/signed block comes afterwards, than
         * the status is set to unencryped
         */
        bool fullySignedOrEncrypted = true;
        bool fullySignedOrEncryptedTmp = true;

        Q_FOREACH (const auto &block, blocks) {

            if (!fullySignedOrEncryptedTmp) {
                fullySignedOrEncrypted = false;
            }

            if (block.type() == NoPgpBlock && !block.text().trimmed().isEmpty()) {
                fullySignedOrEncryptedTmp = false;
1300
                appendMessagePart(MessagePart::Ptr(new MessagePart(mOtp, aCodec->toUnicode(block.text()))));
1301
            } else if (block.type() == PgpMessageBlock) {
Sandro Knauß's avatar
Sandro Knauß committed
1302
                CryptoMessagePart::Ptr mp(new CryptoMessagePart(mOtp, QString(), cryptProto, fromAddress, Q_NULLPTR));
Sandro Knauß's avatar
Sandro Knauß committed
1303 1304
                mp->setDecryptMessage(decryptMessage());
                mp->setIsEncrypted(true);
1305
                appendMessagePart(mp);
1306 1307 1308 1309 1310 1311 1312 1313
                if (!decryptMessage()) {
                    continue;
                }
                mp->startDecryption(block.text(), aCodec);
                if (mp->partMetaData()->inProgress) {
                    continue;
                }
            } else if (block.type() == ClearsignedBlock) {
Sandro Knauß's avatar
Sandro Knauß committed
1314
                CryptoMessagePart::Ptr mp(new CryptoMessagePart(mOtp, QString(), cryptProto, fromAddress, Q_NULLPTR));
1315
                appendMessagePart(mp);
1316 1317 1318 1319 1320
                mp->startVerification(block.text(), aCodec);
            } else {
                continue;
            }

1321 1322
            const auto mp = messageParts().last().staticCast<MessagePart>();
            const PartMetaData *messagePart(mp->partMetaData());
1323 1324

            if (!messagePart->isEncrypted && !messagePart->isSigned && !block.text().trimmed().isEmpty()) {
1325
                mp->setText(aCodec->toUnicode(block.text()));
1326 1327 1328 1329 1330 1331 1332 1333