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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
// SPDX-FileCopyrightText: 2020 Linus Jahn <lnj@kaidan.im>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "QXmppPubSubEvent.h"
#include "QXmppConstants_p.h"
#include "QXmppDataForm.h"
#include "QXmppUtils.h"
#include <QDomElement>
///
/// \class QXmppPubSubEventBase
///
/// The QXmppPubSubEventBase class is an abstract class used for parsing of
/// generic PubSub event notifications as defined by \xep{0060, Publish-
/// Subscribe}.
///
/// This class cannot be used directly. For a full-featured access to the event
/// notifications, please use the QXmppPubSubEvent class.
///
/// \since QXmpp 1.5
///
///
/// \class QXmppPubSubEvent
///
/// \brief The QXmppPubSubEvent class represents a PubSub event notification as
/// defined by \xep{0060, Publish-Subscribe}.
///
/// This class has a template parameter that can be used to define the type of
/// the contained items.
///
/// You can use QXmppPubSubEvent::isPubSubItem() to check whether an DOM element
/// is a <message/> with a PubSub event notification. If you set a special
/// type as a template parameter, validity of the items will also be checked. To
/// check for an event notification with items from \xep{0118, User Tune} for
/// example, you could use the following:
/// \code
/// QXmppPubSubEvent<QXmppTuneItem>::isPubSubEvent(element);
/// \endcode
///
/// \ingroup Stanzas
///
/// \since QXmpp 1.5
///
static const QStringList PUBSUB_EVENTS = {
QStringLiteral("configuration"),
QStringLiteral("delete"),
QStringLiteral("items"),
QStringLiteral("items"), // virtual retract type
QStringLiteral("purge"),
QStringLiteral("subscription"),
};
static QDomElement firstChildElement(const QDomElement &element, const QString &tagName, const QString &namespaceURI)
{
for (QDomNode child = element.firstChild(); !child.isNull(); child = child.nextSibling()) {
if (child.isElement() && child.namespaceURI() == namespaceURI) {
QDomElement elt = child.toElement();
if (tagName.isEmpty() || elt.tagName() == tagName) {
return elt;
}
}
}
return QDomElement();
}
class QXmppPubSubEventPrivate : public QSharedData
{
public:
QXmppPubSubEventPrivate(QXmppPubSubEventBase::EventType type,
const QString &node);
QXmppPubSubEventBase::EventType eventType;
QString node;
QStringList retractIds;
QString redirectUri;
std::optional<QXmppPubSubSubscription> subscription;
std::optional<QXmppDataForm> configurationForm;
};
QXmppPubSubEventPrivate::QXmppPubSubEventPrivate(QXmppPubSubEventBase::EventType type,
const QString &node)
: eventType(type), node(node)
{
}
///
/// Constructs a PubSub event.
///
QXmppPubSubEventBase::QXmppPubSubEventBase(EventType type, const QString &node)
: d(new QXmppPubSubEventPrivate(type, node))
{
setType(QXmppMessage::Normal);
}
/// Default copy-constructor.
QXmppPubSubEventBase::QXmppPubSubEventBase(const QXmppPubSubEventBase &other) = default;
/// Default move-constructor.
QXmppPubSubEventBase::QXmppPubSubEventBase(QXmppPubSubEventBase &&) = default;
QXmppPubSubEventBase::~QXmppPubSubEventBase() = default;
/// Default assignment operator.
QXmppPubSubEventBase &QXmppPubSubEventBase::operator=(const QXmppPubSubEventBase &other) = default;
/// Default move-assignment operator.
QXmppPubSubEventBase &QXmppPubSubEventBase::operator=(QXmppPubSubEventBase &&) = default;
///
/// Returns the event type of the PubSub event.
///
QXmppPubSubEventBase::EventType QXmppPubSubEventBase::eventType() const
{
return d->eventType;
}
///
/// Sets the event type of the PubSub event.
///
void QXmppPubSubEventBase::setEventType(EventType type)
{
d->eventType = type;
}
///
/// Returns the name of the event's node.
///
/// This does not work with Subscription events. In those cases you need to get
/// the node of the subscription.
///
/// \sa subscription()
/// \sa QXmppPubSubSubscription::node()
///
QString QXmppPubSubEventBase::node() const
{
return d->node;
}
///
/// Sets the name of the event's node.
///
/// This does not work with Subscription events. In those cases you need to set
/// the node of the subscription.
///
/// \sa subscription()
/// \sa QXmppPubSubSubscription::setNode()
///
void QXmppPubSubEventBase::setNode(const QString &node)
{
d->node = node;
}
///
/// Returns the item IDs that have been retracted.
///
/// This is only used for the Items event type.
///
QStringList QXmppPubSubEventBase::retractIds() const
{
return d->retractIds;
}
///
/// Sets the item IDs that have been retracted.
///
/// This is only used for the Items event type.
///
void QXmppPubSubEventBase::setRetractIds(const QStringList &retractIds)
{
d->retractIds = retractIds;
}
///
/// Returns the redirect URI to the new node.
///
/// This can be set for delete notifications to inform subscribers of the new
/// node. Inclusion of this is of course optional.
///
QString QXmppPubSubEventBase::redirectUri() const
{
return d->redirectUri;
}
///
/// Sets the redirect URI to the new node.
///
/// This can be set for delete notifications to inform subscribers of the new
/// node. Inclusion of this is of course optional.
///
void QXmppPubSubEventBase::setRedirectUri(const QString &redirectUri)
{
d->redirectUri = redirectUri;
}
///
/// Returns the subscription in case of a Subscription event.
///
std::optional<QXmppPubSubSubscription> QXmppPubSubEventBase::subscription() const
{
return d->subscription;
}
///
/// Sets the subscription in case of a Subscription event.
///
void QXmppPubSubEventBase::setSubscription(const std::optional<QXmppPubSubSubscription> &subscription)
{
d->subscription = subscription;
}
///
/// Returns a configuration data form if the event contains one.
///
std::optional<QXmppDataForm> QXmppPubSubEventBase::configurationForm() const
{
return d->configurationForm;
}
///
/// Sets a configuration data form (or clears it with std::nullopt).
///
void QXmppPubSubEventBase::setConfigurationForm(const std::optional<QXmppDataForm> &configurationForm)
{
d->configurationForm = configurationForm;
}
/// \cond
bool QXmppPubSubEventBase::isPubSubEvent(const QDomElement &stanza, std::function<bool(const QDomElement &)> isItemValid)
{
if (stanza.tagName() != QStringLiteral("message")) {
return false;
}
// find correct "event" element
auto event = firstChildElement(stanza, QStringLiteral("event"), ns_pubsub_event);
auto eventTypeElement = event.firstChildElement();
// check for validity of the event type
EventType eventType;
if (const auto index = PUBSUB_EVENTS.indexOf(eventTypeElement.tagName());
index != -1) {
eventType = EventType(index);
} else {
return false;
}
// check for "node" attribute when required
switch (eventType) {
case Delete:
case Items:
case Retract:
case Purge:
if (!eventTypeElement.hasAttribute(QStringLiteral("node"))) {
return false;
}
break;
case Configuration:
case Subscription:
break;
}
// check individual content
switch (eventType) {
case Delete: {
if (const auto redirect = eventTypeElement.firstChildElement(QStringLiteral("redirect"));
!redirect.isNull() && !redirect.hasAttribute(QStringLiteral("uri"))) {
return false;
}
break;
}
case Items:
case Retract: {
// check validity of the items using isItemValid()
for (auto itemElement = eventTypeElement.firstChildElement(QStringLiteral("item"));
!itemElement.isNull();
itemElement = itemElement.nextSiblingElement(QStringLiteral("item"))) {
if (!isItemValid(itemElement)) {
return false;
}
}
break;
}
case Subscription: {
if (!QXmppPubSubSubscription::isSubscription(eventTypeElement)) {
return false;
}
}
case Configuration:
case Purge:
break;
}
return true;
}
bool QXmppPubSubEventBase::parseExtension(const QDomElement &eventElement, QXmpp::SceMode sceMode)
{
if (sceMode & QXmpp::SceSensitive &&
eventElement.tagName() == QStringLiteral("event") &&
eventElement.namespaceURI() == ns_pubsub_event) {
// check that the query type is valid
const auto eventTypeElement = eventElement.firstChildElement();
if (const auto index = PUBSUB_EVENTS.indexOf(eventTypeElement.tagName());
index != -1) {
d->eventType = EventType(index);
} else {
return false;
}
// Detect our virtual retract event type
if (d->eventType == Items) {
auto child = eventTypeElement.firstChildElement();
if (!child.isNull()) {
if (child.tagName() == QStringLiteral("retract")) {
d->eventType = Retract;
}
}
// Don't support mixed retract/item events.
}
// parse "node" attribute
switch (d->eventType) {
case Configuration:
case Delete:
case Items:
case Retract:
case Purge:
d->node = eventTypeElement.attribute(QStringLiteral("node"));
break;
case Subscription:
break;
}
// check the items using isItemValid()
switch (d->eventType) {
case Delete:
if (auto redirect = eventTypeElement.firstChildElement(QStringLiteral("redirect"));
!redirect.isNull()) {
d->redirectUri = redirect.attribute(QStringLiteral("uri"));
}
break;
case Items:
// parse items
parseItems(eventTypeElement);
break;
case Retract:
// parse retract ids
for (auto retract = eventTypeElement.firstChildElement(QStringLiteral("retract"));
!retract.isNull();
retract = retract.nextSiblingElement(QStringLiteral("retract"))) {
d->retractIds << retract.attribute(QStringLiteral("id"));
}
break;
case Subscription: {
QXmppPubSubSubscription subscription;
subscription.parse(eventTypeElement);
d->subscription = subscription;
break;
}
case Configuration:
if (auto formElement = firstChildElement(eventTypeElement, QStringLiteral("x"), ns_data);
!formElement.isNull()) {
QXmppDataForm form;
form.parse(formElement);
d->configurationForm = form;
}
break;
case Purge:
break;
}
} else {
// handles QXmppMessage default extensions
return QXmppMessage::parseExtension(eventElement, sceMode);
}
return true;
}
void QXmppPubSubEventBase::serializeExtensions(QXmlStreamWriter *writer, QXmpp::SceMode sceMode, const QString &baseNamespace) const
{
QXmppMessage::serializeExtensions(writer, sceMode, baseNamespace);
if (!(sceMode & QXmpp::SceSensitive)) {
return;
}
writer->writeStartElement(QStringLiteral("event"));
writer->writeDefaultNamespace(ns_pubsub_event);
if (d->eventType == Subscription && d->subscription) {
d->subscription->toXml(writer);
} else {
writer->writeStartElement(PUBSUB_EVENTS.at(int(d->eventType)));
// write node attribute
switch (d->eventType) {
case Delete:
case Items:
case Retract:
case Purge:
// node attribute is required
writer->writeAttribute(QStringLiteral("node"), d->node);
break;
case Configuration:
// node attribute is optional
helperToXmlAddAttribute(writer, QStringLiteral("node"), d->node);
break;
case Subscription:
break;
}
switch (d->eventType) {
case Configuration:
if (d->configurationForm) {
d->configurationForm->toXml(writer);
}
break;
case Delete:
if (!d->redirectUri.isEmpty()) {
writer->writeStartElement(QStringLiteral("redirect"));
writer->writeAttribute(QStringLiteral("uri"), d->redirectUri);
writer->writeEndElement();
}
case Items:
// serialize items
serializeItems(writer);
break;
case Retract:
// serialize retract ids
for (const auto &id : std::as_const(d->retractIds)) {
writer->writeStartElement(QStringLiteral("retract"));
writer->writeAttribute(QStringLiteral("id"), id);
writer->writeEndElement();
}
break;
case Purge:
case Subscription:
break;
}
writer->writeEndElement(); // close event's type element
}
writer->writeEndElement(); // </event>
}
/// \endcond
|