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
|
/*
* 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 "imapserverproxy.h"
#include <QDir>
#include <QFile>
#include <KIMAP/KIMAP/LoginJob>
#include <KIMAP/KIMAP/SelectJob>
#include <KIMAP/KIMAP/AppendJob>
#include <KIMAP/KIMAP/CreateJob>
#include <KIMAP/KIMAP/DeleteJob>
#include <KIMAP/KIMAP/StoreJob>
#include <KIMAP/KIMAP/ExpungeJob>
#include <KIMAP/KIMAP/SessionUiProxy>
#include <KCoreAddons/KJob>
#include "log.h"
using namespace Imap;
const char* Imap::Flags::Seen = "\\Seen";
const char* Imap::Flags::Deleted = "\\Deleted";
const char* Imap::Flags::Answered = "\\Answered";
const char* Imap::Flags::Flagged = "\\Flagged";
static KAsync::Job<void> runJob(KJob *job)
{
return KAsync::start<void>([job](KAsync::Future<void> &future) {
QObject::connect(job, &KJob::result, [&future](KJob *job) {
if (job->error()) {
Warning() << "Job failed: " << job->errorString();
future.setError(job->error(), job->errorString());
} else {
future.setFinished();
}
});
job->start();
});
}
class SessionUiProxy : public KIMAP::SessionUiProxy {
public:
bool ignoreSslError( const KSslErrorUiData &errorData ) {
return true;
}
};
ImapServerProxy::ImapServerProxy(const QString &serverUrl, int port) : mSession(new KIMAP::Session(serverUrl, port))
{
mSession->setUiProxy(SessionUiProxy::Ptr(new SessionUiProxy));
mSession->setTimeout(10);
}
KAsync::Job<void> ImapServerProxy::login(const QString &username, const QString &password)
{
auto loginJob = new KIMAP::LoginJob(mSession);
loginJob->setUserName(username);
loginJob->setPassword(password);
loginJob->setAuthenticationMode(KIMAP::LoginJob::Plain);
loginJob->setEncryptionMode(KIMAP::LoginJob::EncryptionMode::AnySslVersion);
return runJob(loginJob);
}
KAsync::Job<void> ImapServerProxy::select(const QString &mailbox)
{
auto select = new KIMAP::SelectJob(mSession);
select->setMailBox(mailbox);
// select->setCondstoreEnabled(serverSupportsCondstore());
return runJob(select);
}
KAsync::Job<void> ImapServerProxy::append(const QString &mailbox, const QByteArray &content, const QList<QByteArray> &flags, const QDateTime &internalDate)
{
auto append = new KIMAP::AppendJob(mSession);
append->setMailBox(mailbox);
append->setContent(content);
append->setFlags(flags);
append->setInternalDate(internalDate);
return runJob(append);
}
KAsync::Job<void> ImapServerProxy::store(const KIMAP::ImapSet &set, const QList<QByteArray> &flags)
{
auto store = new KIMAP::StoreJob(mSession);
store->setUidBased(true);
store->setSequenceSet(set);
store->setFlags(flags);
store->setMode(KIMAP::StoreJob::AppendFlags);
return runJob(store);
}
KAsync::Job<void> ImapServerProxy::create(const QString &mailbox)
{
auto create = new KIMAP::CreateJob(mSession);
create->setMailBox(mailbox);
return runJob(create);
}
KAsync::Job<void> ImapServerProxy::remove(const QString &mailbox)
{
auto job = new KIMAP::DeleteJob(mSession);
job->setMailBox(mailbox);
return runJob(job);
}
KAsync::Job<void> ImapServerProxy::expunge()
{
auto job = new KIMAP::ExpungeJob(mSession);
return runJob(job);
}
KAsync::Job<void> ImapServerProxy::fetch(const KIMAP::ImapSet &set, KIMAP::FetchJob::FetchScope scope, FetchCallback callback)
{
auto fetch = new KIMAP::FetchJob(mSession);
fetch->setSequenceSet(set);
fetch->setUidBased(true);
fetch->setScope(scope);
QObject::connect(fetch, static_cast<void(KIMAP::FetchJob::*)(const QString &,
const QMap<qint64,qint64> &,
const QMap<qint64,qint64> &,
const QMap<qint64,KIMAP::MessageAttribute> &,
const QMap<qint64,KIMAP::MessageFlags> &,
const QMap<qint64,KIMAP::MessagePtr> &)>(&KIMAP::FetchJob::headersReceived),
callback);
return runJob(fetch);
}
KAsync::Job<void> ImapServerProxy::fetch(const KIMAP::ImapSet &set, KIMAP::FetchJob::FetchScope scope, const std::function<void(const QVector<Message> &)> &callback)
{
return fetch(set, scope,
[callback](const QString &mailbox,
const QMap<qint64,qint64> &uids,
const QMap<qint64,qint64> &sizes,
const QMap<qint64,KIMAP::MessageAttribute> &attrs,
const QMap<qint64,KIMAP::MessageFlags> &flags,
const QMap<qint64,KIMAP::MessagePtr> &messages) {
QVector<Message> list;
for (const auto &id : uids.keys()) {
list << Message{uids.value(id), sizes.value(id), attrs.value(id), flags.value(id), messages.value(id)};
}
callback(list);
});
}
KAsync::Job<QList<qint64>> ImapServerProxy::fetchHeaders(const QString &mailbox)
{
auto list = QSharedPointer<QList<qint64>>::create();
KIMAP::FetchJob::FetchScope scope;
scope.parts.clear();
scope.mode = KIMAP::FetchJob::FetchScope::Headers;
//Fetch headers of all messages
return fetch(KIMAP::ImapSet(1, 0), scope,
[list](const QString &mailbox,
const QMap<qint64,qint64> &uids,
const QMap<qint64,qint64> &sizes,
const QMap<qint64,KIMAP::MessageAttribute> &attrs,
const QMap<qint64,KIMAP::MessageFlags> &flags,
const QMap<qint64,KIMAP::MessagePtr> &messages) {
Trace() << "Received " << uids.size() << " headers from " << mailbox;
Trace() << uids.size() << sizes.size() << attrs.size() << flags.size() << messages.size();
//TODO based on the data available here, figure out which messages to actually fetch
//(we only fetched headers and structure so far)
//We could i.e. build chunks to fetch based on the size
for (const auto &id : uids.keys()) {
list->append(uids.value(id));
}
})
.then<QList<qint64>>([list](){
return *list;
});
}
KAsync::Job<void> ImapServerProxy::list(KIMAP::ListJob::Option option, const std::function<void(const QList<KIMAP::MailBoxDescriptor> &mailboxes,const QList<QList<QByteArray> > &flags)> &callback)
{
auto listJob = new KIMAP::ListJob(mSession);
listJob->setOption(option);
// listJob->setQueriedNamespaces(serverNamespaces());
QObject::connect(listJob, &KIMAP::ListJob::mailBoxesReceived,
listJob, callback);
//Figure out the separator character on the first list issued.
if (mSeparatorCharacter.isNull()) {
QObject::connect(listJob, &KIMAP::ListJob::mailBoxesReceived,
listJob, [this](const QList<KIMAP::MailBoxDescriptor> &mailboxes,const QList<QList<QByteArray> > &flags) {
if (!mailboxes.isEmpty() && mSeparatorCharacter.isNull()) {
mSeparatorCharacter = mailboxes.first().separator;
}
}
);
}
return runJob(listJob);
}
KAsync::Job<void> ImapServerProxy::remove(const QString &mailbox, const QByteArray &imapSet)
{
const auto set = KIMAP::ImapSet::fromImapSequenceSet(imapSet);
return select(mailbox).then<void>(store(set, QByteArrayList() << Flags::Deleted)).then<void>(expunge());
}
KAsync::Future<void> ImapServerProxy::fetchFolders(std::function<void(const QVector<Folder> &)> callback)
{
Trace() << "Fetching folders";
auto job = list(KIMAP::ListJob::IncludeUnsubscribed, [callback](const QList<KIMAP::MailBoxDescriptor> &mailboxes, const QList<QList<QByteArray> > &flags){
QVector<Folder> list;
for (const auto &mailbox : mailboxes) {
Trace() << "Found mailbox: " << mailbox.name;
list << Folder{mailbox.name.split(mailbox.separator)};
}
callback(list);
});
return job.exec();
}
KAsync::Future<void> ImapServerProxy::fetchMessages(const Folder &folder, std::function<void(const QVector<Message> &)> callback)
{
Q_ASSERT(!mSeparatorCharacter.isNull());
auto job = select(folder.pathParts.join(mSeparatorCharacter)).then<void, KAsync::Job<void>>([this, callback, folder]() -> KAsync::Job<void> {
return fetchHeaders(folder.pathParts.join(mSeparatorCharacter)).then<void, KAsync::Job<void>, QList<qint64>>([this, callback](const QList<qint64> &uidsToFetch){
Trace() << "Uids to fetch: " << uidsToFetch;
if (uidsToFetch.isEmpty()) {
Trace() << "Nothing to fetch";
callback(QVector<Message>());
return KAsync::null<void>();
}
KIMAP::FetchJob::FetchScope scope;
scope.parts.clear();
scope.mode = KIMAP::FetchJob::FetchScope::Full;
KIMAP::ImapSet set;
set.add(uidsToFetch.toVector());
return fetch(set, scope, callback);
});
});
return job.exec();
}
|