Mailcommon

filterimportersylpheed.cpp
1 /*
2  SPDX-FileCopyrightText: 2012-2023 Laurent Montel <[email protected]>
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 
14 using namespace MailCommon;
15 
16 FilterImporterSylpheed::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 == QLatin1String("rule")) {
33  parseFilters(e);
34  } else {
35  qCDebug(MAILCOMMON_LOG) << " unknown tag " << tag;
36  }
37  }
38 }
39 
40 FilterImporterSylpheed::~FilterImporterSylpheed() = default;
41 
42 QString FilterImporterSylpheed::defaultFiltersSettingsPath()
43 {
44  return QStringLiteral("%1/.sylpheed-2.0/filter.xml").arg(QDir::homePath());
45 }
46 
47 void 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 == QLatin1String("and")) {
52  filter->pattern()->setOp(SearchPattern::OpAnd);
53  } else if (attr == QLatin1String("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 == QLatin1String("match-header")) {
66  if (ruleFilter.hasAttribute(QStringLiteral("name"))) {
67  const QString attr = ruleFilter.attribute(QStringLiteral("name"));
68  if (attr == QLatin1String("From")) {
69  fieldName = "from";
70  } else if (attr == QLatin1String("Cc")) {
71  fieldName = "cc";
72  } else if (attr == QLatin1String("To")) {
73  fieldName = "to";
74  } else if (attr == QLatin1String("Reply-To")) {
75  fieldName = "reply-to";
76  } else if (attr == QLatin1String("Subject")) {
77  fieldName = "subject";
78  } else if (attr == QLatin1String("List-Id")) {
79  fieldName = "list-id";
80  } else if (attr == QLatin1String("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 == QLatin1String("match-any-header")) {
89  fieldName = "<any header>";
90  contentsName = ruleFilter.text();
91  } else if (nexttag == QLatin1String("match-to-or-cc")) {
92  fieldName = "<recipients>";
93  contentsName = ruleFilter.text();
94  } else if (nexttag == QLatin1String("match-body-text")) {
95  fieldName = "<body>";
96  contentsName = ruleFilter.text();
97  } else if (nexttag == QLatin1String("command-test")) {
98  // TODO
99  // Not implemented in kmail
100  } else if (nexttag == QLatin1String("size")) {
101  fieldName = "<size>";
102  contentsName = QString::number(ruleFilter.text().toInt() * 1024); // Stored as kb
103  } else if (nexttag == QLatin1String("age")) {
104  fieldName = "<age in days>";
105  contentsName = ruleFilter.text();
106  } else if (nexttag == QLatin1String("unread")) {
107  fieldName = "<status>";
108  contentsName = QStringLiteral("Unread");
109  } else if (nexttag == QLatin1String("mark")) {
110  // TODO
111  } else if (nexttag == QLatin1String("color-label")) {
112  // TODO
113  } else if (nexttag == QLatin1String("mime")) {
114  // TODO
115  } else if (nexttag == QLatin1String("account-id")) {
116  // TODO
117  } else if (nexttag == QLatin1String("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 == QLatin1String("not-contain")) {
129  functionName = SearchRule::FuncContainsNot;
130  } else if (attr == QLatin1String("contains")) {
131  functionName = SearchRule::FuncContains;
132  } else if (attr == QLatin1String("is-not")) {
133  functionName = SearchRule::FuncNotEqual;
134  } else if (attr == QLatin1String("is")) {
135  functionName = SearchRule::FuncEquals;
136  } else if (attr == QLatin1String("not-regex")) {
137  functionName = SearchRule::FuncNotRegExp;
138  } else if (attr == QLatin1String("regex")) {
139  functionName = SearchRule::FuncRegExp;
140  } else if (attr == QLatin1String("not-in-addressbook")) {
141  functionName = SearchRule::FuncIsNotInAddressbook;
142  } else if (attr == QLatin1String("in-addressbook")) {
143  functionName = SearchRule::FuncIsInAddressbook;
144  } else if (attr == QLatin1String("gt")) {
145  functionName = SearchRule::FuncIsGreater;
146  } else if (attr == QLatin1String("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 
157 void 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 == QLatin1String("move")) {
164  actionName = QStringLiteral("transfer");
165  value = ruleFilter.text();
166  } else if (nexttag == QLatin1String("copy")) {
167  actionName = QStringLiteral("copy");
168  value = ruleFilter.text();
169  } else if (nexttag == QLatin1String("not-receive")) {
170  // TODO
171  } else if (nexttag == QLatin1String("delete")) {
172  actionName = QStringLiteral("delete");
173  } else if (nexttag == QLatin1String("exec")) {
174  actionName = QStringLiteral("execute");
175  value = ruleFilter.text();
176  } else if (nexttag == QLatin1String("exec-async")) {
177  actionName = QStringLiteral("filter app");
178  value = ruleFilter.text();
179  } else if (nexttag == QLatin1String("mark")) {
180  // FIXME add tag ?
181  } else if (nexttag == QLatin1String("color-label")) {
182  // TODO
183  } else if (nexttag == QLatin1String("mark-as-read")) {
184  actionName = QStringLiteral("set status");
185  value = QStringLiteral("R");
186  } else if (nexttag == QLatin1String("forward")) {
187  actionName = QStringLiteral("forward");
188  value = ruleFilter.text();
189  } else if (nexttag == QLatin1String("forward-as-attachment")) {
190  // TODO
191  } else if (nexttag == QLatin1String("redirect")) {
192  actionName = QStringLiteral("redirect");
193  value = ruleFilter.text();
194  } else if (nexttag == QLatin1String("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 
206 void 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 == QLatin1String("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 == QLatin1String("any")) {
225  filter->setApplyOnInbound(true);
226  filter->setApplyOnExplicit(true);
227  } else if (attr == QLatin1String("receiver")) {
228  filter->setApplyOnInbound(true);
229  } else if (attr == QLatin1String("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 == QLatin1String("condition-list")) {
239  parseConditions(ruleFilter, filter);
240  } else if (nexttag == QLatin1String("action-list")) {
241  parseActions(ruleFilter, filter);
242  } else {
243  qCDebug(MAILCOMMON_LOG) << " next tag not implemented " << nexttag;
244  }
245  }
246 
247  appendFilter(filter);
248 }
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition: searchrule.h:29
QString number(int n, int base)
bool isNull() const const
QString homePath()
The MailFilter class.
Definition: mailfilter.h:28
bool isEmpty() const const
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...
Definition: searchrule.cpp:75
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
QDomElement documentElement() const const
Function
Describes operators for comparison of field and contents.
Definition: searchrule.h:40
QDomElement firstChildElement(const QString &tagName) const const
bool hasAttribute(const QString &name) const const
bool isEmpty() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString attribute(const QString &name, const QString &defValue) const const
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 03:58:16 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.