Libksieve

legacy/vacationscriptextractor.h
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 #ifndef LEGACY_VACATIONSCRIPTEXTRACTOR_H
19 #define LEGACY_VACATIONSCRIPTEXTRACTOR_H
20 
21 #include "sieve-vacation.h"
22 #include "../vacationscriptextractor.h"
23 #include "ksieveui_export.h"
24 
25 #include <ksieve/error.h>
26 #include <ksieve/parser.h>
27 #include <ksieve/scriptbuilder.h>
28 
29 #include "libksieve_debug.h"
30 #include <cassert>
31 #include <limits.h>
32 #include <map>
33 #include <set>
34 #include <vector>
35 
36 namespace KSieveUi {
37 namespace Legacy {
38 /*
39 
40 This file only contains legacy code, that can be removed if the legacy code is not needed anymore.
41 See README for further information.
42 
43 */
44 static const GenericInformationExtractor::StateNode spamNodes[] = {
45  { 0, GIE::CommandStart, "if", 1, 0, nullptr }, // 0
46  { 0, GIE::TestStart, "header", 2, 0, nullptr }, // 1
47  { 0, GIE::TaggedArgument, "contains", 3, 0, nullptr }, // 2
48 
49  // accept both string and string-list:
50  { 0, GIE::StringArgument, "x-spam-flag", 9, 4, "x-spam-flag" }, // 3
51  { 0, GIE::StringListArgumentStart, nullptr, 5, 0, nullptr }, // 4
52  { 0, GIE::StringListEntry, "x-spam-flag", 6, 7, "x-spam-flag" }, // 5
53  { 0, GIE::StringListEntry, nullptr, 6, 8, nullptr }, // 6
54  { 0, GIE::StringListArgumentEnd, nullptr, 0, 5, nullptr }, // 7
55  { 0, GIE::StringListArgumentEnd, nullptr, 9, 0, nullptr }, // 8
56 
57  // accept both string and string-list:
58  { 0, GIE::StringArgument, "yes", 15, 10, "spam-flag-yes" }, // 9
59  { 0, GIE::StringListArgumentStart, nullptr, 11, 0, nullptr }, // 10
60  { 0, GIE::StringListEntry, "yes", 12, 13, "spam-flag-yes" }, // 11
61  { 0, GIE::StringListEntry, nullptr, 12, 14, nullptr }, // 12
62  { 0, GIE::StringListArgumentEnd, nullptr, 0, 11, nullptr }, // 13
63  { 0, GIE::StringListArgumentEnd, nullptr, 15, 0, nullptr }, // 14
64 
65  { 0, GIE::TestEnd, nullptr, 16, 0, nullptr }, // 15
66 
67  // block of command, find "stop", take nested if's into account:
68  { 0, GIE::BlockStart, nullptr, 17, 0, nullptr }, // 16
69  { 1, GIE::CommandStart, "stop", 20, 19, "stop" }, // 17
70  { -1, GIE::Any, nullptr, 17, 0, nullptr }, // 18
71  { 0, GIE::BlockEnd, nullptr, 0, 18, nullptr }, // 19
72 
73  { -1, GIE::Any, nullptr, 20, 20, nullptr }, // 20 end state
74 };
75 static const unsigned int numSpamNodes = sizeof spamNodes / sizeof *spamNodes;
76 
77 /*
78 
79 This file only contains legacy code, that can be removed if the legacy code is not needed anymore.
80 See README for further information.
81 
82 */
83 class KSIEVEUI_DEPRECATED SpamDataExtractor : public GenericInformationExtractor
84 {
85 public:
86  SpamDataExtractor() : GenericInformationExtractor(std::vector<StateNode>(spamNodes, spamNodes + numSpamNodes))
87  {
88  }
89 
90  bool found() const
91  {
92  return mResults.count(QStringLiteral("x-spam-flag"))
93  && mResults.count(QStringLiteral("spam-flag-yes"))
94  && mResults.count(QStringLiteral("stop"));
95  }
96 };
97 
98 // to understand this table, study the output of
99 // libksieve/tests/parsertest
100 // 'if not address :domain :contains ["from"] ["mydomain.org"] { keep; stop; }'
101 static const GenericInformationExtractor::StateNode domainNodes[] = {
102  { 0, GIE::CommandStart, "if", 1, 0, nullptr }, // 0
103  { 0, GIE::TestStart, "not", 2, 0, nullptr, }, // 1
104  { 0, GIE::TestStart, "address", 3, 0, nullptr }, // 2
105 
106  // :domain and :contains in arbitrary order:
107  { 0, GIE::TaggedArgument, "domain", 4, 5, nullptr }, // 3
108  { 0, GIE::TaggedArgument, "contains", 7, 0, nullptr }, // 4
109  { 0, GIE::TaggedArgument, "contains", 6, 0, nullptr }, // 5
110  { 0, GIE::TaggedArgument, "domain", 7, 0, nullptr }, // 6
111 
112  // accept both string and string-list:
113  { 0, GIE::StringArgument, "from", 13, 8, "from" }, // 7
114  { 0, GIE::StringListArgumentStart, nullptr, 9, 0, nullptr }, // 8
115  { 0, GIE::StringListEntry, "from", 10, 11, "from" }, // 9
116  { 0, GIE::StringListEntry, nullptr, 10, 12, nullptr }, // 10
117  { 0, GIE::StringListArgumentEnd, nullptr, 0, 9, nullptr }, // 11
118  { 0, GIE::StringListArgumentEnd, nullptr, 13, 0, nullptr }, // 12
119 
120  // string: save, string-list: save last
121  { 0, GIE::StringArgument, nullptr, 17, 14, "domainName" }, // 13
122  { 0, GIE::StringListArgumentStart, nullptr, 15, 0, nullptr }, // 14
123  { 0, GIE::StringListEntry, nullptr, 15, 16, "domainName" }, // 15
124  { 0, GIE::StringListArgumentEnd, nullptr, 17, 0, nullptr }, // 16
125 
126  { 0, GIE::TestEnd, nullptr, 18, 0, nullptr }, // 17
127  { 0, GIE::TestEnd, nullptr, 19, 0, nullptr }, // 18
128 
129  // block of commands, find "stop", take nested if's into account:
130  { 0, GIE::BlockStart, nullptr, 20, 0, nullptr }, // 19
131  { 1, GIE::CommandStart, "stop", 23, 22, "stop" }, // 20
132  { -1, GIE::Any, nullptr, 20, 0, nullptr }, // 21
133  { 0, GIE::BlockEnd, nullptr, 0, 21, nullptr }, // 22
134 
135  { -1, GIE::Any, nullptr, 23, 23, nullptr } // 23 end state
136 };
137 static const unsigned int numDomainNodes = sizeof domainNodes / sizeof *domainNodes;
138 
139 /*
140 
141 This file only contains legacy code, that can be removed if the legacy code is not needed anymore.
142 See README for further information.
143 
144 */
145 class KSIEVEUI_DEPRECATED DomainRestrictionDataExtractor : public GenericInformationExtractor
146 {
147 public:
148  DomainRestrictionDataExtractor() : GenericInformationExtractor(std::vector<StateNode>(domainNodes, domainNodes + numDomainNodes))
149  {
150  }
151 
152  QString domainName() /*not const, since map::op[] isn't const*/
153  {
154  return mResults.count(QStringLiteral("stop")) && mResults.count(QStringLiteral("from"))
155  ? mResults[QStringLiteral("domainName")] : QString();
156  }
157 };
158 
159 // if not allof (currentDate :value "ge" date "YYYY-MM-DD",
160 // currentDate :value "le" date "YYYY-MM-DD") { keep; stop; }
161 static const GenericInformationExtractor::StateNode datesNodes[] = {
162  { 0, GIE::CommandStart, "if", 1, 0, nullptr }, // 0
163  { 0, GIE::TestStart, "not", 2, 0, nullptr }, // 1
164  { 0, GIE::TestStart, "allof", 3, 0, nullptr }, // 2
165 
166  // handle startDate and endDate in arbitrary order
167  { 0, GIE::TestListStart, nullptr, 4, 0, nullptr }, // 3
168  { 0, GIE::TestStart, "currentdate", 5, 0, nullptr }, // 4
169  { 0, GIE::TaggedArgument, "value", 6, 0, nullptr }, // 5
170  { 0, GIE::StringArgument, "ge", 7, 9, nullptr }, // 6
171  { 0, GIE::StringArgument, "date", 8, 0, nullptr }, // 7
172  { 0, GIE::StringArgument, nullptr, 12, 0, "startDate" }, // 8
173  { 0, GIE::StringArgument, "le", 10, 0, nullptr }, // 9
174  { 0, GIE::StringArgument, "date", 11, 0, nullptr }, // 10
175  { 0, GIE::StringArgument, nullptr, 12, 0, "endDate" }, // 11
176  { 0, GIE::TestEnd, nullptr, 13, 0, nullptr }, // 12
177 
178  { 0, GIE::TestStart, "currentdate", 14, 0, nullptr }, // 13
179  { 0, GIE::TaggedArgument, "value", 15, 0, nullptr }, // 14
180  { 0, GIE::StringArgument, "le", 16, 18, nullptr }, // 15
181  { 0, GIE::StringArgument, "date", 17, 0, nullptr }, // 16
182  { 0, GIE::StringArgument, nullptr, 21, 0, "endDate" }, // 17
183  { 0, GIE::StringArgument, "ge", 19, 0, nullptr }, // 18
184  { 0, GIE::StringArgument, "date", 20, 0, nullptr }, // 19
185  { 0, GIE::StringArgument, nullptr, 21, 0, "startDate" }, // 20
186  { 0, GIE::TestEnd, nullptr, 22, 0, nullptr }, // 21
187  { 0, GIE::TestListEnd, nullptr, 23, 0, nullptr }, // 22
188 
189  { 0, GIE::TestEnd, nullptr, 24, 0, nullptr }, // 23
190  { 0, GIE::TestEnd, nullptr, 25, 0, nullptr }, // 24
191 
192  // block of commands, find "stop", take nested if's into account:
193  { 0, GIE::BlockStart, nullptr, 26, 0, nullptr }, // 25
194  { 1, GIE::CommandStart, "stop", 29, 28, "stop" }, // 26
195  { -1, GIE::Any, nullptr, 26, 0, nullptr }, // 27
196  { 0, GIE::BlockEnd, nullptr, 0, 27, nullptr }, // 28
197 
198  { -1, GIE::Any, nullptr, 27, 27, nullptr } // 29 end state
199 };
200 
201 static const unsigned int numDatesNodes = sizeof datesNodes / sizeof *datesNodes;
202 
203 /*
204 
205 This file only contains legacy code, that can be removed if the legacy code is not needed anymore.
206 See README for further information.
207 
208 */
209 class KSIEVEUI_DEPRECATED DateExtractor : public GenericInformationExtractor
210 {
211 public:
212  DateExtractor() : GenericInformationExtractor(std::vector<StateNode>(datesNodes, datesNodes + numDatesNodes))
213  {
214  }
215 
216  QDate endDate() const
217  {
218  return date(QStringLiteral("endDate"));
219  }
220 
221  QDate startDate() const
222  {
223  return date(QStringLiteral("startDate"));
224  }
225 
226 private:
227  QDate date(const QString &name) const
228  {
229  if (mResults.count(name) == 0) {
230  return QDate();
231  } else {
232  return QDate::fromString(mResults.at(name), Qt::ISODate);
233  }
234  }
235 };
236 
237 /*
238 
239 This file only contains legacy code, that can be removed if the legacy code is not needed anymore.
240 See README for further information.
241 
242 */
243 class KSIEVEUI_DEPRECATED VacationDataExtractor : public KSieve::ScriptBuilder
244 {
245  enum Context {
246  None = 0,
247  // command itself:
248  VacationCommand,
249  // tagged args:
250  Days, Addresses, Subject,
251  VacationEnd
252  };
253 public:
254  VacationDataExtractor();
255  ~VacationDataExtractor() override;
256  bool commandFound() const
257  {
258  return mContext == VacationEnd;
259  }
260 
261  int notificationInterval() const
262  {
263  return mNotificationInterval;
264  }
265 
266  const QString &messageText() const
267  {
268  return mMessageText;
269  }
270 
271  const QStringList &aliases() const
272  {
273  return mAliases;
274  }
275 
276  const QString &subject() const
277  {
278  return mSubject;
279  }
280 
281 private:
282  void commandStart(const QString &identifier, int lineNumber) override;
283 
284  void commandEnd(int lineNumber) override;
285 
286  void testStart(const QString &) override
287  {
288  }
289 
290  void testEnd() override
291  {
292  }
293 
294  void testListStart() override
295  {
296  }
297 
298  void testListEnd() override
299  {
300  }
301 
302  void blockStart(int lineNumber) override
303  {
304  Q_UNUSED(lineNumber)
305  }
306 
307  void blockEnd(int lineNumber) override
308  {
309  Q_UNUSED(lineNumber)
310  }
311 
312  void hashComment(const QString &) override
313  {
314  }
315 
316  void bracketComment(const QString &) override
317  {
318  }
319 
320  void lineFeed() override
321  {
322  }
323 
324  void error(const KSieve::Error &e) override;
325  void finished() override;
326 
327  void taggedArgument(const QString &tag) override;
328 
329  void stringArgument(const QString &string, bool, const QString &) override;
330 
331  void numberArgument(unsigned long number, char) override;
332 
333  void stringListArgumentStart() override;
334  void stringListEntry(const QString &string, bool, const QString &) override;
335  void stringListArgumentEnd() override;
336 
337 private:
338  Context mContext;
339  int mNotificationInterval;
340  QString mMessageText;
341  QString mSubject;
342  QStringList mAliases;
343 
344  void reset();
345 };
346 }
347 }
348 
349 #endif // LEGACY_VACATIONSCRIPTEXTRACTOR_H
Subject
QDate fromString(const QString &string, Qt::DateFormat format)
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
int count() const const
KGuiItem reset()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Aug 10 2020 23:12:59 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.