From e01ce46971b85f6b2b778dc4ae41956a668f5b53 Mon Sep 17 00:00:00 2001 From: Linus Jahn Date: Sat, 8 Aug 2020 16:52:35 +0200 Subject: Add PubSubSubAuthorization form --- src/base/QXmppPubSubSubAuthorization.cpp | 178 +++++++++++++++++++++++++++++++ src/base/QXmppPubSubSubAuthorization.h | 63 +++++++++++ 2 files changed, 241 insertions(+) create mode 100644 src/base/QXmppPubSubSubAuthorization.cpp create mode 100644 src/base/QXmppPubSubSubAuthorization.h (limited to 'src/base') diff --git a/src/base/QXmppPubSubSubAuthorization.cpp b/src/base/QXmppPubSubSubAuthorization.cpp new file mode 100644 index 00000000..12fe1222 --- /dev/null +++ b/src/base/QXmppPubSubSubAuthorization.cpp @@ -0,0 +1,178 @@ +/* + * Copyright (C) 2008-2021 The QXmpp developers + * + * Author: + * Linus Jahn + * + * Source: + * https://github.com/qxmpp-project/qxmpp + * + * This file is a part of QXmpp library. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + */ + +#include "QXmppPubSubSubAuthorization.h" + +const auto FORM_TYPE_SUBSCRIBE_AUTHORIZATION = QStringLiteral("http://jabber.org/protocol/pubsub#subscribe_authorization"); +const auto ALLOW_SUBSCRIPTION = QStringLiteral("pubsub#allow"); +const auto NODE = QStringLiteral("pubsub#node"); +const auto SUBSCRIBER_JID = QStringLiteral("pubsub#subscriber_jid"); +const auto SUBID = QStringLiteral("pubsub#subid"); + +/// +/// \class QXmppPubSubSubAuthorization +/// +/// This class represents a PubSub subscribe authorization data form. +/// +/// \since QXmpp 1.5 +/// + +class QXmppPubSubSubAuthorizationPrivate : public QSharedData +{ +public: + std::optional allowSubscription; + QString node; + QString subscriberJid; + QString subid; +}; + +/// +/// Tries to parse a PubSub subscribe authorization form from a plain data form. +/// +/// \returns The parsed data form on success. +/// +std::optional QXmppPubSubSubAuthorization::fromDataForm(const QXmppDataForm &form) +{ + if (auto parsed = QXmppPubSubSubAuthorization(); + QXmppDataFormBase::fromDataForm(form, parsed)) { + return parsed; + } + return std::nullopt; +} + +QXmppPubSubSubAuthorization::QXmppPubSubSubAuthorization() + : d(new QXmppPubSubSubAuthorizationPrivate) +{ +} + +/// Copy-constructor. +QXmppPubSubSubAuthorization::QXmppPubSubSubAuthorization(const QXmppPubSubSubAuthorization &) = default; + +QXmppPubSubSubAuthorization::~QXmppPubSubSubAuthorization() = default; + +/// Assignment operator. +QXmppPubSubSubAuthorization &QXmppPubSubSubAuthorization::operator=(const QXmppPubSubSubAuthorization &) = default; + +/// +/// Returns whether the subscription is allowed. +/// +std::optional QXmppPubSubSubAuthorization::allowSubscription() const +{ + return d->allowSubscription; +} + +/// +/// Sets whether the subscription is allowed. +/// +void QXmppPubSubSubAuthorization::setAllowSubscription(std::optional allowSubscription) +{ + d->allowSubscription = allowSubscription; +} + +/// +/// Returns the node name of the relevant node. +/// +QString QXmppPubSubSubAuthorization::node() const +{ + return d->node; +} + +/// +/// Sets the node name of the relevant node. +/// +void QXmppPubSubSubAuthorization::setNode(const QString &node) +{ + d->node = node; +} + +/// +/// Returns the JID of the user requesting to subscribe. +/// +QString QXmppPubSubSubAuthorization::subscriberJid() const +{ + return d->subscriberJid; +} + +/// +/// Sets the JID of the user requesting to subscribe. +/// +void QXmppPubSubSubAuthorization::setSubscriberJid(const QString &subscriberJid) +{ + d->subscriberJid = subscriberJid; +} + +/// +/// Returns the subscription ID associated with the subscription request. +/// +QString QXmppPubSubSubAuthorization::subid() const +{ + return d->subid; +} + +/// +/// Sets the subscription ID associated with the subscription request. +/// +void QXmppPubSubSubAuthorization::setSubid(const QString &subid) +{ + d->subid = subid; +} + +QString QXmppPubSubSubAuthorization::formType() const +{ + return FORM_TYPE_SUBSCRIBE_AUTHORIZATION; +} + +bool QXmppPubSubSubAuthorization::parseField(const QXmppDataForm::Field &field) +{ + // ignore hidden fields + using Type = QXmppDataForm::Field::Type; + if (field.type() == Type::HiddenField) { + return false; + } + + const auto key = field.key(); + const auto value = field.value(); + + if (key == ALLOW_SUBSCRIPTION) { + d->allowSubscription = parseBool(value); + } else if (key == NODE) { + d->node = value.toString(); + } else if (key == SUBID) { + d->subid = value.toString(); + } else if (key == SUBSCRIBER_JID) { + d->subscriberJid = value.toString(); + } else { + return false; + } + return true; +} + +void QXmppPubSubSubAuthorization::serializeForm(QXmppDataForm &form) const +{ + using Type = QXmppDataForm::Field::Type; + + serializeOptional(form, Type::BooleanField, ALLOW_SUBSCRIPTION, d->allowSubscription); + serializeNullable(form, Type::TextSingleField, NODE, d->node); + serializeNullable(form, Type::TextSingleField, SUBID, d->subid); + serializeNullable(form, Type::JidSingleField, SUBSCRIBER_JID, d->subscriberJid); +} diff --git a/src/base/QXmppPubSubSubAuthorization.h b/src/base/QXmppPubSubSubAuthorization.h new file mode 100644 index 00000000..1c774014 --- /dev/null +++ b/src/base/QXmppPubSubSubAuthorization.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2008-2021 The QXmpp developers + * + * Author: + * Linus Jahn + * + * Source: + * https://github.com/qxmpp-project/qxmpp + * + * This file is a part of QXmpp library. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + */ + +#ifndef QXMPPPUBSUBSUBAUTHORIZATION_H +#define QXMPPPUBSUBSUBAUTHORIZATION_H + +#include "QXmppDataFormBase.h" + +class QXmppPubSubSubAuthorizationPrivate; + +class QXMPP_EXPORT QXmppPubSubSubAuthorization : public QXmppExtensibleDataFormBase +{ +public: + static std::optional fromDataForm(const QXmppDataForm &); + + QXmppPubSubSubAuthorization(); + QXmppPubSubSubAuthorization(const QXmppPubSubSubAuthorization &); + ~QXmppPubSubSubAuthorization(); + + QXmppPubSubSubAuthorization &operator=(const QXmppPubSubSubAuthorization &); + + std::optional allowSubscription() const; + void setAllowSubscription(std::optional allowSubscription); + + QString node() const; + void setNode(const QString &node); + + QString subscriberJid() const; + void setSubscriberJid(const QString &subscriberJid); + + QString subid() const; + void setSubid(const QString &subid); + +protected: + QString formType() const override; + bool parseField(const QXmppDataForm::Field &) override; + void serializeForm(QXmppDataForm &) const override; + +private: + QSharedDataPointer d; +}; + +#endif // QXMPPPUBSUBSUBAUTHORIZATION_H -- cgit v1.2.3