KMime

kmime_header_parsing.h
1/* -*- c++ -*-
2 kmime_header_parsing.h
3
4 KMime, the KDE Internet mail/usenet news message library.
5 SPDX-FileCopyrightText: 2001-2002 Marc Mutz <mutz@kde.org>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#pragma once
11
12#include "kmime_export.h"
13#include "kmime_types.h"
14
15#include <QString>
16#include <QPair>
17
18#include <QDateTime>
19
20template <typename K, typename V> class QMap;
21#include <QStringList>
22
23namespace KMime
24{
25
26namespace Headers
27{
28class Base;
29}
30
31namespace Types
32{
33
34} // namespace KMime::Types
35
36namespace HeaderParsing
37{
38
39/**
40 Parses the encoded word.
41
42 @param scursor pointer to the first character beyond the initial '=' of
43 the input string.
44 @param send pointer to end of input buffer.
45 @param result the decoded string the encoded work represented.
46 @param language The language parameter according to RFC 2231, section 5.
47 @param usedCS the used charset is returned here
48 @param defaultCS the charset to use in case the detected
49 one isn't known to us.
50
51 @return true if the input string was successfully decode; false otherwise.
52*/
53[[nodiscard]] KMIME_EXPORT bool
54parseEncodedWord(const char *&scursor, const char *const send, QString &result,
55 QByteArray &language, QByteArray &usedCS,
56 const QByteArray &defaultCS = QByteArray());
57
58//
59// The parsing squad:
60//
61
62/** You may or may not have already started parsing into the
63 atom. This function will go on where you left off.
64 */
65[[nodiscard]] KMIME_EXPORT bool parseAtom(const char *&scursor,
66 const char *const send,
67 QByteArray &result,
68 bool allow8Bit = false);
69
70/**
71 * More efficient overload, to avoid a copy of the substring
72 */
73[[nodiscard]] KMIME_EXPORT bool parseAtom(const char *&scursor,
74 const char *const send,
75 QPair<const char *, int> &result,
76 bool allow8Bit = false);
77
78enum ParseTokenFlag {
79 ParseTokenNoFlag = 0,
80 ParseTokenAllow8Bit = 1,
81 ParseTokenRelaxedTText = 2
82};
83Q_DECLARE_FLAGS(ParseTokenFlags, ParseTokenFlag)
84
85/** You may or may not have already started parsing into the
86 token. This function will go on where you left off. */
87[[nodiscard]] KMIME_EXPORT bool
88parseToken(const char *&scursor, const char *const send, QByteArray &result,
89 ParseTokenFlags flags = ParseTokenNoFlag);
90
91[[nodiscard]] KMIME_EXPORT bool
92parseToken(const char *&scursor, const char *const send,
93 QPair<const char *, int> &result,
94 ParseTokenFlags flags = ParseTokenNoFlag);
95
96/** @p scursor must be positioned after the opening openChar. */
97[[nodiscard]] KMIME_EXPORT bool
98parseGenericQuotedString(const char *&scursor, const char *const send,
99 QString &result, bool isCRLF,
100 const char openChar = '"', const char closeChar = '"');
101
102/** @p scursor must be positioned right after the opening '(' */
103[[nodiscard]] KMIME_EXPORT bool
104parseComment(const char *&scursor, const char *const send, QString &result,
105 bool isCRLF = false, bool reallySave = true);
106
107/**
108 Parses a phrase.
109
110 You may or may not have already started parsing into the phrase, but
111 only if it starts with atext. If you setup this function to parse a
112 phrase starting with an encoded-word or quoted-string, @p scursor has
113 to point to the char introducing the encoded-word or quoted-string, resp.
114
115 @param scursor pointer to the first character beyond the initial '=' of
116 the input string.
117 @param send pointer to end of input buffer.
118 @param result the parsed string.
119
120 @return true if the input phrase was successfully parsed; false otherwise.
121*/
122[[nodiscard]] KMIME_EXPORT bool parsePhrase(const char *&scursor,
123 const char *const send,
124 QString &result,
125 bool isCRLF = false);
126
127/**
128 Parses into the initial atom.
129 You may or may not have already started parsing into the initial
130 atom, but not up to it's end.
131
132 @param scursor pointer to the first character beyond the initial '=' of
133 the input string.
134 @param send pointer to end of input buffer.
135 @param result the parsed string.
136
137 @return true if the input phrase was successfully parsed; false otherwise.
138*/
139[[nodiscard]] KMIME_EXPORT bool parseDotAtom(const char *&scursor,
140 const char *const send,
141 QByteArray &result,
142 bool isCRLF = false);
143
144/**
145 Eats comment-folding-white-space, skips whitespace, folding and comments
146 (even nested ones) and stops at the next non-CFWS character. After
147 calling this function, you should check whether @p scursor == @p send
148 (end of header reached).
149
150 If a comment with unbalanced parentheses is encountered, @p scursor
151 is being positioned on the opening '(' of the outmost comment.
152
153 @param scursor pointer to the first character beyond the initial '=' of
154 the input string.
155 @param send pointer to end of input buffer.
156 @param isCRLF true if input string is terminated with a CRLF.
157*/
158KMIME_EXPORT void eatCFWS(const char *&scursor, const char *const send,
159 bool isCRLF);
160
161[[nodiscard]] KMIME_EXPORT bool parseDomain(const char *&scursor,
162 const char *const send,
163 QString &result,
164 bool isCRLF = false);
165
166[[nodiscard]] KMIME_EXPORT bool
167parseObsRoute(const char *&scursor, const char *const send, QStringList &result,
168 bool isCRLF = false, bool save = false);
169
170[[nodiscard]] KMIME_EXPORT bool parseAddrSpec(const char *&scursor,
171 const char *const send,
172 Types::AddrSpec &result,
173 bool isCRLF = false);
174
175[[nodiscard]] KMIME_EXPORT bool parseAngleAddr(const char *&scursor,
176 const char *const send,
177 Types::AddrSpec &result,
178 bool isCRLF = false);
179
180/**
181 Parses a single mailbox.
182
183 RFC 2822, section 3.4 defines a mailbox as follows:
184 <pre>mailbox := addr-spec / ([ display-name ] angle-addr)</pre>
185
186 KMime also accepts the legacy format of specifying display names:
187 <pre>mailbox := (addr-spec [ "(" display-name ")" ])
188 / ([ display-name ] angle-addr)
189 / (angle-addr "(" display-name ")")</pre>
190
191 @param scursor pointer to the first character of the input string
192 @param send pointer to end of input buffer
193 @param result the parsing result
194 @param isCRLF true if input string is terminated with a CRLF.
195*/
196KMIME_EXPORT bool parseMailbox(const char *&scursor, const char *const send,
197 Types::Mailbox &result, bool isCRLF = false);
198
199[[nodiscard]] KMIME_EXPORT bool parseGroup(const char *&scursor,
200 const char *const send,
201 Types::Address &result,
202 bool isCRLF = false);
203
204[[nodiscard]] KMIME_EXPORT bool parseAddress(const char *&scursor,
205 const char *const send,
206 Types::Address &result,
207 bool isCRLF = false);
208
209[[nodiscard]] KMIME_EXPORT bool parseAddressList(const char *&scursor,
210 const char *const send,
211 Types::AddressList &result,
212 bool isCRLF = false);
213
214[[nodiscard]] KMIME_EXPORT bool
215parseParameterList(const char *&scursor, const char *const send,
216 QMap<QString, QString> &result, bool isCRLF = false);
217
218/**
219 * Extract the charset embedded in the parameter list if there is one.
220 *
221 * @since 4.5
222 */
223[[nodiscard]] KMIME_EXPORT bool
224parseParameterListWithCharset(const char *&scursor, const char *const send,
226 QByteArray &charset, bool isCRLF = false);
227
228/**
229 Parses an integer number.
230 @param scursor pointer to the first character of the input string
231 @param send pointer to end of input buffer
232 @param result the parsing result
233 @returns The number of parsed digits (don't confuse with @p result!)
234*/
235[[nodiscard]] KMIME_EXPORT int parseDigits(const char *&scursor,
236 const char *const send, int &result);
237
238[[nodiscard]] KMIME_EXPORT bool
239parseTime(const char *&scursor, const char *const send, int &hour, int &min,
240 int &sec, long int &secsEastOfGMT, bool &timeZoneKnown,
241 bool isCRLF = false);
242
243[[nodiscard]] KMIME_EXPORT bool parseDateTime(const char *&scursor,
244 const char *const send,
245 QDateTime &result,
246 bool isCRLF = false);
247[[nodiscard]] KMIME_EXPORT bool parseQDateTime(const char *&scursor,
248 const char *const send,
249 QDateTime &result,
250 bool isCRLF = false);
251
252/** Parses the first header contained the given data.
253 * If a header is found @p head will be shortened to no longer
254 * include the corresponding data, ie. this method can be called
255 * iteratively on the same data until it returns @c null.
256 * @since 6.0
257 */
258[[nodiscard]] KMIME_EXPORT std::unique_ptr<KMime::Headers::Base> parseNextHeader(QByteArrayView &head);
259
260/**
261 * Extract the header header and the body from a complete content.
262 * Internally, it will simply look for the first newline and use that as a
263 * separator between the header and the body.
264 *
265 * @param content the complete mail
266 * @param header return value for the extracted header
267 * @param body return value for the extracted body
268 * @since 4.6
269 */
270KMIME_EXPORT void extractHeaderAndBody(const QByteArray &content,
271 QByteArray &header, QByteArray &body);
272
273} // namespace HeaderParsing
274
275} // namespace KMime
276
277Q_DECLARE_OPERATORS_FOR_FLAGS(KMime::HeaderParsing::ParseTokenFlags)
278
279
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Apr 27 2024 22:14:42 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.