Index: src/XMPPConnection.h ================================================================== --- src/XMPPConnection.h +++ src/XMPPConnection.h @@ -30,17 +30,20 @@ @class XMPPPresence; @class XMPPAuthenticator; @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 /** * \brief A class which abstracts a connection to an XMPP service. */ @@ -62,11 +65,11 @@ XMPPJID *JID; /// The port to connect to short port; /// Whether to use TLS BOOL useTLS; - id delegate; + id delegate; XMPPAuthenticator *authModule; } @property (copy) OFString *username; @property (copy) OFString *password; Index: src/XMPPConnection.m ================================================================== --- src/XMPPConnection.m +++ src/XMPPConnection.m @@ -189,11 +189,12 @@ for (;;) { 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 fromBuffer: buf]; [parser parseBuffer: buf @@ -264,11 +265,15 @@ if ([bindElem.name isEqual: @"bind"] && [bindElem.namespace isEqual: NS_BIND]) { 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]; } } - (void)_handleFeatures: (OFXMLElement*)elem { @@ -311,24 +316,29 @@ if ([iq.ID isEqual: @"bind0"] && [iq.type isEqual: @"result"]) { [self _handleResourceBind: iq]; 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 didBuildElement: (OFXMLElement*)elem { @@ -406,11 +416,14 @@ if ([elem.name isEqual: @"success"]) { [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; [self _startStream]; return; Index: tests/test.m ================================================================== --- tests/test.m +++ tests/test.m @@ -103,13 +103,19 @@ } @catch (id e) { of_log(@"%@", e); } } -- (void)connectionWasClosed: (XMPPConnection*)conn +- (void)connectionWasAuthenticated: (XMPPConnection*)conn +{ + of_log(@"Auth successful"); +} + +- (void)connection: (XMPPConnection*)conn + wasBoundToJID: (XMPPJID*)jid { - of_log(@"Connection was closed!"); + of_log(@"Bound to JID: %@", [jid fullJID]); } - (void)connection: (XMPPConnection*)conn didReceiveIQ: (XMPPIQ*)iq { @@ -125,6 +131,11 @@ - (void)connection: (XMPPConnection*)conn didReceivePresence: (XMPPPresence*)pres { of_log(@"Presence: %@", pres); } + +- (void)connectionWasClosed: (XMPPConnection*)conn +{ + of_log(@"Connection was closed!"); +} @end