Mailcommon

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

KDE's Doxygen guidelines are available online.