QXmpp::Iq::Received Struct
template <typename IqT> requires (IqT::StanzaType == Type::Get || IqT::StanzaType == Type::Set) && Private::HasXmlTag<typename IqT::PayloadType> struct QXmpp::Iq::Received| Header: | #include <QXmppIqHandling.h> |
| Since: | QXmpp 1.18 |
Detailed Description
Hook for incoming IQ requests of the type IqT, e.g. QXmpp::Iq::Get<Payload>. The handler is only called for requests of this type with the tag name and namespace of Payload::XmlTag.
hooks().add<QXmpp::Iq::Received<QXmpp::Iq::Get<VersionQuery>>>([](QXmpp::Iq::Get<VersionQuery> &&iq, const QXmpp::Iq::Context &context) {
return VersionQuery { u"MyApp"_s, u"1.0"_s };
});
The handler gets the request and optionally a QXmpp::Iq::Context, both parameters can also be declared as auto. It returns the payload of the result, QXmpp::Success for an empty result, a QXmppStanza::Error, a QXmpp::Iq::Reply or a std::variant of them, also as QXmppTask. The reply gets the id of the request and is sent to its sender. Requests whose payload cannot be parsed are answered with bad-request.
End-to-end encrypted requests are also passed to the handler and the reply is end-to-end encrypted the same way. Handlers not supporting them check QXmpp::Iq::Context::isEncrypted() and return QXmpp::Iq::Reply::encryptionNotSupported().
Handlers can reply asynchronously, e.g. as coroutine:
hooks().add<QXmpp::Iq::Received<QXmpp::Iq::Set<MyQuery>>>([this](QXmpp::Iq::Set<MyQuery> &&iq, const QXmpp::Iq::Context &context) -> QXmppTask<QXmpp::Iq::Reply> {
if (context.isEncrypted()) {
co_return QXmpp::Iq::Reply::encryptionNotSupported();
}
if (!isAllowed(iq.from)) {
co_return QXmppStanza::Error(QXmppStanza::Error::Cancel, QXmppStanza::Error::Forbidden);
}
co_await store(std::move(iq.payload));
co_return QXmpp::Iq::Reply();
});
If the owner of the handler is destroyed before it has replied, the request is answered with service-unavailable.
Only one handler per type and payload is used. A handler with a lower sequence number (see QXmpp::HookSequence) replaces the handler of another extension. Requests without handler are passed to QXmpp::Client::StanzaReceived.