diff options
Diffstat (limited to 'framework/src/domain/mimetreeparser/otp/mailman.cpp')
-rw-r--r-- | framework/src/domain/mimetreeparser/otp/mailman.cpp | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/framework/src/domain/mimetreeparser/otp/mailman.cpp b/framework/src/domain/mimetreeparser/otp/mailman.cpp new file mode 100644 index 00000000..e79ef0fa --- /dev/null +++ b/framework/src/domain/mimetreeparser/otp/mailman.cpp | |||
@@ -0,0 +1,183 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Sandro Knauß <sknauss@kde.org> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | |||
20 | #include "mailman.h" | ||
21 | |||
22 | #include "utils.h" | ||
23 | |||
24 | #include "objecttreeparser.h" | ||
25 | #include "messagepart.h" | ||
26 | |||
27 | #include <KMime/Content> | ||
28 | |||
29 | #include "mimetreeparser_debug.h" | ||
30 | |||
31 | using namespace MimeTreeParser; | ||
32 | |||
33 | const MailmanBodyPartFormatter *MailmanBodyPartFormatter::self; | ||
34 | |||
35 | const Interface::BodyPartFormatter *MailmanBodyPartFormatter::create() | ||
36 | { | ||
37 | if (!self) { | ||
38 | self = new MailmanBodyPartFormatter(); | ||
39 | } | ||
40 | return self; | ||
41 | } | ||
42 | Interface::BodyPartFormatter::Result MailmanBodyPartFormatter::format(Interface::BodyPart *part, HtmlWriter *writer) const | ||
43 | { | ||
44 | Q_UNUSED(writer) | ||
45 | const auto p = process(*part); | ||
46 | const auto mp = static_cast<MessagePart *>(p.data()); | ||
47 | if (mp) { | ||
48 | mp->html(false); | ||
49 | return Ok; | ||
50 | } | ||
51 | return Failed; | ||
52 | } | ||
53 | |||
54 | bool MailmanBodyPartFormatter::isMailmanMessage(KMime::Content *curNode) const | ||
55 | { | ||
56 | if (!curNode || curNode->head().isEmpty()) { | ||
57 | return false; | ||
58 | } | ||
59 | if (curNode->hasHeader("X-Mailman-Version")) { | ||
60 | return true; | ||
61 | } | ||
62 | if (KMime::Headers::Base *header = curNode->headerByType("X-Mailer")) { | ||
63 | if (header->asUnicodeString().contains(QStringLiteral("MAILMAN"), Qt::CaseInsensitive)) { | ||
64 | return true; | ||
65 | } | ||
66 | } | ||
67 | return false; | ||
68 | } | ||
69 | |||
70 | Interface::MessagePart::Ptr MailmanBodyPartFormatter::process(Interface::BodyPart &part) const | ||
71 | { | ||
72 | KMime::Content *curNode = part.content(); | ||
73 | |||
74 | if (!isMailmanMessage(curNode)) { | ||
75 | return MessagePart::Ptr(); | ||
76 | } | ||
77 | |||
78 | const QString str = QString::fromLatin1(curNode->decodedContent()); | ||
79 | |||
80 | //### | ||
81 | const QLatin1String delim1("--__--__--\n\nMessage:"); | ||
82 | const QLatin1String delim2("--__--__--\r\n\r\nMessage:"); | ||
83 | const QLatin1String delimZ2("--__--__--\n\n_____________"); | ||
84 | const QLatin1String delimZ1("--__--__--\r\n\r\n_____________"); | ||
85 | QString partStr, digestHeaderStr; | ||
86 | int thisDelim = str.indexOf(delim1, Qt::CaseInsensitive); | ||
87 | if (thisDelim == -1) { | ||
88 | thisDelim = str.indexOf(delim2, Qt::CaseInsensitive); | ||
89 | } | ||
90 | if (thisDelim == -1) { | ||
91 | return MessagePart::Ptr(); | ||
92 | } | ||
93 | |||
94 | int nextDelim = str.indexOf(delim1, thisDelim + 1, Qt::CaseInsensitive); | ||
95 | if (-1 == nextDelim) { | ||
96 | nextDelim = str.indexOf(delim2, thisDelim + 1, Qt::CaseInsensitive); | ||
97 | } | ||
98 | if (-1 == nextDelim) { | ||
99 | nextDelim = str.indexOf(delimZ1, thisDelim + 1, Qt::CaseInsensitive); | ||
100 | } | ||
101 | if (-1 == nextDelim) { | ||
102 | nextDelim = str.indexOf(delimZ2, thisDelim + 1, Qt::CaseInsensitive); | ||
103 | } | ||
104 | if (nextDelim < 0) { | ||
105 | return MessagePart::Ptr(); | ||
106 | } | ||
107 | |||
108 | //if ( curNode->mRoot ) | ||
109 | // curNode = curNode->mRoot; | ||
110 | |||
111 | // at least one message found: build a mime tree | ||
112 | digestHeaderStr = QStringLiteral("Content-Type: text/plain\nContent-Description: digest header\n\n"); | ||
113 | digestHeaderStr += str.midRef(0, thisDelim); | ||
114 | |||
115 | MessagePartList::Ptr mpl(new MessagePartList(part.objectTreeParser())); | ||
116 | mpl->appendSubPart(createAndParseTempNode(part, part.topLevelContent(), digestHeaderStr.toLatin1().constData(), "Digest Header")); | ||
117 | //mReader->queueHtml("<br><hr><br>"); | ||
118 | // temporarily change curent node's Content-Type | ||
119 | // to get our embedded RfC822 messages properly inserted | ||
120 | curNode->contentType()->setMimeType("multipart/digest"); | ||
121 | while (-1 < nextDelim) { | ||
122 | int thisEoL = str.indexOf(QLatin1String("\nMessage:"), thisDelim, Qt::CaseInsensitive); | ||
123 | if (-1 < thisEoL) { | ||
124 | thisDelim = thisEoL + 1; | ||
125 | } else { | ||
126 | thisEoL = str.indexOf(QLatin1String("\n_____________"), thisDelim, Qt::CaseInsensitive); | ||
127 | if (-1 < thisEoL) { | ||
128 | thisDelim = thisEoL + 1; | ||
129 | } | ||
130 | } | ||
131 | thisEoL = str.indexOf(QLatin1Char('\n'), thisDelim); | ||
132 | if (-1 < thisEoL) { | ||
133 | thisDelim = thisEoL + 1; | ||
134 | } else { | ||
135 | thisDelim = thisDelim + 1; | ||
136 | } | ||
137 | //while( thisDelim < cstr.size() && '\n' == cstr[thisDelim] ) | ||
138 | // ++thisDelim; | ||
139 | |||
140 | partStr = QStringLiteral("Content-Type: message/rfc822\nContent-Description: embedded message\n\n"); | ||
141 | partStr += str.midRef(thisDelim, nextDelim - thisDelim); | ||
142 | QString subject = QStringLiteral("embedded message"); | ||
143 | QString subSearch = QStringLiteral("\nSubject:"); | ||
144 | int subPos = partStr.indexOf(subSearch, 0, Qt::CaseInsensitive); | ||
145 | if (-1 < subPos) { | ||
146 | subject = partStr.mid(subPos + subSearch.length()); | ||
147 | thisEoL = subject.indexOf(QLatin1Char('\n')); | ||
148 | if (-1 < thisEoL) { | ||
149 | subject.truncate(thisEoL); | ||
150 | } | ||
151 | } | ||
152 | qCDebug(MIMETREEPARSER_LOG) << " embedded message found: \"" << subject; | ||
153 | mpl->appendSubPart(createAndParseTempNode(part, part.topLevelContent(), partStr.toLatin1().constData(), subject.toLatin1().constData())); | ||
154 | //mReader->queueHtml("<br><hr><br>"); | ||
155 | thisDelim = nextDelim + 1; | ||
156 | nextDelim = str.indexOf(delim1, thisDelim, Qt::CaseInsensitive); | ||
157 | if (-1 == nextDelim) { | ||
158 | nextDelim = str.indexOf(delim2, thisDelim, Qt::CaseInsensitive); | ||
159 | } | ||
160 | if (-1 == nextDelim) { | ||
161 | nextDelim = str.indexOf(delimZ1, thisDelim, Qt::CaseInsensitive); | ||
162 | } | ||
163 | if (-1 == nextDelim) { | ||
164 | nextDelim = str.indexOf(delimZ2, thisDelim, Qt::CaseInsensitive); | ||
165 | } | ||
166 | } | ||
167 | // reset curent node's Content-Type | ||
168 | curNode->contentType()->setMimeType("text/plain"); | ||
169 | int thisEoL = str.indexOf(QLatin1String("_____________"), thisDelim); | ||
170 | if (-1 < thisEoL) { | ||
171 | thisDelim = thisEoL; | ||
172 | thisEoL = str.indexOf(QLatin1Char('\n'), thisDelim); | ||
173 | if (-1 < thisEoL) { | ||
174 | thisDelim = thisEoL + 1; | ||
175 | } | ||
176 | } else { | ||
177 | thisDelim = thisDelim + 1; | ||
178 | } | ||
179 | partStr = QStringLiteral("Content-Type: text/plain\nContent-Description: digest footer\n\n"); | ||
180 | partStr += str.midRef(thisDelim); | ||
181 | mpl->appendSubPart(createAndParseTempNode(part, part.topLevelContent(), partStr.toLatin1().constData(), "Digest Footer")); | ||
182 | return mpl; | ||
183 | } | ||