diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | src/base/QXmppConstants.cpp | 2 | ||||
| -rw-r--r-- | src/base/QXmppConstants_p.h | 2 | ||||
| -rw-r--r-- | src/base/QXmppJingleData.cpp (renamed from src/base/QXmppJingleIq.cpp) | 607 | ||||
| -rw-r--r-- | src/base/QXmppJingleData.h | 662 | ||||
| -rw-r--r-- | src/base/QXmppJingleIq.h | 574 | ||||
| -rw-r--r-- | src/base/QXmppMessage.cpp | 35 | ||||
| -rw-r--r-- | src/base/QXmppMessage.h | 6 | ||||
| -rw-r--r-- | src/client/QXmppJingleMessageInitiationManager.cpp | 584 | ||||
| -rw-r--r-- | src/client/QXmppJingleMessageInitiationManager.h | 126 |
10 files changed, 1948 insertions, 655 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 929554c4..7e94efdc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -36,6 +36,7 @@ set(INSTALL_HEADER_FILES base/QXmppIbbIq.h base/QXmppIq.h base/QXmppJingleIq.h + base/QXmppJingleData.h base/QXmppLogger.h base/QXmppMamIq.h base/QXmppMessage.h @@ -108,6 +109,7 @@ set(INSTALL_HEADER_FILES client/QXmppHttpUploadManager.h client/QXmppInvokable.h client/QXmppIqHandling.h + client/QXmppJingleMessageInitiationManager.h client/QXmppMamManager.h client/QXmppMessageHandler.h client/QXmppMessageReceiptManager.h @@ -172,7 +174,7 @@ set(SOURCE_FILES base/QXmppHttpUploadIq.cpp base/QXmppIbbIq.cpp base/QXmppIq.cpp - base/QXmppJingleIq.cpp + base/QXmppJingleData.cpp base/QXmppLogger.cpp base/QXmppMamIq.cpp base/QXmppMessage.cpp @@ -243,6 +245,7 @@ set(SOURCE_FILES client/QXmppInternalClientExtension.cpp client/QXmppInvokable.cpp client/QXmppIqHandling.cpp + client/QXmppJingleMessageInitiationManager.cpp client/QXmppMamManager.cpp client/QXmppMessageReceiptManager.cpp client/QXmppMucManager.cpp diff --git a/src/base/QXmppConstants.cpp b/src/base/QXmppConstants.cpp index be5b62c7..0ddd3db2 100644 --- a/src/base/QXmppConstants.cpp +++ b/src/base/QXmppConstants.cpp @@ -160,6 +160,8 @@ const char *ns_chat_markers = "urn:xmpp:chat-markers:0"; const char *ns_message_processing_hints = "urn:xmpp:hints"; // XEP-0352: Client State Indication const char *ns_csi = "urn:xmpp:csi:0"; +// XEP-0353: Jingle Message Initiation +const char *ns_jingle_message_initiation = "urn:xmpp:jingle-message:0"; // XEP-0357: Push Notifications const char *ns_push = "urn:xmpp:push:0"; // XEP-0359: Unique and Stable Stanza IDs diff --git a/src/base/QXmppConstants_p.h b/src/base/QXmppConstants_p.h index b90346b6..c859de96 100644 --- a/src/base/QXmppConstants_p.h +++ b/src/base/QXmppConstants_p.h @@ -172,6 +172,8 @@ extern const char *ns_chat_markers; extern const char *ns_message_processing_hints; // XEP-0352: Client State Indication extern const char *ns_csi; +// XEP-0353: Jingle Message Initiation +extern const char *ns_jingle_message_initiation; // XEP-0357: Push Notifications extern const char *ns_push; // XEP-0359: Unique and Stable Stanza IDs diff --git a/src/base/QXmppJingleIq.cpp b/src/base/QXmppJingleData.cpp index a89f87a6..ceb31b32 100644 --- a/src/base/QXmppJingleIq.cpp +++ b/src/base/QXmppJingleData.cpp @@ -3,7 +3,7 @@ // // SPDX-License-Identifier: LGPL-2.1-or-later -#include "QXmppJingleIq.h" +#include "QXmppJingleData.h" #include "QXmppConstants_p.h" #include "QXmppUtils.h" @@ -224,9 +224,7 @@ public: QString name; QString senders; - QString descriptionMedia; - quint32 descriptionSsrc; - QString descriptionType; + QXmppJingleDescription description; bool isRtpMultiplexingSupported = false; QString transportType; @@ -237,7 +235,6 @@ public: QString transportFingerprintHash; QString transportFingerprintSetup; - QList<QXmppJinglePayloadType> payloadTypes; QList<QXmppJingleCandidate> transportCandidates; // XEP-0167: Jingle RTP Sessions @@ -253,8 +250,8 @@ public: }; QXmppJingleIqContentPrivate::QXmppJingleIqContentPrivate() - : descriptionSsrc(0) { + description.setSsrc(0); } /// @@ -362,36 +359,74 @@ void QXmppJingleIq::Content::setSenders(const QString &senders) d->senders = senders; } +/// +/// Returns the description as specified by +/// \xep{0167, Jingle RTP Sessions} and RFC 3550. +/// +/// \since QXmpp 0.9 +/// +QXmppJingleDescription QXmppJingleIq::Content::description() const +{ + return d->description; +} + +void QXmppJingleIq::Content::setDescription(const QXmppJingleDescription &description) +{ + d->description = description; +} + +/// \deprecated This method is deprecated since QXmpp 1.6. Use +/// \c QXmppJingleIq::Conent::description().media() instead. QString QXmppJingleIq::Content::descriptionMedia() const { - return d->descriptionMedia; + return d->description.media(); } +/// \deprecated This method is deprecated since QXmpp 1.6. Use +/// \c QXmppJingleIq::Conent::description().setMedia() instead. void QXmppJingleIq::Content::setDescriptionMedia(const QString &media) { - d->descriptionMedia = media; + d->description.setMedia(media); } -/// /// Returns the description's 32-bit synchronization source for the media stream as specified by /// \xep{0167, Jingle RTP Sessions} and RFC 3550. /// /// \since QXmpp 0.9 +/// \deprecated This method is deprecated since QXmpp 1.6. Use +/// \c QXmppJingleIq::Content::description().setSsrc() instead. /// quint32 QXmppJingleIq::Content::descriptionSsrc() const { - return d->descriptionSsrc; + return d->description.ssrc(); } -/// -/// Sets the description's 32-bit synchronization source for the media stream as specified by -/// \xep{0167, Jingle RTP Sessions} and RFC 3550. -/// -/// \since QXmpp 0.9 -/// +/// \deprecated This method is deprecated since QXmpp 1.6. Use +/// \c QXmppJingleIq::Conent::description().setSsrc() instead. void QXmppJingleIq::Content::setDescriptionSsrc(quint32 ssrc) { - d->descriptionSsrc = ssrc; + d->description.setSsrc(ssrc); +} + +/// \deprecated This method is deprecated since QXmpp 1.6. Use +/// \c QXmppJingleIq::Conent::description().addPayloadType() instead. +void QXmppJingleIq::Content::addPayloadType(const QXmppJinglePayloadType &payload) +{ + d->description.addPayloadType(payload); +} + +/// \deprecated This method is deprecated since QXmpp 1.6. Use +/// \c QXmppJingleIq::Conent::description().payloadTypes() instead. +QList<QXmppJinglePayloadType> QXmppJingleIq::Content::payloadTypes() const +{ + return d->description.payloadTypes(); +} + +/// \deprecated This method is deprecated since QXmpp 1.6. Use +/// \c QXmppJingleIq::Conent::description().setPayloadTypes() instead. +void QXmppJingleIq::Content::setPayloadTypes(const QList<QXmppJinglePayloadType> &payloadTypes) +{ + d->description.setPayloadTypes(payloadTypes); } /// @@ -446,23 +481,6 @@ void QXmppJingleIq::Content::setRtpEncryption(const std::optional<QXmppJingleRtp d->rtpEncryption = rtpEncryption; } -void QXmppJingleIq::Content::addPayloadType(const QXmppJinglePayloadType &payload) -{ - d->descriptionType = ns_jingle_rtp; - d->payloadTypes << payload; -} - -QList<QXmppJinglePayloadType> QXmppJingleIq::Content::payloadTypes() const -{ - return d->payloadTypes; -} - -void QXmppJingleIq::Content::setPayloadTypes(const QList<QXmppJinglePayloadType> &payloadTypes) -{ - d->descriptionType = payloadTypes.isEmpty() ? QString() : ns_jingle_rtp; - d->payloadTypes = payloadTypes; -} - void QXmppJingleIq::Content::addTransportCandidate(const QXmppJingleCandidate &candidate) { d->transportType = ns_jingle_ice_udp; @@ -687,9 +705,9 @@ void QXmppJingleIq::Content::parse(const QDomElement &element) // description QDomElement descriptionElement = element.firstChildElement(QStringLiteral("description")); - d->descriptionType = descriptionElement.namespaceURI(); - d->descriptionMedia = descriptionElement.attribute(QStringLiteral("media")); - d->descriptionSsrc = descriptionElement.attribute(QStringLiteral("ssrc")).toULong(); + d->description.setType(descriptionElement.namespaceURI()); + d->description.setMedia(descriptionElement.attribute(QStringLiteral("media"))); + d->description.setSsrc(descriptionElement.attribute(QStringLiteral("ssrc")).toULong()); d->isRtpMultiplexingSupported = !descriptionElement.firstChildElement(QStringLiteral("rtcp-mux")).isNull(); for (auto childElement = descriptionElement.firstChildElement(); @@ -710,7 +728,7 @@ void QXmppJingleIq::Content::parse(const QDomElement &element) while (!child.isNull()) { QXmppJinglePayloadType payload; payload.parse(child); - d->payloadTypes << payload; + d->description.addPayloadType(payload); child = child.nextSiblingElement(QStringLiteral("payload-type")); } @@ -749,13 +767,13 @@ void QXmppJingleIq::Content::toXml(QXmlStreamWriter *writer) const helperToXmlAddAttribute(writer, QStringLiteral("senders"), d->senders); // description - if (!d->descriptionType.isEmpty() || !d->payloadTypes.isEmpty()) { + if (!d->description.type().isEmpty() || !d->description.payloadTypes().isEmpty()) { writer->writeStartElement(QStringLiteral("description")); - writer->writeDefaultNamespace(d->descriptionType); - helperToXmlAddAttribute(writer, QStringLiteral("media"), d->descriptionMedia); + writer->writeDefaultNamespace(d->description.type()); + helperToXmlAddAttribute(writer, QStringLiteral("media"), d->description.media()); - if (d->descriptionSsrc) { - writer->writeAttribute(QStringLiteral("ssrc"), QString::number(d->descriptionSsrc)); + if (d->description.ssrc()) { + writer->writeAttribute(QStringLiteral("ssrc"), QString::number(d->description.ssrc())); } if (d->isRtpMultiplexingSupported) { @@ -769,7 +787,7 @@ void QXmppJingleIq::Content::toXml(QXmlStreamWriter *writer) const jingleRtpFeedbackNegotiationElementsToXml(writer, d->rtpFeedbackProperties, d->rtpFeedbackIntervals); jingleRtpHeaderExtensionsNegotiationElementsToXml(writer, d->rtpHeaderExtensionProperties, d->isRtpHeaderExtensionMixingAllowed); - for (const auto &payload : d->payloadTypes) { + for (const auto &payload : d->description.payloadTypes()) { payload.toXml(writer); } @@ -888,7 +906,7 @@ bool QXmppJingleIq::Content::parseSdp(const QString &sdp) qWarning() << "Could not parse ssrc" << line; return false; } - d->descriptionSsrc = bits[0].toULong(); + d->description.setSsrc(bits[0].toULong()); } } else if (line.startsWith(QStringLiteral("m="))) { // FIXME: what do we do with the profile (bits[2]) ? @@ -897,7 +915,7 @@ bool QXmppJingleIq::Content::parseSdp(const QString &sdp) qWarning() << "Could not parse media" << line; return false; } - d->descriptionMedia = bits[0]; + d->description.setMedia(bits[0]); // parse payload types for (int i = 3; i < bits.size(); ++i) { @@ -912,7 +930,8 @@ bool QXmppJingleIq::Content::parseSdp(const QString &sdp) } } } - setPayloadTypes(payloads); + + d->description.setPayloadTypes(payloads); return true; } @@ -945,7 +964,7 @@ QString QXmppJingleIq::Content::toSdp() const // media QString payloads; QStringList attrs; - for (const QXmppJinglePayloadType &payload : d->payloadTypes) { + for (const QXmppJinglePayloadType &payload : d->description.payloadTypes()) { payloads += " " + QString::number(payload.id()); QString rtpmap = QString::number(payload.id()) + " " + payload.name() + "/" + QString::number(payload.clockrate()); if (payload.channels() > 1) { @@ -970,7 +989,7 @@ QString QXmppJingleIq::Content::toSdp() const attrs << QStringLiteral("a=fmtp:") + QByteArray::number(payload.id()) + QStringLiteral(" ") + paramList.join("; "); } } - sdp << QStringLiteral("m=%1 %2 RTP/AVP%3").arg(d->descriptionMedia, QString::number(localRtpPort), payloads); + sdp << QStringLiteral("m=%1 %2 RTP/AVP%3").arg(d->description.media(), QString::number(localRtpPort), payloads); sdp << QStringLiteral("c=%1").arg(addressToSdp(localRtpAddress)); sdp += attrs; @@ -996,45 +1015,60 @@ QString QXmppJingleIq::Content::toSdp() const /// \endcond +class QXmppJingleIqReasonPrivate : public QSharedData +{ +public: + QXmppJingleIqReasonPrivate(); + + QString m_text; + QXmppJingleReason::Type m_type; + QXmppJingleReason::RtpErrorCondition m_rtpErrorCondition; +}; + +QXmppJingleIqReasonPrivate::QXmppJingleIqReasonPrivate() + : m_type(QXmppJingleReason::Type::None), + m_rtpErrorCondition(QXmppJingleReason::RtpErrorCondition::NoErrorCondition) +{ +} + /// -/// \enum QXmppJingleIq::Reason::RtpErrorCondition -/// -/// Condition of an RTP-specific error +/// \class QXmppJingleReason /// -/// \since QXmpp 1.5 +/// The QXmppJingleReason class represents the "reason" element of a +/// QXmppJingle element. /// -QXmppJingleIq::Reason::Reason() - : m_type(None) +QXmppJingleReason::QXmppJingleReason() + : d(new QXmppJingleIqReasonPrivate()) { } /// Returns the reason's textual description. -QString QXmppJingleIq::Reason::text() const +QString QXmppJingleReason::text() const { - return m_text; + return d->m_text; } /// Sets the reason's textual description. -void QXmppJingleIq::Reason::setText(const QString &text) +void QXmppJingleReason::setText(const QString &text) { - m_text = text; + d->m_text = text; } /// Gets the reason's type. -QXmppJingleIq::Reason::Type QXmppJingleIq::Reason::type() const +QXmppJingleReason::Type QXmppJingleReason::type() const { - return m_type; + return d->m_type; } /// Sets the reason's type. -void QXmppJingleIq::Reason::setType(QXmppJingleIq::Reason::Type type) +void QXmppJingleReason::setType(QXmppJingleReason::Type type) { - m_type = type; + d->m_type = type; } /// @@ -1044,9 +1078,9 @@ void QXmppJingleIq::Reason::setType(QXmppJingleIq::Reason::Type type) /// /// \since QXmpp 1.5 /// -QXmppJingleIq::Reason::RtpErrorCondition QXmppJingleIq::Reason::rtpErrorCondition() const +QXmppJingleReason::RtpErrorCondition QXmppJingleReason::rtpErrorCondition() const { - return m_rtpErrorCondition; + return d->m_rtpErrorCondition; } /// @@ -1056,18 +1090,18 @@ QXmppJingleIq::Reason::RtpErrorCondition QXmppJingleIq::Reason::rtpErrorConditio /// /// \since QXmpp 1.5 /// -void QXmppJingleIq::Reason::setRtpErrorCondition(RtpErrorCondition rtpErrorCondition) +void QXmppJingleReason::setRtpErrorCondition(RtpErrorCondition rtpErrorCondition) { - m_rtpErrorCondition = rtpErrorCondition; + d->m_rtpErrorCondition = rtpErrorCondition; } /// \cond -void QXmppJingleIq::Reason::parse(const QDomElement &element) +void QXmppJingleReason::parse(const QDomElement &element) { - m_text = element.firstChildElement(QStringLiteral("text")).text(); + d->m_text = element.firstChildElement(QStringLiteral("text")).text(); for (int i = AlternativeSession; i <= UnsupportedTransports; i++) { if (!element.firstChildElement(jingle_reasons[i]).isNull()) { - m_type = static_cast<Type>(i); + d->m_type = static_cast<Type>(i); break; } } @@ -1078,27 +1112,29 @@ void QXmppJingleIq::Reason::parse(const QDomElement &element) if (child.namespaceURI() == ns_jingle_rtp_errors) { if (const auto index = JINGLE_RTP_ERROR_CONDITIONS.indexOf(child.tagName()); index != -1) { - m_rtpErrorCondition = RtpErrorCondition(index); + d->m_rtpErrorCondition = RtpErrorCondition(index); } break; } } } -void QXmppJingleIq::Reason::toXml(QXmlStreamWriter *writer) const +void QXmppJingleReason::toXml(QXmlStreamWriter *writer) const { - if (m_type < AlternativeSession || m_type > UnsupportedTransports) { + if (d->m_type < AlternativeSession || d->m_type > UnsupportedTransports) { return; } writer->writeStartElement(QStringLiteral("reason")); - if (!m_text.isEmpty()) { - helperToXmlAddTextElement(writer, QStringLiteral("text"), m_text); + writer->writeDefaultNamespace(ns_jingle); + + if (!d->m_text.isEmpty()) { + helperToXmlAddTextElement(writer, QStringLiteral("text"), d->m_text); } - writer->writeEmptyElement(jingle_reasons[m_type]); + writer->writeEmptyElement(jingle_reasons[d->m_type]); - if (m_rtpErrorCondition != NoErrorCondition) { - writer->writeStartElement(JINGLE_RTP_ERROR_CONDITIONS.at(m_rtpErrorCondition)); + if (d->m_rtpErrorCondition != NoErrorCondition) { + writer->writeStartElement(JINGLE_RTP_ERROR_CONDITIONS.at(d->m_rtpErrorCondition)); writer->writeDefaultNamespace(ns_jingle_rtp_errors); writer->writeEndElement(); } @@ -1120,7 +1156,7 @@ public: QString mujiGroupChatJid; QList<QXmppJingleIq::Content> contents; - QXmppJingleIq::Reason reason; + QXmppJingleReason reason; std::optional<QXmppJingleIq::RtpSessionState> rtpSessionState; }; @@ -1213,14 +1249,14 @@ void QXmppJingleIq::setInitiator(const QString &initiator) /// Returns a reference to the IQ's reason element. -QXmppJingleIq::Reason &QXmppJingleIq::reason() +QXmppJingleReason &QXmppJingleIq::reason() { return d->reason; } /// Returns a const reference to the IQ's reason element. -const QXmppJingleIq::Reason &QXmppJingleIq::reason() const +const QXmppJingleReason &QXmppJingleIq::reason() const { return d->reason; } @@ -1378,6 +1414,7 @@ void QXmppJingleIq::parseElementFromChild(const QDomElement &element) addContent(content); contentElement = contentElement.nextSiblingElement(QStringLiteral("content")); } + QDomElement reasonElement = jingleElement.firstChildElement(QStringLiteral("reason")); d->reason.parse(reasonElement); @@ -2056,6 +2093,142 @@ bool QXmppJinglePayloadType::operator==(const QXmppJinglePayloadType &other) con } } +class QXmppJingleDescriptionPrivate : public QSharedData +{ +public: + QXmppJingleDescriptionPrivate() = default; + + QString media; + quint32 ssrc; + QString type; + QList<QXmppJinglePayloadType> payloadTypes; +}; + +/// +/// \class QXmppJingleDescription +/// +/// \brief The QXmppJingleDescription class represents descriptions for Jingle elements including +/// media type, streaming source, namespace and payload types. +/// +/// \since QXmpp 1.6 +/// + +QXmppJingleDescription::QXmppJingleDescription() + : d(new QXmppJingleDescriptionPrivate()) +{ +} + +QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppJingleDescription) + +/// +/// Returns the media type. +/// +QString QXmppJingleDescription::media() const +{ + return d->media; +} + +/// +/// Sets the media type. +/// +void QXmppJingleDescription::setMedia(const QString &media) +{ + d->media = media; +} + +/// +/// Returns the streaming source. +/// +quint32 QXmppJingleDescription::ssrc() const +{ + return d->ssrc; +} + +/// +/// Sets the streaming source. +/// +void QXmppJingleDescription::setSsrc(quint32 ssrc) +{ + d->ssrc = ssrc; +} + +/// +/// Returns the description namespace. +/// +QString QXmppJingleDescription::type() const +{ + return d->type; +} + +/// +/// Sets the description namespace. +/// +void QXmppJingleDescription::setType(const QString &type) +{ + d->type = type; +} + +/// +/// Adds a payload type to the list of payload types. +/// +void QXmppJingleDescription::addPayloadType(const QXmppJinglePayloadType &payload) +{ + d->type = ns_jingle_rtp; + d->payloadTypes.append(payload); +} + +/// +/// Returns a list of payload types. +/// +const QList<QXmppJinglePayloadType> &QXmppJingleDescription::payloadTypes() const +{ + return d->payloadTypes; +} + +/// +/// Sets the list of payload types. +/// +void QXmppJingleDescription::setPayloadTypes(const QList<QXmppJinglePayloadType> &payloadTypes) +{ + d->type = payloadTypes.isEmpty() ? QString() : ns_jingle_rtp; + d->payloadTypes = payloadTypes; +} + +/// \cond +void QXmppJingleDescription::parse(const QDomElement &element) +{ + d->type = element.namespaceURI(); + d->media = element.attribute(QStringLiteral("media")); + d->ssrc = element.attribute(QStringLiteral("ssrc")).toULong(); + + QDomElement child { element.firstChildElement(QStringLiteral("payload-type")) }; + while (!child.isNull()) { + QXmppJinglePayloadType payload; + payload.parse(child); + d->payloadTypes.append(payload); + child = child.nextSiblingElement(QStringLiteral("payload-type")); + } +} + +void QXmppJingleDescription::toXml(QXmlStreamWriter *writer) const +{ + writer->writeStartElement(QStringLiteral("description")); + writer->writeDefaultNamespace(d->type); + + helperToXmlAddAttribute(writer, QStringLiteral("media"), d->media); + + if (d->ssrc) { + writer->writeAttribute(QStringLiteral("ssrc"), QString::number(d->ssrc)); + } + + for (const auto &payloadType : d->payloadTypes) { + payloadType.toXml(writer); + } + + writer->writeEndElement(); +} +/// \endcond + class QXmppSdpParameterPrivate : public QSharedData { public: @@ -2779,3 +2952,275 @@ bool QXmppJingleRtpHeaderExtensionProperty::isJingleRtpHeaderExtensionProperty(c return element.tagName() == QStringLiteral("rtp-hdrext") && element.namespaceURI() == ns_jingle_rtp_header_extensions_negotiation; } + +class QXmppJingleMessageInitiationElementPrivate : public QSharedData +{ +public: + QXmppJingleMessageInitiationElementPrivate() = default; + + QXmppJingleMessageInitiationElement::Type type { QXmppJingleMessageInitiationElement::Type::None }; + QString id; + + std::optional<QXmppJingleDescription> description; + std::optional<QXmppJingleReason> reason; + QString migratedTo; + + bool containsTieBreak; +}; + +QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppJingleReason) + +/// +/// \enum QXmppJingleMessageInitiationElement::Type +/// +/// Possible types of Jingle Message Initiation elements +/// + +/// +/// \class QXmppJingleMessageInitiationElement +/// +/// \brief The QXmppJingleMessageInitiationElement class represents a Jingle Message Initiation +/// element as specified by \xep{0353}: Jingle Message Initiation. +/// +/// \ingroup Stanzas +/// +/// \since QXmpp 1.6 +/// + +/// +/// \brief Constructs a Jingle Message Initiation element. +/// \param type The JMI element type +/// +QXmppJingleMessageInitiationElement::QXmppJingleMessageInitiationElement() + : d(new QXmppJingleMessageInitiationElementPrivate()) +{ +} + +/// +/// Returns the Jingle Message Initiation element type +/// +QXmppJingleMessageInitiationElement::Type QXmppJingleMessageInitiationElement::type() const +{ + return d->type; +} + +/// +/// Sets the Jingle Message Initiation element type. +/// +void QXmppJingleMessageInitiationElement::setType(Type type) +{ + d->type = type; +} + +/// +/// Returns the Jingle Message Initiation element id. +/// +QString QXmppJingleMessageInitiationElement::id() const +{ + return d->id; +} + +/// +/// Sets the Jingle Message Initiation element id. +/// +void QXmppJingleMessageInitiationElement::setId(const QString &id) +{ + d->id = id; +} + +/// +/// Returns the Jingle Message Initiation element description. +/// +std::optional<QXmppJingleDescription> QXmppJingleMessageInitiationElement::description() const +{ + return d->description; +} + +/// +/// Sets the Jingle Message Initiation element description. +/// +void QXmppJingleMessageInitiationElement::setDescription(std::optional<QXmppJingleDescription> description) +{ + d->description = description; +} + +/// +/// Returns the Jingle Message Initiation element reason. +/// +std::optional<QXmppJingleReason> QXmppJingleMessageInitiationElement::reason() const +{ + return d->reason; +} + +/// +/// Sets the Jingle Message Initiation element reason. +/// +void QXmppJingleMessageInitiationElement::setReason(std::optional<QXmppJingleReason> reason) +{ + d->reason = reason; +} + +/// +/// Returns true if the Jingle Message Initiation element contains a <tie-break/> tag. +/// +bool QXmppJingleMessageInitiationElement::containsTieBreak() const +{ + return d->containsTieBreak; +} + +/// +/// Sets if the Jingle Message Initiation element contains a <tie-break/> tag. +/// +void QXmppJingleMessageInitiationElement::setContainsTieBreak(bool containsTieBreak) +{ + d->containsTieBreak = containsTieBreak; +} + +/// +/// Returns the Jingle Message Initiation element ID migrated to if the Jingle is being migrated +/// to a different device. +/// +QString QXmppJingleMessageInitiationElement::migratedTo() const +{ + return d->migratedTo; +} + +/// +/// Sets the Jingle Message Initiation element ID migrated to if the Jingle is being migrated +/// to a different device. +/// +void QXmppJingleMessageInitiationElement::setMigratedTo(const QString &migratedTo) +{ + d->migratedTo = migratedTo; +} + +/// \cond +void QXmppJingleMessageInitiationElement::parse(const QDomElement &element) +{ + std::optional<Type> type { stringToJmiElementType(element.nodeName()) }; + + if (!type.has_value()) { + return; + } + + d->type = type.value(); + d->id = element.attribute(QStringLiteral("id")); + + // Type::Proceed and Type::Ringing don't need any parsing aside of the id. + switch (d->type) { + case Type::Propose: { + if (const auto &descriptionElement = element.firstChildElement("description"); !descriptionElement.isNull()) { + d->description = QXmppJingleDescription(); + d->description->parse(descriptionElement); + } + + break; + } + case Type::Reject: + case Type::Retract: + d->containsTieBreak = !element.firstChildElement("tie-break").isNull(); + + if (const auto &reasonElement = element.firstChildElement("reason"); !reasonElement.isNull()) { + d->reason = QXmppJingleReason(); + d->reason->parse(reasonElement); + } + + break; + case Type::Finish: + if (auto reasonElement = element.firstChildElement("reason"); !reasonElement.isNull()) { + d->reason = QXmppJingleReason(); + d->reason->parse(reasonElement); + } + + if (auto migratedToElement = element.firstChildElement("migrated"); !migratedToElement.isNull()) { + d->migratedTo = migratedToElement.attribute("to"); + } + + break; + default: + break; + } +} + +void QXmppJingleMessageInitiationElement::toXml(QXmlStreamWriter *writer) const +{ + writer->writeStartElement(jmiElementTypeToString(d->type)); + writer->writeDefaultNamespace(ns_jingle_message_initiation); + + helperToXmlAddAttribute(writer, QStringLiteral("id"), d->id); + + if (d->description) { + d->description->toXml(writer); + } + + if (d->reason) { + d->reason->toXml(writer); + } + + if (d->containsTieBreak) { + writer->writeEmptyElement(QStringLiteral("tie-break")); + } + + if (!d->migratedTo.isEmpty()) { + writer->writeEmptyElement(QStringLiteral("migrated")); + helperToXmlAddAttribute(writer, QStringLiteral("to"), d->migratedTo); + } + + writer->writeEndElement(); +} +/// \endcond + +QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppJingleMessageInitiationElement) + +/// +/// Returns true if passed QDomElement is a Jingle Message Initiation element +/// +bool QXmppJingleMessageInitiationElement::isJingleMessageInitiationElement(const QDomElement &element) +{ + return stringToJmiElementType(element.tagName()).has_value() && element.hasAttribute(QStringLiteral("id")) && element.namespaceURI() == ns_jingle_message_initiation; +} + +/// +/// Takes a Jingle Message Initiation element type and parses it to a string. +/// +QString QXmppJingleMessageInitiationElement::jmiElementTypeToString(Type type) +{ + switch (type) { + case Type::Propose: + return "propose"; + case Type::Ringing: + return "ringing"; + case Type::Proceed: + return "proceed"; + case Type::Reject: + return "reject"; + case Type::Retract: + return "retract"; + case Type::Finish: + return "finish"; + default: + return {}; + } +} + +/// +/// Takes a string and parses it to a Jingle Message Initiation element type. +/// +std::optional<QXmppJingleMessageInitiationElement::Type> QXmppJingleMessageInitiationElement::stringToJmiElementType(const QString &typeStr) +{ + if (typeStr == "propose") { + return Type::Propose; + } else if (typeStr == "ringing") { + return Type::Ringing; + } else if (typeStr == "proceed") { + return Type::Proceed; + } else if (typeStr == "reject") { + return Type::Reject; + } else if (typeStr == "retract") { + return Type::Retract; + } else if (typeStr == "finish") { + return Type::Finish; + } + + return std::nullopt; +} diff --git a/src/base/QXmppJingleData.h b/src/base/QXmppJingleData.h new file mode 100644 index 00000000..e36577be --- /dev/null +++ b/src/base/QXmppJingleData.h @@ -0,0 +1,662 @@ +// SPDX-FileCopyrightText: 2010 Jeremy Lainé <jeremy.laine@m4x.org> +// SPDX-FileCopyrightText: 2022 Melvin Keskin <melvo@olomono.de> +// SPDX-FileCopyrightText: 2023 Tibor Csötönyi <work@taibsu.de> +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#ifndef QXMPPJINGLEIQ_H +#define QXMPPJINGLEIQ_H + +#include "QXmppIq.h" + +#include <variant> + +#include <QHostAddress> + +class QXmppJingleCandidatePrivate; +class QXmppJingleDescriptionPrivate; +class QXmppJingleIqContentPrivate; +class QXmppJingleIqReasonPrivate; +class QXmppJingleIqPrivate; +class QXmppJinglePayloadTypePrivate; +class QXmppJingleRtpCryptoElementPrivate; +class QXmppJingleRtpEncryptionPrivate; +class QXmppJingleRtpFeedbackPropertyPrivate; +class QXmppJingleRtpHeaderExtensionPropertyPrivate; +class QXmppSdpParameterPrivate; +class QXmppJingleMessageInitiationElementPrivate; + +class QXMPP_EXPORT QXmppSdpParameter +{ +public: + QXmppSdpParameter(); + + QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppSdpParameter) + + QString name() const; + void setName(const QString &name); + + QString value() const; + void setValue(const QString &value); + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + + static bool isSdpParameter(const QDomElement &element); + +private: + QSharedDataPointer<QXmppSdpParameterPrivate> d; +}; + +class QXMPP_EXPORT QXmppJingleRtpCryptoElement +{ +public: + QXmppJingleRtpCryptoElement(); + + QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppJingleRtpCryptoElement) + + uint32_t tag() const; + void setTag(uint32_t tag); + + QString cryptoSuite() const; + void setCryptoSuite(const QString &cryptoSuite); + + QString keyParams() const; + void setKeyParams(const QString &keyParams); + + QString sessionParams() const; + void setSessionParams(const QString &sessionParams); + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + + static bool isJingleRtpCryptoElement(const QDomElement &element); + +private: + QSharedDataPointer<QXmppJingleRtpCryptoElementPrivate> d; +}; + +class QXMPP_EXPORT QXmppJingleRtpEncryption +{ +public: + QXmppJingleRtpEncryption(); + + QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppJingleRtpEncryption) + + bool isRequired() const; + void setRequired(bool isRequired); + + QVector<QXmppJingleRtpCryptoElement> cryptoElements() const; + void setCryptoElements(const QVector<QXmppJingleRtpCryptoElement> &cryptoElements); + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + + static bool isJingleRtpEncryption(const QDomElement &element); + +private: + QSharedDataPointer<QXmppJingleRtpEncryptionPrivate> d; +}; + +class QXMPP_EXPORT QXmppJingleRtpFeedbackProperty +{ +public: + QXmppJingleRtpFeedbackProperty(); + + QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppJingleRtpFeedbackProperty) + + QString type() const; + void setType(const QString &type); + + QString subtype() const; + void setSubtype(const QString &subtype); + + QVector<QXmppSdpParameter> parameters() const; + void setParameters(const QVector<QXmppSdpParameter> ¶meters); + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + + static bool isJingleRtpFeedbackProperty(const QDomElement &element); + +private: + QSharedDataPointer<QXmppJingleRtpFeedbackPropertyPrivate> d; +}; + +class QXMPP_EXPORT QXmppJingleRtpFeedbackInterval +{ +public: + QXmppJingleRtpFeedbackInterval(); + + QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppJingleRtpFeedbackInterval) + + uint64_t value() const; + void setValue(uint64_t value); + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + + static bool isJingleRtpFeedbackInterval(const QDomElement &element); + +private: + uint64_t m_value; +}; + +class QXMPP_EXPORT QXmppJingleRtpHeaderExtensionProperty +{ +public: + enum Senders { + /// The initiator and the sender are allowed. + Both, + /// Only the initiator is allowed. + Initiator, + /// Only the responder is allowed. + Responder + }; + + QXmppJingleRtpHeaderExtensionProperty(); + + QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppJingleRtpHeaderExtensionProperty) + + uint32_t id() const; + void setId(uint32_t id); + + QString uri() const; + void setUri(const QString &uri); + + Senders senders() const; + void setSenders(Senders senders); + + QVector<QXmppSdpParameter> parameters() const; + void setParameters(const QVector<QXmppSdpParameter> ¶meters); + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + + static bool isJingleRtpHeaderExtensionProperty(const QDomElement &element); + +private: + QSharedDataPointer<QXmppJingleRtpHeaderExtensionPropertyPrivate> d; +}; + +/// +/// \brief The QXmppJinglePayloadType class represents a payload type +/// as specified by \xep{0167}: Jingle RTP Sessions and RFC 5245. +/// +class QXMPP_EXPORT QXmppJinglePayloadType +{ +public: + QXmppJinglePayloadType(); + QXmppJinglePayloadType(const QXmppJinglePayloadType &other); + ~QXmppJinglePayloadType(); + + unsigned char channels() const; + void setChannels(unsigned char channels); + + unsigned int clockrate() const; + void setClockrate(unsigned int clockrate); + + unsigned char id() const; + void setId(unsigned char id); + + unsigned int maxptime() const; + void setMaxptime(unsigned int maxptime); + + QString name() const; + void setName(const QString &name); + + QMap<QString, QString> parameters() const; + void setParameters(const QMap<QString, QString> ¶meters); + + unsigned int ptime() const; + void setPtime(unsigned int ptime); + + QVector<QXmppJingleRtpFeedbackProperty> rtpFeedbackProperties() const; + void setRtpFeedbackProperties(const QVector<QXmppJingleRtpFeedbackProperty> &rtpFeedbackProperties); + + QVector<QXmppJingleRtpFeedbackInterval> rtpFeedbackIntervals() const; + void setRtpFeedbackIntervals(const QVector<QXmppJingleRtpFeedbackInterval> &rtpFeedbackIntervals); + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + + QXmppJinglePayloadType &operator=(const QXmppJinglePayloadType &other); + bool operator==(const QXmppJinglePayloadType &other) const; + +private: + QSharedDataPointer<QXmppJinglePayloadTypePrivate> d; +}; + +class QXMPP_EXPORT QXmppJingleDescription +{ +public: + QXmppJingleDescription(); + QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppJingleDescription) + + QString media() const; + void setMedia(const QString &media); + + quint32 ssrc() const; + void setSsrc(quint32 ssrc); + + QString type() const; + void setType(const QString &type); + + void addPayloadType(const QXmppJinglePayloadType &payload); + const QList<QXmppJinglePayloadType> &payloadTypes() const; + void setPayloadTypes(const QList<QXmppJinglePayloadType> &payloadTypes); + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + +private: + QSharedDataPointer<QXmppJingleDescriptionPrivate> d; +}; + +/// +/// \brief The QXmppJingleCandidate class represents a transport candidate +/// as specified by \xep{0176}: Jingle ICE-UDP Transport Method. +/// +class QXMPP_EXPORT QXmppJingleCandidate +{ +public: + /// This enum is used to describe a candidate's type. + enum Type { + HostType, ///< Host candidate, a local address/port. + PeerReflexiveType, ///< Peer-reflexive candidate, + ///< the address/port as seen from the peer. + ServerReflexiveType, ///< Server-reflexive candidate, + ///< the address/port as seen by the STUN server + RelayedType ///< Relayed candidate, a candidate from + ///< a TURN relay. + }; + + QXmppJingleCandidate(); + QXmppJingleCandidate(const QXmppJingleCandidate &other); + QXmppJingleCandidate(QXmppJingleCandidate &&); + ~QXmppJingleCandidate(); + + QXmppJingleCandidate &operator=(const QXmppJingleCandidate &other); + QXmppJingleCandidate &operator=(QXmppJingleCandidate &&); + + int component() const; + void setComponent(int component); + + QString foundation() const; + void setFoundation(const QString &foundation); + + int generation() const; + void setGeneration(int generation); + + QHostAddress host() const; + void setHost(const QHostAddress &host); + + QString id() const; + void setId(const QString &id); + + int network() const; + void setNetwork(int network); + + quint16 port() const; + void setPort(quint16 port); + + int priority() const; + void setPriority(int priority); + + QString protocol() const; + void setProtocol(const QString &protocol); + + QXmppJingleCandidate::Type type() const; + void setType(QXmppJingleCandidate::Type); + + bool isNull() const; + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + + static QXmppJingleCandidate::Type typeFromString(const QString &typeStr, bool *ok = nullptr); + static QString typeToString(QXmppJingleCandidate::Type type); + /// \endcond + +private: + QSharedDataPointer<QXmppJingleCandidatePrivate> d; +}; + +class QXMPP_EXPORT QXmppJingleReason +{ +public: + /// This enum is used to describe a reason's type. + enum Type { + None, + AlternativeSession, + Busy, + Cancel, + ConnectivityError, + Decline, + Expired, + FailedApplication, + FailedTransport, + GeneralError, + Gone, + IncompatibleParameters, + MediaError, + SecurityError, + Success, + Timeout, + UnsupportedApplications, + UnsupportedTransports + }; + + /// Condition of an RTP-specific error + /// \since QXmpp 1.5 + enum RtpErrorCondition { + /// There is no error condition. + NoErrorCondition, + /// The encryption offer is rejected. + InvalidCrypto, + /// Encryption is required but not offered. + CryptoRequired + }; + + QXmppJingleReason(); + + QString text() const; + void setText(const QString &text); + + Type type() const; + void setType(Type type); + + RtpErrorCondition rtpErrorCondition() const; + void setRtpErrorCondition(RtpErrorCondition rtpErrorCondition); + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + + /// \endcond + + QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppJingleReason) + +private: + QSharedDataPointer<QXmppJingleIqReasonPrivate> d; +}; + +/// +/// \brief The QXmppJingleIq class represents an IQ used for initiating media +/// sessions as specified by \xep{0166}: Jingle. +/// +/// \ingroup Stanzas +/// +class QXMPP_EXPORT QXmppJingleIq : public QXmppIq +{ +public: + /// This enum is used to describe a Jingle action. + enum Action { + ContentAccept, + ContentAdd, + ContentModify, + ContentReject, + ContentRemove, + DescriptionInfo, + SecurityInfo, + SessionAccept, + SessionInfo, + SessionInitiate, + SessionTerminate, + TransportAccept, + TransportInfo, + TransportReject, + TransportReplace + }; + + enum Creator { + /// The initiator generated the content type. + Initiator, + /// The responder generated the content type. + Responder + }; + + struct RtpSessionStateActive + { + }; + + struct RtpSessionStateHold + { + }; + + struct RtpSessionStateUnhold + { + }; + + struct RtpSessionStateMuting + { + /// True when temporarily not sending media to the other party but continuing to accept + /// media from it, false for ending mute state + bool isMute = true; + /// Creator of the corresponding session + Creator creator; + /// Session to be muted (e.g., only audio or video) + QString name; + }; + + struct RtpSessionStateRinging + { + }; + + using RtpSessionState = std::variant<RtpSessionStateActive, RtpSessionStateHold, RtpSessionStateUnhold, RtpSessionStateMuting, RtpSessionStateRinging>; + + /// Alias to QXmppJingleReason for compatibility. + using Reason = QXmppJingleReason; + + /// \internal + /// + /// The QXmppJingleIq::Content class represents the "content" element of a + /// QXmppJingleIq. + /// + class QXMPP_EXPORT Content + { + public: + Content(); + Content(const QXmppJingleIq::Content &other); + Content(QXmppJingleIq::Content &&); + ~Content(); + + Content &operator=(const Content &other); + Content &operator=(Content &&); + + QString creator() const; + void setCreator(const QString &creator); + + QString name() const; + void setName(const QString &name); + + QString senders() const; + void setSenders(const QString &senders); + + // XEP-0167: Jingle RTP Sessions + QXmppJingleDescription description() const; + void setDescription(const QXmppJingleDescription &description); + +#if QXMPP_DEPRECATED_SINCE(1, 6) + QString descriptionMedia() const; + void setDescriptionMedia(const QString &media); + + quint32 descriptionSsrc() const; + void setDescriptionSsrc(quint32 ssrc); + + void addPayloadType(const QXmppJinglePayloadType &payload); + QList<QXmppJinglePayloadType> payloadTypes() const; + void setPayloadTypes(const QList<QXmppJinglePayloadType> &payloadTypes); +#endif + + bool isRtpMultiplexingSupported() const; + void setRtpMultiplexingSupported(bool isRtpMultiplexingSupported); + + std::optional<QXmppJingleRtpEncryption> rtpEncryption() const; + void setRtpEncryption(const std::optional<QXmppJingleRtpEncryption> &rtpEncryption); + + void addTransportCandidate(const QXmppJingleCandidate &candidate); + QList<QXmppJingleCandidate> transportCandidates() const; + void setTransportCandidates(const QList<QXmppJingleCandidate> &candidates); + + QString transportUser() const; + void setTransportUser(const QString &user); + + QString transportPassword() const; + void setTransportPassword(const QString &password); + + QVector<QXmppJingleRtpFeedbackProperty> rtpFeedbackProperties() const; + void setRtpFeedbackProperties(const QVector<QXmppJingleRtpFeedbackProperty> &rtpFeedbackProperties); + + QVector<QXmppJingleRtpFeedbackInterval> rtpFeedbackIntervals() const; + void setRtpFeedbackIntervals(const QVector<QXmppJingleRtpFeedbackInterval> &rtpFeedbackIntervals); + + QVector<QXmppJingleRtpHeaderExtensionProperty> rtpHeaderExtensionProperties() const; + void setRtpHeaderExtensionProperties(const QVector<QXmppJingleRtpHeaderExtensionProperty> &rtpHeaderExtensionProperties); + + bool isRtpHeaderExtensionMixingAllowed() const; + void setRtpHeaderExtensionMixingAllowed(bool isRtpHeaderExtensionMixingAllowed); + + // XEP-0320: Use of DTLS-SRTP in Jingle Sessions + QByteArray transportFingerprint() const; + void setTransportFingerprint(const QByteArray &fingerprint); + + QString transportFingerprintHash() const; + void setTransportFingerprintHash(const QString &hash); + + QString transportFingerprintSetup() const; + void setTransportFingerprintSetup(const QString &setup); + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + + bool parseSdp(const QString &sdp); + QString toSdp() const; + /// \endcond + + private: + QSharedDataPointer<QXmppJingleIqContentPrivate> d; + }; + + QXmppJingleIq(); + QXmppJingleIq(const QXmppJingleIq &other); + QXmppJingleIq(QXmppJingleIq &&); + ~QXmppJingleIq() override; + + QXmppJingleIq &operator=(const QXmppJingleIq &other); + QXmppJingleIq &operator=(QXmppJingleIq &&); + + Action action() const; + void setAction(Action action); + + void addContent(const Content &content); + QList<Content> contents() const; + void setContents(const QList<Content> &contents); + + QString initiator() const; + void setInitiator(const QString &initiator); + + QXmppJingleReason &reason(); + const QXmppJingleReason &reason() const; + + QString responder() const; + void setResponder(const QString &responder); + +#if QXMPP_DEPRECATED_SINCE(1, 5) + QT_DEPRECATED_X("Use QXmpp::rtpSessionState() instead") + bool ringing() const; + QT_DEPRECATED_X("Use QXmpp::setRtpSessionState() instead") + void setRinging(bool ringing); +#endif + + QString sid() const; + void setSid(const QString &sid); + + QString mujiGroupChatJid() const; + void setMujiGroupChatJid(const QString &mujiGroupChatJid); + + std::optional<RtpSessionState> rtpSessionState() const; + void setRtpSessionState(const std::optional<RtpSessionState> &rtpSessionState); + + /// \cond + static bool isJingleIq(const QDomElement &element); + /// \endcond + +protected: + /// \cond + void parseElementFromChild(const QDomElement &element) override; + void toXmlElementFromChild(QXmlStreamWriter *writer) const override; + /// \endcond + +private: + QSharedDataPointer<QXmppJingleIqPrivate> d; +}; + +class QXMPP_EXPORT QXmppJingleMessageInitiationElement +{ +public: + enum class Type { + None, + Propose, + Ringing, + Proceed, + Reject, + Retract, + Finish + }; + + QXmppJingleMessageInitiationElement(); + QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppJingleMessageInitiationElement) + + Type type() const; + void setType(Type type); + + QString id() const; + void setId(const QString &id); + + std::optional<QXmppJingleDescription> description() const; + void setDescription(std::optional<QXmppJingleDescription> description); + + std::optional<QXmppJingleReason> reason() const; + void setReason(std::optional<QXmppJingleReason> reason); + + bool containsTieBreak() const; + void setContainsTieBreak(bool containsTieBreak); + + QString migratedTo() const; + void setMigratedTo(const QString &migratedTo); + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + + static bool isJingleMessageInitiationElement(const QDomElement &); + static QString jmiElementTypeToString(Type type); + static std::optional<Type> stringToJmiElementType(const QString &typeStr); + +private: + QSharedDataPointer<QXmppJingleMessageInitiationElementPrivate> d; +}; + +Q_DECLARE_METATYPE(QXmppJingleReason::RtpErrorCondition) + +#endif diff --git a/src/base/QXmppJingleIq.h b/src/base/QXmppJingleIq.h index f5da136c..358f7d0b 100644 --- a/src/base/QXmppJingleIq.h +++ b/src/base/QXmppJingleIq.h @@ -1,577 +1,5 @@ // SPDX-FileCopyrightText: 2010 Jeremy Lainé <jeremy.laine@m4x.org> -// SPDX-FileCopyrightText: 2022 Melvin Keskin <melvo@olomono.de> // // SPDX-License-Identifier: LGPL-2.1-or-later -#ifndef QXMPPJINGLEIQ_H -#define QXMPPJINGLEIQ_H - -#include "QXmppIq.h" - -#include <variant> - -#include <QHostAddress> - -class QXmppJingleCandidatePrivate; -class QXmppJingleIqContentPrivate; -class QXmppJingleIqPrivate; -class QXmppJinglePayloadTypePrivate; -class QXmppJingleRtpCryptoElementPrivate; -class QXmppJingleRtpEncryptionPrivate; -class QXmppJingleRtpFeedbackPropertyPrivate; -class QXmppJingleRtpHeaderExtensionPropertyPrivate; -class QXmppSdpParameterPrivate; - -class QXMPP_EXPORT QXmppSdpParameter -{ -public: - QXmppSdpParameter(); - - QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppSdpParameter) - - QString name() const; - void setName(const QString &name); - - QString value() const; - void setValue(const QString &value); - - /// \cond - void parse(const QDomElement &element); - void toXml(QXmlStreamWriter *writer) const; - /// \endcond - - static bool isSdpParameter(const QDomElement &element); - -private: - QSharedDataPointer<QXmppSdpParameterPrivate> d; -}; - -class QXMPP_EXPORT QXmppJingleRtpCryptoElement -{ -public: - QXmppJingleRtpCryptoElement(); - - QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppJingleRtpCryptoElement) - - uint32_t tag() const; - void setTag(uint32_t tag); - - QString cryptoSuite() const; - void setCryptoSuite(const QString &cryptoSuite); - - QString keyParams() const; - void setKeyParams(const QString &keyParams); - - QString sessionParams() const; - void setSessionParams(const QString &sessionParams); - - /// \cond - void parse(const QDomElement &element); - void toXml(QXmlStreamWriter *writer) const; - /// \endcond - - static bool isJingleRtpCryptoElement(const QDomElement &element); - -private: - QSharedDataPointer<QXmppJingleRtpCryptoElementPrivate> d; -}; - -class QXMPP_EXPORT QXmppJingleRtpEncryption -{ -public: - QXmppJingleRtpEncryption(); - - QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppJingleRtpEncryption) - - bool isRequired() const; - void setRequired(bool isRequired); - - QVector<QXmppJingleRtpCryptoElement> cryptoElements() const; - void setCryptoElements(const QVector<QXmppJingleRtpCryptoElement> &cryptoElements); - - /// \cond - void parse(const QDomElement &element); - void toXml(QXmlStreamWriter *writer) const; - /// \endcond - - static bool isJingleRtpEncryption(const QDomElement &element); - -private: - QSharedDataPointer<QXmppJingleRtpEncryptionPrivate> d; -}; - -class QXMPP_EXPORT QXmppJingleRtpFeedbackProperty -{ -public: - QXmppJingleRtpFeedbackProperty(); - - QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppJingleRtpFeedbackProperty) - - QString type() const; - void setType(const QString &type); - - QString subtype() const; - void setSubtype(const QString &subtype); - - QVector<QXmppSdpParameter> parameters() const; - void setParameters(const QVector<QXmppSdpParameter> ¶meters); - - /// \cond - void parse(const QDomElement &element); - void toXml(QXmlStreamWriter *writer) const; - /// \endcond - - static bool isJingleRtpFeedbackProperty(const QDomElement &element); - -private: - QSharedDataPointer<QXmppJingleRtpFeedbackPropertyPrivate> d; -}; - -class QXMPP_EXPORT QXmppJingleRtpFeedbackInterval -{ -public: - QXmppJingleRtpFeedbackInterval(); - - QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppJingleRtpFeedbackInterval) - - uint64_t value() const; - void setValue(uint64_t value); - - /// \cond - void parse(const QDomElement &element); - void toXml(QXmlStreamWriter *writer) const; - /// \endcond - - static bool isJingleRtpFeedbackInterval(const QDomElement &element); - -private: - uint64_t m_value; -}; - -class QXMPP_EXPORT QXmppJingleRtpHeaderExtensionProperty -{ -public: - enum Senders { - /// The initiator and the sender are allowed. - Both, - /// Only the initiator is allowed. - Initiator, - /// Only the responder is allowed. - Responder - }; - - QXmppJingleRtpHeaderExtensionProperty(); - - QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppJingleRtpHeaderExtensionProperty) - - uint32_t id() const; - void setId(uint32_t id); - - QString uri() const; - void setUri(const QString &uri); - - Senders senders() const; - void setSenders(Senders senders); - - QVector<QXmppSdpParameter> parameters() const; - void setParameters(const QVector<QXmppSdpParameter> ¶meters); - - /// \cond - void parse(const QDomElement &element); - void toXml(QXmlStreamWriter *writer) const; - /// \endcond - - static bool isJingleRtpHeaderExtensionProperty(const QDomElement &element); - -private: - QSharedDataPointer<QXmppJingleRtpHeaderExtensionPropertyPrivate> d; -}; - -/// -/// \brief The QXmppJinglePayloadType class represents a payload type -/// as specified by \xep{0167}: Jingle RTP Sessions and RFC 5245. -/// -class QXMPP_EXPORT QXmppJinglePayloadType -{ -public: - QXmppJinglePayloadType(); - QXmppJinglePayloadType(const QXmppJinglePayloadType &other); - ~QXmppJinglePayloadType(); - - unsigned char channels() const; - void setChannels(unsigned char channels); - - unsigned int clockrate() const; - void setClockrate(unsigned int clockrate); - - unsigned char id() const; - void setId(unsigned char id); - - unsigned int maxptime() const; - void setMaxptime(unsigned int maxptime); - - QString name() const; - void setName(const QString &name); - - QMap<QString, QString> parameters() const; - void setParameters(const QMap<QString, QString> ¶meters); - - unsigned int ptime() const; - void setPtime(unsigned int ptime); - - QVector<QXmppJingleRtpFeedbackProperty> rtpFeedbackProperties() const; - void setRtpFeedbackProperties(const QVector<QXmppJingleRtpFeedbackProperty> &rtpFeedbackProperties); - - QVector<QXmppJingleRtpFeedbackInterval> rtpFeedbackIntervals() const; - void setRtpFeedbackIntervals(const QVector<QXmppJingleRtpFeedbackInterval> &rtpFeedbackIntervals); - - /// \cond - void parse(const QDomElement &element); - void toXml(QXmlStreamWriter *writer) const; - /// \endcond - - QXmppJinglePayloadType &operator=(const QXmppJinglePayloadType &other); - bool operator==(const QXmppJinglePayloadType &other) const; - -private: - QSharedDataPointer<QXmppJinglePayloadTypePrivate> d; -}; - -/// -/// \brief The QXmppJingleCandidate class represents a transport candidate -/// as specified by \xep{0176}: Jingle ICE-UDP Transport Method. -/// -class QXMPP_EXPORT QXmppJingleCandidate -{ -public: - /// This enum is used to describe a candidate's type. - enum Type { - HostType, ///< Host candidate, a local address/port. - PeerReflexiveType, ///< Peer-reflexive candidate, - ///< the address/port as seen from the peer. - ServerReflexiveType, ///< Server-reflexive candidate, - ///< the address/port as seen by the STUN server - RelayedType ///< Relayed candidate, a candidate from - ///< a TURN relay. - }; - - QXmppJingleCandidate(); - QXmppJingleCandidate(const QXmppJingleCandidate &other); - QXmppJingleCandidate(QXmppJingleCandidate &&); - ~QXmppJingleCandidate(); - - QXmppJingleCandidate &operator=(const QXmppJingleCandidate &other); - QXmppJingleCandidate &operator=(QXmppJingleCandidate &&); - - int component() const; - void setComponent(int component); - - QString foundation() const; - void setFoundation(const QString &foundation); - - int generation() const; - void setGeneration(int generation); - - QHostAddress host() const; - void setHost(const QHostAddress &host); - - QString id() const; - void setId(const QString &id); - - int network() const; - void setNetwork(int network); - - quint16 port() const; - void setPort(quint16 port); - - int priority() const; - void setPriority(int priority); - - QString protocol() const; - void setProtocol(const QString &protocol); - - QXmppJingleCandidate::Type type() const; - void setType(QXmppJingleCandidate::Type); - - bool isNull() const; - - /// \cond - void parse(const QDomElement &element); - void toXml(QXmlStreamWriter *writer) const; - - static QXmppJingleCandidate::Type typeFromString(const QString &typeStr, bool *ok = nullptr); - static QString typeToString(QXmppJingleCandidate::Type type); - /// \endcond - -private: - QSharedDataPointer<QXmppJingleCandidatePrivate> d; -}; - -/// -/// \brief The QXmppJingleIq class represents an IQ used for initiating media -/// sessions as specified by \xep{0166}: Jingle. -/// -/// \ingroup Stanzas -/// -class QXMPP_EXPORT QXmppJingleIq : public QXmppIq -{ -public: - /// This enum is used to describe a Jingle action. - enum Action { - ContentAccept, - ContentAdd, - ContentModify, - ContentReject, - ContentRemove, - DescriptionInfo, - SecurityInfo, - SessionAccept, - SessionInfo, - SessionInitiate, - SessionTerminate, - TransportAccept, - TransportInfo, - TransportReject, - TransportReplace - }; - - enum Creator { - /// The initiator generated the content type. - Initiator, - /// The responder generated the content type. - Responder - }; - - struct RtpSessionStateActive - { - }; - - struct RtpSessionStateHold - { - }; - - struct RtpSessionStateUnhold - { - }; - - struct RtpSessionStateMuting - { - /// True when temporarily not sending media to the other party but continuing to accept - /// media from it, false for ending mute state - bool isMute = true; - /// Creator of the corresponding session - Creator creator; - /// Session to be muted (e.g., only audio or video) - QString name; - }; - - struct RtpSessionStateRinging - { - }; - - using RtpSessionState = std::variant<RtpSessionStateActive, RtpSessionStateHold, RtpSessionStateUnhold, RtpSessionStateMuting, RtpSessionStateRinging>; - - /// \internal - /// - /// The QXmppJingleIq::Content class represents the "content" element of a - /// QXmppJingleIq. - /// - class QXMPP_EXPORT Content - { - public: - Content(); - Content(const QXmppJingleIq::Content &other); - Content(QXmppJingleIq::Content &&); - ~Content(); - - Content &operator=(const Content &other); - Content &operator=(Content &&); - - QString creator() const; - void setCreator(const QString &creator); - - QString name() const; - void setName(const QString &name); - - QString senders() const; - void setSenders(const QString &senders); - - // XEP-0167: Jingle RTP Sessions - QString descriptionMedia() const; - void setDescriptionMedia(const QString &media); - - quint32 descriptionSsrc() const; - void setDescriptionSsrc(quint32 ssrc); - - bool isRtpMultiplexingSupported() const; - void setRtpMultiplexingSupported(bool isRtpMultiplexingSupported); - - std::optional<QXmppJingleRtpEncryption> rtpEncryption() const; - void setRtpEncryption(const std::optional<QXmppJingleRtpEncryption> &rtpEncryption); - - void addPayloadType(const QXmppJinglePayloadType &payload); - QList<QXmppJinglePayloadType> payloadTypes() const; - void setPayloadTypes(const QList<QXmppJinglePayloadType> &payloadTypes); - - void addTransportCandidate(const QXmppJingleCandidate &candidate); - QList<QXmppJingleCandidate> transportCandidates() const; - void setTransportCandidates(const QList<QXmppJingleCandidate> &candidates); - - QString transportUser() const; - void setTransportUser(const QString &user); - - QString transportPassword() const; - void setTransportPassword(const QString &password); - - QVector<QXmppJingleRtpFeedbackProperty> rtpFeedbackProperties() const; - void setRtpFeedbackProperties(const QVector<QXmppJingleRtpFeedbackProperty> &rtpFeedbackProperties); - - QVector<QXmppJingleRtpFeedbackInterval> rtpFeedbackIntervals() const; - void setRtpFeedbackIntervals(const QVector<QXmppJingleRtpFeedbackInterval> &rtpFeedbackIntervals); - - QVector<QXmppJingleRtpHeaderExtensionProperty> rtpHeaderExtensionProperties() const; - void setRtpHeaderExtensionProperties(const QVector<QXmppJingleRtpHeaderExtensionProperty> &rtpHeaderExtensionProperties); - - bool isRtpHeaderExtensionMixingAllowed() const; - void setRtpHeaderExtensionMixingAllowed(bool isRtpHeaderExtensionMixingAllowed); - - // XEP-0320: Use of DTLS-SRTP in Jingle Sessions - QByteArray transportFingerprint() const; - void setTransportFingerprint(const QByteArray &fingerprint); - - QString transportFingerprintHash() const; - void setTransportFingerprintHash(const QString &hash); - - QString transportFingerprintSetup() const; - void setTransportFingerprintSetup(const QString &setup); - - /// \cond - void parse(const QDomElement &element); - void toXml(QXmlStreamWriter *writer) const; - - bool parseSdp(const QString &sdp); - QString toSdp() const; - /// \endcond - - private: - QSharedDataPointer<QXmppJingleIqContentPrivate> d; - }; - - /// \internal - /// - /// The QXmppJingleIq::Reason class represents the "reason" element of a - /// QXmppJingleIq. - /// - class QXMPP_EXPORT Reason - { - public: - /// This enum is used to describe a reason's type. - enum Type { - None, - AlternativeSession, - Busy, - Cancel, - ConnectivityError, - Decline, - Expired, - FailedApplication, - FailedTransport, - GeneralError, - Gone, - IncompatibleParameters, - MediaError, - SecurityError, - Success, - Timeout, - UnsupportedApplications, - UnsupportedTransports - }; - - enum RtpErrorCondition { - /// There is no error condition. - NoErrorCondition, - /// The encryption offer is rejected. - InvalidCrypto, - /// Encryption is required but not offered. - CryptoRequired - }; - - Reason(); - - QString text() const; - void setText(const QString &text); - - Type type() const; - void setType(Type type); - - RtpErrorCondition rtpErrorCondition() const; - void setRtpErrorCondition(RtpErrorCondition rtpErrorCondition); - - /// \cond - void parse(const QDomElement &element); - void toXml(QXmlStreamWriter *writer) const; - /// \endcond - - private: - QString m_text; - Type m_type; - RtpErrorCondition m_rtpErrorCondition = NoErrorCondition; - }; - - QXmppJingleIq(); - QXmppJingleIq(const QXmppJingleIq &other); - QXmppJingleIq(QXmppJingleIq &&); - ~QXmppJingleIq() override; - - QXmppJingleIq &operator=(const QXmppJingleIq &other); - QXmppJingleIq &operator=(QXmppJingleIq &&); - - Action action() const; - void setAction(Action action); - - void addContent(const Content &content); - QList<Content> contents() const; - void setContents(const QList<Content> &contents); - - QString initiator() const; - void setInitiator(const QString &initiator); - - Reason &reason(); - const Reason &reason() const; - - QString responder() const; - void setResponder(const QString &responder); - -#if QXMPP_DEPRECATED_SINCE(1, 5) - QT_DEPRECATED_X("Use QXmpp::rtpSessionState() instead") - bool ringing() const; - QT_DEPRECATED_X("Use QXmpp::setRtpSessionState() instead") - void setRinging(bool ringing); -#endif - - QString sid() const; - void setSid(const QString &sid); - - QString mujiGroupChatJid() const; - void setMujiGroupChatJid(const QString &mujiGroupChatJid); - - std::optional<RtpSessionState> rtpSessionState() const; - void setRtpSessionState(const std::optional<RtpSessionState> &rtpSessionState); - - /// \cond - static bool isJingleIq(const QDomElement &element); - /// \endcond - -protected: - /// \cond - void parseElementFromChild(const QDomElement &element) override; - void toXmlElementFromChild(QXmlStreamWriter *writer) const override; - /// \endcond - -private: - QSharedDataPointer<QXmppJingleIqPrivate> d; -}; - -Q_DECLARE_METATYPE(QXmppJingleIq::Reason::RtpErrorCondition) - -#endif +#include "QXmppJingleData.h" diff --git a/src/base/QXmppMessage.cpp b/src/base/QXmppMessage.cpp index 9527f5ed..be6cd83e 100644 --- a/src/base/QXmppMessage.cpp +++ b/src/base/QXmppMessage.cpp @@ -2,6 +2,7 @@ // SPDX-FileCopyrightText: 2010 Jeremy Lainé <jeremy.laine@m4x.org> // SPDX-FileCopyrightText: 2018 Linus Jahn <lnj@kaidan.im> // SPDX-FileCopyrightText: 2021 Melvin Keskin <melvo@olomono.de> +// SPDX-FileCopyrightText: 2023 Tibor Csötönyi <work@taibsu.de> // // SPDX-License-Identifier: LGPL-2.1-or-later @@ -11,6 +12,7 @@ #include "QXmppConstants_p.h" #include "QXmppFileShare.h" #include "QXmppGlobal_p.h" +#include "QXmppJingleData.h" #include "QXmppMessageReaction.h" #include "QXmppMixInvitation.h" #ifdef BUILD_OMEMO @@ -123,6 +125,9 @@ public: // XEP-0334: Message Processing Hints quint8 hints; + // XEP-0353: Jingle Message Initiation + std::optional<QXmppJingleMessageInitiationElement> jingleMessageInitiationElement; + // XEP-0359: Unique and Stable Stanza IDs QString stanzaId; QString stanzaIdBy; @@ -872,6 +877,24 @@ void QXmppMessage::removeAllHints() } /// +/// Returns a Jingle Message Initiation element as defined in \xep{0353}: Jingle Message +/// Initiation. +/// +std::optional<QXmppJingleMessageInitiationElement> QXmppMessage::jingleMessageInitiationElement() const +{ + return d->jingleMessageInitiationElement; +} + +/// +/// Sets a Jingle Message Initiation element as defined in \xep{0353}: Jingle Message +/// Initiation. +/// +void QXmppMessage::setJingleMessageInitiationElement(const std::optional<QXmppJingleMessageInitiationElement> &jingleMessageInitiationElement) +{ + d->jingleMessageInitiationElement = jingleMessageInitiationElement; +} + +/// /// Returns the stanza ID of the message according to \xep{0359}: Unique and /// Stable Stanza IDs. /// @@ -1393,6 +1416,13 @@ bool QXmppMessage::parseExtension(const QDomElement &element, QXmpp::SceMode sce } return true; } + // XEP-0353: Jingle Message Initiation + if (QXmppJingleMessageInitiationElement::isJingleMessageInitiationElement(element)) { + QXmppJingleMessageInitiationElement jingleMessageInitiationElement; + jingleMessageInitiationElement.parse(element); + d->jingleMessageInitiationElement = jingleMessageInitiationElement; + return true; + } // XEP-0359: Unique and Stable Stanza IDs if (checkElement(element, QStringLiteral("stanza-id"), ns_sid)) { d->stanzaId = element.attribute(QStringLiteral("id")); @@ -1811,6 +1841,11 @@ void QXmppMessage::serializeExtensions(QXmlStreamWriter *writer, QXmpp::SceMode writer->writeEndElement(); } + // XEP-0353: Jingle Message Initiation + if (d->jingleMessageInitiationElement) { + d->jingleMessageInitiationElement->toXml(writer); + } + // XEP-0367: Message Attaching if (!d->attachId.isEmpty()) { writer->writeStartElement(QStringLiteral("attach-to")); diff --git a/src/base/QXmppMessage.h b/src/base/QXmppMessage.h index be9e9b96..cf6df18d 100644 --- a/src/base/QXmppMessage.h +++ b/src/base/QXmppMessage.h @@ -2,6 +2,7 @@ // SPDX-FileCopyrightText: 2010 Jeremy Lainé <jeremy.laine@m4x.org> // SPDX-FileCopyrightText: 2018 Linus Jahn <lnj@kaidan.im> // SPDX-FileCopyrightText: 2020 Melvin Keskin <melvo@olomono.de> +// SPDX-FileCopyrightText: 2023 Tibor Csötönyi <work@taibsu.de> // // SPDX-License-Identifier: LGPL-2.1-or-later @@ -18,6 +19,7 @@ class QXmppMessagePrivate; class QXmppBitsOfBinaryDataList; +class QXmppJingleMessageInitiationElement; class QXmppMessageReaction; class QXmppMixInvitation; #ifdef BUILD_OMEMO @@ -205,6 +207,10 @@ public: void removeHint(const Hint hint); void removeAllHints(); + // XEP-0353: Jingle Message Initiation + std::optional<QXmppJingleMessageInitiationElement> jingleMessageInitiationElement() const; + void setJingleMessageInitiationElement(const std::optional<QXmppJingleMessageInitiationElement> &jingleMessageInitiationElement); + // XEP-0359: Unique and Stable Stanza IDs QString stanzaId() const; void setStanzaId(const QString &id); diff --git a/src/client/QXmppJingleMessageInitiationManager.cpp b/src/client/QXmppJingleMessageInitiationManager.cpp new file mode 100644 index 00000000..d7a4cea1 --- /dev/null +++ b/src/client/QXmppJingleMessageInitiationManager.cpp @@ -0,0 +1,584 @@ +// SPDX-FileCopyrightText: 2023 Tibor Csötönyi <work@taibsu.de> +// SPDX-FileCopyrightText: 2023 Melvin Keskin <melvo@olomono.de> +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#include "QXmppJingleMessageInitiationManager.h" + +#include "QXmppClient.h" +#include "QXmppConstants_p.h" +#include "QXmppMessage.h" +#include "QXmppPromise.h" +#include "QXmppUtils.h" + +#include <QStringBuilder> +#include <QUuid> + +using namespace QXmpp; +using Jmi = QXmppJingleMessageInitiation; +using JmiManager = QXmppJingleMessageInitiationManager; +using JmiElement = QXmppJingleMessageInitiationElement; +using JmiType = JmiElement::Type; + +class QXmppJingleMessageInitiationPrivate +{ +public: + QXmppJingleMessageInitiationPrivate(JmiManager *manager) + : manager(manager) + { + } + + QXmppTask<SendResult> request(JmiElement &&jmiElement); + + QXmppJingleMessageInitiationManager *manager; + QString id; + QString callPartnerJid; + bool isProceeded { false }; +}; + +/// +/// \brief Creates a Jingle Message Initiation request based on given type. +/// \param type The request type (proceed, accept, reject, retract, finish). +/// +QXmppTask<SendResult> QXmppJingleMessageInitiationPrivate::request(JmiElement &&jmiElement) +{ + jmiElement.setId(id); + return manager->sendMessage(jmiElement, callPartnerJid); +} + +/// +/// \class QXmppJingleMessageInitiation +/// +/// \brief The QXmppJingleMessageInitiation class holds information about the JMI element in the +/// current context. +/// +/// \since QXmpp 1.6 +/// + +/// +/// \brief Constructs a Jingle Message Initiation object. +/// +QXmppJingleMessageInitiation::QXmppJingleMessageInitiation(QXmppJingleMessageInitiationManager *manager) + : d(new QXmppJingleMessageInitiationPrivate(manager)) +{ +} + +QXmppJingleMessageInitiation::~QXmppJingleMessageInitiation() = default; + +/// +/// Creates a JMI element of type "ringing" and sends a request containing the element. +/// +QXmppTask<SendResult> QXmppJingleMessageInitiation::ring() +{ + QXmppJingleMessageInitiationElement jmiElement; + jmiElement.setType(JmiType::Ringing); + + return d->request(std::move(jmiElement)); +} + +/// +/// Creates a JMI element of type "proceed" and sends a request containing the element. +/// +QXmppTask<SendResult> QXmppJingleMessageInitiation::proceed() +{ + QXmppJingleMessageInitiationElement jmiElement; + jmiElement.setType(JmiType::Proceed); + + return d->request(std::move(jmiElement)); +} + +/// +/// Creates a JMI element of type "reject" and sends a request containing the element. +/// The default reason tag/type will be "busy" with text "Busy". +/// +/// \param reason Reason object for reject element +/// \param containsTieBreak Whether the reject element contains a tie-break tag or not +/// +QXmppTask<SendResult> QXmppJingleMessageInitiation::reject(std::optional<QXmppJingleReason> reason, bool containsTieBreak) +{ + JmiElement jmiElement; + jmiElement.setType(JmiType::Reject); + + if (!reason) { + reason = QXmppJingleReason(); + reason->setType(QXmppJingleReason::Busy); + reason->setText(QStringLiteral("Busy")); + } + + jmiElement.setReason(reason); + jmiElement.setContainsTieBreak(containsTieBreak); + + return d->request(std::move(jmiElement)); +} + +/// +/// Creates a JMI element of type "retract" and sends a request containing the element. +/// The default reason tag/type will be "cancel" with text "Retracted". +/// +/// \param reason Reason object for retract element +/// \param containsTieBreak Whether the retract element contains a tie-break tag or not +/// +QXmppTask<SendResult> QXmppJingleMessageInitiation::retract(std::optional<QXmppJingleReason> reason, bool containsTieBreak) +{ + JmiElement jmiElement; + jmiElement.setType(JmiType::Retract); + + if (!reason) { + reason = QXmppJingleReason(); + reason->setType(QXmppJingleReason::Cancel); + reason->setText(QStringLiteral("Retracted")); + } + + jmiElement.setReason(reason); + jmiElement.setContainsTieBreak(containsTieBreak); + + return d->request(std::move(jmiElement)); +} + +/// +/// Creates a JMI element of type "finish" and sends a request containing the element. +/// The default reason type/tag will be "success" with text "Success". +/// +/// \param reason Reason object for finish element +/// \param migratedTo JMI id the session has been migrated to +/// +QXmppTask<SendResult> QXmppJingleMessageInitiation::finish(std::optional<QXmppJingleReason> reason, const QString &migratedTo) +{ + JmiElement jmiElement; + jmiElement.setType(JmiType::Finish); + + if (!reason) { + reason = QXmppJingleReason(); + reason->setType(QXmppJingleReason::Success); + reason->setText(QStringLiteral("Success")); + } + + jmiElement.setReason(reason); + jmiElement.setMigratedTo(migratedTo); + + return d->request(std::move(jmiElement)); +} + +/// +/// Returns the JMI ID. +/// +QString QXmppJingleMessageInitiation::id() const +{ + return d->id; +} + +/// +/// Sets the JMI ID. +/// +void QXmppJingleMessageInitiation::setId(const QString &id) +{ + d->id = id; +} + +/// +/// Sets the call partner's bare JID. +/// +/// Normally, the JMI ID would be sufficient in order to differentiate the JMIs. +/// However, attackers pretending to be the call partner can be mitigated by caching the call +/// partner's JID. +/// +/// \param callPartnerJid bare JID of the call partner +/// +void QXmppJingleMessageInitiation::setCallPartnerJid(const QString &callPartnerJid) +{ + d->callPartnerJid = callPartnerJid; +} + +/// +/// Returns the call partner's bare JID. +/// +/// \return the call partner's bare JID. +/// +QString QXmppJingleMessageInitiation::callPartnerJid() const +{ + return d->callPartnerJid; +} + +/// +/// Returns the "isProceeded" flag, e.g., if the Jingle Message Initiation has already been +/// proceeded. +/// +bool QXmppJingleMessageInitiation::isProceeded() const +{ + return d->isProceeded; +} + +/// +/// Sets the "isProceeded" flag, e.g., if the Jingle Message Initiation has already been +/// proceeded. +/// +void QXmppJingleMessageInitiation::setIsProceeded(bool isProceeded) +{ + d->isProceeded = isProceeded; +} + +/// +/// \fn QXmppJingleMessageInitiation::ringing() +/// +/// Emitted when a propose request was accepted and the device starts ringing. +/// + +/// +/// \fn QXmppJingleMessageInitiation::proceeded(const QString &, const QString &) +/// +/// Emitted when a propose request was successfully processed and accepted. +/// +/// \param id belonging JMI id +/// \param callPartnerResource resource of the call partner about to be called +/// + +/// +/// \fn QXmppJingleMessageInitiation::closed(const Result &) +/// +/// Emitted when a call was ended either through rejection, retraction, finish or an error. +/// +/// \param result close reason +/// + +class QXmppJingleMessageInitiationManagerPrivate +{ +public: + QVector<std::shared_ptr<Jmi>> jmis; +}; + +/// +/// \typedef QXmppJingleMessageInitiationManager::ProposeResult +/// +/// Contains JMI object or an error if sending the propose message failed. +/// + +/// +/// \class QXmppJingleMessageInitiationManager +/// +/// \brief The QXmppJingleMessageInitiationManager class makes it possible to retrieve +/// Jingle Message Initiation elements as defined by \xep{0353, Jingle Message Initiation}. +/// +/// \since QXmpp 1.6 +/// +QXmppJingleMessageInitiationManager::QXmppJingleMessageInitiationManager() + : d(std::make_unique<QXmppJingleMessageInitiationManagerPrivate>()) +{ +} + +QXmppJingleMessageInitiationManager::~QXmppJingleMessageInitiationManager() = default; + +/// \cond +QStringList QXmppJingleMessageInitiationManager::discoveryFeatures() const +{ + return { ns_jingle_message_initiation }; +} +/// \endcond + +/// +/// Creates a proposal JMI element and passes it as a message. +/// +QXmppTask<QXmppJingleMessageInitiationManager::ProposeResult> QXmppJingleMessageInitiationManager::propose(const QString &callPartnerJid, const QXmppJingleDescription &description) +{ + QXmppPromise<ProposeResult> promise; + + QXmppJingleMessageInitiationElement jmiElement; + jmiElement.setType(JmiType::Propose); + jmiElement.setId(QXmppUtils::generateStanzaUuid()); + jmiElement.setDescription(description); + + sendMessage(jmiElement, callPartnerJid).then(this, [this, promise, callPartnerJid](SendResult result) mutable { + if (auto error = std::get_if<QXmppError>(&result)) { + warning(u"Error sending Jingle Message Initiation proposal: " % error->description); + promise.finish(*error); + } else { + promise.finish(addJmi(callPartnerJid)); + } + }); + + return promise.task(); +} + +/// \cond +bool QXmppJingleMessageInitiationManager::handleMessage(const QXmppMessage &message) +{ + // JMI messages must be of type "chat" and contain a <store/> hint. + if (message.type() != QXmppMessage::Chat || !message.hasHint(QXmppMessage::Store)) { + return false; + } + + // Only continue if the message contains a JMI element. + if (auto jmiElement = message.jingleMessageInitiationElement()) { + return handleJmiElement(std::move(*jmiElement), message.from()); + } + + return false; +} + +void QXmppJingleMessageInitiationManager::setClient(QXmppClient *client) +{ + QXmppClientExtension::setClient(client); +} +/// \endcond + +/// +/// Lets the client send a message to user with given callPartnerJid containing the JMI element. +/// +/// \param jmiElement the JMI element to be passed +/// \param callPartnerJid bare JID of the call partner +/// +QXmppTask<SendResult> QXmppJingleMessageInitiationManager::sendMessage(const QXmppJingleMessageInitiationElement &jmiElement, const QString &callPartnerJid) +{ + QXmppMessage message; + message.setTo(callPartnerJid); + message.addHint(QXmppMessage::Store); + message.setJingleMessageInitiationElement(jmiElement); + + return client()->send(std::move(message)); +} + +/// +/// Removes a JMI object from the JMIs vector. +/// +/// \param jmi object to be removed +/// +void QXmppJingleMessageInitiationManager::clear(const std::shared_ptr<QXmppJingleMessageInitiation> &jmi) +{ + d->jmis.erase( + std::remove_if( + d->jmis.begin(), + d->jmis.end(), + [&jmi](const auto &storedJmi) { + return jmi->id() == storedJmi->id() && jmi->callPartnerJid() == storedJmi->callPartnerJid(); + }), + d->jmis.end()); +} + +/// +/// Removes all JMI objects from the JMI vector. +/// +void QXmppJingleMessageInitiationManager::clearAll() +{ + d->jmis.clear(); +} + +bool QXmppJingleMessageInitiationManager::handleJmiElement(QXmppJingleMessageInitiationElement &&jmiElement, const QString &senderJid) +{ + auto jmiElementId = jmiElement.id(); + auto callPartnerJid = QXmppUtils::jidToBareJid(senderJid); + + // Check if there's already a JMI object with jmiElementId and callPartnerJid in JMIs vector. + // That means that a JMI has already been created with given (J)IDs. + auto itr = std::find_if(d->jmis.begin(), d->jmis.end(), [&jmiElementId, &callPartnerJid](const auto &jmi) { + return jmi->id() == jmiElementId && jmi->callPartnerJid() == callPartnerJid; + }); + + if (itr != d->jmis.end()) { + return handleExistingJmi(*itr, jmiElement, QXmppUtils::jidToResource(senderJid)); + } + + if (jmiElement.type() == JmiType::Propose) { + return handleProposeJmiElement(jmiElement, callPartnerJid); + } + + return false; +} + +/// +/// Handles a JMI object which already exists in the JMIs vector. +/// +/// \param existingJmi JMI object to be handled +/// \param jmiElement JMI element to be processed with the JMI object +/// \param callPartnerResource resource of the call partner (i.e., phone, tablet etc.) +/// \return success (true) or failure +/// +bool QXmppJingleMessageInitiationManager::handleExistingJmi(const std::shared_ptr<Jmi> &existingJmi, const QXmppJingleMessageInitiationElement &jmiElement, const QString &callPartnerResource) +{ + switch (jmiElement.type()) { + case JmiType::Ringing: + Q_EMIT existingJmi->ringing(); + return true; + case JmiType::Proceed: + Q_EMIT existingJmi->proceeded(jmiElement.id(), callPartnerResource); + existingJmi->setIsProceeded(true); + return true; + case JmiType::Reject: + Q_EMIT existingJmi->closed( + Jmi::Rejected { jmiElement.reason(), jmiElement.containsTieBreak() }); + return true; + case JmiType::Retract: + Q_EMIT existingJmi->closed( + Jmi::Retracted { jmiElement.reason(), jmiElement.containsTieBreak() }); + return true; + case JmiType::Finish: + existingJmi->finish(jmiElement.reason(), jmiElement.migratedTo()); + Q_EMIT existingJmi->closed( + Jmi::Finished { jmiElement.reason(), jmiElement.migratedTo() }); + return true; + default: + return false; + } +} + +/// +/// Handles a propose JMI element. +/// +/// \param jmiElement to be handled +/// \param callPartnerJid bare JID of the call partner +/// \return success (true) or failure +/// +bool QXmppJingleMessageInitiationManager::handleProposeJmiElement(const QXmppJingleMessageInitiationElement &jmiElement, const QString &callPartnerJid) +{ + // Check if there's already a JMI object with provided callPartnerJid in JMIs vector. + // That means that a propose has already been sent. + auto itr = std::find_if( + d->jmis.cbegin(), + d->jmis.cend(), + [&callPartnerJid](const auto &jmi) { + return jmi->callPartnerJid() == callPartnerJid; + }); + + // Tie break case or usual JMI proposal? + if (itr != d->jmis.end()) { + return handleTieBreak(*itr, jmiElement, callPartnerJid); + } + + Q_EMIT proposed(addJmi(callPartnerJid), jmiElement.id(), jmiElement.description()); + return true; +} + +/// +/// Handles a tie break case as defined in https://xmpp.org/extensions/xep-0353.html#tie-breaking. +/// \param existingJmi existing JMI object to be handled +/// \param jmiElement JMI element to be processed with existing JMI object +/// \param callPartnerResource resource of the call partner (i.e., phone, tablet etc.) +/// \return success (true) or failure +/// +bool QXmppJingleMessageInitiationManager::handleTieBreak(const std::shared_ptr<Jmi> &existingJmi, const QXmppJingleMessageInitiationElement &jmiElement, const QString &callPartnerResource) +{ + // Tie break -> session is set to be expired + QXmppJingleReason reason; + reason.setType(QXmppJingleReason::Expired); + + // Existing (proceeded) or non-existing session? + if (existingJmi->isProceeded()) { + return handleExistingSession(existingJmi, jmiElement.id()); + } + + // Tie break in propose state (no existing session) - two parties try calling each other + // at the same time, the proposal with the lower ID overrules the other one. + return handleNonExistingSession(existingJmi, jmiElement.id(), callPartnerResource); +} + +/// +/// Device switch: session already exists and will be migrated to new device with id jmiElementId. +/// +/// \param existingJmi Current JMI object +/// \param jmiElementId New (counterpart's) session JMI element ID +/// +bool QXmppJingleMessageInitiationManager::handleExistingSession(const std::shared_ptr<Jmi> &existingJmi, const QString &jmiElementId) +{ + // Old session will be finished with reason "Expired". + QXmppJingleReason reason; + reason.setType(QXmppJingleReason::Expired); + reason.setText(QStringLiteral("Session migrated")); + + // Tell the old session to be finished. + Q_EMIT existingJmi->closed(Jmi::Finished { reason, jmiElementId }); + + existingJmi->finish(reason, jmiElementId).then(this, [this, existingJmi, jmiElementId](SendResult result) { + if (auto *error = std::get_if<QXmppError>(&result)) { + Q_EMIT existingJmi->closed(*error); + } else { + // Then, proceed (accept) the new proposal and set the JMI ID + // to the ID of the received JMI element. + existingJmi->setId(jmiElementId); + existingJmi->proceed().then(this, [existingJmi](SendResult result) { + if (auto *error = std::get_if<QXmppError>(&result)) { + Q_EMIT existingJmi->closed(*error); + } else { + // The session is now closed as it is finished. + existingJmi->setIsProceeded(true); + } + }); + } + }); + + return true; +} + +/// +/// \brief Tie break in propose state (no existing session) - two parties try calling each other +/// at the same time, the proposal with the lower ID overrules the other one. +/// +/// \param existingJmi Current JMI object +/// \param jmiElementId Counterpart's JMI element ID +/// +bool QXmppJingleMessageInitiationManager::handleNonExistingSession(const std::shared_ptr<Jmi> &existingJmi, const QString &jmiElementId, const QString &callPartnerResource) +{ + QXmppJingleReason reason; + reason.setType(QXmppJingleReason::Expired); + reason.setText(QStringLiteral("Tie-Break")); + + if (QUuid::fromString(existingJmi->id()) < QUuid::fromString(jmiElementId)) { + // Jingle message initiator with lower ID rejects the other proposal. + existingJmi->setId(jmiElementId); + existingJmi->reject(std::move(reason), true).then(this, [existingJmi](auto result) { + if (auto *error = std::get_if<QXmppError>(&result)) { + Q_EMIT existingJmi->closed(*error); + } + }); + } else { + // Jingle message initiator with higher ID retracts its proposal. + existingJmi->retract(std::move(reason), true).then(this, [this, existingJmi, jmiElementId, callPartnerResource](auto result) { + if (auto error = std::get_if<QXmppError>(&result)) { + Q_EMIT existingJmi->closed(*error); + } else { + // Afterwards, JMI ID is changed to lower ID. + existingJmi->setId(jmiElementId); + + // Finally, the call is being accepted. + existingJmi->proceed().then(this, [existingJmi, jmiElementId, callPartnerResource](SendResult result) { + if (auto *error = std::get_if<QXmppError>(&result)) { + Q_EMIT existingJmi->closed(*error); + } else { + existingJmi->setIsProceeded(true); + Q_EMIT existingJmi->proceeded(jmiElementId, callPartnerResource); + } + }); + } + }); + } + + return true; +} + +/// +/// Adds a JMI object to the JMIs vector and sets the bare JID of the call partner in the JMI object. +/// \param callPartnerJid bare JID of the call partner +/// \return The newly created JMI +/// +std::shared_ptr<QXmppJingleMessageInitiation> QXmppJingleMessageInitiationManager::addJmi(const QString &callPartnerJid) +{ + auto jmi { std::make_shared<QXmppJingleMessageInitiation>(this) }; + jmi->setCallPartnerJid(callPartnerJid); + d->jmis.append(jmi); + return jmi; +} + +/// +/// Returns the JMIs vector. +/// +const QVector<std::shared_ptr<QXmppJingleMessageInitiation>> &QXmppJingleMessageInitiationManager::jmis() const +{ + return d->jmis; +} + +/// +/// \fn QXmppJingleMessageInitiationManager::proposed(const std::shared_ptr<QXmppJingleMessageInitiation> &, const QString &, const QXmppJingleDescription &) +/// +/// Emitted when a call has been proposed. +/// +/// \param jmi Jingle Message Initiation object of proposed session +/// \param id JMI element id +/// \param description JMI element's description containing media type (i.e., audio, video) +/// diff --git a/src/client/QXmppJingleMessageInitiationManager.h b/src/client/QXmppJingleMessageInitiationManager.h new file mode 100644 index 00000000..cd5bb494 --- /dev/null +++ b/src/client/QXmppJingleMessageInitiationManager.h @@ -0,0 +1,126 @@ +// SPDX-FileCopyrightText: 2023 Tibor Csötönyi <work@taibsu.de> +// SPDX-FileCopyrightText: 2023 Melvin Keskin <melvo@olomono.de> +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#ifndef QXMPPJINGLEMESSAGEINITIATIONMANAGER_H +#define QXMPPJINGLEMESSAGEINITIATIONMANAGER_H + +#include "QXmppClientExtension.h" +#include "QXmppError.h" +#include "QXmppJingleIq.h" +#include "QXmppMessageHandler.h" +#include "QXmppSendResult.h" +#include "QXmppTask.h" + +class QXmppJingleMessageInitiationManager; +class QXmppJingleMessageInitiationPrivate; +class QXmppJingleMessageInitiationManagerPrivate; + +class QXMPP_EXPORT QXmppJingleMessageInitiation : public QObject +{ + Q_OBJECT +public: + struct Rejected + { + std::optional<QXmppJingleReason> reason; + bool containsTieBreak; + }; + + struct Retracted + { + std::optional<QXmppJingleReason> reason; + bool containsTieBreak; + }; + + struct Finished + { + std::optional<QXmppJingleReason> reason; + QString migratedTo; + }; + + /// Variant of Rejected, Retracted, Finished or Error result types + using Result = std::variant<Rejected, Retracted, Finished, QXmppError>; + + QXmppJingleMessageInitiation(QXmppJingleMessageInitiationManager *manager); + ~QXmppJingleMessageInitiation(); + + QXmppTask<QXmpp::SendResult> ring(); + QXmppTask<QXmpp::SendResult> proceed(); + QXmppTask<QXmpp::SendResult> reject(std::optional<QXmppJingleReason> reason, bool containsTieBreak = false); + QXmppTask<QXmpp::SendResult> retract(std::optional<QXmppJingleReason> reason, bool containsTieBreak = false); + QXmppTask<QXmpp::SendResult> finish(std::optional<QXmppJingleReason> reason, const QString &migratedTo = {}); + + Q_SIGNAL void ringing(); + Q_SIGNAL void proceeded(const QString &id, const QString &callPartnerResource); + Q_SIGNAL void closed(const Result &result); + +private: + QString id() const; + void setId(const QString &id); + void setCallPartnerJid(const QString &callPartnerJid); + QString callPartnerJid() const; + bool isProceeded() const; + void setIsProceeded(bool isProceeded); + + std::unique_ptr<QXmppJingleMessageInitiationPrivate> d; + + friend class QXmppJingleMessageInitiationManager; + friend class tst_QXmppJingleMessageInitiationManager; +}; + +class QXMPP_EXPORT QXmppJingleMessageInitiationManager : public QXmppClientExtension, public QXmppMessageHandler +{ + Q_OBJECT +public: + using ProposeResult = std::variant<std::shared_ptr<QXmppJingleMessageInitiation>, QXmppError>; + + QXmppJingleMessageInitiationManager(); + ~QXmppJingleMessageInitiationManager(); + + /// \cond + QStringList discoveryFeatures() const override; + /// \endcond + + QXmppTask<ProposeResult> propose( + const QString &callPartnerJid, + const QXmppJingleDescription &description); + + Q_SIGNAL void proposed( + const std::shared_ptr<QXmppJingleMessageInitiation> &jmi, + const QString &id, + const std::optional<QXmppJingleDescription> &description); + +protected: + /// \cond + bool handleMessage(const QXmppMessage &) override; + void setClient(QXmppClient *client) override; + /// \endcond + +private: + QXmppTask<QXmpp::SendResult> sendMessage( + const QXmppJingleMessageInitiationElement &jmiElement, + const QString &callPartnerJid); + + void clear(const std::shared_ptr<QXmppJingleMessageInitiation> &jmi); + void clearAll(); + + bool handleJmiElement(QXmppJingleMessageInitiationElement &&jmiElement, const QString &senderJid); + bool handleExistingJmi(const std::shared_ptr<QXmppJingleMessageInitiation> &existingJmi, const QXmppJingleMessageInitiationElement &jmiElement, const QString &callPartnerResource); + bool handleProposeJmiElement(const QXmppJingleMessageInitiationElement &jmiElement, const QString &callPartnerJid); + bool handleTieBreak(const std::shared_ptr<QXmppJingleMessageInitiation> &existingJmi, const QXmppJingleMessageInitiationElement &jmiElement, const QString &callPartnerResource); + bool handleExistingSession(const std::shared_ptr<QXmppJingleMessageInitiation> &existingJmi, const QString &jmiElementId); + bool handleNonExistingSession(const std::shared_ptr<QXmppJingleMessageInitiation> &existingJmi, const QString &jmiElementId, const QString &callPartnerResource); + std::shared_ptr<QXmppJingleMessageInitiation> addJmi(const QString &callPartnerJid); + const QVector<std::shared_ptr<QXmppJingleMessageInitiation>> &jmis() const; + +private: + std::unique_ptr<QXmppJingleMessageInitiationManagerPrivate> d; + + friend class QXmppJingleMessageInitiationPrivate; + friend class tst_QXmppJingleMessageInitiationManager; +}; + +Q_DECLARE_METATYPE(QXmppJingleMessageInitiation::Result) + +#endif // QXMPPJINGLEMESSAGEINITIATIONMANAGER_H |
