From 454a3fc1be82f051285a4680334df4070f9ec559 Mon Sep 17 00:00:00 2001 From: manuroe Date: Wed, 13 Feb 2019 17:10:55 +0100 Subject: [PATCH] MXTools: Update isMatrixEventIdentifier to support room v3 event ids --- MatrixSDK/Utils/MXTools.m | 20 +++++++++++++------- MatrixSDKTests/MXToolsTests.m | 1 + 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/MatrixSDK/Utils/MXTools.m b/MatrixSDK/Utils/MXTools.m index de3f66cba2..0c0a3d391a 100644 --- a/MatrixSDK/Utils/MXTools.m +++ b/MatrixSDK/Utils/MXTools.m @@ -23,16 +23,17 @@ #endif #pragma mark - Constant definition -NSString *const kMXToolsRegexStringForEmailAddress = @"[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}"; +NSString *const kMXToolsRegexStringForEmailAddress = @"[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}"; // The HS domain part in Matrix identifiers #define MATRIX_HOMESERVER_DOMAIN_REGEX @"[A-Z0-9.-]+(\\.[A-Z]{2,})?+(\\:[0-9]{2,})?" -NSString *const kMXToolsRegexStringForMatrixUserIdentifier = @"@[\\x21-\\x39\\x3B-\\x7F]+:" MATRIX_HOMESERVER_DOMAIN_REGEX; -NSString *const kMXToolsRegexStringForMatrixRoomAlias = @"#[A-Z0-9._%#@+-]+:" MATRIX_HOMESERVER_DOMAIN_REGEX; -NSString *const kMXToolsRegexStringForMatrixRoomIdentifier = @"![A-Z0-9]+:" MATRIX_HOMESERVER_DOMAIN_REGEX; -NSString *const kMXToolsRegexStringForMatrixEventIdentifier = @"\\$[A-Z0-9]+:" MATRIX_HOMESERVER_DOMAIN_REGEX; -NSString *const kMXToolsRegexStringForMatrixGroupIdentifier = @"\\+[A-Z0-9=_\\-./]+:" MATRIX_HOMESERVER_DOMAIN_REGEX; +NSString *const kMXToolsRegexStringForMatrixUserIdentifier = @"@[\\x21-\\x39\\x3B-\\x7F]+:" MATRIX_HOMESERVER_DOMAIN_REGEX; +NSString *const kMXToolsRegexStringForMatrixRoomAlias = @"#[A-Z0-9._%#@+-]+:" MATRIX_HOMESERVER_DOMAIN_REGEX; +NSString *const kMXToolsRegexStringForMatrixRoomIdentifier = @"![A-Z0-9]+:" MATRIX_HOMESERVER_DOMAIN_REGEX; +NSString *const kMXToolsRegexStringForMatrixEventIdentifier = @"\\$[A-Z0-9]+:" MATRIX_HOMESERVER_DOMAIN_REGEX; +NSString *const kMXToolsRegexStringForMatrixEventIdentifierV3 = @"\\$[A-Z0-9\\/+]+"; +NSString *const kMXToolsRegexStringForMatrixGroupIdentifier = @"\\+[A-Z0-9=_\\-./]+:" MATRIX_HOMESERVER_DOMAIN_REGEX; #pragma mark - MXTools static private members @@ -45,6 +46,7 @@ static NSRegularExpression *isMatrixRoomAliasRegex; static NSRegularExpression *isMatrixRoomIdentifierRegex; static NSRegularExpression *isMatrixEventIdentifierRegex; +static NSRegularExpression *isMatrixEventIdentifierV3Regex; static NSRegularExpression *isMatrixGroupIdentifierRegex; // A regex to find new lines @@ -117,6 +119,9 @@ + (void)initialize options:NSRegularExpressionCaseInsensitive error:nil]; isMatrixEventIdentifierRegex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"^%@$", kMXToolsRegexStringForMatrixEventIdentifier] options:NSRegularExpressionCaseInsensitive error:nil]; + isMatrixEventIdentifierV3Regex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"^%@$", kMXToolsRegexStringForMatrixEventIdentifierV3] + options:NSRegularExpressionCaseInsensitive error:nil]; + isMatrixGroupIdentifierRegex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"^%@$", kMXToolsRegexStringForMatrixGroupIdentifier] options:NSRegularExpressionCaseInsensitive error:nil]; @@ -337,7 +342,8 @@ + (BOOL)isMatrixEventIdentifier:(NSString *)inputString { if (inputString) { - return (nil != [isMatrixEventIdentifierRegex firstMatchInString:inputString options:0 range:NSMakeRange(0, inputString.length)]); + return (nil != [isMatrixEventIdentifierRegex firstMatchInString:inputString options:0 range:NSMakeRange(0, inputString.length)]) + || (nil != [isMatrixEventIdentifierV3Regex firstMatchInString:inputString options:0 range:NSMakeRange(0, inputString.length)]); } return NO; } diff --git a/MatrixSDKTests/MXToolsTests.m b/MatrixSDKTests/MXToolsTests.m index a8537585c5..e1129e075a 100644 --- a/MatrixSDKTests/MXToolsTests.m +++ b/MatrixSDKTests/MXToolsTests.m @@ -60,6 +60,7 @@ - (void)testMatrixIdentifiers XCTAssertTrue([MXTools isMatrixUserIdentifier:@"@+33012:matrix.org"]); XCTAssertTrue([MXTools isMatrixEventIdentifier:@"$123456EventId:matrix.org"]); + XCTAssertTrue([MXTools isMatrixEventIdentifier:@"$pmOSN/DognfuSfhdW/qivXT19lfCWpdSfaPFKDBTJUk+"]); XCTAssertTrue([MXTools isMatrixRoomIdentifier:@"!an1234Room:matrix.org"]);