Libksieve

legacy/vacationscriptextractor.h
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #pragma once
8 
9 #include "../vacationscriptextractor.h"
10 #include "ksieveui_export.h"
11 #include "sieve-vacation.h"
12 
13 #include <ksieve/error.h>
14 #include <ksieve/parser.h>
15 #include <ksieve/scriptbuilder.h>
16 
17 #include "libksieve_debug.h"
18 #include <cassert>
19 #include <limits.h>
20 #include <map>
21 #include <set>
22 #include <vector>
23 
24 namespace KSieveUi
25 {
26 namespace Legacy
27 {
28 /*
29 
30 This file only contains legacy code, that can be removed if the legacy code is not needed anymore.
31 See README for further information.
32 
33 */
34 static const GenericInformationExtractor::StateNode spamNodes[] = {
35  {0, GIE::CommandStart, "if", 1, 0, nullptr}, // 0
36  {0, GIE::TestStart, "header", 2, 0, nullptr}, // 1
37  {0, GIE::TaggedArgument, "contains", 3, 0, nullptr}, // 2
38 
39  // accept both string and string-list:
40  {0, GIE::StringArgument, "x-spam-flag", 9, 4, "x-spam-flag"}, // 3
41  {0, GIE::StringListArgumentStart, nullptr, 5, 0, nullptr}, // 4
42  {0, GIE::StringListEntry, "x-spam-flag", 6, 7, "x-spam-flag"}, // 5
43  {0, GIE::StringListEntry, nullptr, 6, 8, nullptr}, // 6
44  {0, GIE::StringListArgumentEnd, nullptr, 0, 5, nullptr}, // 7
45  {0, GIE::StringListArgumentEnd, nullptr, 9, 0, nullptr}, // 8
46 
47  // accept both string and string-list:
48  {0, GIE::StringArgument, "yes", 15, 10, "spam-flag-yes"}, // 9
49  {0, GIE::StringListArgumentStart, nullptr, 11, 0, nullptr}, // 10
50  {0, GIE::StringListEntry, "yes", 12, 13, "spam-flag-yes"}, // 11
51  {0, GIE::StringListEntry, nullptr, 12, 14, nullptr}, // 12
52  {0, GIE::StringListArgumentEnd, nullptr, 0, 11, nullptr}, // 13
53  {0, GIE::StringListArgumentEnd, nullptr, 15, 0, nullptr}, // 14
54 
55  {0, GIE::TestEnd, nullptr, 16, 0, nullptr}, // 15
56 
57  // block of command, find "stop", take nested if's into account:
58  {0, GIE::BlockStart, nullptr, 17, 0, nullptr}, // 16
59  {1, GIE::CommandStart, "stop", 20, 19, "stop"}, // 17
60  {-1, GIE::Any, nullptr, 17, 0, nullptr}, // 18
61  {0, GIE::BlockEnd, nullptr, 0, 18, nullptr}, // 19
62 
63  {-1, GIE::Any, nullptr, 20, 20, nullptr}, // 20 end state
64 };
65 static const unsigned int numSpamNodes = sizeof spamNodes / sizeof *spamNodes;
66 
67 /*
68 
69 This file only contains legacy code, that can be removed if the legacy code is not needed anymore.
70 See README for further information.
71 
72 */
73 class KSIEVEUI_DEPRECATED SpamDataExtractor : public GenericInformationExtractor
74 {
75 public:
76  SpamDataExtractor()
77  : GenericInformationExtractor(std::vector<StateNode>(spamNodes, spamNodes + numSpamNodes))
78  {
79  }
80 
81  bool found() const
82  {
83  return mResults.count(QStringLiteral("x-spam-flag")) && mResults.count(QStringLiteral("spam-flag-yes")) && mResults.count(QStringLiteral("stop"));
84  }
85 };
86 
87 // to understand this table, study the output of
88 // libksieve/tests/parsertest
89 // 'if not address :domain :contains ["from"] ["mydomain.org"] { keep; stop; }'
90 static const GenericInformationExtractor::StateNode domainNodes[] = {
91  {0, GIE::CommandStart, "if", 1, 0, nullptr}, // 0
92  {
93  0,
94  GIE::TestStart,
95  "not",
96  2,
97  0,
98  nullptr,
99  }, // 1
100  {0, GIE::TestStart, "address", 3, 0, nullptr}, // 2
101 
102  // :domain and :contains in arbitrary order:
103  {0, GIE::TaggedArgument, "domain", 4, 5, nullptr}, // 3
104  {0, GIE::TaggedArgument, "contains", 7, 0, nullptr}, // 4
105  {0, GIE::TaggedArgument, "contains", 6, 0, nullptr}, // 5
106  {0, GIE::TaggedArgument, "domain", 7, 0, nullptr}, // 6
107 
108  // accept both string and string-list:
109  {0, GIE::StringArgument, "from", 13, 8, "from"}, // 7
110  {0, GIE::StringListArgumentStart, nullptr, 9, 0, nullptr}, // 8
111  {0, GIE::StringListEntry, "from", 10, 11, "from"}, // 9
112  {0, GIE::StringListEntry, nullptr, 10, 12, nullptr}, // 10
113  {0, GIE::StringListArgumentEnd, nullptr, 0, 9, nullptr}, // 11
114  {0, GIE::StringListArgumentEnd, nullptr, 13, 0, nullptr}, // 12
115 
116  // string: save, string-list: save last
117  {0, GIE::StringArgument, nullptr, 17, 14, "domainName"}, // 13
118  {0, GIE::StringListArgumentStart, nullptr, 15, 0, nullptr}, // 14
119  {0, GIE::StringListEntry, nullptr, 15, 16, "domainName"}, // 15
120  {0, GIE::StringListArgumentEnd, nullptr, 17, 0, nullptr}, // 16
121 
122  {0, GIE::TestEnd, nullptr, 18, 0, nullptr}, // 17
123  {0, GIE::TestEnd, nullptr, 19, 0, nullptr}, // 18
124 
125  // block of commands, find "stop", take nested if's into account:
126  {0, GIE::BlockStart, nullptr, 20, 0, nullptr}, // 19
127  {1, GIE::CommandStart, "stop", 23, 22, "stop"}, // 20
128  {-1, GIE::Any, nullptr, 20, 0, nullptr}, // 21
129  {0, GIE::BlockEnd, nullptr, 0, 21, nullptr}, // 22
130 
131  {-1, GIE::Any, nullptr, 23, 23, nullptr} // 23 end state
132 };
133 static const unsigned int numDomainNodes = sizeof domainNodes / sizeof *domainNodes;
134 
135 /*
136 
137 This file only contains legacy code, that can be removed if the legacy code is not needed anymore.
138 See README for further information.
139 
140 */
141 class KSIEVEUI_DEPRECATED DomainRestrictionDataExtractor : public GenericInformationExtractor
142 {
143 public:
144  DomainRestrictionDataExtractor()
145  : GenericInformationExtractor(std::vector<StateNode>(domainNodes, domainNodes + numDomainNodes))
146  {
147  }
148 
149  QString domainName() /*not const, since map::op[] isn't const*/
150  {
151  return mResults.count(QStringLiteral("stop")) && mResults.count(QStringLiteral("from")) ? mResults[QStringLiteral("domainName")] : QString();
152  }
153 };
154 
155 // if not allof (currentDate :value "ge" date "YYYY-MM-DD",
156 // currentDate :value "le" date "YYYY-MM-DD") { keep; stop; }
157 static const GenericInformationExtractor::StateNode datesNodes[] = {
158  {0, GIE::CommandStart, "if", 1, 0, nullptr}, // 0
159  {0, GIE::TestStart, "not", 2, 0, nullptr}, // 1
160  {0, GIE::TestStart, "allof", 3, 0, nullptr}, // 2
161 
162  // handle startDate and endDate in arbitrary order
163  {0, GIE::TestListStart, nullptr, 4, 0, nullptr}, // 3
164  {0, GIE::TestStart, "currentdate", 5, 0, nullptr}, // 4
165  {0, GIE::TaggedArgument, "value", 6, 0, nullptr}, // 5
166  {0, GIE::StringArgument, "ge", 7, 9, nullptr}, // 6
167  {0, GIE::StringArgument, "date", 8, 0, nullptr}, // 7
168  {0, GIE::StringArgument, nullptr, 12, 0, "startDate"}, // 8
169  {0, GIE::StringArgument, "le", 10, 0, nullptr}, // 9
170  {0, GIE::StringArgument, "date", 11, 0, nullptr}, // 10
171  {0, GIE::StringArgument, nullptr, 12, 0, "endDate"}, // 11
172  {0, GIE::TestEnd, nullptr, 13, 0, nullptr}, // 12
173 
174  {0, GIE::TestStart, "currentdate", 14, 0, nullptr}, // 13
175  {0, GIE::TaggedArgument, "value", 15, 0, nullptr}, // 14
176  {0, GIE::StringArgument, "le", 16, 18, nullptr}, // 15
177  {0, GIE::StringArgument, "date", 17, 0, nullptr}, // 16
178  {0, GIE::StringArgument, nullptr, 21, 0, "endDate"}, // 17
179  {0, GIE::StringArgument, "ge", 19, 0, nullptr}, // 18
180  {0, GIE::StringArgument, "date", 20, 0, nullptr}, // 19
181  {0, GIE::StringArgument, nullptr, 21, 0, "startDate"}, // 20
182  {0, GIE::TestEnd, nullptr, 22, 0, nullptr}, // 21
183  {0, GIE::TestListEnd, nullptr, 23, 0, nullptr}, // 22
184 
185  {0, GIE::TestEnd, nullptr, 24, 0, nullptr}, // 23
186  {0, GIE::TestEnd, nullptr, 25, 0, nullptr}, // 24
187 
188  // block of commands, find "stop", take nested if's into account:
189  {0, GIE::BlockStart, nullptr, 26, 0, nullptr}, // 25
190  {1, GIE::CommandStart, "stop", 29, 28, "stop"}, // 26
191  {-1, GIE::Any, nullptr, 26, 0, nullptr}, // 27
192  {0, GIE::BlockEnd, nullptr, 0, 27, nullptr}, // 28
193 
194  {-1, GIE::Any, nullptr, 27, 27, nullptr} // 29 end state
195 };
196 
197 static const unsigned int numDatesNodes = sizeof datesNodes / sizeof *datesNodes;
198 
199 /*
200 
201 This file only contains legacy code, that can be removed if the legacy code is not needed anymore.
202 See README for further information.
203 
204 */
205 class KSIEVEUI_DEPRECATED DateExtractor : public GenericInformationExtractor
206 {
207 public:
208  DateExtractor()
209  : GenericInformationExtractor(std::vector<StateNode>(datesNodes, datesNodes + numDatesNodes))
210  {
211  }
212 
213  QDate endDate() const
214  {
215  return date(QStringLiteral("endDate"));
216  }
217 
218  QDate startDate() const
219  {
220  return date(QStringLiteral("startDate"));
221  }
222 
223 private:
224  QDate date(const QString &name) const
225  {
226  if (mResults.count(name) == 0) {
227  return {};
228  } else {
229  return QDate::fromString(mResults.at(name), Qt::ISODate);
230  }
231  }
232 };
233 
234 /*
235 
236 This file only contains legacy code, that can be removed if the legacy code is not needed anymore.
237 See README for further information.
238 
239 */
240 class KSIEVEUI_DEPRECATED VacationDataExtractor : public KSieve::ScriptBuilder
241 {
242  enum Context {
243  None = 0,
244  // command itself:
245  VacationCommand,
246  // tagged args:
247  Days,
248  Addresses,
249  Subject,
250  VacationEnd
251  };
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 = None;
339  int mNotificationInterval = 0;
340  QString mMessageText;
341  QString mSubject;
342  QStringList mAliases;
343 
344  void reset();
345 };
346 }
347 }
348 
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-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.