Mailcommon

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

KDE's Doxygen guidelines are available online.