Index: src/XMPPConnection.h ================================================================== --- src/XMPPConnection.h +++ src/XMPPConnection.h @@ -77,10 +77,11 @@ XMPPJID *JID; uint16_t port; id delegate; OFMutableDictionary *callbacks; XMPPAuthenticator *authModule; + BOOL streamOpen; BOOL needsSession; BOOL encryptionRequired, encrypted; unsigned int lastID; XMPPRoster *roster; } @@ -92,10 +93,11 @@ @property (assign) id delegate; @property (readonly, retain) XMPPRoster *roster; @property (readonly, retain, getter=socket) OFTCPSocket *sock; @property (assign) BOOL encryptionRequired; @property (readonly) BOOL encrypted; +@property (readonly) BOOL streamOpen; #endif /** * \return A new autoreleased XMPPConnection */ @@ -104,10 +106,15 @@ /** * Connects to the XMPP service. */ - (void)connect; +/** + * Closes the stream to the XMPP service + */ +- (void)close; + /** * Checks the certificate presented by the server. * Throws SSLInvalidCertificateException on failure. */ - (void)checkCertificate; Index: src/XMPPConnection.m ================================================================== --- src/XMPPConnection.m +++ src/XMPPConnection.m @@ -63,10 +63,11 @@ @try { sock = [[OFTCPSocket alloc] init]; port = 5222; encrypted = NO; + streamOpen = NO; callbacks = [[OFMutableDictionary alloc] init]; } @catch (id e) { [self release]; @throw e; } @@ -313,10 +314,15 @@ - (BOOL)encrypted { return encrypted; } + +- (BOOL)streamOpen +{ + return streamOpen; +} - (void)checkCertificate { X509Certificate *cert; OFDictionary *SANs; @@ -459,14 +465,16 @@ didNotExpectCloseTag: (OFString *)name withPrefix: (OFString *)prefix namespace: (OFString *)ns { if (![name isEqual: @"stream"] || ![prefix isEqual: @"stream"] || - ![ns isEqual: XMPP_NS_STREAM]) { + ![ns isEqual: XMPP_NS_STREAM]) @throw [OFMalformedXMLException exceptionWithClass: [builder class] parser: nil]; + else { + [self close]; } } - (void)XMPP_startStream { @@ -494,10 +502,19 @@ [sock writeFormat: @"\n" @"", domain]; + streamOpen = YES; +} + +- (void)close +{ + if (streamOpen) { + [sock writeString: @""]; + streamOpen = NO; + } } - (void)XMPP_handleStanza: (OFXMLElement*)element { if ([[element name] isEqual: @"iq"]) { @@ -528,13 +545,12 @@ return; } if ([[element name] isEqual: @"error"]) { OFString *condition, *reason; - [parser setDelegate: self]; - [sock writeString: @""]; - [sock close]; + [self close]; + [sock 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"