Mailcommon

filterimporterprocmail.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 "filterimporterprocmail.h"
8 #include "filter/filtermanager.h"
9 #include "filter/mailfilter.h"
10 #include "mailcommon_debug.h"
11 #include <KLocalizedString>
12 
13 #include <QDir>
14 #include <QFile>
15 
16 using namespace MailCommon;
17 
18 FilterImporterProcmail::FilterImporterProcmail(QFile *file)
19  : FilterImporterAbstract()
20 {
21  QTextStream stream(file);
22  readStream(stream);
23 }
24 
25 FilterImporterProcmail::FilterImporterProcmail(QString string)
26  : FilterImporterAbstract()
27 {
28  QTextStream stream(&string);
29  readStream(stream);
30 }
31 
32 FilterImporterProcmail::~FilterImporterProcmail() = default;
33 
34 void FilterImporterProcmail::readStream(QTextStream &stream)
35 {
36  MailFilter *filter = nullptr;
37  while (!stream.atEnd()) {
38  QString line = stream.readLine();
39  qCDebug(MAILCOMMON_LOG) << " line :" << line << " filter " << filter;
40  filter = parseLine(stream, line, filter);
41  }
42 
43  appendFilter(filter);
44 }
45 
46 QString FilterImporterProcmail::defaultFiltersSettingsPath()
47 {
48  return QDir::homePath();
49 }
50 
51 QString FilterImporterProcmail::createUniqFilterName()
52 {
53  return i18n("Procmail filter %1", ++mFilterCount);
54 }
55 
56 MailCommon::MailFilter *FilterImporterProcmail::parseLine(QTextStream &stream, QString line, MailCommon::MailFilter *filter)
57 {
58  Q_UNUSED(stream)
59  if (line.isEmpty()) {
60  // Empty line
61  return filter;
62  } else if (line.startsWith(QLatin1Char('#'))) {
63  // Commented line
64  return filter;
65  } else if (line.startsWith(QLatin1String(":0"))) {
66  appendFilter(filter);
67  filter = new MailFilter();
68  const QString uniqName = createUniqFilterName();
69  filter->pattern()->setName(uniqName);
70  filter->setToolbarName(uniqName);
71  } else if (line.startsWith(QLatin1String("* "))) {
72  line.remove(0, 2);
73  QByteArray fieldName;
74  SearchRule::Function functionName = SearchRule::FuncRegExp;
75  if (line.startsWith(QLatin1String("^From:"))) {
76  line.remove(QStringLiteral("^From:"));
77  fieldName = "from";
78  } else if (line.startsWith(QLatin1String("^Subject:"))) {
79  line.remove(QStringLiteral("^Subject:"));
80  fieldName = "subject";
81  } else if (line.startsWith(QLatin1String("^Sender:"))) {
82  line.remove(QStringLiteral("^Sender:"));
83  } else if (line.startsWith(QLatin1String("^(To|Cc):"))) {
84  line.remove(QStringLiteral("^(To|Cc):"));
85  fieldName = "<recipients>";
86  } else {
87  qCDebug(MAILCOMMON_LOG) << " line condition not parsed :" << line;
88  }
89  SearchRule::Ptr rule = SearchRule::createInstance(fieldName, functionName, line);
90  filter->pattern()->append(rule);
91  // Condition
92  } else if (line.startsWith(QLatin1Char('!'))) {
93  line.remove(QLatin1Char('!'));
94  // Redirect email
95  } else if (line.startsWith(QLatin1Char('|'))) {
96  // Shell
97  const QString actionName(QStringLiteral("execute"));
98  const QString value(line);
99  createFilterAction(filter, actionName, value);
100  } else if (line.startsWith(QLatin1Char('{'))) {
101  // Block
102  } else if (line.startsWith(QLatin1Char('}'))) {
103  // End block
104  } else {
105  const QString actionName(QStringLiteral("transfer"));
106  const QString value(line);
107  createFilterAction(filter, actionName, value);
108  // Folder
109  }
110 
111  return filter;
112 }
std::shared_ptr< SearchRule > Ptr
Defines a pointer to a search rule.
Definition: searchrule.h:29
QString homePath()
The MailFilter class.
Definition: mailfilter.h:28
QString i18n(const char *text, const TYPE &arg...)
bool atEnd() const const
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
QString readLine(qint64 maxlen)
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
Function
Describes operators for comparison of field and contents.
Definition: searchrule.h:40
QString & remove(int position, int n)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Oct 1 2022 04:00:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.