ObjXMPP  Diff

Differences From Artifact [677aa509d2]:

To Artifact [17919bf855]:


1
2
3
4
5
6
7
8
9
/*
 * Copyright (c) 2010, 2011, Jonathan Schleifer <js@webkeks.org>
 * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
 *
 * https://webkeks.org/hg/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.

|







1
2
3
4
5
6
7
8
9
/*
 * Copyright (c) 2010, 2011, 2012, Jonathan Schleifer <js@webkeks.org>
 * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
 *
 * https://webkeks.org/hg/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
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
#import "XMPPStanza.h"
#import "XMPPJID.h"
#import "XMPPIQ.h"
#import "XMPPMessage.h"
#import "XMPPPresence.h"
#import "XMPPRoster.h"
#import "XMPPRosterItem.h"

#import "XMPPExceptions.h"
#import "namespaces.h"

@implementation XMPPConnection
+ connection
{
	return [[[self alloc] init] autorelease];
}

- init
{
	self = [super init];

	@try {
		sock = [[OFTCPSocket alloc] init];
		port = 5222;
		encrypted = NO;
		streamOpen = NO;

		callbacks = [[OFMutableDictionary alloc] init];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;







>


















>







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
78
#import "XMPPStanza.h"
#import "XMPPJID.h"
#import "XMPPIQ.h"
#import "XMPPMessage.h"
#import "XMPPPresence.h"
#import "XMPPRoster.h"
#import "XMPPRosterItem.h"
#import "XMPPMulticastDelegate.h"
#import "XMPPExceptions.h"
#import "namespaces.h"

@implementation XMPPConnection
+ connection
{
	return [[[self alloc] init] autorelease];
}

- init
{
	self = [super init];

	@try {
		sock = [[OFTCPSocket alloc] init];
		port = 5222;
		encrypted = NO;
		streamOpen = NO;
		delegates = [[XMPPMulticastDelegate alloc] init];
		callbacks = [[OFMutableDictionary alloc] init];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
85
86
87
88
89
90
91

92
93
94
95
96
97
98
	[password release];
	[privateKeyFile release];
	[certificateFile release];
	[server release];
	[domain release];
	[resource release];
	[JID release];

	[callbacks release];
	[authModule release];
	[roster release];

	[super dealloc];
}








>







87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
	[password release];
	[privateKeyFile release];
	[certificateFile release];
	[server release];
	[domain release];
	[resource release];
	[JID release];
	[delegates release];
	[callbacks release];
	[authModule release];
	[roster release];

	[super dealloc];
}

300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
			return;
	}
}

- (void)parseBuffer: (const char*)buffer
	 withLength: (size_t)length
{
	if (length < 1 && [delegate respondsToSelector:
	    @selector(connectionWasClosed:)]) {
		[delegate connectionWasClosed: self];
		return;
	}

	[parser parseBuffer: buffer
		 withLength: length];

	[oldParser release];







|
|
|







303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
			return;
	}
}

- (void)parseBuffer: (const char*)buffer
	 withLength: (size_t)length
{
	if (length < 1) {
		[delegates broadcastSelector: @selector(connectionWasClosed:)
			       forConnection: self];
		return;
	}

	[parser parseBuffer: buffer
		 withLength: length];

	[oldParser release];
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
403
404
405
406
407
408
409
410

411
412
413
414


415
416
417
418
419
420
421
422
423
424
425
426
427
		@throw [SSLInvalidCertificateException
		    exceptionWithClass: isa
				reason: @"No matching identifier"];
}

- (void)sendStanza: (OFXMLElement*)element
{
	if ([delegate respondsToSelector:
	    @selector(connection:didSendElement:)])
		[delegate connection: self
		      didSendElement: element];

	[sock writeString: [element XMLString]];
}

-	(void)sendIQ: (XMPPIQ*)iq
  withCallbackObject: (id)object
	    selector: (SEL)selector
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

	@try {
		if (![iq ID])
			[iq setID: [self generateStanzaID]];

		[callbacks setObject: [XMPPCallback

					 callbackWithCallbackObject: object
							   selector: selector]

			      forKey: [iq ID]];
	} @finally {
		[pool release];
	}

	[self sendStanza: iq];
}

#ifdef OF_HAVE_BLOCKS
-      (void)sendIQ: (XMPPIQ*)iq
  withCallbackBlock: (xmpp_callback_block_t)callback;
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

	@try {
		if (![iq ID])
			[iq setID: [self generateStanzaID]];



		[callbacks setObject: [XMPPCallback
					  callbackWithCallbackBlock: callback]
			      forKey: [iq ID]];
	} @finally {
		[pool release];
	}

	[self sendStanza: iq];
}
#endif

- (OFString*)generateStanzaID
{







<
|
|
|








|
>
|
|
|

<
>
|
|
>
|
<
|
|
<





|

|
>
|
|
|

>
>
|
<
|
<
|
<







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

403
404

405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420

421

422

423
424
425
426
427
428
429
		@throw [SSLInvalidCertificateException
		    exceptionWithClass: isa
				reason: @"No matching identifier"];
}

- (void)sendStanza: (OFXMLElement*)element
{

	[delegates broadcastSelector: @selector(connection:didSendElement:)
		       forConnection: self
			  withObject: element];

	[sock writeString: [element XMLString]];
}

-	(void)sendIQ: (XMPPIQ*)iq
  withCallbackObject: (id)object
	    selector: (SEL)selector
{
	OFAutoreleasePool *pool;
	XMPPCallback *callback;

	if (![iq ID])
		[iq setID: [self generateStanzaID]];


	pool = [[OFAutoreleasePool alloc] init];
	callback = [XMPPCallback callbackWithCallbackObject: object
						   selector: selector];
	[callbacks setObject: callback
		      forKey: [iq ID]];

	[pool release];


	[self sendStanza: iq];
}

#ifdef OF_HAVE_BLOCKS
-      (void)sendIQ: (XMPPIQ*)iq
  withCallbackBlock: (xmpp_callback_block_t)block;
{
	OFAutoreleasePool *pool;
	XMPPCallback *callback;

	if (![iq ID])
		[iq setID: [self generateStanzaID]];

	pool = [[OFAutoreleasePool alloc] init];
	callback = [XMPPCallback callbackWithCallbackBlock: block];
	[callbacks setObject: callback

		      forKey: [iq ID]];

	[pool release];


	[self sendStanza: iq];
}
#endif

- (OFString*)generateStanzaID
{
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
	if ([element name] == nil)
		return;

	[element setDefaultNamespace: XMPP_NS_CLIENT];
	[element setPrefix: @"stream"
	      forNamespace: XMPP_NS_STREAM];

	if ([delegate respondsToSelector:
	    @selector(connection:didReceiveElement:)])
		[delegate connection: self
		   didReceiveElement: element];

	if ([[element namespace] isEqual: XMPP_NS_CLIENT])
		[self XMPP_handleStanza: element];

	if ([[element namespace] isEqual: XMPP_NS_STREAM])
		[self XMPP_handleStream: element];








<
|
|
|







484
485
486
487
488
489
490

491
492
493
494
495
496
497
498
499
500
	if ([element name] == nil)
		return;

	[element setDefaultNamespace: XMPP_NS_CLIENT];
	[element setPrefix: @"stream"
	      forNamespace: XMPP_NS_STREAM];


	[delegates broadcastSelector: @selector(connection:didReceiveElement:)
		       forConnection: self
			  withObject: element];

	if ([[element namespace] isEqual: XMPP_NS_CLIENT])
		[self XMPP_handleStanza: element];

	if ([[element namespace] isEqual: XMPP_NS_STREAM])
		[self XMPP_handleStream: element];

688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716

- (void)XMPP_handleTLS: (OFXMLElement*)element
{
	if ([[element name] isEqual: @"proceed"]) {
		/* FIXME: Catch errors here */
		SSLSocket *newSock;

		if ([delegate respondsToSelector:
		    @selector(connectionWillUpgradeToTLS:)])
			[delegate connectionWillUpgradeToTLS: self];

		newSock = [[SSLSocket alloc] initWithSocket: sock
					     privateKeyFile: privateKeyFile
					    certificateFile: certificateFile];
		[sock release];
		sock = newSock;

		encrypted = YES;

		if ([delegate respondsToSelector:
		    @selector(connectionDidUpgradeToTLS:)])
			[delegate connectionDidUpgradeToTLS: self];

		/* Stream restart */
		[self XMPP_startStream];

		return;
	}








|
|
|









|
|
|







689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717

- (void)XMPP_handleTLS: (OFXMLElement*)element
{
	if ([[element name] isEqual: @"proceed"]) {
		/* FIXME: Catch errors here */
		SSLSocket *newSock;

		[delegates broadcastSelector: @selector(
						  connectionWillUpgradeToTLS:)
			       forConnection: self];

		newSock = [[SSLSocket alloc] initWithSocket: sock
					     privateKeyFile: privateKeyFile
					    certificateFile: certificateFile];
		[sock release];
		sock = newSock;

		encrypted = YES;

		[delegates broadcastSelector: @selector(
						  connectionDidUpgradeToTLS:)
			       forConnection: self];

		/* Stream restart */
		[self XMPP_startStream];

		return;
	}

744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
		return;
	}

	if ([[element name] isEqual: @"success"]) {
		[authModule continueWithData: [OFDataArray
		    dataArrayWithBase64EncodedString: [element stringValue]]];

		if ([delegate respondsToSelector:
		    @selector(connectionWasAuthenticated:)])
			[delegate connectionWasAuthenticated: self];

		/* Stream restart */
		[self XMPP_startStream];

		return;
	}








|
|
|







745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
		return;
	}

	if ([[element name] isEqual: @"success"]) {
		[authModule continueWithData: [OFDataArray
		    dataArrayWithBase64EncodedString: [element stringValue]]];

		[delegates broadcastSelector: @selector(
						  connectionWWasAuthenticated:)
			       forConnection: self];

		/* Stream restart */
		[self XMPP_startStream];

		return;
	}

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
	}

	if ([iq elementForName: @"query"
		     namespace: XMPP_NS_ROSTER])
		if ([roster handleIQ: iq])
			return;


	if ([delegate respondsToSelector: @selector(connection:didReceiveIQ:)])
		handled = [delegate connection: self
				  didReceiveIQ: iq];

	if (!handled && ![[iq type] isEqual: @"error"] &&
	    ![[iq type] isEqual: @"result"]) {
		[self sendStanza: [iq errorIQWithType: @"cancel"
					    condition: @"service-unavailable"]];
	}
}

- (void)XMPP_handleMessage: (XMPPMessage*)message
{
	if ([delegate respondsToSelector:
	     @selector(connection:didReceiveMessage:)])
		[delegate connection: self
		   didReceiveMessage: message];
}

- (void)XMPP_handlePresence: (XMPPPresence*)presence
{
	if ([delegate respondsToSelector:
	     @selector(connection:didReceivePresence:)])
		[delegate connection: self
		  didReceivePresence: presence];
}

- (void)XMPP_handleFeatures: (OFXMLElement*)element
{
	OFXMLElement *starttls = [element elementForName: @"starttls"
					       namespace: XMPP_NS_STARTTLS];
	OFXMLElement *bind = [element elementForName: @"bind"







>
|
|
|










<
|
|
|




<
|
|
|







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
	}

	if ([iq elementForName: @"query"
		     namespace: XMPP_NS_ROSTER])
		if ([roster handleIQ: iq])
			return;

	handled = [delegates broadcastSelector: @selector(
						    connection:didReceiveIQ:)
				 forConnection: self
				    withObject: iq];

	if (!handled && ![[iq type] isEqual: @"error"] &&
	    ![[iq type] isEqual: @"result"]) {
		[self sendStanza: [iq errorIQWithType: @"cancel"
					    condition: @"service-unavailable"]];
	}
}

- (void)XMPP_handleMessage: (XMPPMessage*)message
{

	[delegates broadcastSelector: @selector(connection:didReceiveMessage:)
		       forConnection: self
			  withObject: message];
}

- (void)XMPP_handlePresence: (XMPPPresence*)presence
{

	[delegates broadcastSelector: @selector(connection:didReceivePresence:)
		       forConnection: self
			  withObject: presence];
}

- (void)XMPP_handleFeatures: (OFXMLElement*)element
{
	OFXMLElement *starttls = [element elementForName: @"starttls"
					       namespace: XMPP_NS_STARTTLS];
	OFXMLElement *bind = [element elementForName: @"bind"
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
	JID = [[XMPPJID alloc] initWithString: [jidElement stringValue]];

	if (needsSession) {
		[self XMPP_sendSession];
		return;
	}

	if ([delegate respondsToSelector: @selector(connection:wasBoundToJID:)])
		[delegate connection: self
		       wasBoundToJID: JID];
}

- (void)XMPP_sendSession
{
	XMPPIQ *iq;

	iq = [XMPPIQ IQWithType: @"set"
			     ID: [self generateStanzaID]];
	[iq addChild: [OFXMLElement elementWithName: @"session"
					  namespace: XMPP_NS_SESSION]];
	[self		sendIQ: iq
	    withCallbackObject: self
		      selector: @selector(XMPP_handleSession:)];
}

- (void)XMPP_handleSession: (XMPPIQ*)iq
{
	if (![[iq type] isEqual: @"result"])
		assert(0);

	if ([delegate respondsToSelector: @selector(connection:wasBoundToJID:)])
		[delegate connection: self
		       wasBoundToJID: JID];
}

- (OFString*)XMPP_IDNAToASCII: (OFString*)domain_
{
	OFString *ret;
	char *cDomain;
	Idna_rc rc;







|
|
|




















|
|
|







975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
	JID = [[XMPPJID alloc] initWithString: [jidElement stringValue]];

	if (needsSession) {
		[self XMPP_sendSession];
		return;
	}

	[delegates broadcastSelector: @selector(connection:wasBoundToJID:)
		       forConnection: self
			  withObject: JID];
}

- (void)XMPP_sendSession
{
	XMPPIQ *iq;

	iq = [XMPPIQ IQWithType: @"set"
			     ID: [self generateStanzaID]];
	[iq addChild: [OFXMLElement elementWithName: @"session"
					  namespace: XMPP_NS_SESSION]];
	[self		sendIQ: iq
	    withCallbackObject: self
		      selector: @selector(XMPP_handleSession:)];
}

- (void)XMPP_handleSession: (XMPPIQ*)iq
{
	if (![[iq type] isEqual: @"result"])
		assert(0);

	[delegates broadcastSelector: @selector(connection:wasBoundToJID:)
		       forConnection: self
			  withObject: JID];
}

- (OFString*)XMPP_IDNAToASCII: (OFString*)domain_
{
	OFString *ret;
	char *cDomain;
	Idna_rc rc;
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054





1055
1056
1057
1058
1059
1060
1061
1062
}

- (uint16_t)port
{
	return port;
}

- (void)setDelegate: (id <XMPPConnectionDelegate>)delegate_
{
	delegate = (id <XMPPConnectionDelegate, OFObject>)delegate_;
}

- (id <XMPPConnectionDelegate>)delegate
{





	return delegate;
}

- (XMPPRoster*)roster
{
	return [[roster retain] autorelease];
}
@end







|

|


|

>
>
>
>
>
|







1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
}

- (uint16_t)port
{
	return port;
}

- (void)addDelegate: (id <XMPPConnectionDelegate>)delegate
{
	[delegates addDelegate: delegate];
}

- (void)removeDelegate: (id <XMPPConnectionDelegate>)delegate
{
	[delegates removeDelegate: delegate];
}

- (XMPPMulticastDelegate*)XMPP_delegates
{
	return delegates;
}

- (XMPPRoster*)roster
{
	return [[roster retain] autorelease];
}
@end