Libksieve

legacy/vacationutils.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 /*
8 
9 This file only contains legacy code, that can be removed if the legacy code is not needed anymore.
10 See README for further information.
11 
12 */
13 #include "vacationutils.h"
14 #include "sieve-vacation.h"
15 #include "vacationscriptextractor.h"
16 
17 #include <QDate>
18 #include <QRegularExpression>
19 
21 using namespace KSieveUi::Legacy::VacationUtils;
22 using namespace KSieveUi;
23 
24 static inline QString dotstuff(QString s) // krazy:exclude=passbyvalue
25 {
26  if (s.startsWith(QLatin1Char('.'))) {
27  return QLatin1Char('.') + s.replace(QLatin1String("\n."), QStringLiteral("\n.."));
28  } else {
29  return s.replace(QLatin1String("\n."), QStringLiteral("\n.."));
30  }
31 }
32 
33 static inline QString stringReplace(QString s)
34 {
35  s.replace(QRegularExpression(QStringLiteral("[\n\t]+")), QStringLiteral(" "));
36  return s.replace(QLatin1Char('\"'), QStringLiteral("\\\""));
37 }
38 
39 /*
40 
41 This file only contains legacy code, that can be removed if the legacy code is not needed anymore.
42 See README for further information.
43 
44 */
45 bool Legacy::VacationUtils::parseScript(const QString &script,
46  QString &messageText,
47  QString &subject,
48  int &notificationInterval,
49  AddrSpecList &aliases,
50  bool &sendForSpam,
51  QString &domainName,
52  QDate &startDate,
53  QDate &endDate)
54 {
55  const QString trimmedScript = script.trimmed();
56  if (trimmedScript.isEmpty()) {
57  return false;
58  }
59 
60  // The trimmed() call below prevents parsing errors. The
61  // slave somehow omits the last \n, which results in a lone \r at
62  // the end, leading to a parse error.
63  const QByteArray scriptUTF8 = trimmedScript.toUtf8();
64  qCDebug(LIBKSIEVE_LOG) << "scriptUtf8 = \"" + scriptUTF8 + "\"";
65  KSieve::Parser parser(scriptUTF8.begin(), scriptUTF8.begin() + scriptUTF8.length());
66  KSieveUi::Legacy::VacationDataExtractor vdx;
67  KSieveUi::Legacy::SpamDataExtractor sdx;
68  KSieveUi::Legacy::DomainRestrictionDataExtractor drdx;
69  KSieveUi::Legacy::DateExtractor dtx;
70  KSieveExt::MultiScriptBuilder tsb(&vdx, &sdx, &drdx, &dtx);
71  parser.setScriptBuilder(&tsb);
72  if (!parser.parse() || !vdx.commandFound()) {
73  return false;
74  }
75  messageText = vdx.messageText().trimmed();
76  if (!vdx.subject().isEmpty()) {
77  subject = vdx.subject().trimmed();
78  }
79  notificationInterval = vdx.notificationInterval();
80  aliases.clear();
81  const QStringList lstAliases = vdx.aliases();
82  for (const QString &alias : lstAliases) {
84  a.fromUnicodeString(alias);
85  aliases.append(a.addrSpec());
86  }
87  if (!VacationSettings::allowOutOfOfficeUploadButNoSettings()) {
88  sendForSpam = !sdx.found();
89  domainName = drdx.domainName();
90  }
91  startDate = dtx.startDate();
92  endDate = dtx.endDate();
93  return true;
94 }
95 
96 /*
97 
98 This file only contains legacy code, that can be removed if the legacy code is not needed anymore.
99 See README for further information.
100 
101 */
102 QString Legacy::VacationUtils::composeScript(const QString &messageText,
103  const QString &subject,
104  int notificationInterval,
105  const AddrSpecList &addrSpecs,
106  bool sendForSpam,
107  const QString &domain,
108  const QDate &startDate,
109  const QDate &endDate)
110 {
111  QString addressesArgument;
112  QStringList aliases;
113  if (!addrSpecs.empty()) {
114  addressesArgument += QLatin1String(":addresses [ ");
115  QStringList sl;
116  AddrSpecList::const_iterator end = addrSpecs.constEnd();
117  for (AddrSpecList::const_iterator it = addrSpecs.begin(); it != end; ++it) {
118  sl.push_back(QLatin1Char('"')
119  + (*it).asString().replace(QLatin1Char('\\'), QStringLiteral("\\\\")).replace(QLatin1Char('"'), QStringLiteral("\\\""))
120  + QLatin1Char('"'));
121  aliases.push_back((*it).asString());
122  }
123  addressesArgument += sl.join(QLatin1String(", ")) + QLatin1String(" ] ");
124  }
125 
126  QString script = QStringLiteral("require \"vacation\";\n");
127  if (startDate.isValid() && endDate.isValid()) {
128  script += QStringLiteral(
129  "require \"relational\";\n"
130  "require \"date\";\n\n");
131  } else {
132  script += QStringLiteral("\n");
133  }
134 
135  if (!sendForSpam) {
136  script += QStringLiteral(
137  "if header :contains \"X-Spam-Flag\" \"YES\""
138  " { keep; stop; }\n"); // FIXME?
139  }
140  if (!domain.isEmpty()) { // FIXME
141  script += QStringLiteral("if not address :domain :contains \"from\" \"%1\" { keep; stop; }\n").arg(domain);
142  }
143 
144  if (startDate.isValid() && endDate.isValid()) {
145  script += QStringLiteral(
146  "if not allof(currentdate :value \"ge\" \"date\" \"%1\","
147  " currentdate :value \"le\" \"date\" \"%2\")"
148  " { keep; stop; }\n")
149  .arg(startDate.toString(Qt::ISODate), endDate.toString(Qt::ISODate));
150  }
151 
152  script += QLatin1String("vacation ");
153  script += addressesArgument;
154  if (notificationInterval > 0) {
155  script += QStringLiteral(":days %1 ").arg(notificationInterval);
156  }
157 
158  if (!subject.trimmed().isEmpty()) {
159  script += QStringLiteral(":subject \"%1\" ").arg(stringReplace(subject).trimmed());
160  }
161 
162  script += QStringLiteral("text:\n");
163  script += dotstuff(messageText.isEmpty() ? KSieveUi::VacationUtils::defaultMessageText() : messageText);
164  script += QStringLiteral("\n.\n;\n");
165  return script;
166 }
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 & 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:23
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Apr 16 2021 23:09:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.