1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#ifndef QXMPPIQHANDLING_H
#define QXMPPIQHANDLING_H
#include "QXmppClient.h"
#include "QXmppE2eeMetadata.h"
#include "QXmppFutureUtils_p.h"
#include "QXmppIq.h"
#include <QDomElement>
namespace QXmpp {
namespace Private {
QXMPP_EXPORT void sendIqReply(QXmppClient *client,
const QString &requestId,
const QString &requestFrom,
const std::optional<QXmppE2eeMetadata> &e2eeMetadata,
QXmppIq &&iq);
QXMPP_EXPORT std::tuple<bool, QString, QString> checkIsIqRequest(const QDomElement &el);
template<typename... VariantTypes>
void processHandleIqResult(QXmppClient *client,
const QString &requestId,
const QString &requestFrom,
const std::optional<QXmppE2eeMetadata> &e2eeMetadata,
std::variant<VariantTypes...> &&result)
{
std::visit([&](auto &&value) {
using T = std::decay_t<decltype(value)>;
static_assert(
std::is_base_of_v<QXmppIq, T> || std::is_same_v<QXmppStanza::Error, T>,
"QXmppIq based type or QXmppStanza::Error required");
if constexpr (std::is_base_of_v<QXmppIq, T>) {
sendIqReply(client, requestId, requestFrom, e2eeMetadata, std::move(value));
} else if constexpr (std::is_same_v<QXmppStanza::Error, T>) {
QXmppIq iq;
iq.setType(QXmppIq::Error);
iq.setError(value);
sendIqReply(client, requestId, requestFrom, e2eeMetadata, std::move(iq));
}
},
std::move(result));
}
template<typename T>
void processHandleIqResult(QXmppClient *client,
const QString &requestId,
const QString &requestFrom,
const std::optional<QXmppE2eeMetadata> &e2eeMetadata,
QXmppTask<T> future)
{
future.then(client, [client, requestId, requestFrom, e2eeMetadata](T result) {
processHandleIqResult(client, requestId, requestFrom, e2eeMetadata, result);
});
}
template<typename T, typename = std::enable_if_t<std::is_base_of_v<QXmppIq, T>, void>>
void processHandleIqResult(QXmppClient *client,
const QString &requestId,
const QString &requestFrom,
const std::optional<QXmppE2eeMetadata> &e2eeMetadata,
T &&result)
{
sendIqReply(client, requestId, requestFrom, e2eeMetadata, std::move(result));
}
template<typename Handler, typename IqType>
auto invokeIqHandler(Handler handler, IqType &&iq)
{
if constexpr (std::is_invocable<Handler, IqType &&>()) {
return handler(std::move(iq));
} else {
return handler->handleIq(std::move(iq));
}
}
template<typename IqType, typename Handler>
bool handleIqType(Handler handler,
QXmppClient *client,
const QDomElement &element,
const std::optional<QXmppE2eeMetadata> &e2eeMetadata,
const QString &tagName,
const QString &xmlNamespace)
{
if (IqType::checkIqType(tagName, xmlNamespace)) {
IqType iq;
iq.parse(element);
iq.setE2eeMetadata(e2eeMetadata);
auto id = iq.id(), from = iq.from();
processHandleIqResult(client, id, from, e2eeMetadata,
invokeIqHandler(std::forward<Handler>(handler), std::move(iq)));
return true;
}
return false;
}
} // namespace Private
///
/// Parses IQ requests, calls a handler and sends an IQ result or error.
///
/// It is the easiest to explain this function with a few examples.
///
/// ```
/// auto handled = QXmpp::handleIqElements<QXmppVersionIq>(element, e2eeMetadata, client, [](QXmppVersionIq iq) -> std::variant<QXmppVersionIq, QXmppStanza::Error> {
/// if (iq.type() == QXmppIq::Get) {
/// QXmppVersionIq response;
/// response.setName("MyApp");
/// response.setVersion("1.0");
/// // id, to and type of the IQ are set automatically.
/// return response;
/// } else if (iq.type() == QXmppIq::Set) {
/// return QXmppStanza::Error(QXmppStanza::Error::Cancel, QXmppStanza::Error::BadRequest, "IQ must be of type 'get'.");
/// }
/// });
/// ```
///
/// It is also possible to handle multiple IQ types.
/// ```
/// auto handled = QXmpp::handleIqElements<QXmppVersionIq, QXmppVCardIq>(
/// element, e2eeMetadata, client, [](std::variant<QXmppVersionIq, QXmppVCardIq> iqVariant) {
/// // ...
/// });
/// ```
/// It doesn't need to be a std::variant, it's only important that the object is callable with all
/// the IQ types. You can for example use different lambdas per type using this 'overloaded' helper.
/// ```
/// template<class... Ts>
/// struct overloaded : Ts... {
/// using Ts::operator()...;
/// };
/// // explicit deduction guide (not needed as of C++20)
/// template<class... Ts>
/// overloaded(Ts...) -> overloaded<Ts...>;
///
/// auto handled = QXmpp::handleIqElements<QXmppVersionIq, QXmppVCardIq>(
/// element, e2eeMetadata, client, overloaded {
/// [](QXmppVersionIq iq) {
/// // ...
/// },
/// [](QXmppVCardIq iq) {
/// // ...
/// }
/// });
/// ```
///
/// And another option is to an object with `handleIq()` functions.
/// ```
/// auto handled = QXmpp::handleIqElements<QXmppVersionIq, QXmppVCardIq>(element, e2eeMetadata, client, this);
/// // will call this->handleIq(QXmppVersionIq) or this->handleIq(QXmppVCardIq)
/// ```
///
/// The return type of the handler function can be:
/// 1. an QXmppIq based type
/// 2. a std::variant of QXmppIq based types (multiple are possible) and optionally also QXmppStanza::Error
/// 3. a QXmppTask of 1. or 2.
///
/// You don't need to set the values for id or the to address on the IQ result because that's done
/// automatically. Unless you want to return an error IQ you also don't need to set the IQ type.
///
/// If you return an QXmppStanza::Error, a normal QXmppIq with the error will be sent.
///
/// The provided optional QXmppE2eeMetadata is set on the parsed IQ and used to correctly encrypt
/// the IQ response using QXmppClient::reply().
///
/// \param element The DOM element that might contain an IQ.
/// \param e2eeMetadata The end-to-end encryption metadata that is used to encrypt the response
/// correctly and to be set on the parsed IQ.
/// \param client The client that should be used to send the response.
/// \param handler Function that can handle the IQ types from the template parameter or an object
/// that has handleIq() functions for each of the IQ types.
/// \return Whether the IQ could be parsed, handled and a response was or will be sent.
/// \since QXmpp 1.5
///
template<typename... IqTypes, typename Handler>
bool handleIqRequests(const QDomElement &element,
const std::optional<QXmppE2eeMetadata> &e2eeMetadata,
QXmppClient *client,
Handler handler)
{
if (auto [isRequest, tagName, xmlns] = Private::checkIsIqRequest(element); isRequest) {
return (Private::handleIqType<IqTypes>(handler, client, element, e2eeMetadata, tagName, xmlns) || ...);
}
return false;
}
///
/// Parses IQ requests, calls a handler and sends an IQ result or error.
///
/// It is the easiest to explain this function with a few examples.
///
/// ```
/// auto handled = QXmpp::handleIqElements<QXmppVersionIq>(element, client, [](QXmppVersionIq iq) -> std::variant<QXmppVersionIq, QXmppStanza::Error> {
/// if (iq.type() == QXmppIq::Get) {
/// QXmppVersionIq response;
/// response.setName("MyApp");
/// response.setVersion("1.0");
/// // id, to and type of the IQ are set automatically.
/// return response;
/// } else if (iq.type() == QXmppIq::Set) {
/// return QXmppStanza::Error(QXmppStanza::Error::Cancel, QXmppStanza::Error::BadRequest, "IQ must be of type 'get'.");
/// }
/// });
/// ```
///
/// It is also possible to handle multiple IQ types.
/// ```
/// auto handled = QXmpp::handleIqElements<QXmppVersionIq, QXmppVCardIq>(
/// element, client, [](std::variant<QXmppVersionIq, QXmppVCardIq> iqVariant) {
/// // ...
/// });
/// ```
/// It doesn't need to be a std::variant, it's only important that the object is callable with all
/// the IQ types. You can for example use different lambdas per type using this 'overloaded' helper.
/// ```
/// template<class... Ts>
/// struct overloaded : Ts... {
/// using Ts::operator()...;
/// };
/// // explicit deduction guide (not needed as of C++20)
/// template<class... Ts>
/// overloaded(Ts...) -> overloaded<Ts...>;
///
/// auto handled = QXmpp::handleIqElements<QXmppVersionIq, QXmppVCardIq>(
/// element, client, overloaded {
/// [](QXmppVersionIq iq) {
/// // ...
/// },
/// [](QXmppVCardIq iq) {
/// // ...
/// }
/// });
/// ```
///
/// And another option is to an object with `handleIq()` functions.
/// ```
/// auto handled = QXmpp::handleIqElements<QXmppVersionIq, QXmppVCardIq>(element, client, this);
/// // will call this->handleIq(QXmppVersionIq) or this->handleIq(QXmppVCardIq)
/// ```
///
/// The return type of the handler function can be:
/// 1. an QXmppIq based type
/// 2. a std::variant of QXmppIq based types (multiple are possible) and optionally also QXmppStanza::Error
/// 3. a QXmppTask of 1. or 2.
///
/// You don't need to set the values for id or the to address on the IQ result because that's done
/// automatically. Unless you want to return an error IQ you also don't need to set the IQ type.
///
/// If you return an QXmppStanza::Error, a normal QXmppIq with the error will be sent.
///
/// \param element The DOM element that might contain an IQ.
/// \param client The client that should be used to send the response.
/// \param handler Function that can handle the IQ types from the template parameter or an object
/// that has handleIq() functions for each of the IQ types.
/// \return Whether the IQ could be parsed, handled and a response was or will be sent.
/// \since QXmpp 1.5
///
template<typename... IqTypes, typename Handler>
bool handleIqRequests(const QDomElement &element, QXmppClient *client, Handler handler)
{
return handleIqRequests<IqTypes...>(element, std::nullopt, client, std::forward<Handler>(handler));
}
} // namespace QXmpp
#endif // QXMPPIQHANDLING_H
|