10 #include "kmime_parsers_p.h"
12 #include <QRegularExpression>
14 using namespace KMime::Parser;
23 , m_boundary(boundary)
27 bool MultiPart::parse()
39 if ((pos1 = m_src.indexOf(b, pos1)) == -1 || pos1 == 0 ||
40 m_src[pos1 - 1] ==
'\n') {
48 if (m_src[pos1] ==
'-' && m_src[pos1 + 1] ==
'-') {
52 }
else if ((pos1 - blen) > 1) {
53 m_preamble = m_src.left(pos1 - blen - 1);
57 while (pos1 > -1 && pos2 > -1) {
60 if ((pos1 = m_src.indexOf(
'\n', pos1)) > -1) {
65 if ((pos2 = m_src.indexOf(b, pos2)) == -1 ||
66 m_src[pos2 - 1] ==
'\n') {
73 part = m_src.
mid(pos1, m_src.length() - pos1);
78 part = m_src.
mid(pos1, pos2 - pos1 - 1);
81 if (m_src[pos2] ==
'-' && m_src[pos2 + 1] ==
'-') {
84 if ((pos1 = m_src.indexOf(
'\n', pos1)) > -1) {
86 m_epilouge = m_src.
mid(pos1 + 1, m_src.length() - pos1 - 1);
102 NonMimeParser::NonMimeParser(
const QByteArray &src) :
103 m_src(src), m_partNr(-1), m_totalNr(-1)
107 NonMimeParser::~NonMimeParser() =
default;
122 if (tmp ==
"JPG" || tmp ==
"JPEG") {
123 mimeType = QByteArrayLiteral(
"image/jpeg");
124 }
else if (tmp ==
"GIF") {
125 mimeType = QByteArrayLiteral(
"image/gif");
126 }
else if (tmp ==
"PNG") {
127 mimeType = QByteArrayLiteral(
"image/png");
128 }
else if (tmp ==
"TIFF" || tmp ==
"TIF") {
129 mimeType = QByteArrayLiteral(
"image/tiff");
130 }
else if (tmp ==
"XPM") {
131 mimeType = QByteArrayLiteral(
"image/x-xpixmap");
132 }
else if (tmp ==
"XBM") {
133 mimeType = QByteArrayLiteral(
"image/x-xbitmap");
134 }
else if (tmp ==
"BMP") {
135 mimeType = QByteArrayLiteral(
"image/bmp");
136 }
else if (tmp ==
"TXT" ||
142 mimeType = QByteArrayLiteral(
"text/plain");
143 }
else if (tmp ==
"HTML" || tmp ==
"HTM") {
144 mimeType = QByteArrayLiteral(
"text/html");
146 mimeType = QByteArrayLiteral(
"application/octet-stream");
149 mimeType = QByteArrayLiteral(
"application/octet-stream");
152 mimeType = QByteArrayLiteral(
"application/octet-stream");
161 NonMimeParser(src), m_subject(subject)
164 bool UUEncoded::parse()
168 bool firstIteration =
true;
175 int beginPos = currentPos;
176 int uuStart = currentPos;
182 bool containsBegin =
false;
183 bool containsEnd =
false;
187 if ((beginPos = srcStr.indexOf(beginRegex, currentPos)) > -1 &&
188 (beginPos == 0 || m_src.at(beginPos - 1) ==
'\n')) {
189 containsBegin =
true;
190 uuStart = m_src.
indexOf(
'\n', beginPos);
198 beginPos = currentPos;
201 if ((endPos = m_src.indexOf(
"\nend", (uuStart > 0) ? uuStart - 1 : 0)) == -1) {
202 endPos = m_src.length();
207 if ((containsBegin && containsEnd) || firstIteration) {
211 for (
int idx = uuStart; idx < endPos; idx++) {
212 if (m_src[idx] ==
'\n') {
214 if (idx + 1 < endPos && m_src[idx + 1] ==
'M') {
222 if (MCount == 0 || (lineCount - MCount) > 10 ||
223 ((!containsBegin || !containsEnd) && (MCount < 15))) {
229 if ((!containsBegin || !containsEnd) && !m_subject.isNull()) {
232 pos =
match.capturedStart(0);
233 len =
match.capturedLength(0);
235 tmp = m_subject.
mid(pos, len);
247 m_text.append(m_src.mid(currentPos, beginPos - currentPos));
252 fileName = m_src.
mid(beginPos + 10, uuStart - beginPos - 11);
256 m_filenames.
append(fileName);
258 m_bins.
append(m_src.mid(uuStart, endPos - uuStart + 1));
259 m_mimeTypes.
append(guessMimeType(fileName));
260 firstIteration =
false;
262 int next = m_src.indexOf(
'\n', endPos + 1);
277 m_text.append(m_src.right(m_src.length() - currentPos));
279 return ((!m_bins.isEmpty()) || isPartial());
284 YENCEncoded::YENCEncoded(
const QByteArray &src) :
294 int iPos = src.
indexOf(sought);
296 int pos1 = src.
indexOf(
' ', iPos);
297 int pos2 = src.
indexOf(
'\r', iPos);
298 int pos3 = src.
indexOf(
'\t', iPos);
299 int pos4 = src.
indexOf(
'\n', iPos);
300 if (pos2 >= 0 && (pos1 < 0 || pos1 > pos2)) {
303 if (pos3 >= 0 && (pos1 < 0 || pos1 > pos3)) {
306 if (pos4 >= 0 && (pos1 < 0 || pos1 > pos4)) {
311 char c = src.
at(iPos);
312 if (c >=
'0' && c <=
'9') {
314 *value = src.
mid(iPos, pos1 - iPos).
toInt();
321 bool YENCEncoded::parse()
326 int beginPos = currentPos;
327 int yencStart = currentPos;
328 bool containsPart =
false;
331 if ((beginPos = m_src.indexOf(
"=ybegin ", currentPos)) > -1 &&
332 (beginPos == 0 || m_src.at(beginPos - 1) ==
'\n')) {
333 yencStart = m_src.
indexOf(
'\n', beginPos);
334 if (yencStart == -1) {
339 if (m_src.indexOf(
"=ypart", yencStart) == yencStart) {
341 yencStart = m_src.indexOf(
'\n', yencStart);
342 if (yencStart == -1) {
353 int namePos = meta.
indexOf(
"name=");
358 int eolPos = meta.
indexOf(
'\r', namePos);
360 eolPos = meta.
indexOf(
'\n', namePos);
366 fileName = meta.
mid(namePos + 5, eolPos - (namePos + 5));
370 if (!yencMeta(meta,
"line", ¥cLine)) {
375 if (!yencMeta(meta,
"size", ¥cSize)) {
383 if (!yencMeta(meta,
"part", &m_partNr)) {
387 if (!yencMeta(meta,
"begin", &partBegin) ||
388 !yencMeta(meta,
"end", &partEnd)) {
392 if (!yencMeta(meta,
"total", &m_totalNr)) {
393 m_totalNr = m_partNr + 1;
395 if (yencSize == partEnd - partBegin + 1) {
398 yencSize = partEnd - partBegin + 1;
406 bool lineStart =
true;
408 bool containsEnd =
false;
412 int ch = m_src.at(pos);
417 if (lineLength != yencLine && totalSize != yencSize) {
421 }
else if (ch ==
'\n') {
428 ch = m_src.at(pos + 1);
429 if (lineStart && ch ==
'y') {
438 if (totalSize >= yencSize) {
441 binary[totalSize++] = ch;
451 if (totalSize >= yencSize) {
454 binary[totalSize++] = ch;
466 if (totalSize != yencSize) {
472 eolPos = m_src.
indexOf(
'\n', pos);
477 meta = m_src.
mid(pos, eolPos - pos);
478 if (!yencMeta(meta,
"size", &totalSize)) {
482 if (totalSize != yencSize) {
487 m_filenames.
append(fileName);
488 m_mimeTypes.
append(guessMimeType(fileName));
493 m_text.
append(m_src.mid(currentPos, beginPos - currentPos));
495 currentPos = eolPos + 1;
503 m_text.
append(m_src.right(m_src.length() - currentPos));