ObjXMPP  Check-in [ffbc5cb529]

Overview
Comment:Adjust to ObjFW changes
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ffbc5cb529282a3f8920efe47dd72bbcb75a0fa2de2e4b1cf7aaf85acd3fb89f
User & Date: js on 2018-12-22 20:59:02
Other Links: manifest | tags
Context
2019-01-20
12:38
Make use of objfw-config's new package support check-in: 874510053a user: js tags: trunk
2018-12-22
20:59
Adjust to ObjFW changes check-in: ffbc5cb529 user: js tags: trunk
2018-11-11
19:13
Add MUC namespace check-in: 5a6c7fcfb8 user: js tags: trunk
Changes

Modified src/XMPPConnection.h from [4fbe933481] to [8e9ce8e74b].

25
26
27
28
29
30
31


32
33
34
35
36
37
38
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40







+
+







#import <ObjFW/ObjFW.h>

#import "XMPPCallback.h"
#import "XMPPStorage.h"

OF_ASSUME_NONNULL_BEGIN

#define XMPP_CONNECTION_BUFFER_LENGTH 512

@class XMPPConnection;
@class XMPPJID;
@class XMPPIQ;
@class XMPPMessage;
@class XMPPPresence;
@class XMPPAuthenticator;
@class SSLSocket;
106
107
108
109
110
111
112

113
114


115
116
117
118
119
120
121
108
109
110
111
112
113
114
115
116

117
118
119
120
121
122
123
124
125







+

-
+
+







-  (void)connection: (XMPPConnection *)connection
  didReceiveMessage: (XMPPMessage *)message;

/*!
 * @brief This callback is called when the connection was closed.
 *
 * @param connection The connection that was closed
 * @param error The error XML element the stream encountered or nil
 */
- (void)connectionWasClosed: (XMPPConnection *)connection;
- (void)connectionWasClosed: (XMPPConnection *)connection
		      error: (nullable OFXMLElement *)error;

/*!
 * @brief This callback is called when the connection threw an exception.
 *
 * @param connection The connection which threw an exception
 * @param exception The exception the connection threw
 */
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
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
168
169
170
171







-
+
-


+












+







 */
- (void)connectionDidUpgradeToTLS: (XMPPConnection *)connection;
@end

/*!
 * @brief A class which abstracts a connection to an XMPP service.
 */
@interface XMPPConnection: OFObject <OFXMLParserDelegate,
@interface XMPPConnection: OFObject
    OFXMLElementBuilderDelegate>
{
	OF_KINDOF(OFTCPSocket *) _socket;
	char _buffer[XMPP_CONNECTION_BUFFER_LENGTH];
	OFXMLParser *_parser, *_oldParser;
	OFXMLElementBuilder *_elementBuilder, *_oldElementBuilder;
	OFString *_Nullable _username, *_Nullable _password, *_Nullable _server;
	OFString *_Nullable _resource;
	bool _usesAnonymousAuthentication;
	OFString *_Nullable _privateKeyFile, *_Nullable _certificateFile;
	const char *_Nullable _privateKeyPassphrase;
	OFString *_Nullable _domain, *_Nullable _domainToASCII;
	XMPPJID *_Nullable _JID;
	uint16_t _port;
	id <XMPPStorage> _Nullable _dataStorage;
	OFString *_Nullable _language;
	OFMutableArray *_nextSRVRecords;
	XMPPMulticastDelegate *_delegates;
	OFMutableDictionary OF_GENERIC(OFString *, XMPPCallback *) *_callbacks;
	XMPPAuthenticator *_authModule;
	bool _streamOpen, _needsSession, _encryptionRequired, _encrypted;
	bool _supportsRosterVersioning, _supportsStreamManagement;
	unsigned int _lastID;
}

Modified src/XMPPConnection.m from [0b3550d382] to [55a67721ce].

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
52
53
54
55
56
57
58



59




60
61







62
63




64
65
66
67
68
69
70







-
-
-
+
-
-
-
-
+
+
-
-
-
-
-
-
-


-
-
-
-







#import "XMPPStanza.h"
#import "XMPPXMLElementBuilder.h"

#import "namespaces.h"

#import <ObjFW/macros.h>

#define BUFFER_LENGTH 512

@interface XMPPConnection ()
@interface XMPPConnection () <OFDNSResolverDelegate, OFTCPSocketDelegate,
- (void)xmpp_socketDidConnect: (OFTCPSocket *)socket
		      context: (OFArray *)nextSRVRecords
		    exception: (id)exception;
- (void)xmpp_tryNextSRVRecord: (OFArray *)SRVRecords;
    OFXMLParserDelegate, OFXMLElementBuilderDelegate>
- (void)xmpp_tryNextSRVRecord;
-  (void)xmpp_resolver: (OFDNSResolver *)resolver
  didResolveDomainName: (OFString *)domainName
	 answerRecords: (OFDictionary *)answerRecords
      authorityRecords: (OFDictionary *)authorityRecords
     additionalRecords: (OFDictionary *)additionalRecords
	       context: (OFString *)domainToASCII
	     exception: (id)exception;
-  (bool)xmpp_parseBuffer: (const void *)buffer
		   length: (size_t)length;
- (bool)xmpp_stream: (OFStream *)stream
  didReadIntoBuffer: (char *)buffer
	     length: (size_t)length
	  exception: (OFException *)exception;
- (void)xmpp_startStream;
- (void)xmpp_handleStanza: (OFXMLElement *)element;
- (void)xmpp_handleStream: (OFXMLElement *)element;
- (void)xmpp_handleTLS: (OFXMLElement *)element;
- (void)xmpp_handleSASL: (OFXMLElement *)element;
- (void)xmpp_handleIQ: (XMPPIQ *)IQ;
- (void)xmpp_handleMessage: (XMPPMessage *)message;
138
139
140
141
142
143
144

145
146
147
148
149
150
151
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137







+







	[_password release];
	[_privateKeyFile release];
	[_certificateFile release];
	[_server release];
	[_domain release];
	[_resource release];
	[_JID release];
	[_nextSRVRecords release];
	[_delegates release];
	[_callbacks release];
	[_authModule release];

	[super dealloc];
}

267
268
269
270
271
272
273
274
275
276




277
278
279
280
281
282


283
284
285
286
287
288
289




290
291
292
293
294
295
296
297


298
299
300
301
302
303
304

305
306


307
308
309
310
311
312





313
314

315
316
317
318
319
320
321

322
323
324
325
326
327
328
329
330
331
332
333
253
254
255
256
257
258
259



260
261
262
263
264


265


266
267
268
269
270




271
272
273
274
275
276
277
278
279



280
281




282
283

284
285

286
287
288





289
290
291
292
293
294

295




296
297

298
299
300
301
302

303
304
305
306
307
308
309







-
-
-
+
+
+
+

-
-

-
-
+
+



-
-
-
-
+
+
+
+





-
-
-
+
+
-
-
-
-


-
+

-
+
+

-
-
-
-
-
+
+
+
+
+

-
+
-
-
-
-


-
+




-







		}
	} else
		_password = nil;

	[old release];
}

- (void)xmpp_socketDidConnect: (OFTCPSocket *)socket
		      context: (OFArray *)nextSRVRecords
		    exception: (id)exception
-     (void)socket: (OF_KINDOF(OFTCPSocket *))sock
  didConnectToHost: (OFString *)host
	      port: (uint16_t)port
	 exception: (id)exception
{
	char *buffer;

	if (exception != nil) {
		if (nextSRVRecords != nil) {
			[self xmpp_tryNextSRVRecord: nextSRVRecords];
		if ([_nextSRVRecords count] > 0) {
			[self xmpp_tryNextSRVRecord];
			return;
		}

		[_delegates
		    broadcastSelector: @selector(connection:didThrowException:)
			   withObject: self
			   withObject: exception];
		[_delegates broadcastSelector: @selector(connection:
						   didThrowException:)
				   withObject: self
				   withObject: exception];
		return;
	}

	[self xmpp_startStream];

	buffer = [self allocMemoryWithSize: BUFFER_LENGTH];
	[_socket asyncReadIntoBuffer: buffer
			      length: BUFFER_LENGTH
	[_socket asyncReadIntoBuffer: _buffer
			      length: XMPP_CONNECTION_BUFFER_LENGTH];
			      target: self
			    selector: @selector(xmpp_stream:didReadIntoBuffer:
					  length:exception:)
			     context: nil];
}

- (void)xmpp_tryNextSRVRecord: (OFArray *)SRVRecords
- (void)xmpp_tryNextSRVRecord
{
	OFSRVDNSResourceRecord *record = [SRVRecords objectAtIndex: 0];
	OFSRVDNSResourceRecord *record =
	    [[[_nextSRVRecords objectAtIndex: 0] copy] autorelease];

	SRVRecords = [SRVRecords objectsInRange:
	    of_range(1, [SRVRecords count] - 1)];
	if ([SRVRecords count] == 0)
		SRVRecords = nil;

	if ([_nextSRVRecords count] == 0) {
		[_nextSRVRecords release];
		_nextSRVRecords = nil;
	}

	[_socket asyncConnectToHost: [record target]
			       port: [record port]
			       port: [record port]];
			     target: self
			   selector: @selector(xmpp_socketDidConnect:
					 context:exception:)
			    context: SRVRecords];
}

-  (void)xmpp_resolver: (OFDNSResolver *)resolver
-	(void)resolver: (OFDNSResolver *)resolver
  didResolveDomainName: (OFString *)domainName
	 answerRecords: (OFDictionary *)answerRecords
      authorityRecords: (OFDictionary *)authorityRecords
     additionalRecords: (OFDictionary *)additionalRecords
	       context: (OFString *)domainToASCII
	     exception: (id)exception
{
	OFMutableArray *records = [OFMutableArray array];

	if (exception != nil) {
		[_delegates
		    broadcastSelector: @selector(connection:didThrowException:)
341
342
343
344
345
346
347
348
349
350



351
352
353
354
355
356
357



358

359
360
361
362
363
364
365
366
367
368

369
370
371
372

373
374
375
376
377
378

379
380

381
382
383
384
385
386
387
388
389
390
391
392
393
394
395




396
397
398
399
400
401
402
317
318
319
320
321
322
323



324
325
326




327
328
329
330
331
332

333
334
335
336
337
338
339
340
341
342
343
344
345
346
347

348




349

350


351





352
353
354
355
356
357
358
359


360
361
362
363
364
365
366
367
368
369
370







-
-
-
+
+
+
-
-
-
-



+
+
+
-
+










+



-
+
-
-
-
-

-
+
-
-
+
-
-
-
-
-








-
-
+
+
+
+







		if ([record isKindOfClass: [OFSRVDNSResourceRecord class]])
		       [records addObject: record];

	/* TODO: Sort records */
	[records makeImmutable];

	if ([records count] == 0) {
		/* Fall back to A / AAA record. */
		[_socket asyncConnectToHost: domainToASCII
				       port: _port
		/* Fall back to A / AAAA record. */
		[_socket asyncConnectToHost: _domainToASCII
				       port: _port];
				     target: self
				   selector: @selector(xmpp_socketDidConnect:
						 context:exception:)
				    context: nil];
		return;
	}

	[_nextSRVRecords release];
	_nextSRVRecords = nil;
	_nextSRVRecords = [records mutableCopy];
	[self xmpp_tryNextSRVRecord: records];
	[self xmpp_tryNextSRVRecord];
}

- (void)asyncConnect
{
	void *pool = objc_autoreleasePoolPush();

	if (_socket != nil)
		@throw [OFAlreadyConnectedException exception];

	_socket = [[OFTCPSocket alloc] init];
	[_socket setDelegate: self];

	if (_server != nil)
		[_socket asyncConnectToHost: _server
				       port: _port
				       port: _port];
				     target: self
				   selector: @selector(xmpp_socketDidConnect:
						 context:exception:)
				    context: nil];
	else
		[[OFThread DNSResolver]
		[[OFThread DNSResolver] asyncResolveHost: _domainToASCII
		    asyncResolveHost: _domainToASCII
			      target: self
						delegate: self];
			    selector: @selector(xmpp_resolver:
					  didResolveDomainName:answerRecords:
					  authorityRecords:additionalRecords:
					  context:exception:)
			     context: _domainToASCII];

	objc_autoreleasePoolPop(pool);
}

-  (bool)xmpp_parseBuffer: (const void *)buffer
		   length: (size_t)length
{
	if ([_socket isAtEndOfStream]) {
		[_delegates broadcastSelector: @selector(connectionWasClosed:)
				   withObject: self];
		[_delegates broadcastSelector: @selector(connectionWasClosed:
						   error:)
				   withObject: self
				   withObject: nil];
		return false;
	}

	@try {
		[_parser parseBuffer: buffer
			     length: length];
	} @catch (OFMalformedXMLException *e) {
418
419
420
421
422
423
424
425
426


427
428

429
430
431
432
433
434
435
386
387
388
389
390
391
392


393
394
395

396
397
398
399
400
401
402
403







-
-
+
+

-
+







	[_oldParser release];
	[_oldElementBuilder release];

	_oldParser = nil;
	_oldElementBuilder = nil;
}

- (bool)xmpp_stream: (OFStream *)stream
  didReadIntoBuffer: (char *)buffer
-      (bool)stream: (OF_KINDOF(OFStream *))stream
  didReadIntoBuffer: (void *)buffer
	     length: (size_t)length
	  exception: (OFException *)exception
	  exception: (id)exception
{
	if (exception != nil) {
		[_delegates broadcastSelector: @selector(connection:
						   didThrowException:)
				   withObject: self
				   withObject: exception];
		[self close];
452
453
454
455
456
457
458
459
460


461
462
463
464
465
466
467
468
469
470
471
472
473
420
421
422
423
424
425
426


427
428






429
430
431
432
433
434
435







-
-
+
+
-
-
-
-
-
-







	if (_oldParser != nil || _oldElementBuilder != nil) {
		[_oldParser release];
		[_oldElementBuilder release];

		_oldParser = nil;
		_oldElementBuilder = nil;

		[_socket asyncReadIntoBuffer: buffer
				      length: BUFFER_LENGTH
		[_socket asyncReadIntoBuffer: _buffer
				      length: XMPP_CONNECTION_BUFFER_LENGTH];
				      target: self
				    selector: @selector(xmpp_stream:
						  didReadIntoBuffer:length:
						  exception:)
				     context: nil];

		return false;
	}

	return true;
}

- (bool)streamOpen
582
583
584
585
586
587
588
589

590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605

606
607
608
609
610
611
612
544
545
546
547
548
549
550

551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566

567
568
569
570
571
572
573
574







-
+















-
+







{
	return [OFString stringWithFormat: @"objxmpp_%u", _lastID++];
}

-    (void)parser: (OFXMLParser *)parser
  didStartElement: (OFString *)name
	   prefix: (OFString *)prefix
	namespace: (OFString *)NS
	namespace: (OFString *)namespace
       attributes: (OFArray *)attributes
{
	if (![name isEqual: @"stream"]) {
		// No dedicated stream error for this, may not even be XMPP
		[self close];
		[_socket close];
		return;
	}

	if (![prefix isEqual: @"stream"]) {
		[self xmpp_sendStreamError: @"bad-namespace-prefix"
				      text: nil];
		return;
	}

	if (![NS isEqual: XMPP_NS_STREAM]) {
	if (![namespace isEqual: XMPP_NS_STREAM]) {
		[self xmpp_sendStreamError: @"invalid-namespace"
				      text: nil];
		return;
	}

	for (OFXMLAttribute *attribute in attributes) {
		if ([[attribute name] isEqual: @"from"] &&
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771

772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807




808
809
810


811
812
813

814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
713
714
715
716
717
718
719

720












721




































722
723
724
725



726
727



728






















729
730
731
732
733
734
735







-

-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
-
-
-
+
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







		[self xmpp_handleFeatures: element];
		return;
	}

	if ([[element name] isEqual: @"error"]) {
		OFString *condition, *reason;
		[self close];
		[_socket close]; // Remote has already closed his stream

		if ([element elementForName: @"bad-format"
				  namespace: XMPP_NS_XMPP_STREAM])
			condition = @"bad-format";
		else if ([element elementForName: @"bad-namespace-prefix"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"bad-namespace-prefix";
		else if ([element elementForName: @"conflict"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"conflict";
		else if ([element elementForName: @"connection-timeout"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"connection-timeout";
		[_delegates broadcastSelector: @selector(connectionWasClosed:)
		else if ([element elementForName: @"host-gone"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"host-gone";
		else if ([element elementForName: @"host-unknown"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"host-unknown";
		else if ([element elementForName: @"improper-addressing"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"improper-addressing";
		else if ([element elementForName: @"internal-server-error"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"internal-server-error";
		else if ([element elementForName: @"invalid-from"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"invalid-from";
		else if ([element elementForName: @"invalid-namespace"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"invalid-namespace";
		else if ([element elementForName: @"invalid-xml"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"invalid-xml";
		else if ([element elementForName: @"not-authorized"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"not-authorized";
		else if ([element elementForName: @"not-well-formed"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"not-well-formed";
		else if ([element elementForName: @"policy-violation"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"policy-violation";
		else if ([element elementForName: @"remote-connection-failed"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"remote-connection-failed";
		else if ([element elementForName: @"reset"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"reset";
				   withObject: self
				   withObject: element];

		condition = [[[element elementsForNamespace:
		else if ([element elementForName: @"resource-constraint"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"resource-constraint";
		    XMPP_NS_XMPP_STREAM] firstObject] name];

		else if ([element elementForName: @"restricted-xml"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"restricted-xml";
		if (condition == nil)
		else if ([element elementForName: @"see-other-host"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"see-other-host";
		else if ([element elementForName: @"system-shutdown"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"system-shutdown";
		else if ([element elementForName: @"undefined-condition"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"undefined-condition";
		else if ([element elementForName: @"unsupported-encoding"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"unsupported-encoding";
		else if ([element elementForName: @"unsupported-feature"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"unsupported-feature";
		else if ([element elementForName: @"unsupported-stanza-type"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"unsupported-stanza-type";
		else if ([element elementForName: @"unsupported-version"
				       namespace: XMPP_NS_XMPP_STREAM])
			condition = @"unsupported-version";
		else
			condition = @"undefined";

		reason = [[element
		    elementForName: @"text"
			 namespace: XMPP_NS_XMPP_STREAM] stringValue];

		@throw [XMPPStreamErrorException
866
867
868
869
870
871
872

873
874
875
876
877
878
879
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773







+







		[newSock setCertificateFile: _certificateFile];
		[newSock setPrivateKeyFile: _privateKeyFile];
		[newSock setPrivateKeyPassphrase: _privateKeyPassphrase];
#endif
		[newSock startTLSWithExpectedHost: nil];
		[_socket release];
		_socket = newSock;
		[_socket setDelegate: self];

		_encrypted = true;

		[_delegates broadcastSelector: @selector(
						   connectionDidUpgradeToTLS:)
				   withObject: self];

Modified tests/test.m from [654249e42f] to [d958b131ca].

265
266
267
268
269
270
271

272
273

274
275
265
266
267
268
269
270
271
272
273

274
275
276







+

-
+


-  (void)connection: (XMPPConnection *)conn
  didThrowException: (id)e
{
	@throw e;
}

- (void)connectionWasClosed: (XMPPConnection *)conn
		      error: (OFXMLElement *)error
{
	of_log(@"Connection was closed!");
	of_log(@"Connection was closed: %@", error);
}
@end