︙ | | |
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
-
+
|
* @brief The access token used by the client.
*/
@property (readonly, nonatomic) OFString *accessToken;
/**
* @brief The homeserver used by the client.
*/
@property (readonly, nonatomic) OFURL *homeserver;
@property (readonly, nonatomic) OFIRI *homeserver;
/**
* @brief The storage used by the client.
*/
@property (readonly, nonatomic) id <MTXStorage> storage;
/**
|
︙ | | |
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
-
+
-
+
-
+
-
+
-
+
|
/**
* @brief Creates a new client with the specified access token on the specified
* homeserver.
*
* @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 <MTXStorage>)storage;
/**
* @brief Logs into the homeserver and creates a new client.
*
* @param user The user to log into
* @param password The password to log in with
* @param homeserver The homeserver to log into
* @param storage The storage the client should use
* @param block A block to call once login succeeded or failed
*/
+ (void)logInWithUser: (OFString *)user
password: (OFString *)password
homeserver: (OFURL *)homeserver
homeserver: (OFIRI *)homeserver
storage: (id <MTXStorage>)storage
block: (MTXClientLoginBlock)block;
/**
* @brief Initializes an already allocated client with the specified access
* token on the specified homeserver.
*
* @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 <MTXStorage>)storage
OF_DESIGNATED_INITIALIZER;
/**
* @brief Starts the sync loop.
*/
- (void)startSyncLoop;
|
︙ | | |
︙ | | |
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
-
+
-
+
-
+
-
+
|
#import "MTXLeaveRoomFailedException.h"
#import "MTXLoginFailedException.h"
#import "MTXLogoutFailedException.h"
#import "MTXSendMessageFailedException.h"
#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];
if (homeserver.user != nil || homeserver.password != nil ||
homeserver.query != nil || homeserver.fragment != nil)
@throw [OFInvalidArgumentException exception];
}
@implementation MTXClient
{
bool _syncing;
}
+ (instancetype)clientWithUserID: (OFString *)userID
deviceID: (OFString *)deviceID
accessToken: (OFString *)accessToken
homeserver: (OFURL *)homeserver
homeserver: (OFIRI *)homeserver
storage: (id <MTXStorage>)storage
{
return [[[self alloc] initWithUserID: userID
deviceID: deviceID
accessToken: accessToken
homeserver: homeserver
storage: storage] autorelease];
}
+ (void)logInWithUser: (OFString *)user
password: (OFString *)password
homeserver: (OFURL *)homeserver
homeserver: (OFIRI *)homeserver
storage: (id <MTXStorage>)storage
block: (MTXClientLoginBlock)block
{
void *pool = objc_autoreleasePoolPush();
validateHomeserver(homeserver);
|
︙ | | |
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
-
+
+
-
+
-
-
-
+
+
+
+
-
-
+
+
-
+
|
OFString *userID = response[@"user_id"];
OFString *deviceID = response[@"device_id"];
OFString *accessToken = response[@"access_token"];
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;
}
} else
realHomeserver = homeserver;
|
︙ | | |
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
-
+
|
objc_autoreleasePoolPop(pool);
}
- (instancetype)initWithUserID: (OFString *)userID
deviceID: (OFString *)deviceID
accessToken: (OFString *)accessToken
homeserver: (OFURL *)homeserver
homeserver: (OFIRI *)homeserver
storage: (id <MTXStorage>)storage
{
self = [super init];
@try {
validateHomeserver(homeserver);
|
︙ | | |
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
_syncing = true;
void *pool = objc_autoreleasePoolPush();
MTXRequest *request = [self
requestWithPath: @"/_matrix/client/r0/sync"];
unsigned long long timeoutMs = _syncTimeout * 1000;
OFMutableDictionary<OFString *, OFString *> *query =
[OFMutableDictionary dictionaryWithObject: @(timeoutMs).stringValue
forKey: @"timeout"];
query[@"since"] = [_storage nextBatchForDeviceID: _deviceID];
request.query = query;
OFMutableArray<OFPair <OFString *, OFString *> *> *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) {
if (_syncExceptionHandler != NULL)
_syncExceptionHandler(exception);
return;
}
|
︙ | | |
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
-
+
+
|
return;
}
OFString *nextBatch = response[@"next_batch"];
if (![nextBatch isKindOfClass: OFString.class]) {
if (_syncExceptionHandler != NULL)
_syncExceptionHandler(
[OFInvalidServerReplyException exception]);
[OFInvalidServerResponseException
exception]);
return;
}
@try {
[_storage transactionWithBlock: ^ {
[_storage setNextBatch: nextBatch
forDeviceID: _deviceID];
|
︙ | | |
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
-
+
+
-
+
+
|
response: response
client: self]);
return;
}
OFArray<OFString *> *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;
}
}
block(response[@"joined_rooms"], nil);
}];
|
︙ | | |
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
-
+
+
|
response: response
client: self]);
return;
}
OFString *roomID = response[@"room_id"];
if (![roomID isKindOfClass: OFString.class]) {
block(nil, [OFInvalidServerReplyException exception]);
block(nil,
[OFInvalidServerResponseException exception]);
return;
}
block(roomID, nil);
}];
objc_autoreleasePoolPop(pool);
|
︙ | | |
︙ | | |
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
-
+
-
+
-
+
-
+
-
+
-
+
|
* @brief The access token to use.
*
* Some requests are unauthenticated - for those, the access token is `nil`.
*/
@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.
*
* Defaults to `OF_HTTP_REQUEST_METHOD_GET`.
*/
@property (nonatomic) OFHTTPRequestMethod method;
/**
* @brief The path of the request.
*/
@property (copy, nonatomic) OFString *path;
/**
* @brief The query for the request.
* @brief The query items for the request.
*/
@property (copy, nullable, nonatomic)
OFDictionary<OFString *, OFString *> *query;
OFArray<OFPair<OFString *, OFString *> *> *queryItems;
/**
* @brief An optional body to send along with the request.
*
* This is a dictionary that gets serialized to JSON when the request is sent.
*/
@property (copy, nullable, nonatomic) OFDictionary<OFString *, id> *body;
/**
* @brief Creates a new request with the specified access token and homeserver.
*
* @param accessToken An (optional) access token to use
* @param homeserver The homeserver the request will be sent to
* @return An autoreleased MTXRequest
*/
+ (instancetype)requestWithPath: (OFString *)path
accessToken: (nullable OFString *)accessToken
homeserver: (OFURL *)homeserver;
homeserver: (OFIRI *)homeserver;
/**
* @brief Initializes an already allocated request with the specified access
* token and homeserver.
*
* @param accessToken An (optional) access token to use
* @param homeserver The homeserver the request will be sent to
* @return An initialized MTXRequest
*/
- (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
* succeeded or failed.
*
* @param block The block to call once the request succeeded or failed
*/
- (void)performWithBlock: (MTXRequestBlock)block;
@end
OF_ASSUME_NONNULL_END
|
︙ | | |
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
-
+
-
+
|
{
OFData *_body;
MTXRequestBlock _block;
}
+ (instancetype)requestWithPath: (OFString *)path
accessToken: (OFString *)accessToken
homeserver: (OFURL *)homeserver
homeserver: (OFIRI *)homeserver
{
return [[[self alloc] initWithPath: path
accessToken: accessToken
homeserver: homeserver] autorelease];
}
- (instancetype)initWithPath: (OFString *)path
accessToken: (OFString *)accessToken
homeserver: (OFURL *)homeserver
homeserver: (OFIRI *)homeserver
{
self = [super init];
@try {
_accessToken = [accessToken copy];
_homeserver = [homeserver copy];
_path = [path copy];
|
︙ | | |
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
-
+
-
-
-
+
+
+
-
+
|
- (void)performWithBlock: (MTXRequestBlock)block
{
void *pool = objc_autoreleasePoolPush();
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";
if (_accessToken != nil)
headers[@"Authorization"] =
[OFString stringWithFormat: @"Bearer %@", _accessToken];
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;
OFHTTPClient *client = [OFHTTPClient client];
client.delegate = self;
_block = [block copy];
|
︙ | | |