From aaa64971fcf3d72d8b66ebf37b8c1005017a5ea4 Mon Sep 17 00:00:00 2001 From: Linus Jahn Date: Fri, 2 Oct 2020 22:56:58 +0200 Subject: Port remaining Qt-6-removed APIs --- src/client/QXmppOutgoingClient.cpp | 62 +++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 24 deletions(-) (limited to 'src/client/QXmppOutgoingClient.cpp') diff --git a/src/client/QXmppOutgoingClient.cpp b/src/client/QXmppOutgoingClient.cpp index ed3cafb8..9f3e5e58 100644 --- a/src/client/QXmppOutgoingClient.cpp +++ b/src/client/QXmppOutgoingClient.cpp @@ -52,7 +52,7 @@ #include #include #include -#include +#include #include #include #include @@ -492,13 +492,8 @@ void QXmppOutgoingClient::handleStanza(const QDomElement &nodeRecv) emit connected(); } else if (ns == ns_stream && nodeRecv.tagName() == "error") { // handle redirects - QRegExp redirectRegex("([^:]+)(:[0-9]+)?"); - if (redirectRegex.exactMatch(nodeRecv.firstChildElement("see-other-host").text())) { - d->redirectHost = redirectRegex.cap(0); - if (!redirectRegex.cap(2).isEmpty()) - d->redirectPort = redirectRegex.cap(2).mid(1).toUShort(); - else - d->redirectPort = 5222; + const auto otherHost = nodeRecv.firstChildElement("see-other-host"); + if (!otherHost.isNull() && setResumeAddress(otherHost.text())) { QXmppStream::disconnectFromHost(); return; } @@ -573,11 +568,12 @@ void QXmppOutgoingClient::handleStanza(const QDomElement &nodeRecv) // bind result if (bind.type() == QXmppIq::Result) { if (!bind.jid().isEmpty()) { - QRegExp jidRegex("^([^@/]+)@([^@/]+)/(.+)$"); - if (jidRegex.exactMatch(bind.jid())) { - configuration().setUser(jidRegex.cap(1)); - configuration().setDomain(jidRegex.cap(2)); - configuration().setResource(jidRegex.cap(3)); + static const QRegularExpression jidRegex("^([^@/]+)@([^@/]+)/(.+)$"); + + if (const auto match = jidRegex.match(bind.jid()); match.hasMatch()) { + configuration().setUser(match.captured(1)); + configuration().setDomain(match.captured(2)); + configuration().setResource(match.captured(3)); } else { warning("Bind IQ received with invalid JID: " + bind.jid()); } @@ -683,17 +679,7 @@ void QXmppOutgoingClient::handleStanza(const QDomElement &nodeRecv) d->smId = streamManagementEnabled.id(); d->canResume = streamManagementEnabled.resume(); if (streamManagementEnabled.resume() && !streamManagementEnabled.location().isEmpty()) { - QRegExp locationRegex("([^:]+)(:[0-9]+)?"); - if (locationRegex.exactMatch(streamManagementEnabled.location())) { - d->resumeHost = locationRegex.cap(0); - if (!locationRegex.cap(2).isEmpty()) - d->resumePort = locationRegex.cap(2).mid(1).toUShort(); - else - d->resumePort = 5222; - } else { - d->resumeHost = QString(); - d->resumePort = 0; - } + setResumeAddress(streamManagementEnabled.location()); } enableStreamManagement(true); @@ -776,6 +762,34 @@ void QXmppOutgoingClient::pingTimeout() emit error(QXmppClient::KeepAliveError); } +bool QXmppOutgoingClient::setResumeAddress(const QString &address) +{ + if (const auto location = parseHostAddress(address); + !location.first.isEmpty()) { + d->resumeHost = location.first; + + if (location.second > 0) { + d->resumePort = location.second; + } else { + d->resumePort = 5222; + } + return true; + } + + d->resumeHost.clear(); + d->resumePort = 0; + return false; +} + +std::pair QXmppOutgoingClient::parseHostAddress(const QString &address) +{ + QUrl url("//" + address); + if (url.isValid() && !url.host().isEmpty()) { + return { url.host(), url.port() }; + } + return { {}, -1 }; +} + void QXmppOutgoingClientPrivate::sendNonSASLAuth(bool plainText) { QXmppNonSASLAuthIq authQuery; -- cgit v1.2.3