summaryrefslogtreecommitdiff
path: root/noncore/unsupported/mail2/libmail/imapresponse.cpp
blob: ce5b18b01a2c426c044b7c20452fc04c4f58484f (plain)
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
#include "imapresponse.h"


IMAPResponseParser::IMAPResponseParser()
{
}

void IMAPResponseParser::parse ( const QString &_data )
{	
	QString data = _data;

	int pos = 0;
	int len = data. length ( );
	
	
	while ( pos < len ) {
		pos = data. find ( QRegExp ( "[^\\s]" ), pos );
		
		if (( pos < 0 ) ||  ( pos >= len ))
			break;
	
		switch ( data [pos]. latin1 ( )) {
			case '*': {
				qDebug ( "* ASTERIX\n" );
			
				int eol = data. find ( '\n', pos );
				int bracket = data. findRev ( '{', eol );
				int rest = data. find ( QRegExp ( "[^\\s]" ), pos + 1 );
				
				qDebug ( "pos=%d, rest=%d, bracket=%d, eol=%d\n", pos, rest, bracket, eol );
				
				if ( bracket > pos ) {
					uint needdata = data. mid ( bracket + 1, data. find ( '}', bracket + 1 ) - bracket - 1 ). toUInt ( );
					
					if ( needdata ) {
						qDebug ( "nd=%d - hd=%d\n", needdata, ( len - eol - 1 ));
					
						while ( needdata > ( len - eol - 1 )) {
							qDebug ( "emitting need more...\n" );
							emit needMoreData ( data );
							len = data. length ( );
						}
						qDebug ( "Got all data...\n" );
					
						QString tmp = data. mid ( rest, eol - rest + 1 + needdata );
						
						int tail = 0;	
						
						while ( data [eol - rest + 1 + needdata + tail] != ')' )
							tail++;
						tmp. append ( data. mid ( eol - rest + 1 + needdata, tail + 1 ));
							
					
						qDebug ( "Complete parse = |%s|\n", tmp.latin1());
					
						parseResponse ( tmp );
						
						pos = rest + needdata + tail + 1;
						break;
					}									
				}
				
				parseResponse ( data. mid ( rest, eol - rest + 1 ). stripWhiteSpace ( ));				
				break;
			}          
			case '+': {
				qDebug ( "+ PLUS\n" );
				
				emit needMoreData ( data );
				len = data. length ( );
				break;
			}			          
			default : {
				qDebug ( "OTHER: '%s...'\n", data. mid ( pos, 20 ). latin1 ( ));
			
				uint rest = data. find ( ' ', pos + 1 );
				rest = data. find ( QRegExp ( "[^\\s]" ), rest + 1 );
				_iresponse. setTag ( data. mid ( pos, rest - pos ). stripWhiteSpace ( ));
				parseResponse ( data. mid ( rest, data. find ( '\n', rest )). stripWhiteSpace ( ), true );
				break;
			}
		}

		// skip to end-of-line
		while (( pos < len ) && ( data [pos] != '\n' ))
			pos++;
	}
}


IMAPResponse IMAPResponseParser::response()
{
	return _iresponse;
}

void IMAPResponseParser::parseResponse(const QString &data, bool tagged)
{
	QString response, line;
	int pos;
	bool isNum = false;


//	qDebug ( "\n\n#### PRD #### : #%s#\n\n", data.latin1());

	if ((pos = data.find(' ')) != -1) {
		response = data.left(pos).upper();
		response.toInt(&isNum);
		line = data.right(data.length() - pos - 1);
	} else {
		qWarning("IMAPResponseParser: parseResponse: No response found.");
		return;
	}

	if (response == "OK" && tagged) {
		IMAPResponseStatusResponse status(OK, line);
		status.setResponseCode(getResponseCode(status.comment()));
		_iresponse.setStatusResponse(status);
	} else if (response == "OK" && !tagged) {
		IMAPResponseOK ok(line, getResponseCode(line));
		_iresponse.addOK(ok);
	} else if (response == "NO" && tagged) {
		IMAPResponseStatusResponse status(NO, line);
		status.setResponseCode(getResponseCode(status.comment()));
		_iresponse.setStatusResponse(status);
	} else if (response == "NO" && !tagged) {
		IMAPResponseNO no(line, getResponseCode(line));
		_iresponse.addNO(no);
	} else if (response == "BAD" && tagged) {
		IMAPResponseStatusResponse status(BAD, line);
		status.setResponseCode(getResponseCode(status.comment()));
		_iresponse.setStatusResponse(status);
	} else if (response == "BAD" && !tagged) {
		IMAPResponseBAD bad(line, getResponseCode(line));
		_iresponse.addBAD(bad);
	} else if (response == "PREAUTH" && tagged) {
		IMAPResponseStatusResponse status(PREAUTH, line);
		_iresponse.setStatusResponse(status);
	} else if (response == "PREAUTH" && !tagged) {
		qDebug("IMAPResponseParser: responseParser: got untagged PREAUTH response.");
		// XXX
	} else if (response == "BYE") {
		IMAPResponseStatusResponse status(BYE, line);
		if (!tagged) status.setExitedUnexpected(true);
		_iresponse.setStatusResponse(status);
	} else if (response == "CAPABILITY") {
		IMAPResponseCAPABILITY capability(QStringList::split(' ', line));
		_iresponse.addCAPABILITY(capability);
	} else if (response == "LIST") {
		QStringList list = splitData(line, true);

		QStringList flags;
		parseParenthesizedList(list[0], flags);

		removeLimiters(list[1]);
		removeLimiters(list[2]);		
		IMAPResponseLIST rlist(parseFlagList(flags), list[1], list[2]);
		_iresponse.addLIST(rlist);
	} else if (response == "LSUB") {
		QStringList list = splitData(line, true);

		QStringList flags;
		parseParenthesizedList(list[0], flags);

		removeLimiters(list[1]);
		removeLimiters(list[2]);
		IMAPResponseLSUB lsub(parseFlagList(flags), list[1], list[2]);
		_iresponse.addLSUB(lsub);
	} else if (response == "STATUS") {
		QStringList list = splitData(line, true);

		removeLimiters(list[0]);
		IMAPResponseSTATUS status(list[0]);

		QStringList flags;
		parseParenthesizedList(list[1], flags);
		QStringList::Iterator it;
		for (it = flags.begin(); it != flags.end(); it++) {
			if (*it == "MESSAGES") status.setMessages(*(++it));
			else if (*it == "RECENT") status.setRecent(*(++it));
			else if (*it == "UIDNEXT") status.setUidnext(*(++it));
			else if (*it == "UIDVALIDITY") status.setUidvalidity(*(++it));
			else if (*it == "UNSEEN") status.setUnseen(*(++it));
			else qWarning((QString("IMAPResponseParser: parseResponse: Unknown status data: " )+ *(it++) + "|").latin1());
		}
		_iresponse.addSTATUS(status);
	} else if (response == "SEARCH") {
		IMAPResponseSEARCH search(QStringList::split(' ', line));
		_iresponse.addSEARCH(search);
	} else if (response == "FLAGS") {
		QStringList list;
		parseParenthesizedList(line, list);

		IMAPResponseFLAGS flags(parseFlagList(list));
		_iresponse.addFLAGS(flags);
	} else if (isNum) {
		QStringList list = QStringList::split(' ', line);
		if (list[0] == "EXISTS") {
			IMAPResponseEXISTS exists(response);
			_iresponse.addEXISTS(exists);
		} else if (list[0] == "RECENT") {
			IMAPResponseRECENT recent(response);
			_iresponse.addRECENT(recent);
		} else if (list[0] == "EXPUNGE") {
			IMAPResponseEXPUNGE expunge(response);
			_iresponse.addEXPUNGE(expunge);
		} else if (list[0] == "FETCH") {
			IMAPResponseFETCH fetch;
			QStringList::Iterator it;

			qDebug ( "Got FETCH\n" );

			QStringList fetchList = splitData(line, true);
			QStringList list;
			
			qDebug ( "fl [0]=%s, fl [1]=%s, fl[2]=%s\n", fetchList[0].latin1(),fetchList[1].latin1(),fetchList[2].latin1());
			
			parseParenthesizedList(fetchList[1], list);

			for (it = list.begin(); it != list.end(); it++) {
				qDebug ( "Checking list[] == %s\n", (*it).latin1());
			
				if (*it == "BODY") {
					qDebug("IMAPResponseParser: responseParser: got FETCH::BODY");
					// XXX
				} else if ((*it).find(QRegExp("^BODY\\[\\d+\\]")) != -1) {
					qDebug("IMAPResponseParser: responseParser: got FETCH::BODY[x]");

					QString number = ( *it ). mid ( 5, ( *it ). length ( ) - 6 );
					QString bodydata = *(++it);

//					QStringList blist;
//					parseParenthesizedList(bodydata, blist);

					IMAPResponseBodyPart bodypart;
//					QString tmp;
//					for (unsigned int i = 2; i < blist.count(); i++) {
//						if (i != 2) tmp += " " + blist[i];
//						else tmp += blist[i];
//					}
					bodypart.setData(bodydata);

//					QString tmp = list[0];
//					tmp.replace(0, 5, "");
//					tmp.truncate(blist[0].length() - 1);
					bodypart.setPart(number);

					qDebug("added bodypart [%s]: '%s'\n\n", number.latin1(), bodydata.latin1());

					fetch.addBodyPart(bodypart);
				} else if (*it == "BODYSTRUCTURE") {
					qDebug("IMAPResponseParser: responseParser: got FETCH::BODYSTRUCTURE");
/*
					QString bsdata = *(++it);
					QStringList bsList;
					parseParenthesizedList(bsdata, bsList);

					IMAPResponseBodystructure bodystructure;
					QStringList attachml;

					for (int i = 0; i < bsList.count() - 1; i++) {
						parseParenthesizedList(bsList[0], attachml);

						IMAPResponseBodypart bodypart;
						bodypart.setMimeTypeMain(attachml[0] == "NIL" ? QString(0) : attachml[0]);
						bodypart.setMimeTypeSub(attachml[1] == "NIL" ? QString(0) : attachml[1]);
						bodypart.setAddData(attachml[2] == "NIL" ? QString(0) : attachml[2]);
						// 3 (NIL)
						// 4 (NIL)
						bodypart.setEncoding(attachml[5] == "NIL" ? QString(0) : attachml[5]);
						bodypart.setSize(attachml[6] == "NIL" ? QString(0) : attachml[6]);
						bodypart.setLength(attachml[7] == "NIL" ? QString(0) : attachml[7]);
						bodypart.setAttachInfo(attachml[8] == "NIL" ? QString(0) : attachml[8]);
						// 9 (NIL)
						// 10 (NIL)

						bodystructure.addBodyPart(bodypart);
					}
*/
				} else if (*it == "ENVELOPE") {
					QString envdata = *(++it);
					QStringList envList;
					parseParenthesizedList(envdata, envList);

					IMAPResponseEnvelope envelope;
					envelope.setMailDate(envList[0] == "NIL" ? QString(0) : removeLimiters(envList[0]));
					envelope.setSubject(envList[1] == "NIL" ? QString(0) : removeLimiters(envList[1]));

					QStringList froml, senderl, replytol, tol, ccl, bccl;
					QStringList froma, sendera, replytoa, toa, cca, bcca;
					parseParenthesizedList(envList[2], froml);
					parseParenthesizedList(envList[3], senderl);
					parseParenthesizedList(envList[4], replytol);
					parseParenthesizedList(envList[5], tol);
					parseParenthesizedList(envList[6], ccl);
					parseParenthesizedList(envList[7], bccl);

					QStringList::Iterator it;
					for (it = froml.begin(); it != froml.end(); it++) {
						parseParenthesizedList(*it, froma);
						if (froml[0] != "NIL")
							envelope.addFrom(IMAPResponseAddress(
								removeLimiters(froma[0]),
								removeLimiters(froma[1]),
								removeLimiters(froma[2]),
								removeLimiters(froma[3])));
					}

					for (it = senderl.begin(); it != senderl.end(); it++) {
						parseParenthesizedList(*it, sendera);
						if (senderl[0] != "NIL")
							envelope.addSender(IMAPResponseAddress(
								removeLimiters(sendera[0]), 
								removeLimiters(sendera[1]), 
								removeLimiters(sendera[2]), 
								removeLimiters(sendera[3])));
					}

					for (it = replytol.begin(); it != replytol.end(); it++) {
						parseParenthesizedList(*it, replytoa);				
						if (replytol[0] != "NIL")
							envelope.addReplyTo(IMAPResponseAddress(
								removeLimiters(replytoa[0]),
								removeLimiters(replytoa[1]),
								removeLimiters(replytoa[2]), 
								removeLimiters(replytoa[3])));
					}

					for (it = tol.begin(); it != tol.end(); it++) {
						parseParenthesizedList(*it, toa);
						if (tol[0] != "NIL") 
							envelope.addTo(IMAPResponseAddress(
								removeLimiters(toa[0]),
								removeLimiters(toa[1]),
								removeLimiters(toa[2]),
								removeLimiters(toa[3])));
					}

					for (it = ccl.begin(); it != ccl.end(); it++) {
						parseParenthesizedList(*it, cca);
						if (ccl[0] != "NIL")
							envelope.addCc(IMAPResponseAddress(
								removeLimiters(cca[0]),
								removeLimiters(cca[1]),
								removeLimiters(cca[2]),
								removeLimiters(cca[3])));
					}

					for (it = bccl.begin(); it != bccl.end(); it++) {
						parseParenthesizedList(*it, bcca);
						if (bccl[0] != "NIL")
							envelope.addBcc(IMAPResponseAddress(
								removeLimiters(bcca[0]),
								removeLimiters(bcca[1]),
								removeLimiters(bcca[2]),
								removeLimiters(bcca[3])));
					}

					envelope.setInReplyTo(envList[7] == "NIL" ? QString(0) : removeLimiters(envList[7]));
					envelope.setMessageId(envList[8] == "NIL" ? QString(0) : removeLimiters(envList[8]));
				
					fetch.setEnvelope(envelope);
				} else if (*it == "FLAGS") {
					QString flagdata = *(++it);
					QStringList flags;
					parseParenthesizedList(flagdata, flags);
					fetch.setFlags(parseFlagList(flags));
				} else if (*it == "INTERNALDATE") {
					fetch.setInternalDate(removeLimiters(*(++it)));
				} else if (*it == "RFC822" || *it == "BODY[]") {
					qDebug("IMAPResponseParser: responseParser: got FETCH::RFC822");
					// XXX
				} else if (*it == "RFC822.HEADER" || *it == "BODY.PEEK[HEADER]") {
					qDebug("IMAPResponseParser: responseParser: got FETCH::RFC822.HEADER");
					// XXX
				} else if (*it == "RFC822.SIZE") {
					fetch.setRFC822Size(*(++it));
				} else if (*it == "RFC822.TEXT" || *it == "BODY[TEXT]") {
					qDebug("IMAPResponseParser: responseParser: got FETCH::RFC822.TEXT");
					// XXX
				} else if (*it == "UID") {
					fetch.setUid(*(++it));
				}	
			}
			_iresponse.addFETCH(fetch);
		}
	} else qWarning((QString("IMAPResponseParser: parseResponse: Unknown response: ") + response + "|").latin1());
	
}

QStringList IMAPResponseParser::splitData(const QString &data, bool withBrackets)
{
	int b = 0;
	bool a = false, noappend = false, escaped = false;	 
	QString temp;
	QStringList list;

	qDebug ( "sd: '%s'\n", data.latin1());

	for (unsigned int i = 0; i <= data.length(); i++) {
		if (withBrackets && data[i] == '(' && !a) b++;
		else if (withBrackets && data[i] == ')' && !a) b--;

		if (data[i] == '{' && !escaped && !a ) {
			qDebug ( "sd: found a {\n" );
		
			int p = data. find ( '}', i + 1 );
			int eol = data. find ( '\n', i + 1 );

			if ( p > int( i )) {
				int len = data. mid ( i + 1, p - i - 1 ). toInt ( );
		
				qDebug ( "sd: skipping %d bytes\n", len );
			
				if ( b == 0 ) {			
					temp = data. mid ( eol + 1, len );
					noappend = false;
					i = eol + len;
					continue;
				}
				else {
					temp. append ( '{' );
					temp. append ( QString::number ( len ));
					temp. append ( "}\r\n" );
					temp. append ( data. mid ( eol + 1, len ));
					i = eol + len;
					continue;
				}
			}
		}

		if (data[i] == '\"' && !escaped) a = !a;
		else escaped = false;

		if (data[i] == '\\' && data[i + 1] == '\"') escaped = true;

		if ((data[i] == ' ' || i == data.length()) && b == 0 && !a) {
			list.append(temp);
			temp = QString::null;
			if (data[i] == ' ') noappend = true;
		}

		if (!noappend) temp += data[i];
		noappend = false;
	}

	return list;
}

void IMAPResponseParser::parseParenthesizedList(const QString &data, QStringList &parsed)
{
	QString data_(data);
	removeLimiters(data_, '(', ')');
	parsed = splitData(data_, true);
}

void IMAPResponseParser::splitTagData(const QString &line, QString &tag, QString &data)
{
	int pos;
	if ((pos = line.find(' ')) != -1) {
		tag = line.left(pos);
		data = line.right(line.length() - pos - 1);
	} else qWarning((QString("IMAPResponseParser: splitTagData: tag not found. Line was ") + line + "|").latin1());
}

QString IMAPResponseParser::removeLimiters(QString &string, const QChar &sl, const QChar &el)
{
	QString tmpString;
	string = string. stripWhiteSpace ( );
	
	if (string[0] == sl && string[string.length() - 1] == el) {
		string.truncate(string.length() - 1);
		string.replace(0, 1, "");

		for (unsigned int i = 1; i <= string.length(); i++) {
			if (string[i - 1] == '\\' && sl == '\"') ++i;
			tmpString += string[i - 1];
		}
	}
	return tmpString;
}

IMAPResponseEnums::IMAPResponseCode IMAPResponseParser::getResponseCode(const QString &line)
{
	if (line.find(QRegExp((QString) "^\\[.*\\]" + ' ' + ".*")) != -1) {
		int pos = line.find("] ");
		QString code = line.left(pos + 1).upper();

		if (code.find(QRegExp("[ALERT]")) != -1) return ALERT;
		else if (code.find(QRegExp("[NEWNAME .* .*]")) != -1) return NEWNAME; // XXX
		else if (code.find(QRegExp("[PARSE]")) != -1) return PARSE;
		else if (code.find(QRegExp("[PERMANENTFLAGS \\d*]")) != -1) return PERMANENTFLAGS; // XXX
		else if (code.find(QRegExp("[READ-ONLY]")) != -1) return READONLY;
		else if (code.find(QRegExp("[READ-WRITE]")) != -1) return READWRITE;
		else if (code.find(QRegExp("[TRYCREATE]")) != -1) return TRYCREATE;
		else if (code.find(QRegExp("[UIDVALIDITY \\d*]")) != -1) return UIDVALIDITY; // XXX
		else if (code.find(QRegExp("[UNSEEN \\d*]")) != -1) return UNSEEN; // XXX
		else {
			qWarning((QString("IMAPResponseParser: getResponseCode: Unknown code: ") + code + "|").latin1());
			return UnknownCode;
		}
	}
	return NoCode;
}

QValueList<IMAPResponseEnums::IMAPResponseFlags> IMAPResponseParser::parseFlagList(const QStringList &flagList)
{
	QValueList<IMAPResponseFlags> flags;
	QStringList::ConstIterator it;
	for (it = flagList.begin(); it != flagList.end(); it++) {
		QString flag = (*it).lower();
		if (flag == "\\seen") flags.append(Seen);
		else if (flag == "\\answered") flags.append(Answered);
		else if (flag == "\\flagged") flags.append(Flagged);
		else if (flag == "\\deleted") flags.append(Deleted);
		else if (flag == "\\draft") flags.append(Draft);
		else if (flag == "\\recent") flags.append(Recent);
		else if (flag == "\\noinferiors") flags.append(Noinferiors);
		else if (flag == "\\noselect") flags.append(Noselect);
		else if (flag == "\\marked") flags.append(Marked);
		else if (flag == "\\unmarked") flags.append(Unmarked);
		else if (flag.isEmpty()) { }
		else qWarning((QString("IMAPResponseParser: parseFlagList: Unknown flag: ") + *it + "|").latin1());
	}
	return flags;
}