Libksieve

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

KDE's Doxygen guidelines are available online.