KIMAP2

imapstreamparser.h
1/*
2 Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
3 Copyright (c) 2009 Andras Mantia <amantia@kde.org>
4 Copyright (c) 2017 Christian Mollekopf <mollekopf@kolabsys.com>
5
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.
20*/
21
22#ifndef KIMAP2_IMAPSTREAMPARSER_P_H
23#define KIMAP2_IMAPSTREAMPARSER_P_H
24
25#include "kimap2_export.h"
26
27#include <QtCore/QByteArray>
28#include <QtCore/QList>
29#include <QtCore/QScopedPointer>
30#include <functional>
31#include <message_p.h>
32
33class QIODevice;
34
35namespace KIMAP2
36{
37
38/**
39 Parser for IMAP messages that operates on a local socket stream.
40*/
41class KIMAP2_EXPORT ImapStreamParser
42{
43public:
44 /**
45 * Construct the parser.
46 * @param socket the local socket to work with.
47 * @param serverModeEnabled true if the parser has to assume we're writing a server (e.g. sends
48 * continuation message automatically)
49 */
50 explicit ImapStreamParser(QIODevice *socket, bool serverModeEnabled = false);
51
52 /**
53 * Return everything that remained from the command.
54 * @return the remaining command data
55 */
56 QByteArray readUntilCommandEnd();
57
58 int availableDataSize() const;
59
60 void parseStream();
61
62 void onResponseReceived(std::function<void(const Message &)>);
63
64 bool error() const;
65
66 QByteArray currentBuffer() const;
67
68private:
69
70 /**
71 * Remove already read data from the internal buffer if necessary.
72 */
73 void trimBuffer();
74
75 /**
76 * Inform the client to send more literal data.
77 */
78 void sendContinuationResponse(qint64 size);
79
80
81 int readFromSocket();
82 void processBuffer();
83
84 char at(int pos) const;
85 QByteArray mid(int start, int end = -1) const;
86 QByteArray midRef(int start, int end) const;
87 int length() const;
88
89 QByteArray &buffer();
90 const QByteArray &buffer() const;
91
93 QList<Message::Part> *m_currentPayload;
94
95 QIODevice *m_socket;
96 bool m_isServerModeEnabled;
97 bool m_processing;
98 int m_position;
99 int m_readPosition;
100 qint64 m_literalSize;
101 QByteArray m_data1;
102 QByteArray m_data2;
103 QByteArray *m_current;
104 int m_bufferSize;
105
106 enum States {
107 InitState,
108 QuotedStringState,
109 LiteralStringState,
110 StringState,
111 WhitespaceState,
112 AngleBracketStringState,
113 SublistString,
114 CRLFState
115 };
116 States m_currentState;
117 States m_lastState;
118
119 void setState(States state);
120 void forwardToState(States state);
121 void resetState();
122
123 int m_listCounter;
124 int m_stringStartPos;
125 bool m_readingLiteral;
126 bool m_error;
127
128 std::function<void(const char *data, const int size)> string;
129 std::function<void()> listStart;
130 std::function<void()> listEnd;
131 std::function<void()> responseCodeStart;
132 std::function<void()> responseCodeEnd;
133 std::function<void(int size)> literalStart;
134 std::function<void(const char *data, const int size)> literalPart;
135 std::function<void()> literalEnd;
136 std::function<void()> lineEnd;
137
138 void onString(std::function<void(const char *data, const int size)> f)
139 {
140 string = f;
141 }
142
143 void onListStart(std::function<void()> f)
144 {
145 listStart = f;
146 }
147
148 void onListEnd(std::function<void()> f)
149 {
150 listEnd = f;
151 }
152
153 void onResponseCodeStart(std::function<void()> f)
154 {
155 responseCodeStart = f;
156 }
157
158 void onResponseCodeEnd(std::function<void()> f)
159 {
160 responseCodeEnd = f;
161 }
162
163 void onLiteralStart(std::function<void(int size)> f)
164 {
165 literalStart = f;
166 }
167
168 void onLiteralPart(std::function<void(const char *data, const int size)> f)
169 {
170 literalPart = f;
171 }
172
173 void onLiteralEnd(std::function<void()> f)
174 {
175 literalEnd = f;
176 }
177
178 void onLineEnd(std::function<void()> f)
179 {
180 lineEnd = f;
181 }
182
183 std::function<void(const Message &)> responseReceived;
184
185 void setupCallbacks();
186
187 QList<QByteArray> *m_list;
188 QByteArray m_literalData;
189};
190
191}
192
193#endif
Parser for IMAP messages that operates on a local socket stream.
Q_SCRIPTABLE Q_NOREPLY void start()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:09:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.