Libksieve

vacationscriptextractor.cpp
1 /*
2  Copyright (C) 2013-2020 Laurent Montel <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "vacationscriptextractor.h"
21 
22 //TODO: add unittests for VacationDataExtractor
23 
24 using namespace KSieveUi;
25 VacationDataExtractor::VacationDataExtractor()
26  : KSieve::ScriptBuilder()
27  , mContext(None)
28  , mNotificationInterval(0)
29  , mActive(true)
30  , mInIfBlock(false)
31  , mFoundInBlock(false)
32  , mBlockLevel(0)
33  , mLineStart(0)
34  , mLineEnd(0)
35  , mMailAction(VacationUtils::Keep)
36  , mMailActionContext(None)
37 {
38  qCDebug(LIBKSIEVE_LOG);
39 }
40 
41 VacationDataExtractor::~VacationDataExtractor()
42 {
43 }
44 
45 void VacationDataExtractor::commandStart(const QString &identifier, int lineNumber)
46 {
47  qCDebug(LIBKSIEVE_LOG) << "(\"" << identifier << "\")";
48  if (identifier == QLatin1String("if") && mContext == None) {
49  mContext = IfBlock;
50  mLineStart = lineNumber;
51  mInIfBlock = true;
52  }
53 
54  if (commandFound() && (!mFoundInBlock || mBlockLevel > 0)) {
55  if (identifier == QLatin1String("discard")) {
56  mMailAction = VacationUtils::Discard;
57  } else if (identifier == QLatin1String("redirect")) {
58  mMailAction = VacationUtils::Sendto;
59  mMailActionContext = RedirectCommand;
60  }
61  }
62 
63  if (identifier != QLatin1String("vacation")) {
64  return;
65  }
66 
67  if (mContext != IfBlock) {
68  mLineStart = lineNumber;
69  }
70 
71  reset();
72  mContext = VacationCommand;
73  mFoundInBlock = (mBlockLevel > 0);
74 }
75 
76 void VacationDataExtractor::commandEnd(int lineNumber)
77 {
78  qCDebug(LIBKSIEVE_LOG);
79  if (mContext != None && mContext != IfBlock && mContext != VacationEnd) {
80  mContext = VacationEnd;
81  mLineEnd = lineNumber;
82  }
83  mMailActionContext = None;
84 }
85 
86 void VacationDataExtractor::error(const KSieve::Error &e)
87 {
88  qCDebug(LIBKSIEVE_LOG) << e.asString() << "@" << e.line() << "," << e.column();
89 }
90 
91 void VacationDataExtractor::finished()
92 {
93 }
94 
95 void VacationDataExtractor::testStart(const QString &test)
96 {
97  if (mContext == IfBlock) {
98  if (test == QLatin1String("true") || test == QLatin1String("false")) {
99  mActive = (test == QLatin1String("true"));
100  mIfComment = QString();
101  }
102  }
103 }
104 
105 void VacationDataExtractor::hashComment(const QString &comment)
106 {
107  if (mContext == IfBlock) {
108  mIfComment += comment;
109  }
110 }
111 
112 void VacationDataExtractor::blockStart(int lineNumber)
113 {
114  Q_UNUSED(lineNumber)
115  mBlockLevel++;
116 }
117 
118 void VacationDataExtractor::blockEnd(int lineNumber)
119 {
120  mBlockLevel--;
121  if (mBlockLevel == 0 && !commandFound()) { //We are in main level again, and didn't found vacation in block
122  mActive = true;
123  mIfComment = QString();
124  } else if (mInIfBlock && mBlockLevel == 0 && commandFound()) {
125  mLineEnd = lineNumber;
126  mInIfBlock = false;
127  }
128 }
129 
130 void VacationDataExtractor::taggedArgument(const QString &tag)
131 {
132  qCDebug(LIBKSIEVE_LOG) << "(\"" << tag << "\")";
133  if (mMailActionContext == RedirectCommand) {
134  if (tag == QLatin1String("copy")) {
135  mMailAction = VacationUtils::CopyTo;
136  }
137  }
138  if (mContext != VacationCommand) {
139  return;
140  }
141  if (tag == QLatin1String("days")) {
142  mContext = Days;
143  } else if (tag == QLatin1String("addresses")) {
144  mContext = Addresses;
145  } else if (tag == QLatin1String("subject")) {
146  mContext = Subject;
147  }
148 }
149 
150 void VacationDataExtractor::stringArgument(const QString &string, bool, const QString &)
151 {
152  qCDebug(LIBKSIEVE_LOG) << "(\"" << string << "\")";
153  if (mContext == Addresses) {
154  mAliases.push_back(string);
155  mContext = VacationCommand;
156  } else if (mContext == Subject) {
157  mSubject = string;
158  mContext = VacationCommand;
159  } else if (mContext == VacationCommand) {
160  mMessageText = string;
161  mContext = VacationCommand;
162  }
163  if (mMailActionContext == RedirectCommand) {
164  mMailActionRecipient = string;
165  }
166 }
167 
168 void VacationDataExtractor::numberArgument(unsigned long number, char)
169 {
170  qCDebug(LIBKSIEVE_LOG) << "(\"" << number << "\")";
171  if (mContext != Days) {
172  return;
173  }
174  if (number > INT_MAX) {
175  mNotificationInterval = INT_MAX;
176  } else {
177  mNotificationInterval = number;
178  }
179  mContext = VacationCommand;
180 }
181 
182 void VacationDataExtractor::stringListArgumentStart()
183 {
184 }
185 
186 void VacationDataExtractor::stringListEntry(const QString &string, bool, const QString &)
187 {
188  qCDebug(LIBKSIEVE_LOG) << "(\"" << string << "\")";
189  if (mContext != Addresses) {
190  return;
191  }
192  mAliases.push_back(string);
193 }
194 
195 void VacationDataExtractor::stringListArgumentEnd()
196 {
197  qCDebug(LIBKSIEVE_LOG);
198  if (mContext != Addresses) {
199  return;
200  }
201  mContext = VacationCommand;
202 }
203 
204 void VacationDataExtractor::reset()
205 {
206  qCDebug(LIBKSIEVE_LOG);
207  mContext = None;
208  mMailAction = VacationUtils::Keep;
209  mMailActionRecipient = QString();
210  mNotificationInterval = 0;
211  mAliases.clear();
212  mMessageText.clear();
213 }
214 
215 RequireExtractor::RequireExtractor()
216  : KSieve::ScriptBuilder()
217  , mContext(None)
218  , mLineStart(0)
219  , mLineEnd(0)
220 {
221 }
222 
223 RequireExtractor::~RequireExtractor()
224 {
225 }
226 
227 void RequireExtractor::commandStart(const QString &identifier, int lineNumber)
228 {
229  if (identifier == QLatin1String("require") && mContext == None) {
230  mContext = RequireCommand;
231  mLineStart = lineNumber;
232  }
233 }
234 
235 void RequireExtractor::commandEnd(int lineNumber)
236 {
237  if (mContext == RequireCommand) {
238  mContext = EndState;
239  mLineEnd = lineNumber;
240  }
241 }
242 
243 void RequireExtractor::error(const KSieve::Error &e)
244 {
245  qCDebug(LIBKSIEVE_LOG) << e.asString() << "@" << e.line() << "," << e.column();
246 }
247 
248 void RequireExtractor::finished()
249 {
250 }
251 
252 void RequireExtractor::stringArgument(const QString &string, bool, const QString &)
253 {
254  mRequirements << string;
255 }
256 
257 void RequireExtractor::stringListEntry(const QString &string, bool, const QString &)
258 {
259  mRequirements << string;
260 }
Subject
void push_back(QChar ch)
KGuiItem reset()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Jul 14 2020 23:08:50 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.