Mailcommon

filterimportersylpheed.cpp
1/*
2 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "filterimportersylpheed.h"
8#include "filter/mailfilter.h"
9#include "mailcommon_debug.h"
10
11#include <QDir>
12#include <QFile>
13
14using namespace MailCommon;
15
16FilterImporterSylpheed::FilterImporterSylpheed(QFile *file)
17 : FilterImporterAbstract()
18{
19 QDomDocument doc;
20 if (!loadDomElement(doc, file)) {
21 return;
22 }
23 QDomElement filters = doc.documentElement();
24
25 if (filters.isNull()) {
26 qCDebug(MAILCOMMON_LOG) << "No filters defined";
27 return;
28 }
29
30 for (QDomElement e = filters.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
31 const QString tag = e.tagName();
32 if (tag == QLatin1StringView("rule")) {
33 parseFilters(e);
34 } else {
35 qCDebug(MAILCOMMON_LOG) << " unknown tag " << tag;
36 }
37 }
38}
39
40FilterImporterSylpheed::~FilterImporterSylpheed() = default;
41
42QString FilterImporterSylpheed::defaultFiltersSettingsPath()
43{
44 return QStringLiteral("%1/.sylpheed-2.0/filter.xml").arg(QDir::homePath());
45}
46
47void FilterImporterSylpheed::parseConditions(const QDomElement &e, MailCommon::MailFilter *filter)
48{
49 if (e.hasAttribute(QStringLiteral("bool"))) {
50 const QString attr = e.attribute(QStringLiteral("bool"));
51 if (attr == QLatin1StringView("and")) {
52 filter->pattern()->setOp(SearchPattern::OpAnd);
53 } else if (attr == QLatin1StringView("or")) {
54 filter->pattern()->setOp(SearchPattern::OpOr);
55 } else {
56 qCDebug(MAILCOMMON_LOG) << " bool not defined: " << attr;
57 }
58 }
59 for (QDomElement ruleFilter = e.firstChildElement(); !ruleFilter.isNull(); ruleFilter = ruleFilter.nextSiblingElement()) {
60 QString contentsName;
61 QByteArray fieldName;
62 SearchRule::Function functionName = SearchRule::FuncNone;
63
64 const QString nexttag = ruleFilter.tagName();
65 if (nexttag == QLatin1StringView("match-header")) {
66 if (ruleFilter.hasAttribute(QStringLiteral("name"))) {
67 const QString attr = ruleFilter.attribute(QStringLiteral("name"));
68 if (attr == QLatin1StringView("From")) {
69 fieldName = "from";
70 } else if (attr == QLatin1StringView("Cc")) {
71 fieldName = "cc";
72 } else if (attr == QLatin1StringView("To")) {
73 fieldName = "to";
74 } else if (attr == QLatin1StringView("Reply-To")) {
75 fieldName = "reply-to";
76 } else if (attr == QLatin1StringView("Subject")) {
77 fieldName = "subject";
78 } else if (attr == QLatin1StringView("List-Id")) {
79 fieldName = "list-id";
80 } else if (attr == QLatin1StringView("X-ML-Name")) {
81 fieldName = "x-mailing-list";
82 }
83 if (fieldName.isEmpty()) {
84 qCDebug(MAILCOMMON_LOG) << " match-header not implemented " << attr;
85 }
86 }
87 contentsName = ruleFilter.text();
88 } else if (nexttag == QLatin1StringView("match-any-header")) {
89 fieldName = "<any header>";
90 contentsName = ruleFilter.text();
91 } else if (nexttag == QLatin1StringView("match-to-or-cc")) {
92 fieldName = "<recipients>";
93 contentsName = ruleFilter.text();
94 } else if (nexttag == QLatin1StringView("match-body-text")) {
95 fieldName = "<body>";
96 contentsName = ruleFilter.text();
97 } else if (nexttag == QLatin1StringView("command-test")) {
98 // TODO
99 // Not implemented in kmail
100 } else if (nexttag == QLatin1StringView("size")) {
101 fieldName = "<size>";
102 contentsName = QString::number(ruleFilter.text().toInt() * 1024); // Stored as kb
103 } else if (nexttag == QLatin1StringView("age")) {
104 fieldName = "<age in days>";
105 contentsName = ruleFilter.text();
106 } else if (nexttag == QLatin1StringView("unread")) {
107 fieldName = "<status>";
108 contentsName = QStringLiteral("Unread");
109 } else if (nexttag == QLatin1StringView("mark")) {
110 // TODO
111 } else if (nexttag == QLatin1StringView("color-label")) {
112 // TODO
113 } else if (nexttag == QLatin1StringView("mime")) {
114 // TODO
115 } else if (nexttag == QLatin1StringView("account-id")) {
116 // TODO
117 } else if (nexttag == QLatin1StringView("target-folder")) {
118 // TODO
119 } else {
120 qCDebug(MAILCOMMON_LOG) << " tag not recognize " << nexttag;
121 }
122 if (fieldName.isEmpty()) {
123 qCDebug(MAILCOMMON_LOG) << " field not implemented " << nexttag;
124 }
125
126 if (ruleFilter.hasAttribute(QStringLiteral("type"))) {
127 const QString attr = ruleFilter.attribute(QStringLiteral("type"));
128 if (attr == QLatin1StringView("not-contain")) {
129 functionName = SearchRule::FuncContainsNot;
130 } else if (attr == QLatin1StringView("contains")) {
131 functionName = SearchRule::FuncContains;
132 } else if (attr == QLatin1StringView("is-not")) {
133 functionName = SearchRule::FuncNotEqual;
134 } else if (attr == QLatin1StringView("is")) {
135 functionName = SearchRule::FuncEquals;
136 } else if (attr == QLatin1StringView("not-regex")) {
137 functionName = SearchRule::FuncNotRegExp;
138 } else if (attr == QLatin1StringView("regex")) {
139 functionName = SearchRule::FuncRegExp;
140 } else if (attr == QLatin1StringView("not-in-addressbook")) {
141 functionName = SearchRule::FuncIsNotInAddressbook;
142 } else if (attr == QLatin1StringView("in-addressbook")) {
143 functionName = SearchRule::FuncIsInAddressbook;
144 } else if (attr == QLatin1StringView("gt")) {
145 functionName = SearchRule::FuncIsGreater;
146 } else if (attr == QLatin1StringView("lt")) {
147 functionName = SearchRule::FuncIsLess;
148 } else {
149 qCDebug(MAILCOMMON_LOG) << " Attr type not implemented :" << attr;
150 }
151 }
152 SearchRule::Ptr rule = SearchRule::createInstance(fieldName, functionName, contentsName);
153 filter->pattern()->append(rule);
154 }
155}
156
157void FilterImporterSylpheed::parseActions(const QDomElement &e, MailCommon::MailFilter *filter)
158{
159 for (QDomElement ruleFilter = e.firstChildElement(); !ruleFilter.isNull(); ruleFilter = ruleFilter.nextSiblingElement()) {
160 QString actionName;
161 const QString nexttag = ruleFilter.tagName();
162 QString value = ruleFilter.text();
163 if (nexttag == QLatin1StringView("move")) {
164 actionName = QStringLiteral("transfer");
165 value = ruleFilter.text();
166 } else if (nexttag == QLatin1StringView("copy")) {
167 actionName = QStringLiteral("copy");
168 value = ruleFilter.text();
169 } else if (nexttag == QLatin1StringView("not-receive")) {
170 // TODO
171 } else if (nexttag == QLatin1StringView("delete")) {
172 actionName = QStringLiteral("delete");
173 } else if (nexttag == QLatin1StringView("exec")) {
174 actionName = QStringLiteral("execute");
175 value = ruleFilter.text();
176 } else if (nexttag == QLatin1StringView("exec-async")) {
177 actionName = QStringLiteral("filter app");
178 value = ruleFilter.text();
179 } else if (nexttag == QLatin1StringView("mark")) {
180 // FIXME add tag ?
181 } else if (nexttag == QLatin1StringView("color-label")) {
182 // TODO
183 } else if (nexttag == QLatin1StringView("mark-as-read")) {
184 actionName = QStringLiteral("set status");
185 value = QStringLiteral("R");
186 } else if (nexttag == QLatin1StringView("forward")) {
187 actionName = QStringLiteral("forward");
188 value = ruleFilter.text();
189 } else if (nexttag == QLatin1StringView("forward-as-attachment")) {
190 // TODO
191 } else if (nexttag == QLatin1StringView("redirect")) {
192 actionName = QStringLiteral("redirect");
193 value = ruleFilter.text();
194 } else if (nexttag == QLatin1StringView("stop-eval")) {
195 filter->setStopProcessingHere(true);
196 break;
197 }
198
199 if (actionName.isEmpty()) {
200 qCDebug(MAILCOMMON_LOG) << " tag not recognize " << nexttag;
201 }
202 createFilterAction(filter, actionName, value);
203 }
204}
205
206void FilterImporterSylpheed::parseFilters(const QDomElement &e)
207{
208 auto filter = new MailCommon::MailFilter();
209 if (e.hasAttribute(QStringLiteral("enabled"))) {
210 const QString attr = e.attribute(QStringLiteral("enabled"));
211 if (attr == QLatin1StringView("false")) {
212 filter->setEnabled(false);
213 }
214 }
215
216 if (e.hasAttribute(QStringLiteral("name"))) {
217 const QString attr = e.attribute(QStringLiteral("name"));
218 filter->pattern()->setName(attr);
219 filter->setToolbarName(attr);
220 }
221
222 if (e.hasAttribute(QStringLiteral("timing"))) {
223 const QString attr = e.attribute(QStringLiteral("timing"));
224 if (attr == QLatin1StringView("any")) {
225 filter->setApplyOnInbound(true);
226 filter->setApplyOnExplicit(true);
227 } else if (attr == QLatin1StringView("receiver")) {
228 filter->setApplyOnInbound(true);
229 } else if (attr == QLatin1StringView("manual")) {
230 filter->setApplyOnInbound(false);
231 filter->setApplyOnExplicit(true);
232 } else {
233 qCDebug(MAILCOMMON_LOG) << " timing not defined: " << attr;
234 }
235 }
236 for (QDomElement ruleFilter = e.firstChildElement(); !ruleFilter.isNull(); ruleFilter = ruleFilter.nextSiblingElement()) {
237 const QString nexttag = ruleFilter.tagName();
238 if (nexttag == QLatin1StringView("condition-list")) {
239 parseConditions(ruleFilter, filter);
240 } else if (nexttag == QLatin1StringView("action-list")) {
241 parseActions(ruleFilter, filter);
242 } else {
243 qCDebug(MAILCOMMON_LOG) << " next tag not implemented " << nexttag;
244 }
245 }
246
247 appendFilter(filter);
248}
The MailFilter class.
Definition mailfilter.h:29
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition searchrule.h:29
Function
Describes operators for comparison of field and contents.
Definition searchrule.h:40
static SearchRule::Ptr createInstance(const QByteArray &field=QByteArray(), Function function=FuncContains, const QString &contents=QString())
Creates a new search rule of a certain type by instantiating the appropriate subclass depending on th...
The filter dialog.
bool isEmpty() const const
QString homePath()
QDomElement documentElement() const const
QString attribute(const QString &name, const QString &defValue) const const
bool hasAttribute(const QString &name) const const
QDomElement firstChildElement(const QString &tagName, const QString &namespaceURI) const const
bool isNull() const const
QString arg(Args &&... args) const const
bool isEmpty() const const
QString number(double n, char format, int precision)
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:14:00 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.