diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-24 12:32:55 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-24 12:32:55 +0200 |
commit | 53df06854ffdcf164c195884783cadf8f53fea2c (patch) | |
tree | 8d03cdb72fe3de590cd2f1e5d1138004dce46754 | |
parent | 3e443520eaa3559c9f74134007f407b569f22443 (diff) | |
download | sink-53df06854ffdcf164c195884783cadf8f53fea2c.tar.gz sink-53df06854ffdcf164c195884783cadf8f53fea2c.zip |
Validate subject and mime message
-rw-r--r-- | examples/imapresource/imapresource.cpp | 53 | ||||
-rw-r--r-- | tests/mailtest.cpp | 6 |
2 files changed, 54 insertions, 5 deletions
diff --git a/examples/imapresource/imapresource.cpp b/examples/imapresource/imapresource.cpp index 6adfeb3..61031d8 100644 --- a/examples/imapresource/imapresource.cpp +++ b/examples/imapresource/imapresource.cpp | |||
@@ -51,6 +51,53 @@ | |||
51 | 51 | ||
52 | using namespace Imap; | 52 | using namespace Imap; |
53 | 53 | ||
54 | class FolderUpdater : public Sink::Preprocessor | ||
55 | { | ||
56 | public: | ||
57 | FolderUpdater(const QByteArray &drafts) {} | ||
58 | |||
59 | void updatedIndexedProperties(Sink::ApplicationDomain::BufferAdaptor &newEntity) | ||
60 | { | ||
61 | const auto mimeMessagePath = newEntity.getProperty("mimeMessage").toString(); | ||
62 | QFile f(mimeMessagePath); | ||
63 | if (!f.open(QIODevice::ReadOnly)) { | ||
64 | Warning() << "Failed to open the file: " << mimeMessagePath; | ||
65 | return; | ||
66 | } | ||
67 | auto mapped = f.map(0, qMin((qint64)8000, f.size())); | ||
68 | if (!mapped) { | ||
69 | Warning() << "Failed to map file"; | ||
70 | return; | ||
71 | } | ||
72 | |||
73 | KMime::Message *msg = new KMime::Message; | ||
74 | msg->setHead(KMime::CRLFtoLF(QByteArray::fromRawData(reinterpret_cast<const char*>(mapped), f.size()))); | ||
75 | msg->parse(); | ||
76 | |||
77 | newEntity.setProperty("subject", msg->subject(true)->asUnicodeString()); | ||
78 | newEntity.setProperty("sender", msg->from(true)->asUnicodeString()); | ||
79 | newEntity.setProperty("senderName", msg->from(true)->asUnicodeString()); | ||
80 | newEntity.setProperty("date", msg->date(true)->dateTime()); | ||
81 | } | ||
82 | |||
83 | void newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
84 | { | ||
85 | updatedIndexedProperties(newEntity); | ||
86 | } | ||
87 | |||
88 | void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity, | ||
89 | Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
90 | { | ||
91 | updatedIndexedProperties(newEntity); | ||
92 | } | ||
93 | |||
94 | void deletedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
95 | { | ||
96 | } | ||
97 | |||
98 | QSharedPointer<ImapFolderAdaptorFactory> mFolderAdaptorFactory; | ||
99 | }; | ||
100 | |||
54 | ImapResource::ImapResource(const QByteArray &instanceIdentifier, const QSharedPointer<Sink::Pipeline> &pipeline) | 101 | ImapResource::ImapResource(const QByteArray &instanceIdentifier, const QSharedPointer<Sink::Pipeline> &pipeline) |
55 | : Sink::GenericResource(instanceIdentifier, pipeline), | 102 | : Sink::GenericResource(instanceIdentifier, pipeline), |
56 | mMailAdaptorFactory(QSharedPointer<ImapMailAdaptorFactory>::create()), | 103 | mMailAdaptorFactory(QSharedPointer<ImapMailAdaptorFactory>::create()), |
@@ -62,9 +109,11 @@ ImapResource::ImapResource(const QByteArray &instanceIdentifier, const QSharedPo | |||
62 | mUser = config.value("user").toString(); | 109 | mUser = config.value("user").toString(); |
63 | mPassword = config.value("password").toString(); | 110 | mPassword = config.value("password").toString(); |
64 | 111 | ||
65 | // auto folderUpdater = new FolderUpdater(QByteArray()); | 112 | auto folderUpdater = new FolderUpdater(QByteArray()); |
113 | folderUpdater->mFolderAdaptorFactory = mFolderAdaptorFactory; | ||
114 | |||
66 | addType(ENTITY_TYPE_MAIL, mMailAdaptorFactory, | 115 | addType(ENTITY_TYPE_MAIL, mMailAdaptorFactory, |
67 | QVector<Sink::Preprocessor*>() << new DefaultIndexUpdater<Sink::ApplicationDomain::Mail>); | 116 | QVector<Sink::Preprocessor*>() << folderUpdater << new DefaultIndexUpdater<Sink::ApplicationDomain::Mail>); |
68 | addType(ENTITY_TYPE_FOLDER, mFolderAdaptorFactory, | 117 | addType(ENTITY_TYPE_FOLDER, mFolderAdaptorFactory, |
69 | QVector<Sink::Preprocessor*>() << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder>); | 118 | QVector<Sink::Preprocessor*>() << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder>); |
70 | } | 119 | } |
diff --git a/tests/mailtest.cpp b/tests/mailtest.cpp index c3b33bc..3ca8eaa 100644 --- a/tests/mailtest.cpp +++ b/tests/mailtest.cpp | |||
@@ -153,13 +153,13 @@ void MailTest::testCreateModifyDeleteMail() | |||
153 | VERIFYEXEC(Store::create(mail)); | 153 | VERIFYEXEC(Store::create(mail)); |
154 | VERIFYEXEC(ResourceControl::flushMessageQueue(query.resources)); | 154 | VERIFYEXEC(ResourceControl::flushMessageQueue(query.resources)); |
155 | { | 155 | { |
156 | auto job = Store::fetchAll<Mail>(Query::RequestedProperties(QByteArrayList() << Mail::Folder::name << Mail::Subject::name)) | 156 | auto job = Store::fetchAll<Mail>(Query::RequestedProperties(QByteArrayList() << Mail::Folder::name << Mail::Subject::name << Mail::MimeMessage::name)) |
157 | .then<void, QList<Mail::Ptr>>([=](const QList<Mail::Ptr> &mails) { | 157 | .then<void, QList<Mail::Ptr>>([=](const QList<Mail::Ptr> &mails) { |
158 | QCOMPARE(mails.size(), 1); | 158 | QCOMPARE(mails.size(), 1); |
159 | auto mail = *mails.first(); | 159 | auto mail = *mails.first(); |
160 | // QCOMPARE(mail.getSubject(), subject); | 160 | QCOMPARE(mail.getSubject(), subject); |
161 | QCOMPARE(mail.getFolder(), folder.identifier()); | 161 | QCOMPARE(mail.getFolder(), folder.identifier()); |
162 | // TODO test access to mime message | 162 | QVERIFY(QFile(mail.getMimeMessagePath()).exists()); |
163 | 163 | ||
164 | // return Store::remove(*mail) | 164 | // return Store::remove(*mail) |
165 | // .then(ResourceControl::flushReplayQueue(query.resources)) // The change needs to be replayed already | 165 | // .then(ResourceControl::flushReplayQueue(query.resources)) // The change needs to be replayed already |