Libksieve

vacationscriptextractor.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "vacationscriptextractor.h"
8 
9 // TODO: add unittests for VacationDataExtractor
10 
11 using namespace KSieveUi;
12 VacationDataExtractor::VacationDataExtractor()
13  : KSieve::ScriptBuilder()
14 {
15  qCDebug(LIBKSIEVE_LOG);
16 }
17 
18 VacationDataExtractor::~VacationDataExtractor()
19 {
20 }
21 
22 void VacationDataExtractor::commandStart(const QString &identifier, int lineNumber)
23 {
24  qCDebug(LIBKSIEVE_LOG) << "(\"" << identifier << "\")";
25  if (identifier == QLatin1String("if") && mContext == None) {
26  mContext = IfBlock;
27  mLineStart = lineNumber;
28  mInIfBlock = true;
29  }
30 
31  if (commandFound() && (!mFoundInBlock || mBlockLevel > 0)) {
32  if (identifier == QLatin1String("discard")) {
33  mMailAction = VacationUtils::Discard;
34  } else if (identifier == QLatin1String("redirect")) {
35  mMailAction = VacationUtils::Sendto;
36  mMailActionContext = RedirectCommand;
37  }
38  }
39 
40  if (identifier != QLatin1String("vacation")) {
41  return;
42  }
43 
44  if (mContext != IfBlock) {
45  mLineStart = lineNumber;
46  }
47 
48  reset();
49  mContext = VacationCommand;
50  mFoundInBlock = (mBlockLevel > 0);
51 }
52 
53 void VacationDataExtractor::commandEnd(int lineNumber)
54 {
55  qCDebug(LIBKSIEVE_LOG);
56  if (mContext != None && mContext != IfBlock && mContext != VacationEnd) {
57  mContext = VacationEnd;
58  mLineEnd = lineNumber;
59  }
60  mMailActionContext = None;
61 }
62 
63 void VacationDataExtractor::error(const KSieve::Error &e)
64 {
65  qCDebug(LIBKSIEVE_LOG) << e.asString() << "@" << e.line() << "," << e.column();
66 }
67 
68 void VacationDataExtractor::finished()
69 {
70 }
71 
72 void VacationDataExtractor::testStart(const QString &test)
73 {
74  if (mContext == IfBlock) {
75  if (test == QLatin1String("true") || test == QLatin1String("false")) {
76  mActive = (test == QLatin1String("true"));
77  mIfComment = QString();
78  }
79  }
80 }
81 
82 void VacationDataExtractor::hashComment(const QString &comment)
83 {
84  if (mContext == IfBlock) {
85  mIfComment += comment;
86  }
87 }
88 
89 void VacationDataExtractor::blockStart(int lineNumber)
90 {
91  Q_UNUSED(lineNumber)
92  mBlockLevel++;
93 }
94 
95 void VacationDataExtractor::blockEnd(int lineNumber)
96 {
97  mBlockLevel--;
98  if (mBlockLevel == 0 && !commandFound()) { // We are in main level again, and didn't found vacation in block
99  mActive = true;
100  mIfComment = QString();
101  } else if (mInIfBlock && mBlockLevel == 0 && commandFound()) {
102  mLineEnd = lineNumber;
103  mInIfBlock = false;
104  }
105 }
106 
107 void VacationDataExtractor::taggedArgument(const QString &tag)
108 {
109  qCDebug(LIBKSIEVE_LOG) << "(\"" << tag << "\")";
110  if (mMailActionContext == RedirectCommand) {
111  if (tag == QLatin1String("copy")) {
112  mMailAction = VacationUtils::CopyTo;
113  }
114  }
115  if (mContext != VacationCommand) {
116  return;
117  }
118  if (tag == QLatin1String("days")) {
119  mContext = Days;
120  } else if (tag == QLatin1String("addresses")) {
121  mContext = Addresses;
122  } else if (tag == QLatin1String("subject")) {
123  mContext = Subject;
124  }
125 }
126 
127 void VacationDataExtractor::stringArgument(const QString &string, bool, const QString &)
128 {
129  qCDebug(LIBKSIEVE_LOG) << "(\"" << string << "\")";
130  if (mContext == Addresses) {
131  mAliases.push_back(string);
132  mContext = VacationCommand;
133  } else if (mContext == Subject) {
134  mSubject = string;
135  mContext = VacationCommand;
136  } else if (mContext == VacationCommand) {
137  mMessageText = string;
138  mContext = VacationCommand;
139  }
140  if (mMailActionContext == RedirectCommand) {
141  mMailActionRecipient = string;
142  }
143 }
144 
145 void VacationDataExtractor::numberArgument(unsigned long number, char)
146 {
147  qCDebug(LIBKSIEVE_LOG) << "(\"" << number << "\")";
148  if (mContext != Days) {
149  return;
150  }
151  if (number > INT_MAX) {
152  mNotificationInterval = INT_MAX;
153  } else {
154  mNotificationInterval = number;
155  }
156  mContext = VacationCommand;
157 }
158 
159 void VacationDataExtractor::stringListArgumentStart()
160 {
161 }
162 
163 void VacationDataExtractor::stringListEntry(const QString &string, bool, const QString &)
164 {
165  qCDebug(LIBKSIEVE_LOG) << "(\"" << string << "\")";
166  if (mContext != Addresses) {
167  return;
168  }
169  mAliases.push_back(string);
170 }
171 
172 void VacationDataExtractor::stringListArgumentEnd()
173 {
174  qCDebug(LIBKSIEVE_LOG);
175  if (mContext != Addresses) {
176  return;
177  }
178  mContext = VacationCommand;
179 }
180 
181 void VacationDataExtractor::reset()
182 {
183  qCDebug(LIBKSIEVE_LOG);
184  mContext = None;
185  mMailAction = VacationUtils::Keep;
186  mMailActionRecipient = QString();
187  mNotificationInterval = 0;
188  mAliases.clear();
189  mMessageText.clear();
190 }
191 
192 RequireExtractor::RequireExtractor()
193  : KSieve::ScriptBuilder()
194  , mContext(None)
195  , mLineStart(0)
196  , mLineEnd(0)
197 {
198 }
199 
200 RequireExtractor::~RequireExtractor()
201 {
202 }
203 
204 void RequireExtractor::commandStart(const QString &identifier, int lineNumber)
205 {
206  if (identifier == QLatin1String("require") && mContext == None) {
207  mContext = RequireCommand;
208  mLineStart = lineNumber;
209  }
210 }
211 
212 void RequireExtractor::commandEnd(int lineNumber)
213 {
214  if (mContext == RequireCommand) {
215  mContext = EndState;
216  mLineEnd = lineNumber;
217  }
218 }
219 
220 void RequireExtractor::error(const KSieve::Error &e)
221 {
222  qCDebug(LIBKSIEVE_LOG) << e.asString() << "@" << e.line() << "," << e.column();
223 }
224 
225 void RequireExtractor::finished()
226 {
227 }
228 
229 void RequireExtractor::stringArgument(const QString &string, bool, const QString &)
230 {
231  mRequirements << string;
232 }
233 
234 void RequireExtractor::stringListEntry(const QString &string, bool, const QString &)
235 {
236  mRequirements << string;
237 }
Subject
void push_back(QChar ch)
KGuiItem reset()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Apr 11 2021 23:09:37 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.