Skip to content

Commit

Permalink
Port remaining Qt-6-removed APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
lnjX committed Oct 3, 2020
1 parent d69efa6 commit 008038c
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 70 deletions.
7 changes: 4 additions & 3 deletions src/base/QXmppSasl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ QMap<QByteArray, QByteArray> QXmppSaslDigestMd5::parseMessage(const QByteArray &
QMap<QByteArray, QByteArray> map;
int startIndex = 0;
int pos = 0;
while ((pos = ba.indexOf(QStringLiteral("="), startIndex)) >= 0) {
while ((pos = ba.indexOf('=', startIndex)) >= 0) {
// key get name and skip equals
const QByteArray key = ba.mid(startIndex, pos - startIndex).trimmed();
pos++;
Expand Down Expand Up @@ -927,9 +927,10 @@ QByteArray QXmppSaslDigestMd5::serializeMessage(const QMap<QByteArray, QByteArra
if (quote) {
value.replace(QByteArrayLiteral("\\"), QByteArrayLiteral("\\\\"));
value.replace(QByteArrayLiteral("\""), QByteArrayLiteral("\\\""));
ba.append(QStringLiteral("\"") + value + QStringLiteral("\""));
} else
ba.append('"' + value + '"');
} else {
ba.append(value);
}
}
return ba;
}
1 change: 1 addition & 0 deletions src/base/QXmppStun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <QNetworkInterface>
#include <QTimer>
#include <QUdpSocket>
#include <QVariant>

#define STUN_ID_SIZE 12
#define STUN_RTO_INTERVAL 500
Expand Down
54 changes: 16 additions & 38 deletions src/base/QXmppUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
#include <QRandomGenerator>
#endif
#include <QRegExp>
#include <QRegularExpression>
#include <QString>
#include <QStringList>
#include <QUuid>
Expand Down Expand Up @@ -116,30 +116,8 @@ static quint32 crctable[256] = {
///
QDateTime QXmppUtils::datetimeFromString(const QString &str)
{
QRegExp tzRe(QStringLiteral("(Z|([+-])([0-9]{2}):([0-9]{2}))"));
int tzPos = tzRe.indexIn(str, 19);
if (str.size() < 20 || tzPos < 0)
return QDateTime();

// process date and time
QDateTime dt = QDateTime::fromString(str.left(19), QStringLiteral("yyyy-MM-ddThh:mm:ss"));
dt.setTimeSpec(Qt::UTC);

// process milliseconds
if (tzPos > 20 && str.at(19) == '.') {
QString millis = (str.mid(20, tzPos - 20) + QStringLiteral("000")).left(3);
dt = dt.addMSecs(millis.toInt());
}

// process time zone
if (tzRe.cap(1) != QStringLiteral("Z")) {
int offset = tzRe.cap(3).toInt() * 3600 + tzRe.cap(4).toInt() * 60;
if (tzRe.cap(2) == QStringLiteral("+"))
dt = dt.addSecs(-offset);
else
dt = dt.addSecs(offset);
}
return dt;
// Qt::ISODate parses milliseconds, but doesn't output them
return QDateTime::fromString(str, Qt::ISODate).toUTC();
}

///
Expand All @@ -148,11 +126,9 @@ QDateTime QXmppUtils::datetimeFromString(const QString &str)
///
QString QXmppUtils::datetimeToString(const QDateTime &dt)
{
QDateTime utc = dt.toUTC();
if (utc.time().msec())
return utc.toString(QStringLiteral("yyyy-MM-ddThh:mm:ss.zzzZ"));
else
return utc.toString(QStringLiteral("yyyy-MM-ddThh:mm:ssZ"));
if (dt.time().msec())
return dt.toUTC().toString(Qt::ISODateWithMs);
return dt.toUTC().toString(Qt::ISODate);
}

///
Expand All @@ -161,21 +137,23 @@ QString QXmppUtils::datetimeToString(const QDateTime &dt)
///
int QXmppUtils::timezoneOffsetFromString(const QString &str)
{
QRegExp tzRe(QStringLiteral("(Z|([+-])([0-9]{2}):([0-9]{2}))"));
if (!tzRe.exactMatch(str))
static const QRegularExpression timezoneRegex(QStringLiteral("(Z|([+-])([0-9]{2}):([0-9]{2}))"));

const auto match = timezoneRegex.match(str);
if (!match.hasMatch())
return 0;

// No offset from UTC
if (tzRe.cap(1) == QStringLiteral("Z"))
if (match.captured(1) == u'Z')
return 0;

// Calculate offset
const int offset = tzRe.cap(3).toInt() * 3600 +
tzRe.cap(4).toInt() * 60;
if (tzRe.cap(2) == QStringLiteral("-"))
const int offset = match.captured(3).toInt() * 3600 +
match.captured(4).toInt() * 60;

if (match.captured(2) == u'-')
return -offset;
else
return offset;
return offset;
}

///
Expand Down
1 change: 0 additions & 1 deletion src/base/QXmppUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class QByteArray;
class QDateTime;
class QDomElement;
class QString;
class QStringList;

/// \brief The QXmppUtils class contains static utility functions.
///
Expand Down
1 change: 0 additions & 1 deletion src/client/QXmppClientExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "QXmppLogger.h"

class QDomElement;
class QStringList;

class QXmppClient;
class QXmppClientExtensionPrivate;
Expand Down
4 changes: 4 additions & 0 deletions src/client/QXmppInvokable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ QVariant QXmppInvokable::dispatch(const QByteArray &method, const QList<QVariant
genericArgs.value(7, QGenericArgument()),
genericArgs.value(8, QGenericArgument()),
genericArgs.value(9, QGenericArgument()))) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
QVariant returnValue(QMetaType(resultType), result);
#else
QVariant returnValue(resultType, result);
#endif
QMetaType::destroy(resultType, result);
return returnValue;
} else {
Expand Down
62 changes: 38 additions & 24 deletions src/client/QXmppOutgoingClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#include <QCoreApplication>
#include <QDomDocument>
#include <QHostAddress>
#include <QRegExp>
#include <QRegularExpression>
#include <QStringList>
#include <QTimer>
#include <QXmlStreamWriter>
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<QString, int> 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;
Expand Down
3 changes: 3 additions & 0 deletions src/client/QXmppOutgoingClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ private Q_SLOTS:
void pingTimeout();

private:
bool setResumeAddress(const QString &address);
static std::pair<QString, int> parseHostAddress(const QString &address);

friend class QXmppOutgoingClientPrivate;
QXmppOutgoingClientPrivate *const d;
};
Expand Down
6 changes: 5 additions & 1 deletion src/server/QXmppServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <QFileInfo>
#include <QPluginLoader>
#include <QSslCertificate>
#include <QSslConfiguration>
#include <QSslKey>
#include <QSslSocket>

Expand Down Expand Up @@ -886,8 +887,11 @@ void QXmppSslServer::incomingConnection(qintptr socketDescriptor)
}

if (!d->localCertificate.isNull() && !d->privateKey.isNull()) {
auto sslConfig = socket->sslConfiguration();
sslConfig.setCaCertificates(sslConfig.caCertificates() + d->caCertificates);
socket->setSslConfiguration(sslConfig);

socket->setProtocol(QSsl::AnyProtocol);
socket->addCaCertificates(d->caCertificates);
socket->setLocalCertificate(d->localCertificate);
socket->setPrivateKey(d->privateKey);
}
Expand Down
1 change: 0 additions & 1 deletion src/server/QXmppServerExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <QVariant>

class QDomElement;
class QStringList;

class QXmppServer;
class QXmppServerExtensionPrivate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ void tst_QXmppRegistrationManager::sendStreamFeaturesToManager(bool registration
// hacky hack to include stream namespace
QByteArray manipulatedXml = buffer.data();
manipulatedXml.replace("stream:", QByteArray());
manipulatedXml.insert(9, QStringLiteral(" xmlns=\"%1\"").arg("http://etherx.jabber.org/streams"));
manipulatedXml.insert(9, QByteArray(" xmlns=\"http://etherx.jabber.org/streams\""));

QDomDocument doc;
doc.setContent(manipulatedXml, true);
Expand Down

0 comments on commit 008038c

Please sign in to comment.