From 4eb4d6bc9cb8c9e50ee1c79aab7eee3aae8850a2 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Mon, 21 Mar 2011 15:44:42 +0100 Subject: [PATCH] Move auth and bound handling to delegate. --- src/XMPPConnection.h | 7 +++++-- src/XMPPConnection.m | 31 ++++++++++++++++++++++--------- tests/test.m | 15 +++++++++++++-- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/XMPPConnection.h b/src/XMPPConnection.h index 8ec0210..057d996 100644 --- a/src/XMPPConnection.h +++ b/src/XMPPConnection.h @@ -32,13 +32,16 @@ @protocol XMPPConnectionDelegate @optional -- (void)connectionWasClosed: (XMPPConnection*)conn; +- (void)connectionWasAuthenticated: (XMPPConnection*)conn; +- (void)connection: (XMPPConnection*)conn + wasBoundToJID: (XMPPJID*)jid; - (void)connection: (XMPPConnection*)conn didReceiveIQ: (XMPPIQ*)iq; - (void)connection: (XMPPConnection*)conn didReceivePresence: (XMPPPresence*)pres; - (void)connection: (XMPPConnection*)conn didReceiveMessage: (XMPPMessage*)msg; +- (void)connectionWasClosed: (XMPPConnection*)conn; @end /** @@ -64,7 +67,7 @@ short port; /// Whether to use TLS BOOL useTLS; - id delegate; + id delegate; XMPPAuthenticator *authModule; } diff --git a/src/XMPPConnection.m b/src/XMPPConnection.m index dbbe1e4..c551f35 100644 --- a/src/XMPPConnection.m +++ b/src/XMPPConnection.m @@ -191,7 +191,8 @@ size_t len = [sock readNBytes: 512 intoBuffer: buf]; - if (len < 1) + if (len < 1 && [delegate respondsToSelector: + @selector(connectionWasClosed:)]) [delegate connectionWasClosed: self]; [of_stdout writeNBytes: len @@ -266,7 +267,11 @@ OFXMLElement *jidElem = bindElem.children.firstObject; JID = [[XMPPJID alloc] initWithString: [jidElem.children.firstObject stringValue]]; - of_log(@"Bound to JID: %@", [JID fullJID]); + + if ([delegate respondsToSelector: + @selector(connection:wasBoundToJID:)]) + [delegate connection: self + wasBoundToJID: JID]; } } @@ -313,20 +318,25 @@ return; } - [delegate connection: self - didReceiveIQ: iq]; + if ([delegate respondsToSelector: @selector(connection:didReceiveIQ:)]) + [delegate connection: self + didReceiveIQ: iq]; } - (void)_handleMessage: (XMPPMessage*)msg { - [delegate connection: self - didReceiveMessage: msg]; + if ([delegate respondsToSelector: + @selector(connection:didReceiveMessage:)]) + [delegate connection: self + didReceiveMessage: msg]; } - (void)_handlePresence: (XMPPPresence*)pres { - [delegate connection: self - didReceivePresence: pres]; + if ([delegate respondsToSelector: + @selector(connection:didReceivePresence:)]) + [delegate connection: self + didReceivePresence: pres]; } - (void)elementBuilder: (OFXMLElementBuilder*)b @@ -408,7 +418,10 @@ [authModule parseServerFinalMessage: [OFDataArray dataArrayWithBase64EncodedString: [elem.children.firstObject stringValue]]]; - of_log(@"Auth successful"); + + if ([delegate respondsToSelector: + @selector(connectionWasAuthenticated:)]) + [delegate connectionWasAuthenticated: self]; /* Stream restart */ parser.delegate = self; diff --git a/tests/test.m b/tests/test.m index 9558730..0f2647a 100644 --- a/tests/test.m +++ b/tests/test.m @@ -105,9 +105,15 @@ OF_APPLICATION_DELEGATE(AppDelegate) } } -- (void)connectionWasClosed: (XMPPConnection*)conn +- (void)connectionWasAuthenticated: (XMPPConnection*)conn { - of_log(@"Connection was closed!"); + of_log(@"Auth successful"); +} + +- (void)connection: (XMPPConnection*)conn + wasBoundToJID: (XMPPJID*)jid +{ + of_log(@"Bound to JID: %@", [jid fullJID]); } - (void)connection: (XMPPConnection*)conn @@ -127,4 +133,9 @@ OF_APPLICATION_DELEGATE(AppDelegate) { of_log(@"Presence: %@", pres); } + +- (void)connectionWasClosed: (XMPPConnection*)conn +{ + of_log(@"Connection was closed!"); +} @end