diff options
author | Don Sanders <[email protected]> | 2011-08-01 21:38:16 +1000 |
---|---|---|
committer | Don Sanders <[email protected]> | 2011-08-01 21:38:16 +1000 |
commit | 8556a4d4a129e2aaba6e8ec788d7597d35a77a42 (patch) | |
tree | c8ed27e4417b4a04d07d3d0e5247b1b0094c807e | |
parent | 724c9433cd71e07f4d77271c5fa65fce343cbdfe (diff) | |
parent | b1eb30a4f7bfe72be3435e6a2d1442ecd51616eb (diff) |
Merge commit 'refs/merge-requests/18' of git://gitorious.org/qt-labs/messagingframework
-rw-r--r-- | src/libraries/qmfclient/qmailmessage.cpp | 93 | ||||
-rw-r--r-- | src/plugins/messageservices/imap/imapclient.cpp | 12 | ||||
-rw-r--r-- | src/plugins/messageservices/imap/imapprotocol.cpp | 24 |
3 files changed, 104 insertions, 25 deletions
diff --git a/src/libraries/qmfclient/qmailmessage.cpp b/src/libraries/qmfclient/qmailmessage.cpp index 4e42b811..274d2a3c 100644 --- a/src/libraries/qmfclient/qmailmessage.cpp +++ b/src/libraries/qmfclient/qmailmessage.cpp @@ -2732,7 +2732,7 @@ QMailMessageHeaderPrivate::QMailMessageHeaderPrivate() { } -enum NewLineStatus { None, Cr, CrLf }; +enum NewLineStatus { None, Cr, CrLf, Lf }; static QList<QByteArray> parseHeaders(const QByteArray& input) { @@ -2775,10 +2775,33 @@ static QList<QByteArray> parseHeaders(const QByteArray& input) else { status = None; } - } - else { + } else if (status == Lf) { + if (*it == ' ' || *it == '\t') { + // The LF was folded + if ((it - begin) > 1) { + progress.append(QByteArray(begin, (it - begin - 1))); + } + begin = it; + } + else { + // That was an unescaped CRLF + if ((it - begin) > 1) { + progress.append(QByteArray(begin, (it - begin) - 1)); + } + if (!progress.isEmpty()) { + // Non-empty field + result.append(progress); + progress.clear(); + } + begin = it; + } + status = None; + + } else { if (*it == QMailMessage::CarriageReturn) status = Cr; + else if (*it == QMailMessage::LineFeed) + status = Lf; } } @@ -4195,7 +4218,7 @@ void QMailMessagePartContainerPrivate::parseMimeSinglePart(const QMailMessageHea void QMailMessagePartContainerPrivate::parseMimeMultipart(const QMailMessageHeader& partHeader, LongString body, bool insertIntoSelf) { - static const QByteArray newLine(QMailMessage::CRLF); + static const QByteArray lineFeed(1, QMailMessage::LineFeed); static const QByteArray marker("--"); QMailMessagePart part; @@ -4220,16 +4243,20 @@ void QMailMessagePartContainerPrivate::parseMimeMultipart(const QMailMessageHead // Separate the body into parts delimited by the boundary, and parse them individually QByteArray partDelimiter = marker + boundary; - QByteArray partTerminator = newLine + partDelimiter + marker; + QByteArray partTerminator = lineFeed + partDelimiter + marker; int startPos = body.indexOf(partDelimiter, 0); if (startPos != -1) startPos += partDelimiter.length(); // Subsequent delimiters include the leading newline - partDelimiter.prepend(newLine); + partDelimiter.prepend(lineFeed); int endPos = body.indexOf(partTerminator, 0); + + // Consider CRLF also (which happens to be the standard, so we honor it) + if (endPos > 1 && body.mid(endPos - 1, 1).indexOf(QByteArray(1, QMailMessage::CarriageReturn)) != -1) + endPos--; // invalid message handling: handles truncated multipart messages if (endPos == -1) @@ -4238,19 +4265,29 @@ void QMailMessagePartContainerPrivate::parseMimeMultipart(const QMailMessageHead while ((startPos != -1) && (startPos < endPos)) { // Skip the boundary line - startPos = body.indexOf(newLine, startPos); + startPos = body.indexOf(lineFeed, startPos); + + if (startPos > 0 && body.mid(startPos - 1, 1).indexOf(QByteArray(1, QMailMessage::CarriageReturn)) != -1) + startPos--; if ((startPos != -1) && (startPos < endPos)) { // Parse the section up to the next boundary marker int nextPos = body.indexOf(partDelimiter, startPos); + // Honor CRLF too... + if (nextPos > 0 && body.mid(nextPos - 1, 1).indexOf(QByteArray(1, QMailMessage::CarriageReturn)) != -1) + nextPos--; + // invalid message handling: handles truncated multipart messages if (nextPos == -1) nextPos = body.length() - 1; multipartContainer->parseMimePart(body.mid(startPos, (nextPos - startPos))); + if (body.mid(nextPos, 1).indexOf(QByteArray(1, QMailMessage::CarriageReturn)) ==0) + nextPos++; + // Try the next part startPos = nextPos + partDelimiter.length(); } @@ -4263,10 +4300,26 @@ void QMailMessagePartContainerPrivate::parseMimeMultipart(const QMailMessageHead bool QMailMessagePartContainerPrivate::parseMimePart(LongString body) { - static const QByteArray delimiter((QByteArray(QMailMessage::CRLF) + QMailMessage::CRLF)); + static const QByteArray CRLFdelimiter((QByteArray(QMailMessage::CRLF) + QMailMessage::CRLF)); + static const QByteArray CRdelimiter(2, QMailMessage::CarriageReturn); + static const QByteArray LFdelimiter(2, QMailMessage::LineFeed); + + int CRLFindex = body.indexOf(CRLFdelimiter); + int LFindex = body.indexOf(LFdelimiter); + int CRindex = body.indexOf(CRdelimiter); + + int endPos = CRLFindex; + QByteArray delimiter = CRLFdelimiter; + if (endPos == -1 || (LFindex > -1 && LFindex < endPos)) { + endPos = LFindex; + delimiter = LFdelimiter; + } + if (endPos == -1 || (CRindex > -1 && CRindex < endPos)) { + endPos = CRindex; + delimiter = CRdelimiter; + } int startPos = 0; - int endPos = body.indexOf(delimiter); if (startPos <= endPos) { // startPos is the offset of the header, endPos of the delimiter preceding the body @@ -8275,12 +8328,28 @@ void QMailMessage::refreshPreview() /*! \internal */ QMailMessage QMailMessage::fromRfc2822(LongString& ls) { - const QByteArray terminator((QByteArray(QMailMessage::CRLF) + QMailMessage::CRLF)); + const QByteArray CRLFterminator((QByteArray(QMailMessage::CRLF) + QMailMessage::CRLF)); + const QByteArray LFterminator(2, QMailMessage::LineFeed); + const QByteArray CRterminator(2, QMailMessage::CarriageReturn); QMailMessage mail; mail.setMessageType(QMailMessage::Email); - int pos = ls.indexOf(terminator); + int CRLFindex = ls.indexOf(CRLFterminator); + int LFindex = ls.indexOf(LFterminator); + int CRindex = ls.indexOf(CRterminator); + + int pos = CRLFindex; + QByteArray terminator = CRLFterminator; + if (pos == -1 || (LFindex > -1 && LFindex < pos)) { + pos = LFindex; + terminator = LFterminator; + } + if (pos == -1 || (CRindex > -1 && CRindex < pos)) { + pos = CRindex; + terminator = CRterminator; + } + if (pos == -1) { // No body? Parse entirety as header mail.setHeader( QMailMessageHeader( ls.toQByteArray() ) ); @@ -8289,7 +8358,7 @@ QMailMessage QMailMessage::fromRfc2822(LongString& ls) mail.setHeader( QMailMessageHeader( ls.left(pos).toQByteArray() ) ); // Parse the remainder as content - mail.partContainerImpl()->fromRfc2822( ls.mid(pos + 4) ); + mail.partContainerImpl()->fromRfc2822( ls.mid(pos + terminator.length()) ); } if (mail.metaDataImpl()->_date.isNull()) { diff --git a/src/plugins/messageservices/imap/imapclient.cpp b/src/plugins/messageservices/imap/imapclient.cpp index 1eb2d560..f3d1f9ed 100644 --- a/src/plugins/messageservices/imap/imapclient.cpp +++ b/src/plugins/messageservices/imap/imapclient.cpp @@ -930,7 +930,7 @@ static bool updateParts(QMailMessagePart &part, const QByteArray &bodyData) // Separate the body into parts delimited by the boundary, and update them individually QByteArray partDelimiter = marker + boundary; - QByteArray partTerminator = newLine + partDelimiter + marker; + QByteArray partTerminator = QByteArray(1, QMailMessage::LineFeed) + partDelimiter + marker; int startPos = bodyData.indexOf(partDelimiter, 0); if (startPos != -1) @@ -943,6 +943,9 @@ static bool updateParts(QMailMessagePart &part, const QByteArray &bodyData) int partIndex = 0; int endPos = bodyData.indexOf(partTerminator, 0); + if (endPos > 0 && bodyData[endPos - 1] == QMailMessage::CarriageReturn) { + endPos--; + } while ((startPos != -1) && (startPos < endPos)) { // Skip the boundary line startPos = bodyData.indexOf(newLine, startPos); @@ -951,6 +954,10 @@ static bool updateParts(QMailMessagePart &part, const QByteArray &bodyData) // Parse the section up to the next boundary marker int nextPos = bodyData.indexOf(partDelimiter, startPos); + if (nextPos > 0 && bodyData[nextPos - 1] == QMailMessage::CarriageReturn) { + nextPos--; + } + // Find the beginning of the part body startPos = bodyData.indexOf(bodyDelimiter, startPos); if ((startPos != -1) && (startPos < nextPos)) { @@ -963,6 +970,9 @@ static bool updateParts(QMailMessagePart &part, const QByteArray &bodyData) ++partIndex; } + if (bodyData[nextPos] == QMailMessage::CarriageReturn) { + nextPos++; + } // Move to the next part startPos = nextPos + partDelimiter.length(); } diff --git a/src/plugins/messageservices/imap/imapprotocol.cpp b/src/plugins/messageservices/imap/imapprotocol.cpp index 94aae732..fb9926b5 100644 --- a/src/plugins/messageservices/imap/imapprotocol.cpp +++ b/src/plugins/messageservices/imap/imapprotocol.cpp @@ -193,24 +193,24 @@ static QString token( QString str, QChar c1, QChar c2, int *index ) // The strings we're tokenizing use CRLF as the line delimiters - assume that the // caller considers the sequence to be atomic. - if (c1 == QMailMessage::LineFeed) - c1 = QMailMessage::CarriageReturn; + if (c1 == QMailMessage::CarriageReturn) + c1 = QMailMessage::LineFeed; start = str.indexOf( c1, *index, Qt::CaseInsensitive ); if (start == -1) return QString(); - // Bypass the LF if necessary - if (c1 == QMailMessage::CarriageReturn) - start += 1; - - if (c2 == QMailMessage::LineFeed) - c2 = QMailMessage::CarriageReturn; + if (c2 == QMailMessage::CarriageReturn) + c2 = QMailMessage::LineFeed; stop = str.indexOf( c2, ++start, Qt::CaseInsensitive ); if (stop == -1) return QString(); + // Exclude the CR if necessary + if (stop && (str[stop-1] == QMailMessage::CarriageReturn)) + --stop; + // Bypass the LF if necessary - *index = stop + (c2 == QMailMessage::CarriageReturn ? 2 : 1); + *index = stop + 1; return str.mid( start, stop - start ); } @@ -3192,9 +3192,9 @@ void ImapProtocol::processResponse(QString line) setPrecedingLiteral(QString()); - if (remainder.endsWith("\r\n")) { + if (remainder.endsWith("\n")) { // Is this trailing part followed by a literal data segment? - QRegExp literalPattern("\\{(\\d*)\\}\\r\\n"); + QRegExp literalPattern("\\{(\\d*)\\}\\r?\\n"); int literalIndex = literalPattern.indexIn(remainder); if (literalIndex != -1) { // We are waiting for literal data to complete this line @@ -3216,7 +3216,7 @@ void ImapProtocol::processResponse(QString line) } // Is this line followed by a literal data segment? - QRegExp literalPattern("\\{(\\d*)\\}\\r\\n"); + QRegExp literalPattern("\\{(\\d*)\\}\\r?\\n"); int literalIndex = literalPattern.indexIn(line); if (literalIndex != -1) { // We are waiting for literal data to complete this line |