Libksieve

legacy/vacationutils.cpp
1 /*
2  Copyright (C) 2013-2020 Laurent Montel <[email protected]>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 /*
19 
20 This file only contains legacy code, that can be removed if the legacy code is not needed anymore.
21 See README for further information.
22 
23 */
24 #include "vacationutils.h"
25 #include "vacationscriptextractor.h"
26 #include "sieve-vacation.h"
27 
28 #include <QDate>
29 #include <QRegularExpression>
30 
32 using namespace KSieveUi::Legacy::VacationUtils;
33 using namespace KSieveUi;
34 
35 static inline QString dotstuff(QString s) // krazy:exclude=passbyvalue
36 {
37  if (s.startsWith(QLatin1Char('.'))) {
38  return QLatin1Char('.') + s.replace(QLatin1String("\n."), QStringLiteral("\n.."));
39  } else {
40  return s.replace(QLatin1String("\n."), QStringLiteral("\n.."));
41  }
42 }
43 
44 static inline QString stringReplace(QString s)
45 {
46  s.replace(QRegularExpression(QStringLiteral("[\n\t]+")), QStringLiteral(" "));
47  return s.replace(QLatin1Char('\"'), QStringLiteral("\\\""));
48 }
49 
50 /*
51 
52 This file only contains legacy code, that can be removed if the legacy code is not needed anymore.
53 See README for further information.
54 
55 */
56 bool Legacy::VacationUtils::parseScript(const QString &script, QString &messageText, QString &subject, int &notificationInterval, AddrSpecList &aliases, bool &sendForSpam, QString &domainName, QDate &startDate, QDate &endDate)
57 {
58  const QString trimmedScript = script.trimmed();
59  if (trimmedScript.isEmpty()) {
60  return false;
61  }
62 
63  // The trimmed() call below prevents parsing errors. The
64  // slave somehow omits the last \n, which results in a lone \r at
65  // the end, leading to a parse error.
66  const QByteArray scriptUTF8 = trimmedScript.toUtf8();
67  qCDebug(LIBKSIEVE_LOG) << "scriptUtf8 = \"" + scriptUTF8 + "\"";
68  KSieve::Parser parser(scriptUTF8.begin(),
69  scriptUTF8.begin() + scriptUTF8.length());
70  KSieveUi::Legacy::VacationDataExtractor vdx;
71  KSieveUi::Legacy::SpamDataExtractor sdx;
72  KSieveUi::Legacy::DomainRestrictionDataExtractor drdx;
73  KSieveUi::Legacy::DateExtractor dtx;
74  KSieveExt::MultiScriptBuilder tsb(&vdx, &sdx, &drdx, &dtx);
75  parser.setScriptBuilder(&tsb);
76  if (!parser.parse() || !vdx.commandFound()) {
77  return false;
78  }
79  messageText = vdx.messageText().trimmed();
80  if (!vdx.subject().isEmpty()) {
81  subject = vdx.subject().trimmed();
82  }
83  notificationInterval = vdx.notificationInterval();
84  aliases.clear();
85  const QStringList lstAliases = vdx.aliases();
86  for (const QString &alias : lstAliases) {
88  a.fromUnicodeString(alias);
89  aliases.append(a.addrSpec());
90  }
91  if (!VacationSettings::allowOutOfOfficeUploadButNoSettings()) {
92  sendForSpam = !sdx.found();
93  domainName = drdx.domainName();
94  }
95  startDate = dtx.startDate();
96  endDate = dtx.endDate();
97  return true;
98 }
99 
100 /*
101 
102 This file only contains legacy code, that can be removed if the legacy code is not needed anymore.
103 See README for further information.
104 
105 */
106 QString Legacy::VacationUtils::composeScript(const QString &messageText, const QString &subject, int notificationInterval, const AddrSpecList &addrSpecs, bool sendForSpam, const QString &domain, const QDate &startDate, const QDate &endDate)
107 {
108  QString addressesArgument;
109  QStringList aliases;
110  if (!addrSpecs.empty()) {
111  addressesArgument += QLatin1String(":addresses [ ");
112  QStringList sl;
113  AddrSpecList::const_iterator end = addrSpecs.constEnd();
114  for (AddrSpecList::const_iterator it = addrSpecs.begin(); it != end; ++it) {
115  sl.push_back(QLatin1Char('"') + (*it).asString().replace(QLatin1Char('\\'), QStringLiteral("\\\\")).replace(QLatin1Char('"'), QStringLiteral("\\\"")) + QLatin1Char('"'));
116  aliases.push_back((*it).asString());
117  }
118  addressesArgument += sl.join(QLatin1String(", ")) + QLatin1String(" ] ");
119  }
120 
121  QString script = QStringLiteral("require \"vacation\";\n");
122  if (startDate.isValid() && endDate.isValid()) {
123  script += QStringLiteral("require \"relational\";\n"
124  "require \"date\";\n\n");
125  } else {
126  script += QStringLiteral("\n");
127  }
128 
129  if (!sendForSpam) {
130  script += QStringLiteral("if header :contains \"X-Spam-Flag\" \"YES\""
131  " { keep; stop; }\n"); // FIXME?
132  }
133  if (!domain.isEmpty()) { // FIXME
134  script += QStringLiteral("if not address :domain :contains \"from\" \"%1\" { keep; stop; }\n").arg(domain);
135  }
136 
137  if (startDate.isValid() && endDate.isValid()) {
138  script += QStringLiteral("if not allof(currentdate :value \"ge\" \"date\" \"%1\","
139  " currentdate :value \"le\" \"date\" \"%2\")"
140  " { keep; stop; }\n").arg(startDate.toString(Qt::ISODate),
141  endDate.toString(Qt::ISODate));
142  }
143 
144  script += QLatin1String("vacation ");
145  script += addressesArgument;
146  if (notificationInterval > 0) {
147  script += QStringLiteral(":days %1 ").arg(notificationInterval);
148  }
149 
150  if (!subject.trimmed().isEmpty()) {
151  script += QStringLiteral(":subject \"%1\" ").arg(stringReplace(subject).trimmed());
152  }
153 
154  script += QStringLiteral("text:\n");
155  script += dotstuff(messageText.isEmpty() ? KSieveUi::VacationUtils::defaultMessageText() : messageText);
156  script += QStringLiteral("\n.\n;\n");
157  return script;
158 }
QString toString(Qt::DateFormat format) const const
void push_back(const T &value)
QString join(const QString &separator) const const
int length() const const
void clear()
void fromUnicodeString(const QString &s)
bool isEmpty() const const
QString trimmed() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
bool isValid() const const
QByteArray::iterator begin()
QString subject() const
QString & replace(int position, int n, QChar after)
const QList< QKeySequence > & end()
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
Parser for the Sieve grammar.
Definition: ksieve/parser.h:45
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 1 2020 23:08:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.