1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
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
512
513
514
515
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
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
610
611
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
689
690
691
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
|
/*
* Copyright (C) 2015 Christian Mollekopf <chrigi_1@fastmail.fm>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "imapresource.h"
#include "facade.h"
#include "entitybuffer.h"
#include "pipeline.h"
#include "mail_generated.h"
#include "createentity_generated.h"
#include "modifyentity_generated.h"
#include "deleteentity_generated.h"
#include "domainadaptor.h"
#include "resourceconfig.h"
#include "commands.h"
#include "index.h"
#include "log.h"
#include "domain/mail.h"
#include "definitions.h"
#include "facadefactory.h"
#include "indexupdater.h"
#include "inspection.h"
#include "synchronizer.h"
#include "sourcewriteback.h"
#include "entitystore.h"
#include "remoteidmap.h"
#include "query.h"
#include <QDate>
#include <QUuid>
#include <QDir>
#include <QDirIterator>
#include "imapserverproxy.h"
#include "entityreader.h"
//This is the resources entity type, and not the domain type
#define ENTITY_TYPE_MAIL "mail"
#define ENTITY_TYPE_FOLDER "folder"
#undef DEBUG_AREA
#define DEBUG_AREA "resource.imap"
using namespace Imap;
using namespace Sink;
static QHash<QByteArray, QString> specialPurposeFolders()
{
QHash<QByteArray, QString> hash;
//FIXME localize
hash.insert("drafts", "Drafts");
return hash;
}
static QHash<QString, QByteArray> specialPurposeNames()
{
QHash<QString, QByteArray> hash;
for (const auto &value : specialPurposeFolders().values()) {
hash.insert(value.toLower(), specialPurposeFolders().key(value));
}
return hash;
}
//specialpurpose, name
static QHash<QByteArray, QString> sSpecialPurposeFolders = specialPurposeFolders();
//Lowercase-name, specialpurpose
static QHash<QString, QByteArray> sSpecialPurposeNames = specialPurposeNames();
class DraftsProcessor : public Sink::Preprocessor
{
public:
DraftsProcessor() {}
QByteArray ensureDraftsFolder(Sink::Storage::Transaction &transaction)
{
if (mDraftsFolder.isEmpty()) {
//Try to find an existing drafts folder
Sink::EntityReader<ApplicationDomain::Folder> reader(mResourceInstanceIdentifier, mResourceType, transaction);
reader.query(Sink::Query().filter<ApplicationDomain::Folder::SpecialPurpose>(Query::Comparator("drafts", Query::Comparator::Contains)),
[this](const ApplicationDomain::Folder &f) -> bool{
mDraftsFolder = f.identifier();
return false;
});
if (mDraftsFolder.isEmpty()) {
Trace() << "Failed to find a drafts folder, creating a new one";
auto folder = ApplicationDomain::Folder::create(mResourceInstanceIdentifier);
folder.setSpecialPurpose(QByteArrayList() << "drafts");
folder.setName(sSpecialPurposeFolders.value("drafts"));
folder.setIcon("folder");
//This processes the pipeline synchronously
createEntity(folder);
mDraftsFolder = folder.identifier();
}
}
return mDraftsFolder;
}
void newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE
{
if (newEntity.getProperty("draft").toBool()) {
newEntity.setProperty("folder", ensureDraftsFolder(transaction));
}
}
void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity,
Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE
{
if (newEntity.getProperty("draft").toBool()) {
newEntity.setProperty("folder", ensureDraftsFolder(transaction));
}
}
QByteArray mDraftsFolder;
QByteArray mResourceInstanceIdentifier;
QByteArray mResourceType;
};
class MailPropertyExtractor : public Sink::Preprocessor
{
public:
MailPropertyExtractor() {}
void updatedIndexedProperties(Sink::ApplicationDomain::BufferAdaptor &newEntity)
{
const auto mimeMessagePath = newEntity.getProperty("mimeMessage").toString();
Trace() << "Updating indexed properties " << mimeMessagePath;
QFile f(mimeMessagePath);
if (!f.open(QIODevice::ReadOnly)) {
Warning() << "Failed to open the file: " << mimeMessagePath;
return;
}
auto mapped = f.map(0, qMin((qint64)8000, f.size()));
if (!mapped) {
Warning() << "Failed to map file";
return;
}
KMime::Message *msg = new KMime::Message;
msg->setHead(KMime::CRLFtoLF(QByteArray::fromRawData(reinterpret_cast<const char*>(mapped), f.size())));
msg->parse();
newEntity.setProperty("subject", msg->subject(true)->asUnicodeString());
newEntity.setProperty("sender", msg->from(true)->asUnicodeString());
newEntity.setProperty("senderName", msg->from(true)->asUnicodeString());
newEntity.setProperty("date", msg->date(true)->dateTime());
}
void newEntity(const QByteArray &uid, qint64 revision, Sink::ApplicationDomain::BufferAdaptor &newEntity, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE
{
updatedIndexedProperties(newEntity);
}
void modifiedEntity(const QByteArray &uid, qint64 revision, const Sink::ApplicationDomain::BufferAdaptor &oldEntity, Sink::ApplicationDomain::BufferAdaptor &newEntity,
Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE
{
updatedIndexedProperties(newEntity);
}
};
static qint64 uidFromMailRid(const QByteArray &remoteId)
{
auto ridParts = remoteId.split(':');
Q_ASSERT(ridParts.size() == 2);
return ridParts.last().toLongLong();
}
static QByteArray folderIdFromMailRid(const QByteArray &remoteId)
{
auto ridParts = remoteId.split(':');
Q_ASSERT(ridParts.size() == 2);
return ridParts.first();
}
static QByteArray assembleMailRid(const QByteArray &folderLocalId, qint64 imapUid)
{
return folderLocalId + ':' + QByteArray::number(imapUid);
}
static QByteArray assembleMailRid(const ApplicationDomain::Mail &mail, qint64 imapUid)
{
return assembleMailRid(mail.getFolder(), imapUid);
}
class ImapSynchronizer : public Sink::Synchronizer {
public:
ImapSynchronizer(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier)
: Sink::Synchronizer(resourceType, resourceInstanceIdentifier)
{
}
QByteArray createFolder(const QString &folderName, const QString &folderPath, const QString &parentFolderRid, const QByteArray &icon)
{
Trace() << "Creating folder: " << folderName << parentFolderRid;
const auto remoteId = folderPath.toUtf8();
const auto bufferType = ENTITY_TYPE_FOLDER;
Sink::ApplicationDomain::Folder folder;
folder.setProperty(ApplicationDomain::Folder::Name::name, folderName);
folder.setProperty(ApplicationDomain::Folder::Icon::name, icon);
QHash<QByteArray, Query::Comparator> mergeCriteria;
if (sSpecialPurposeNames.contains(folderName.toLower())) {
auto type = sSpecialPurposeNames.value(folderName.toLower());
folder.setProperty(ApplicationDomain::Folder::SpecialPurpose::name, QVariant::fromValue(QByteArrayList() << type));
mergeCriteria.insert(ApplicationDomain::Folder::SpecialPurpose::name, Query::Comparator(type, Query::Comparator::Contains));
}
if (!parentFolderRid.isEmpty()) {
folder.setProperty("parent", syncStore().resolveRemoteId(ENTITY_TYPE_FOLDER, parentFolderRid.toUtf8()));
}
createOrModify(bufferType, remoteId, folder, mergeCriteria);
return remoteId;
}
void synchronizeFolders(const QVector<Folder> &folderList)
{
const QByteArray bufferType = ENTITY_TYPE_FOLDER;
Trace() << "Found folders " << folderList.size();
scanForRemovals(bufferType,
[this, &bufferType](const std::function<void(const QByteArray &)> &callback) {
//TODO Instead of iterating over all entries in the database, which can also pick up the same item multiple times,
//we should rather iterate over an index that contains every uid exactly once. The remoteId index would be such an index,
//but we currently fail to iterate over all entries in an index it seems.
// auto remoteIds = synchronizationTransaction.openDatabase("rid.mapping." + bufferType, std::function<void(const Sink::Storage::Error &)>(), true);
auto mainDatabase = Sink::Storage::mainDatabase(transaction(), bufferType);
mainDatabase.scan("", [&](const QByteArray &key, const QByteArray &) {
callback(key);
return true;
});
},
[&folderList](const QByteArray &remoteId) -> bool {
// folderList.contains(remoteId)
for (const auto folderPath : folderList) {
if (folderPath.path == remoteId) {
return true;
}
}
return false;
}
);
for (const auto &f : folderList) {
createFolder(f.pathParts.last(), f.path, f.parentPath(), "folder");
}
}
void synchronizeMails(const QString &path, const QVector<Message> &messages)
{
auto time = QSharedPointer<QTime>::create();
time->start();
const QByteArray bufferType = ENTITY_TYPE_MAIL;
Trace() << "Importing new mail." << path;
const auto folderLocalId = syncStore().resolveRemoteId(ENTITY_TYPE_FOLDER, path.toUtf8());
int count = 0;
for (const auto &message : messages) {
count++;
const auto remoteId = assembleMailRid(folderLocalId, message.uid);
Trace() << "Found a mail " << remoteId << message.msg->subject(true)->asUnicodeString() << message.flags;
Sink::ApplicationDomain::Mail mail;
mail.setFolder(folderLocalId);
const auto directory = Sink::resourceStorageLocation(mResourceInstanceIdentifier) + "/" + path.toUtf8();
QDir().mkpath(directory);
auto filePath = directory + "/" + QByteArray::number(message.uid);
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly)) {
Warning() << "Failed to open file for writing: " << file.errorString();
}
const auto content = message.msg->encodedContent();
file.write(content);
//Close before calling createOrModify, to ensure the file is available
file.close();
mail.setMimeMessagePath(filePath);
mail.setUnread(!message.flags.contains(Imap::Flags::Seen));
mail.setImportant(message.flags.contains(Imap::Flags::Flagged));
createOrModify(bufferType, remoteId, mail);
}
commitSync();
const auto elapsed = time->elapsed();
Log() << "Synchronized " << count << " mails in " << path << Sink::Log::TraceTime(elapsed) << " " << elapsed/qMax(count, 1) << " [ms/mail]";
}
void synchronizeRemovals(const QString &path, const QSet<qint64> &messages)
{
auto time = QSharedPointer<QTime>::create();
time->start();
const QByteArray bufferType = ENTITY_TYPE_MAIL;
Trace() << "Finding removed mail.";
const auto folderLocalId = syncStore().resolveRemoteId(ENTITY_TYPE_FOLDER, path.toUtf8());
int count = 0;
auto property = Sink::ApplicationDomain::Mail::Folder::name;
scanForRemovals(bufferType,
[&](const std::function<void(const QByteArray &)> &callback) {
Index index(bufferType + ".index." + property, transaction());
index.lookup(folderLocalId, [&](const QByteArray &sinkId) {
callback(sinkId);
},
[&](const Index::Error &error) {
Warning() << "Error in index: " << error.message << property;
});
},
[messages, path, &count](const QByteArray &remoteId) -> bool {
if (messages.contains(uidFromMailRid(remoteId))) {
return true;
}
count++;
return false;
}
);
const auto elapsed = time->elapsed();
Log() << "Removed " << count << " mails in " << path << Sink::Log::TraceTime(elapsed) << " " << elapsed/qMax(count, 1) << " [ms/mail]";
}
KAsync::Job<void> synchronizeWithSource() Q_DECL_OVERRIDE
{
Log() << " Synchronizing";
return KAsync::start<void>([this](KAsync::Future<void> future) {
ImapServerProxy imap(mServer, mPort);
auto loginFuture = imap.login(mUser, mPassword).exec();
loginFuture.waitForFinished();
if (loginFuture.errorCode()) {
Warning() << "Login failed.";
future.setError(1, "Login failed");
return;
} else {
Trace() << "Login was successful";
}
QVector<Folder> folderList;
auto folderFuture = imap.fetchFolders([this, &imap, &folderList](const QVector<Folder> &folders) {
synchronizeFolders(folders);
commit();
commitSync();
folderList << folders;
}).exec();
folderFuture.waitForFinished();
if (folderFuture.errorCode()) {
Warning() << "Folder sync failed.";
future.setError(1, "Folder list sync failed");
return;
} else {
Trace() << "Folder sync was successful";
}
for (const auto &folder : folderList) {
QSet<qint64> uids;
auto messagesFuture = imap.fetchMessages(folder, [this, folder, &uids](const QVector<Message> &messages) {
Trace() << "Synchronizing mails" << folder.normalizedPath();
for (const auto &msg : messages) {
uids << msg.uid;
}
synchronizeMails(folder.normalizedPath(), messages);
commit();
commitSync();
}).exec();
messagesFuture.waitForFinished();
if (messagesFuture.errorCode()) {
future.setError(1, "Folder sync failed: " + folder.normalizedPath());
return;
}
//Remove what there is to remove
synchronizeRemovals(folder.normalizedPath(), uids);
commit();
commitSync();
Trace() << "Folder synchronized: " << folder.normalizedPath();
}
Log() << "Done Synchronizing";
future.setFinished();
});
}
public:
QString mServer;
int mPort;
QString mUser;
QString mPassword;
QByteArray mResourceInstanceIdentifier;
};
class ImapWriteback : public Sink::SourceWriteBack
{
public:
ImapWriteback(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier) : Sink::SourceWriteBack(resourceType, resourceInstanceIdentifier)
{
}
KAsync::Job<QByteArray> replay(const ApplicationDomain::Mail &mail, Sink::Operation operation, const QByteArray &oldRemoteId, const QList<QByteArray> &changedProperties) Q_DECL_OVERRIDE
{
auto imap = QSharedPointer<ImapServerProxy>::create(mServer, mPort);
auto login = imap->login(mUser, mPassword);
if (operation == Sink::Operation_Creation) {
QString mailbox = syncStore().resolveLocalId(ENTITY_TYPE_FOLDER, mail.getFolder());
QByteArray content = KMime::LFtoCRLF(mail.getMimeMessage());
QByteArrayList flags;
if (!mail.getUnread()) {
flags << Imap::Flags::Seen;
}
if (mail.getImportant()) {
flags << Imap::Flags::Flagged;
}
QDateTime internalDate = mail.getDate();
auto rid = QSharedPointer<QByteArray>::create();
return login.then(imap->append(mailbox, content, flags, internalDate))
.then<void, qint64>([imap, mailbox, rid, mail](qint64 uid) {
const auto remoteId = assembleMailRid(mail, uid);
//FIXME this get's called after the final error handler? WTF?
Trace() << "Finished creating a new mail: " << remoteId;
*rid = remoteId;
}).then<QByteArray>([rid, imap]() { //FIXME fix KJob so we don't need this extra clause
return *rid;
});
} else if (operation == Sink::Operation_Removal) {
const auto folderId = folderIdFromMailRid(oldRemoteId);
const QString mailbox = syncStore().resolveLocalId(ENTITY_TYPE_FOLDER, folderId);
const auto uid = uidFromMailRid(oldRemoteId);
Trace() << "Removing a mail: " << oldRemoteId << "in the mailbox: " << mailbox;
KIMAP::ImapSet set;
set.add(uid);
return login.then(imap->remove(mailbox, set))
.then<QByteArray>([imap, oldRemoteId]() {
Trace() << "Finished removing a mail: " << oldRemoteId;
return QByteArray();
});
} else if (operation == Sink::Operation_Modification) {
const QString mailbox = syncStore().resolveLocalId(ENTITY_TYPE_FOLDER, mail.getFolder());
const auto uid = uidFromMailRid(oldRemoteId);
Trace() << "Modifying a mail: " << oldRemoteId << " in the mailbox: " << mailbox << changedProperties;
QByteArrayList flags;
if (!mail.getUnread()) {
flags << Imap::Flags::Seen;
}
if (mail.getImportant()) {
flags << Imap::Flags::Flagged;
}
const bool messageChanged = changedProperties.contains(Sink::ApplicationDomain::Mail::MimeMessage::name);
if (messageChanged) {
QByteArray content = KMime::LFtoCRLF(mail.getMimeMessage());
QDateTime internalDate = mail.getDate();
auto rid = QSharedPointer<QByteArray>::create();
KIMAP::ImapSet set;
set.add(uid);
return login.then(imap->append(mailbox, content, flags, internalDate))
.then<void, qint64>([imap, mailbox, rid, mail](qint64 uid) {
const auto remoteId = assembleMailRid(mail, uid);
Trace() << "Finished creating a modified mail: " << remoteId;
*rid = remoteId;
})
.then(imap->remove(mailbox, set))
.then<QByteArray>([rid, imap]() {
return *rid;
});
} else {
KIMAP::ImapSet set;
set.add(uid);
return login.then(imap->select(mailbox))
.then(imap->storeFlags(set, flags))
.then<void, qint64>([imap, mailbox](qint64 uid) {
Trace() << "Finished modifying mail: " << uid;
})
.then<QByteArray>([oldRemoteId, imap]() {
return oldRemoteId;
});
}
}
return KAsync::null<QByteArray>();
}
KAsync::Job<QByteArray> replay(const ApplicationDomain::Folder &folder, Sink::Operation operation, const QByteArray &oldRemoteId, const QList<QByteArray> &changedProperties) Q_DECL_OVERRIDE
{
auto imap = QSharedPointer<ImapServerProxy>::create(mServer, mPort);
auto login = imap->login(mUser, mPassword);
if (operation == Sink::Operation_Creation) {
QString parentFolder;
if (!folder.getParent().isEmpty()) {
parentFolder = syncStore().resolveLocalId(ENTITY_TYPE_FOLDER, folder.getParent());
}
Trace() << "Creating a new folder: " << parentFolder << folder.getName();
auto rid = QSharedPointer<QByteArray>::create();
auto createFolder = login.then<QString>(imap->createSubfolder(parentFolder, folder.getName()))
.then<void, QString>([imap, rid](const QString &createdFolder) {
Trace() << "Finished creating a new folder: " << createdFolder;
*rid = createdFolder.toUtf8();
});
if (folder.getSpecialPurpose().isEmpty()) {
return createFolder
.then<QByteArray>([rid](){
return *rid;
});
} else { //We try to merge special purpose folders first
auto specialPurposeFolders = QSharedPointer<QHash<QByteArray, QString>>::create();
auto mergeJob = imap->login(mUser, mPassword)
.then<void>(imap->fetchFolders([=](const QVector<Imap::Folder> &folders) {
for (const auto &f : folders) {
if (sSpecialPurposeNames.contains(f.pathParts.last().toLower())) {
specialPurposeFolders->insert(sSpecialPurposeNames.value(f.pathParts.last().toLower()), f.path);
};
}
}))
.then<void, KAsync::Job<void>>([specialPurposeFolders, folder, imap, parentFolder, rid]() -> KAsync::Job<void> {
for (const auto &purpose : folder.getSpecialPurpose()) {
if (specialPurposeFolders->contains(purpose)) {
auto f = specialPurposeFolders->value(purpose);
Trace() << "Merging specialpurpose folder with: " << f << " with purpose: " << purpose;
*rid = f.toUtf8();
return KAsync::null<void>();
}
Trace() << "No match found for merging, creating a new folder";
return imap->createSubfolder(parentFolder, folder.getName())
.then<void, QString>([imap, rid](const QString &createdFolder) {
Trace() << "Finished creating a new folder: " << createdFolder;
*rid = createdFolder.toUtf8();
});
})
.then<QByteArray>([rid](){
return *rid;
});
return mergeJob;
}
} else if (operation == Sink::Operation_Removal) {
Trace() << "Removing a folder: " << oldRemoteId;
return login.then<void>(imap->remove(oldRemoteId))
.then<QByteArray>([oldRemoteId, imap]() {
Trace() << "Finished removing a folder: " << oldRemoteId;
return QByteArray();
});
} else if (operation == Sink::Operation_Modification) {
Trace() << "Renaming a folder: " << oldRemoteId << folder.getName();
auto rid = QSharedPointer<QByteArray>::create();
return login.then<QString>(imap->renameSubfolder(oldRemoteId, folder.getName()))
.then<void, QString>([imap, rid](const QString &createdFolder) {
Trace() << "Finished renaming a folder: " << createdFolder;
*rid = createdFolder.toUtf8();
})
.then<QByteArray>([rid](){
return *rid;
});
}
return KAsync::null<QByteArray>();
}
public:
QString mServer;
int mPort;
QString mUser;
QString mPassword;
QByteArray mResourceInstanceIdentifier;
};
ImapResource::ImapResource(const QByteArray &instanceIdentifier, const QSharedPointer<Sink::Pipeline> &pipeline)
: Sink::GenericResource(PLUGIN_NAME, instanceIdentifier, pipeline)
{
auto config = ResourceConfig::getConfiguration(instanceIdentifier);
mServer = config.value("server").toString();
mPort = config.value("port").toInt();
mUser = config.value("user").toString();
mPassword = config.value("password").toString();
auto synchronizer = QSharedPointer<ImapSynchronizer>::create(PLUGIN_NAME, instanceIdentifier);
synchronizer->mServer = mServer;
synchronizer->mPort = mPort;
synchronizer->mUser = mUser;
synchronizer->mPassword = mPassword;
synchronizer->mResourceInstanceIdentifier = instanceIdentifier;
setupSynchronizer(synchronizer);
auto changereplay = QSharedPointer<ImapWriteback>::create(PLUGIN_NAME, instanceIdentifier);
changereplay->mServer = mServer;
changereplay->mPort = mPort;
changereplay->mUser = mUser;
changereplay->mPassword = mPassword;
setupChangereplay(changereplay);
setupPreprocessors(ENTITY_TYPE_MAIL, QVector<Sink::Preprocessor*>() << new DraftsProcessor << new MailPropertyExtractor << new DefaultIndexUpdater<Sink::ApplicationDomain::Mail>);
setupPreprocessors(ENTITY_TYPE_FOLDER, QVector<Sink::Preprocessor*>() << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder>);
}
void ImapResource::removeFromDisk(const QByteArray &instanceIdentifier)
{
GenericResource::removeFromDisk(instanceIdentifier);
Sink::Storage(Sink::storageLocation(), instanceIdentifier + ".synchronization", Sink::Storage::ReadWrite).removeFromDisk();
}
KAsync::Job<void> ImapResource::inspect(int inspectionType, const QByteArray &inspectionId, const QByteArray &domainType, const QByteArray &entityId, const QByteArray &property, const QVariant &expectedValue)
{
auto synchronizationStore = QSharedPointer<Sink::Storage>::create(Sink::storageLocation(), mResourceInstanceIdentifier + ".synchronization", Sink::Storage::ReadOnly);
auto synchronizationTransaction = synchronizationStore->createTransaction(Sink::Storage::ReadOnly);
auto mainStore = QSharedPointer<Sink::Storage>::create(Sink::storageLocation(), mResourceInstanceIdentifier, Sink::Storage::ReadOnly);
auto transaction = mainStore->createTransaction(Sink::Storage::ReadOnly);
auto entityStore = QSharedPointer<Sink::EntityStore>::create(mResourceType, mResourceInstanceIdentifier, transaction);
auto syncStore = QSharedPointer<Sink::RemoteIdMap>::create(synchronizationTransaction);
Trace() << "Inspecting " << inspectionType << domainType << entityId << property << expectedValue;
if (domainType == ENTITY_TYPE_MAIL) {
const auto mail = entityStore->read<Sink::ApplicationDomain::Mail>(entityId);
const auto folder = entityStore->read<Sink::ApplicationDomain::Folder>(mail.getFolder());
const auto folderRemoteId = syncStore->resolveLocalId(ENTITY_TYPE_FOLDER, mail.getFolder());
const auto mailRemoteId = syncStore->resolveLocalId(ENTITY_TYPE_MAIL, mail.identifier());
if (mailRemoteId.isEmpty() || folderRemoteId.isEmpty()) {
Warning() << "Missing remote id for folder or mail. " << mailRemoteId << folderRemoteId;
return KAsync::error<void>();
}
const auto uid = uidFromMailRid(mailRemoteId);
Trace() << "Mail remote id: " << folderRemoteId << mailRemoteId << mail.identifier() << folder.identifier();
KIMAP::ImapSet set;
set.add(uid);
if (set.isEmpty()) {
return KAsync::error<void>(1, "Couldn't determine uid of mail.");
}
KIMAP::FetchJob::FetchScope scope;
scope.mode = KIMAP::FetchJob::FetchScope::Full;
auto imap = QSharedPointer<ImapServerProxy>::create(mServer, mPort);
auto messageByUid = QSharedPointer<QHash<qint64, Imap::Message>>::create();
auto inspectionJob = imap->login(mUser, mPassword)
.then<void>(imap->select(folderRemoteId))
.then<void>(imap->fetch(set, scope, [imap, messageByUid](const QVector<Imap::Message> &messages) {
for (const auto &m : messages) {
messageByUid->insert(m.uid, m);
}
}));
if (inspectionType == Sink::ResourceControl::Inspection::PropertyInspectionType) {
if (property == "unread") {
return inspectionJob.then<void, KAsync::Job<void>>([=]() {
auto msg = messageByUid->value(uid);
if (expectedValue.toBool() && msg.flags.contains(Imap::Flags::Seen)) {
return KAsync::error<void>(1, "Expected unread but couldn't find it.");
}
if (!expectedValue.toBool() && !msg.flags.contains(Imap::Flags::Seen)) {
return KAsync::error<void>(1, "Expected read but couldn't find it.");
}
return KAsync::null<void>();
});
}
if (property == "subject") {
return inspectionJob.then<void, KAsync::Job<void>>([=]() {
auto msg = messageByUid->value(uid);
if (msg.msg->subject(true)->asUnicodeString() != expectedValue.toString()) {
return KAsync::error<void>(1, "Subject not as expected: " + msg.msg->subject(true)->asUnicodeString());
}
return KAsync::null<void>();
});
}
}
if (inspectionType == Sink::ResourceControl::Inspection::ExistenceInspectionType) {
return inspectionJob.then<void, KAsync::Job<void>>([=]() {
if (!messageByUid->contains(uid)) {
Warning() << "Existing messages are: " << messageByUid->keys();
Warning() << "We're looking for: " << uid;
return KAsync::error<void>(1, "Couldn't find message: " + mailRemoteId);
}
return KAsync::null<void>();
});
}
}
if (domainType == ENTITY_TYPE_FOLDER) {
const auto remoteId = syncStore->resolveLocalId(ENTITY_TYPE_FOLDER, entityId);
const auto folder = entityStore->read<Sink::ApplicationDomain::Folder>(entityId);
if (inspectionType == Sink::ResourceControl::Inspection::CacheIntegrityInspectionType) {
Warning() << "Inspecting cache integrity" << remoteId;
int expectedCount = 0;
Index index("mail.index.folder", transaction);
index.lookup(entityId, [&](const QByteArray &sinkId) {
expectedCount++;
},
[&](const Index::Error &error) {
Warning() << "Error in index: " << error.message << property;
});
auto set = KIMAP::ImapSet::fromImapSequenceSet("1:*");
KIMAP::FetchJob::FetchScope scope;
scope.mode = KIMAP::FetchJob::FetchScope::Headers;
auto imap = QSharedPointer<ImapServerProxy>::create(mServer, mPort);
auto messageByUid = QSharedPointer<QHash<qint64, Imap::Message>>::create();
auto inspectionJob = imap->login(mUser, mPassword)
.then<void>(imap->select(remoteId))
.then<void>(imap->fetch(set, scope, [=](const QVector<Imap::Message> &messages) {
for (const auto &m : messages) {
messageByUid->insert(m.uid, m);
}
}))
.then<void, KAsync::Job<void>>([imap, messageByUid, expectedCount]() {
if (messageByUid->size() != expectedCount) {
return KAsync::error<void>(1, QString("Wrong number of messages on the server; found %1 instead of %2.").arg(messageByUid->size()).arg(expectedCount));
}
return KAsync::null<void>();
});
return inspectionJob;
}
if (inspectionType == Sink::ResourceControl::Inspection::ExistenceInspectionType) {
auto folderByPath = QSharedPointer<QSet<QString>>::create();
auto folderByName = QSharedPointer<QSet<QString>>::create();
auto imap = QSharedPointer<ImapServerProxy>::create(mServer, mPort);
auto inspectionJob = imap->login(mUser, mPassword)
.then<void>(imap->fetchFolders([=](const QVector<Imap::Folder> &folders) {
for (const auto &f : folders) {
*folderByPath << f.normalizedPath();
*folderByName << f.pathParts.last();
}
}))
.then<void, KAsync::Job<void>>([folderByName, folderByPath, folder, remoteId, imap]() {
if (!folderByName->contains(folder.getName())) {
Warning() << "Existing folders are: " << *folderByPath;
Warning() << "We're looking for: " << folder.getName();
return KAsync::error<void>(1, "Wrong folder name: " + remoteId);
}
return KAsync::null<void>();
});
return inspectionJob;
}
}
return KAsync::null<void>();
}
ImapResourceFactory::ImapResourceFactory(QObject *parent)
: Sink::ResourceFactory(parent)
{
}
Sink::Resource *ImapResourceFactory::createResource(const QByteArray &instanceIdentifier)
{
return new ImapResource(instanceIdentifier);
}
void ImapResourceFactory::registerFacades(Sink::FacadeFactory &factory)
{
factory.registerFacade<Sink::ApplicationDomain::Mail, ImapResourceMailFacade>(PLUGIN_NAME);
factory.registerFacade<Sink::ApplicationDomain::Folder, ImapResourceFolderFacade>(PLUGIN_NAME);
}
void ImapResourceFactory::registerAdaptorFactories(Sink::AdaptorFactoryRegistry ®istry)
{
registry.registerFactory<Sink::ApplicationDomain::Mail, ImapMailAdaptorFactory>(PLUGIN_NAME);
registry.registerFactory<Sink::ApplicationDomain::Folder, ImapFolderAdaptorFactory>(PLUGIN_NAME);
}
|