Commit 1c31d1e1 authored by Daniel Vrátil's avatar Daniel Vrátil 🤖
Browse files

NodeHelper::fromAsString: make it work with extra content nodes

Currently NodeHelper::fromAsString() can only return the sender only if the
passed KMime::Content is directly part of the original KMime::Message node
tree. This patch makes NodeHelper::fromAsString() non-static, so that it
can also perform lookups for extra KMime::Content parts.

Differential Revision: https://phabricator.kde.org/D3387
parent 33328f72
......@@ -222,5 +222,54 @@ void NodeHelperTest::testCreateTempDir()
QVERIFY(!QDir(path).exists());
}
void NodeHelperTest::testFromAsString()
{
const QString tlSender = QStringLiteral("Foo <foo@example.com>");
const QString encSender = QStringLiteral("Bar <bar@example.com>");
NodeHelper helper;
// msg (KMime::Message)
// |- subNode
// |- encNode (KMime::Message)
// |- encSubNode
//
// subNode
// |- subExtra
//
// encSubNode
// |- encSubExtra
KMime::Message msg;
msg.from(true)->fromUnicodeString(tlSender, "UTF-8");
auto node = msg.topLevel();
auto subNode = new KMime::Content();
auto subExtra = new KMime::Content();
// Encapsulated message
KMime::Message *encMsg = new KMime::Message;
encMsg->from(true)->fromUnicodeString(encSender, "UTF-8");
auto encNode = encMsg->topLevel();
auto encSubNode = new KMime::Content();
auto encSubExtra = new KMime::Content();
node->addContent(subNode);
node->addContent(encMsg);
encNode->addContent(encSubNode);
helper.attachExtraContent(subNode, subExtra);
helper.attachExtraContent(encSubNode, encSubExtra);
QCOMPARE(helper.fromAsString(node), tlSender);
QCOMPARE(helper.fromAsString(subNode), tlSender);
QCOMPARE(helper.fromAsString(subExtra), tlSender);
QEXPECT_FAIL("", "Returning sender of encapsulated message is not yet implemented", Continue);
QCOMPARE(helper.fromAsString(encNode), encSender);
QEXPECT_FAIL("", "Returning sender of encapsulated message is not yet implemented", Continue);
QCOMPARE(helper.fromAsString(encSubNode), encSender);
QEXPECT_FAIL("", "Returning sender of encapsulated message is not yet implemented", Continue);
QCOMPARE(helper.fromAsString(encSubExtra), encSender);
}
QTEST_GUILESS_MAIN(NodeHelperTest)
......@@ -38,6 +38,7 @@ private Q_SLOTS:
void testLocalFiles();
void testHREF();
void testCreateTempDir();
void testFromAsString();
};
}
......
......@@ -76,7 +76,7 @@ Interface::MessagePart::Ptr ApplicationPGPEncryptedBodyPartFormatter::process(In
EncryptedMessagePart::Ptr mp(new EncryptedMessagePart(part.objectTreeParser(),
data->decodedText(), QGpgME::openpgp(),
NodeHelper::fromAsString(data), node));
part.nodeHelper()->fromAsString(data), node));
mp->setIsEncrypted(true);
mp->setDecryptMessage(part.source()->decryptMessage());
PartMetaData *messagePart(mp->partMetaData());
......
......@@ -99,7 +99,7 @@ Interface::MessagePart::Ptr ApplicationPkcs7MimeBodyPartFormatter::process(Inter
auto _mp = EncryptedMessagePart::Ptr(new EncryptedMessagePart(part.objectTreeParser(),
node->decodedText(), smimeCrypto,
NodeHelper::fromAsString(node), node));
part.nodeHelper()->fromAsString(node), node));
mp = _mp;
_mp->setIsEncrypted(true);
_mp->setDecryptMessage(part.source()->decryptMessage());
......@@ -150,7 +150,7 @@ Interface::MessagePart::Ptr ApplicationPkcs7MimeBodyPartFormatter::process(Inter
const QByteArray signaturetext = signTestNode->decodedContent();
auto _mp = SignedMessagePart::Ptr(new SignedMessagePart(part.objectTreeParser(),
aCodec->toUnicode(signaturetext), smimeCrypto,
NodeHelper::fromAsString(node), signTestNode));
part.nodeHelper()->fromAsString(node), signTestNode));
mp = _mp;
//mp->setDecryptMessage(part.source()->decryptMessage());
PartMetaData *messagePart(mp->partMetaData());
......
......@@ -89,7 +89,7 @@ Interface::MessagePart::Ptr MultiPartEncryptedBodyPartFormatter::process(Interfa
EncryptedMessagePart::Ptr mp(new EncryptedMessagePart(part.objectTreeParser(),
data->decodedText(), useThisCryptProto,
NodeHelper::fromAsString(data), node));
part.nodeHelper()->fromAsString(data), node));
mp->setIsEncrypted(true);
mp->setDecryptMessage(part.source()->decryptMessage());
PartMetaData *messagePart(mp->partMetaData());
......
......@@ -101,7 +101,7 @@ Interface::MessagePart::Ptr MultiPartSignedBodyPartFormatter::process(Interface:
SignedMessagePart::Ptr mp(new SignedMessagePart(part.objectTreeParser(),
aCodec->toUnicode(cleartext), protocol,
NodeHelper::fromAsString(node), signature));
part.nodeHelper()->fromAsString(node), signature));
PartMetaData *messagePart(mp->partMetaData());
if (protocol) {
......
......@@ -245,7 +245,7 @@ bool TextMessagePart::decryptMessage() const
void TextMessagePart::parseContent()
{
const auto aCodec = mOtp->codecFor(mNode);
const QString &fromAddress = NodeHelper::fromAsString(mNode);
const QString &fromAddress = mOtp->nodeHelper()->fromAsString(mNode);
mSignatureState = KMMsgNotSigned;
mEncryptionState = KMMsgNotEncrypted;
const auto blocks = prepareMessageForDecryption(mNode->decodedContent());
......
......@@ -793,12 +793,20 @@ QStringList NodeHelper::supportedEncodings(bool usAscii)
return encodings;
}
QString NodeHelper::fromAsString(KMime::Content *node)
QString NodeHelper::fromAsString(KMime::Content *node) const
{
KMime::Message *topLevel = dynamic_cast<KMime::Message *>(node->topLevel());
if (topLevel) {
if (auto topLevel = dynamic_cast<KMime::Message *>(node->topLevel())) {
return topLevel->from()->asUnicodeString();
} else {
auto realNode = std::find_if(mExtraContents.cbegin(), mExtraContents.cend(),
[node](const QList<KMime::Content*> &nodes) {
return nodes.contains(node);
});
if (realNode != mExtraContents.cend()) {
return fromAsString(realNode.key());
}
}
return QString();
}
......
......@@ -210,7 +210,7 @@ public:
*/
static QStringList supportedEncodings(bool usAscii); //TODO(Andras) move to a utility class?
static QString fromAsString(KMime::Content *node);
QString fromAsString(KMime::Content *node) const;
KMime::Content *decryptedNodeForContent(KMime::Content *content) const;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment