From 4252cbf3f212f71dfe8b447d5b2b9eb5e7140b62 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Mon, 15 Jul 2024 20:31:53 +0000 Subject: [PATCH] Adjust to ObjFW changes FossilOrigin-Name: 531eb84cf5c27b39f7701dfa446886e2962aad9a8b60c3623e3b70833b18c592 --- ObjMatrix.oc | 2 +- README.md | 5 +- configure.ac | 6 +-- src/MTXClient.h | 12 ++--- src/MTXClient.m | 59 +++++++++++++++--------- src/MTXRequest.h | 12 ++--- src/MTXRequest.m | 14 +++--- src/exceptions/MTXLoginFailedException.h | 6 +-- src/exceptions/MTXLoginFailedException.m | 4 +- tests/{tests.m => Tests.m} | 4 +- 10 files changed, 69 insertions(+), 55 deletions(-) rename tests/{tests.m => Tests.m} (96%) diff --git a/ObjMatrix.oc b/ObjMatrix.oc index 35a9b4b..3314422 100644 --- a/ObjMatrix.oc +++ b/ObjMatrix.oc @@ -1,5 +1,5 @@ package_format 1 -package_depends_on ObjOpenSSL +package_depends_on ObjFWTLS package_depends_on ObjSQLite3 LIBS="-lobjmatrix $LIBS" FRAMEWORK_LIBS="-framework ObjMatrix $FRAMEWORK_LIBS" diff --git a/README.md b/README.md index ea31c46..a01db62 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,7 @@ It is currently in early development stages. ## How to build it? -You need [ObjFW](https://objfw.nil.im), -[ObjOpenSSL](https://fossil.nil.im/objopenssl) and +You need [ObjFW](https://objfw.nil.im) and [ObjSQLite3](https://fossil.nil.im/objsqlite3) installed in order to do this. ObjMatrix uses modern Objective-C, and hence cannot be compiled with GCC, but @@ -18,7 +17,7 @@ only with Clang. So install Clang first and ObjFW will automatically pick it up. You can install them all like this: - $ for i in objfw objopenssl objsqlite3 objmatrix; do + $ for i in objfw objsqlite3 objmatrix; do fossil clone https://fossil.nil.im/$i $i.fossil && mkdir $i && cd $i && diff --git a/configure.ac b/configure.ac index b1597ea..708bbfe 100644 --- a/configure.ac +++ b/configure.ac @@ -16,10 +16,10 @@ AS_IF([test x"$OBJFW_CONFIG" = x""], [ AC_MSG_ERROR(You need ObjFW and objfw-config installed!) ]) -AS_IF([$OBJFW_CONFIG --package ObjOpenSSL], [ - OBJFW_CONFIG_FLAGS="$OBJFW_CONFIG_FLAGS --package ObjOpenSSL" +AS_IF([$OBJFW_CONFIG --package ObjFWTLS], [ + OBJFW_CONFIG_FLAGS="$OBJFW_CONFIG_FLAGS --package ObjFWTLS" ], [ - AC_MSG_ERROR(ObjOpenSSL not found!) + AC_MSG_ERROR(ObjFWTLS not found!) ]) AS_IF([$OBJFW_CONFIG --package ObjSQLite3], [ OBJFW_CONFIG_FLAGS="$OBJFW_CONFIG_FLAGS --package ObjSQLite3" diff --git a/src/MTXClient.h b/src/MTXClient.h index ef521f2..2120d2d 100644 --- a/src/MTXClient.h +++ b/src/MTXClient.h @@ -92,7 +92,7 @@ typedef void (^MTXClientRoomJoinBlock)(OFString *_Nullable roomID, /** * @brief The homeserver used by the client. */ -@property (readonly, nonatomic) OFURL *homeserver; +@property (readonly, nonatomic) OFIRI *homeserver; /** * @brief The storage used by the client. @@ -118,14 +118,14 @@ typedef void (^MTXClientRoomJoinBlock)(OFString *_Nullable roomID, * @param userID The user ID for the client * @param deviceID The device ID for the client * @param accessToken The access token for the client - * @param homeserver The URL of the homeserver + * @param homeserver The IRI of the homeserver * @param storage The storage the client should use * @return An autoreleased MTXClient */ + (instancetype)clientWithUserID: (OFString *)userID deviceID: (OFString *)deviceID accessToken: (OFString *)accessToken - homeserver: (OFURL *)homeserver + homeserver: (OFIRI *)homeserver storage: (id )storage; /** @@ -139,7 +139,7 @@ typedef void (^MTXClientRoomJoinBlock)(OFString *_Nullable roomID, */ + (void)logInWithUser: (OFString *)user password: (OFString *)password - homeserver: (OFURL *)homeserver + homeserver: (OFIRI *)homeserver storage: (id )storage block: (MTXClientLoginBlock)block; @@ -150,14 +150,14 @@ typedef void (^MTXClientRoomJoinBlock)(OFString *_Nullable roomID, * @param userID The user ID for the client * @param deviceID The device ID for the client * @param accessToken The access token for the client - * @param homeserver The URL of the homeserver + * @param homeserver The IRI of the homeserver * @param storage The storage the client should use * @return An initialized MTXClient */ - (instancetype)initWithUserID: (OFString *)userID deviceID: (OFString *)deviceID accessToken: (OFString *)accessToken - homeserver: (OFURL *)homeserver + homeserver: (OFIRI *)homeserver storage: (id )storage OF_DESIGNATED_INITIALIZER; diff --git a/src/MTXClient.m b/src/MTXClient.m index 1402ed6..8450cba 100644 --- a/src/MTXClient.m +++ b/src/MTXClient.m @@ -32,12 +32,12 @@ #import "MTXSyncFailedException.h" static void -validateHomeserver(OFURL *homeserver) +validateHomeserver(OFIRI *homeserver) { if (![homeserver.scheme isEqual: @"http"] && ![homeserver.scheme isEqual: @"https"]) @throw [OFUnsupportedProtocolException - exceptionWithURL: homeserver]; + exceptionWithIRI: homeserver]; if (homeserver.path != nil && ![homeserver.path isEqual: @"/"]) @throw [OFInvalidArgumentException exception]; @@ -55,7 +55,7 @@ validateHomeserver(OFURL *homeserver) + (instancetype)clientWithUserID: (OFString *)userID deviceID: (OFString *)deviceID accessToken: (OFString *)accessToken - homeserver: (OFURL *)homeserver + homeserver: (OFIRI *)homeserver storage: (id )storage { return [[[self alloc] initWithUserID: userID @@ -67,7 +67,7 @@ validateHomeserver(OFURL *homeserver) + (void)logInWithUser: (OFString *)user password: (OFString *)password - homeserver: (OFURL *)homeserver + homeserver: (OFIRI *)homeserver storage: (id )storage block: (MTXClientLoginBlock)block { @@ -112,22 +112,24 @@ validateHomeserver(OFURL *homeserver) if (![userID isKindOfClass: OFString.class] || ![deviceID isKindOfClass: OFString.class] || ![accessToken isKindOfClass: OFString.class]) { - block(nil, [OFInvalidServerReplyException exception]); + block(nil, + [OFInvalidServerResponseException exception]); return; } - OFString *baseURL = + OFString *baseIRI = response[@"well_known"][@"m.homeserver"][@"base_url"]; - if (baseURL != nil && - ![baseURL isKindOfClass: OFString.class]) { - block(nil, [OFInvalidServerReplyException exception]); + if (baseIRI != nil && + ![baseIRI isKindOfClass: OFString.class]) { + block(nil, + [OFInvalidServerResponseException exception]); return; } - OFURL *realHomeserver; - if (baseURL != nil) { + OFIRI *realHomeserver; + if (baseIRI != nil) { @try { - realHomeserver = [OFURL URLWithString: baseURL]; + realHomeserver = [OFIRI IRIWithString: baseIRI]; } @catch (id e) { block(nil, e); return; @@ -149,7 +151,7 @@ validateHomeserver(OFURL *homeserver) - (instancetype)initWithUserID: (OFString *)userID deviceID: (OFString *)deviceID accessToken: (OFString *)accessToken - homeserver: (OFURL *)homeserver + homeserver: (OFIRI *)homeserver storage: (id )storage { self = [super init]; @@ -212,11 +214,20 @@ validateHomeserver(OFURL *homeserver) MTXRequest *request = [self requestWithPath: @"/_matrix/client/r0/sync"]; unsigned long long timeoutMs = _syncTimeout * 1000; - OFMutableDictionary *query = - [OFMutableDictionary dictionaryWithObject: @(timeoutMs).stringValue - forKey: @"timeout"]; - query[@"since"] = [_storage nextBatchForDeviceID: _deviceID]; - request.query = query; + OFMutableArray *> *queryItems = + [OFMutableArray array]; + OFString *since = [_storage nextBatchForDeviceID: _deviceID]; + + [queryItems addObject: + [OFPair pairWithFirstObject: @"timeout" + secondObject: @(timeoutMs).stringValue]]; + + if (since != nil) + [queryItems addObject: + [OFPair pairWithFirstObject: @"since" + secondObject: since]]; + + request.queryItems = queryItems; [request performWithBlock: ^ (MTXResponse response, int statusCode, id exception) { if (exception != nil) { @@ -238,7 +249,8 @@ validateHomeserver(OFURL *homeserver) if (![nextBatch isKindOfClass: OFString.class]) { if (_syncExceptionHandler != NULL) _syncExceptionHandler( - [OFInvalidServerReplyException exception]); + [OFInvalidServerResponseException + exception]); return; } @@ -324,13 +336,15 @@ validateHomeserver(OFURL *homeserver) OFArray *joinedRooms = response[@"joined_rooms"]; if (![joinedRooms isKindOfClass: OFArray.class]) { - block(nil, [OFInvalidServerReplyException exception]); + block(nil, + [OFInvalidServerResponseException exception]); return; } for (OFString *room in joinedRooms) { if (![room isKindOfClass: OFString.class]) { block(nil, - [OFInvalidServerReplyException exception]); + [OFInvalidServerResponseException + exception]); return; } } @@ -365,7 +379,8 @@ validateHomeserver(OFURL *homeserver) OFString *roomID = response[@"room_id"]; if (![roomID isKindOfClass: OFString.class]) { - block(nil, [OFInvalidServerReplyException exception]); + block(nil, + [OFInvalidServerResponseException exception]); return; } diff --git a/src/MTXRequest.h b/src/MTXRequest.h index f20b9f9..485b3f5 100644 --- a/src/MTXRequest.h +++ b/src/MTXRequest.h @@ -54,9 +54,9 @@ typedef void (^MTXRequestBlock)(MTXResponse _Nullable response, int statusCode, @property (readonly, nonatomic, nullable) OFString *accessToken; /** - * @brief The URL of the homeserver to send the request to. + * @brief The IRI of the homeserver to send the request to. */ -@property (readonly, nonatomic) OFURL *homeserver; +@property (readonly, nonatomic) OFIRI *homeserver; /** * @brief The HTTP request method. @@ -71,10 +71,10 @@ typedef void (^MTXRequestBlock)(MTXResponse _Nullable response, int statusCode, @property (copy, nonatomic) OFString *path; /** - * @brief The query for the request. + * @brief The query items for the request. */ @property (copy, nullable, nonatomic) - OFDictionary *query; + OFArray *> *queryItems; /** * @brief An optional body to send along with the request. @@ -92,7 +92,7 @@ typedef void (^MTXRequestBlock)(MTXResponse _Nullable response, int statusCode, */ + (instancetype)requestWithPath: (OFString *)path accessToken: (nullable OFString *)accessToken - homeserver: (OFURL *)homeserver; + homeserver: (OFIRI *)homeserver; /** * @brief Initializes an already allocated request with the specified access @@ -104,7 +104,7 @@ typedef void (^MTXRequestBlock)(MTXResponse _Nullable response, int statusCode, */ - (instancetype)initWithPath: (OFString *)path accessToken: (nullable OFString *)accessToken - homeserver: (OFURL *)homeserver; + homeserver: (OFIRI *)homeserver; /** * @brief Performs the request and calls the specified block once the request diff --git a/src/MTXRequest.m b/src/MTXRequest.m index d42a5b6..cea0dfa 100644 --- a/src/MTXRequest.m +++ b/src/MTXRequest.m @@ -30,7 +30,7 @@ + (instancetype)requestWithPath: (OFString *)path accessToken: (OFString *)accessToken - homeserver: (OFURL *)homeserver + homeserver: (OFIRI *)homeserver { return [[[self alloc] initWithPath: path accessToken: accessToken @@ -39,7 +39,7 @@ - (instancetype)initWithPath: (OFString *)path accessToken: (OFString *)accessToken - homeserver: (OFURL *)homeserver + homeserver: (OFIRI *)homeserver { self = [super init]; @@ -91,11 +91,11 @@ if (_block != nil) /* Not the best exception to indicate it's already in-flight. */ - @throw [OFAlreadyConnectedException exception]; + @throw [OFAlreadyOpenException exceptionWithObject: self]; - OFMutableURL *requestURL = [[_homeserver mutableCopy] autorelease]; - requestURL.path = _path; - requestURL.queryDictionary = _query; + OFMutableIRI *requestIRI = [[_homeserver mutableCopy] autorelease]; + requestIRI.path = _path; + requestIRI.queryItems = _queryItems; OFMutableDictionary *headers = [OFMutableDictionary dictionary]; headers[@"User-Agent"] = @"ObjMatrix"; @@ -105,7 +105,7 @@ if (_body != nil) headers[@"Content-Length"] = @(_body.count).stringValue; - OFHTTPRequest *request = [OFHTTPRequest requestWithURL: requestURL]; + OFHTTPRequest *request = [OFHTTPRequest requestWithIRI: requestIRI]; request.method = _method; request.headers = headers; diff --git a/src/exceptions/MTXLoginFailedException.h b/src/exceptions/MTXLoginFailedException.h index 1e7797d..cf066a1 100644 --- a/src/exceptions/MTXLoginFailedException.h +++ b/src/exceptions/MTXLoginFailedException.h @@ -28,16 +28,16 @@ OF_ASSUME_NONNULL_BEGIN @interface MTXLoginFailedException: OFException @property (readonly, nonatomic) OFString *user; -@property (readonly, nonatomic) OFURL *homeserver; +@property (readonly, nonatomic) OFIRI *homeserver; @property (readonly, nonatomic) int statusCode; @property (readonly, nonatomic) MTXResponse response; + (instancetype)exceptionWithUser: (OFString *)user - homeserver: (OFURL *)homeserver + homeserver: (OFIRI *)homeserver statusCode: (int)statusCode response: (MTXResponse)response; - (instancetype)initWithUser: (OFString *)user - homeserver: (OFURL *)homeserver + homeserver: (OFIRI *)homeserver statusCode: (int)statusCode response: (MTXResponse)response; @end diff --git a/src/exceptions/MTXLoginFailedException.m b/src/exceptions/MTXLoginFailedException.m index 149105b..61fdf1b 100644 --- a/src/exceptions/MTXLoginFailedException.m +++ b/src/exceptions/MTXLoginFailedException.m @@ -24,7 +24,7 @@ @implementation MTXLoginFailedException + (instancetype)exceptionWithUser: (OFString *)user - homeserver: (OFURL *)homeserver + homeserver: (OFIRI *)homeserver statusCode: (int)statusCode response: (MTXResponse)response { @@ -35,7 +35,7 @@ } - (instancetype)initWithUser: (OFString *)user - homeserver: (OFURL *)homeserver + homeserver: (OFIRI *)homeserver statusCode: (int)statusCode response: (MTXResponse)response { diff --git a/tests/tests.m b/tests/Tests.m similarity index 96% rename from tests/tests.m rename to tests/Tests.m index 88e7eeb..ab66d3f 100644 --- a/tests/tests.m +++ b/tests/Tests.m @@ -35,7 +35,7 @@ OF_APPLICATION_DELEGATE(Tests) OFString *_roomID; } -- (void)applicationDidFinishLaunching +- (void)applicationDidFinishLaunching: (OFNotification *)notification { __auto_type environment = OFApplication.environment; if (environment[@"OBJMATRIX_USER"] == nil || @@ -47,7 +47,7 @@ OF_APPLICATION_DELEGATE(Tests) [OFApplication terminateWithStatus: 1]; } - OFURL *homeserver = [OFURL URLWithString: environment[@"OBJMATRIX_HS"]]; + OFIRI *homeserver = [OFIRI IRIWithString: environment[@"OBJMATRIX_HS"]]; id storage = [MTXSQLite3Storage storageWithPath: @"tests.db"]; [MTXClient logInWithUser: environment[@"OBJMATRIX_USER"]