-
Notifications
You must be signed in to change notification settings - Fork 3
/
PMaildServerPop3.cpp
372 lines (339 loc) · 9.09 KB
/
PMaildServerPop3.cpp
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
#include "PMaildServerPop3.hpp"
#include "PMaildCore.hpp"
#include "PMaildMail.hpp"
#include <QStringList>
PMaildServerPop3::PMaildServerPop3(QSslSocket *_sock, PMaildCore *_core, PMaildServer *_server) :
PMaildServerBase(_sock, _core, _server)
{
lid_max = 0;
}
quint64 PMaildServerPop3::getLidFromId(quint64 id) {
if (id2lid.contains(id)) return id2lid.value(id);
lid_max++;
id2lid.insert(id, lid_max);
lid2id.insert(lid_max, id);
return lid_max;
}
void PMaildServerPop3::welcome() {
writeLine("+OK "+core->getHostName()+" POP3 (pmaild v3.0.0)");
}
void PMaildServerPop3::handleUnknownCommand() {
writeLine("-ERR Unknown command");
}
void PMaildServerPop3::parseInBuffer() {
while(true) {
if (status != VALID) break;
int pos = buf_in.indexOf('\n');
if (pos == -1) break; // no full line in buffer
QByteArray cmd = buf_in.left(pos).trimmed();
buf_in.remove(0, pos+1);
if (!cmd.isEmpty()) {
switch(pop3_input_mode) {
case NORMAL:
parseInCommand(cmd);
break;
case AUTH_PLAIN:
pop3_input_mode = NORMAL;
handleAuthPlainComplete(cmd);
break;
}
}
}
// TODO: implement buf_in size check here, fail if buffer too big (protect against infinitely increasing buffer)
}
void PMaildServerPop3::server_cmd_quit(const QList<QByteArray>&) {
if (!user.isNull()) {
for(auto i = toDelete.begin(); i != toDelete.end(); i++) {
PMaildMail mail = user.getEmailById(*i);
if (mail.isNull()) continue;
mail.erase();
}
}
writeLine("+OK "+core->getHostName()+" closing control connexion.");
close();
}
void PMaildServerPop3::server_cmd_stls(const QList<QByteArray>&) {
if (isSsl()) {
writeLine("-ERR STLS only available in PLAIN mode. An encryption mode is already enabled");
return;
}
writeLine("+OK Begin TLS negotiation");
sock->startServerEncryption();
}
void PMaildServerPop3::server_cmd_capa(const QList<QByteArray>&) {
QStringList capa;
capa << "TOP" << "PIPELINING" << "UIDL" << "IMPLEMENTATION pMaild 3.0";
if (isSsl()) {
capa << "USER" << "SASL PLAIN"; // SASL CRAM-MD5 DIGEST-MD5 PLAIN
} else {
capa << "STLS";
}
writeLine("+OK");
for(int i = 0; i < capa.size(); i++) writeLine(capa.at(i).toUtf8());
writeLine(".");
}
void PMaildServerPop3::server_cmd_noop(const QList<QByteArray>&) {
if (user.isNull()) {
writeLine("-ERR need to login first");
return;
}
writeLine("+OK");
}
void PMaildServerPop3::server_cmd_user(const QList<QByteArray>&p) {
if (!user.isNull()) {
writeLine("-ERR already logged in");
return;
}
if (!isSsl()) {
writeLine("-ERR Please STLS before logging in");
return;
}
if (p.size() != 1) {
writeLine("-ERR Syntax: USER <login>");
return;
}
auth_data_username = p.at(0);
writeLine("+OK Provide PASSword now, please");
}
void PMaildServerPop3::server_cmd_pass(const QList<QByteArray>&p) {
if (!user.isNull()) {
writeLine("-ERR already logged in");
return;
}
if (!isSsl()) {
writeLine("-ERR Please STLS before logging in");
return;
}
if (p.size() != 1) {
writeLine("-ERR Syntax: PASS <password>");
return;
}
PMaildUser luser = core->getUser(QString::fromUtf8(auth_data_username));
if (!luser.auth(QString::fromUtf8(p.at(0)))) {
writeLine("-ERR Authorization denied");
return;
}
user = luser;
writeLine("+OK Authorization granted");
}
void PMaildServerPop3::server_cmd_auth(const QList<QByteArray>&p) {
if (!user.isNull()) {
writeLine("-ERR already logged in");
return;
}
if (!isSsl()) {
writeLine("-ERR Please STLS before logging in");
return;
}
if (p.size() == 0) {
writeLine("+OK list of SASL extensions follows");
writeLine("PLAIN");
writeLine(".");
return;
}
if (p.at(0) != "PLAIN") {
writeLine("-ERR Unknown AUTH method");
return;
}
if (p.size() == 1) {
writeLine("+ go ahead");
pop3_input_mode = AUTH_PLAIN;
return;
}
handleAuthPlainComplete(p.at(1));
}
void PMaildServerPop3::handleAuthPlainComplete(const QByteArray &b64) {
QByteArray s = QByteArray::fromBase64(b64);
if (s.at(0) != 0) {
writeLine("-ERR syntax error");
return;
}
QList<QByteArray> r = s.split(0);
if (r.size() != 3) {
writeLine("-ERR syntax error");
return;
}
PMaildUser luser = core->getUser(QString::fromUtf8(r.at(1)));
if (!luser.auth(QString::fromUtf8(r.at(2)))) {
writeLine("-ERR Authorization denied");
return;
}
user = luser;
writeLine("+OK Authorization granted");
}
void PMaildServerPop3::server_cmd_rset(const QList<QByteArray>&) {
if (user.isNull()) {
writeLine("-ERR need to login first");
return;
}
toDelete.clear();
writeLine("+OK");
}
void PMaildServerPop3::server_cmd_stat(const QList<QByteArray>&) {
if (user.isNull()) {
writeLine("-ERR need to login first");
return;
}
qint64 size = 0, count = 0;
QList<PMaildMail> list = user.listEmailsByFolder(0); // Folder 0 = INBOX
for(int i = 0; i < list.size(); i++) {
if (list.at(i).isDeleted()) continue;
// TODO: check local toDelete
size += list.at(i).getSize();
getLidFromId(list.at(i).getId()); // assign a local id
count++;
}
writeLine(QString("+OK %1 %2").arg(count).arg(size).toUtf8());
}
void PMaildServerPop3::server_cmd_list(const QList<QByteArray>&p) {
if (user.isNull()) {
writeLine("-ERR need to login first");
return;
}
if (p.size() > 0) {
quint64 lid = p.at(0).toLongLong();
if (!lid2id.contains(lid)) {
writeLine("-ERR Unknown message");
return;
}
quint64 id = lid2id.value(lid);
if (toDelete.contains(id)) {
writeLine("-ERR Message was deleted, you can still restore it using RSET");
return;
}
PMaildMail mail = user.getEmailById(id);
if (mail.isNull()) {
writeLine("-ERR email has been lost");
return;
}
writeLine(QString("+OK %1 %2").arg(getLidFromId(mail.getId())).arg(mail.getSize()).toUtf8());
return;
}
QList<PMaildMail> list = user.listEmailsByFolder(0); // Folder 0 = INBOX
writeLine("+OK");
for(int i = 0; i < list.size(); i++) {
// TODO: return correct mail id
writeLine(QString("%1 %2").arg(getLidFromId(list.at(i).getId())).arg(list.at(i).getSize()).toUtf8());
}
writeLine(".");
}
void PMaildServerPop3::server_cmd_uidl(const QList<QByteArray>&p) {
if (user.isNull()) {
writeLine("-ERR need to login first");
return;
}
if (p.size() > 0) {
quint64 lid = p.at(0).toLongLong();
if (!lid2id.contains(lid)) {
writeLine("-ERR Unknown message");
return;
}
quint64 id = lid2id.value(lid);
if (toDelete.contains(id)) {
writeLine("-ERR Message was deleted, you can still restore it using RSET");
return;
}
PMaildMail mail = user.getEmailById(id);
if (mail.isNull()) {
writeLine("-ERR email has been lost");
return;
}
writeLine(QString("+OK %1 %2").arg(getLidFromId(mail.getId())).arg(mail.getUniqName()).toUtf8());
return;
}
QList<PMaildMail> list = user.listEmailsByFolder(0); // Folder 0 = INBOX
writeLine("+OK");
for(int i = 0; i < list.size(); i++) {
// TODO: return correct mail id
writeLine(QString("%1 %2").arg(getLidFromId(list.at(i).getId())).arg(list.at(i).getUniqName()).toUtf8());
}
writeLine(".");
}
void PMaildServerPop3::server_cmd_dele(const QList<QByteArray>&p) {
if (user.isNull()) {
writeLine("-ERR need to login first");
return;
}
if (p.size() != 1) {
writeLine("-ERR syntax error. Should use DELE <id>");
return;
}
quint64 lid = p.at(0).toLongLong();
if (!lid2id.contains(lid)) {
writeLine("-ERR Unknown message");
return;
}
quint64 id = lid2id.value(lid);
if (toDelete.contains(id)) {
writeLine("-ERR Message is already marked for deletion");
return;
}
toDelete.insert(id);
writeLine("+OK Nessage marked for deletion");
}
void PMaildServerPop3::server_cmd_retr(const QList<QByteArray>&p) {
if (user.isNull()) {
writeLine("-ERR need to login first");
return;
}
if (p.size() != 1) {
writeLine("-ERR syntax error. Should use RETR <id>");
return;
}
quint64 lid = p.at(0).toLongLong();
if (!lid2id.contains(lid)) {
writeLine("-ERR Unknown message");
return;
}
quint64 id = lid2id.value(lid);
if (toDelete.contains(id)) {
writeLine("-ERR Message is marked for deletion");
return;
}
PMaildMail mail = user.getEmailById(id);
if (mail.isNull()) {
writeLine("-ERR mail is missing from server");
return;
}
QByteArray data = mail.readAll();
data = data.replace("\n.", "\n..");
writeLine("+OK");
write(data);
if (data.right(1) != "\n") write("\r\n");
writeLine(".");
mail.unsetFlag("recent");
}
void PMaildServerPop3::server_cmd_top(const QList<QByteArray>&p) {
if (user.isNull()) {
writeLine("-ERR need to login first");
return;
}
if ((p.size() < 1) || (p.size() > 2)) {
writeLine("-ERR syntax error. Should use TOP <id> [lines]");
return;
}
quint64 lid = p.at(0).toLongLong();
if (!lid2id.contains(lid)) {
writeLine("-ERR Unknown message");
return;
}
quint64 id = lid2id.value(lid);
if (toDelete.contains(id)) {
writeLine("-ERR Message is marked for deletion");
return;
}
PMaildMail mail = user.getEmailById(id);
if (mail.isNull()) {
writeLine("-ERR mail is missing from server");
return;
}
int lines = 0;
if (p.size() == 2) lines = p.at(1).toInt();
QByteArray data = mail.readLines(lines);
data = data.replace("\n.", "\n..");
writeLine("+OK");
write(data);
if (data.right(1) != "\n") write("\r\n");
writeLine(".");
mail.unsetFlag("recent");
}