diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2019-07-08 21:31:40 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2019-07-09 10:19:31 +0900 |
commit | 651478c1681ba6f93e22c20328a048dbbc263ffe (patch) | |
tree | 26075cb319c61a441bb8540079d711a1bc04f954 /lib | |
parent | 9f0c237dbd1d684e0ce9a7a51bf415577c209f5e (diff) | |
download | libquotient-651478c1681ba6f93e22c20328a048dbbc263ffe.tar.gz libquotient-651478c1681ba6f93e22c20328a048dbbc263ffe.zip |
Move serverPart() to the public API
Also: Connection::resolveServer() now only accepts MXIDs, not domains.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/connection.cpp | 28 | ||||
-rw-r--r-- | lib/connection.h | 4 | ||||
-rw-r--r-- | lib/util.cpp | 14 | ||||
-rw-r--r-- | lib/util.h | 8 |
4 files changed, 26 insertions, 28 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp index ff066def..6d06c603 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -162,33 +162,11 @@ Connection::~Connection() stopSync(); } -static const auto ServerPartRegEx = QStringLiteral( - "(\\[[^]]+\\]|[^:@]+)" // Either IPv6 address or hostname/IPv4 address - "(?::(\\d{1,5}))?" // Optional port -); - -QString serverPart(const QString& mxId) +void Connection::resolveServer(const QString& mxid) { - static QString re = "^[@!#$+].+?:(" // Localpart and colon - % ServerPartRegEx % ")$"; - static QRegularExpression parser(re, - QRegularExpression::UseUnicodePropertiesOption); // Because Asian digits - return parser.match(mxId).captured(1); -} - -void Connection::resolveServer(const QString& mxidOrDomain) -{ - // mxIdOrDomain may be something as complex as - // @username:[IPv6:address]:port, or as simple as a plain serverpart. - static QRegularExpression parser( - "^(@.+?:)?" // Optional username (allow everything for compatibility) - % ServerPartRegEx % '$', - QRegularExpression::UseUnicodePropertiesOption); // Because Asian digits - auto match = parser.match(mxidOrDomain); - - auto maybeBaseUrl = QUrl::fromUserInput(match.captured(2)); + auto maybeBaseUrl = QUrl::fromUserInput(serverPart(mxid)); maybeBaseUrl.setScheme("https"); // Instead of the Qt-default "http" - if (!match.hasMatch() || !maybeBaseUrl.isValid()) + if (maybeBaseUrl.isEmpty() || !maybeBaseUrl.isValid()) { emit resolveError( tr("%1 is not a valid homeserver address") diff --git a/lib/connection.h b/lib/connection.h index 4ab8d5ba..11499a6e 100644 --- a/lib/connection.h +++ b/lib/connection.h @@ -408,8 +408,8 @@ namespace QMatrixClient /** Set the homeserver base URL */ void setHomeserver(const QUrl& baseUrl); - /** Determine and set the homeserver from domain or MXID */ - void resolveServer(const QString& mxidOrDomain); + /** Determine and set the homeserver from MXID */ + void resolveServer(const QString& mxid); void connectToServer(const QString& user, const QString& password, const QString& initialDeviceName, diff --git a/lib/util.cpp b/lib/util.cpp index 88cba959..b9639843 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -112,6 +112,20 @@ qreal QMatrixClient::stringToHueF(const QString &string) return hueF; } +static const auto ServerPartRegEx = QStringLiteral( + "(\\[[^]]+\\]|[^:@]+)" // Either IPv6 address or hostname/IPv4 address + "(?::(\\d{1,5}))?" // Optional port +); + +QString QMatrixClient::serverPart(const QString& mxId) +{ + static QString re = "^[@!#$+].+?:(" // Localpart and colon + % ServerPartRegEx % ")$"; + static QRegularExpression parser(re, + QRegularExpression::UseUnicodePropertiesOption); // Because Asian digits + return parser.match(mxId).captured(1); +} + // Tests for function_traits<> #ifdef Q_CC_CLANG @@ -300,27 +300,33 @@ namespace QMatrixClient void linkifyUrls(QString& htmlEscapedText); /** Sanitize the text before showing in HTML + * * This does toHtmlEscaped() and removes Unicode BiDi marks. */ QString sanitized(const QString& plainText); /** Pretty-print plain text into HTML + * * This includes HTML escaping of <,>,",& and calling linkifyUrls() */ QString prettyPrint(const QString& plainText); /** Return a path to cache directory after making sure that it exists + * * The returned path has a trailing slash, clients don't need to append it. * \param dir path to cache directory relative to the standard cache path */ QString cacheLocation(const QString& dirName); /** Hue color component of based of the hash of the string. + * * The implementation is based on XEP-0392: * https://xmpp.org/extensions/xep-0392.html * Naming and range are the same as QColor's hueF method: * https://doc.qt.io/qt-5/qcolor.html#integer-vs-floating-point-precision */ qreal stringToHueF(const QString& string); -} // namespace QMatrixClient + /** Extract the serverpart from MXID */ + QString serverPart(const QString& mxId); +} // namespace QMatrixClient |