Index: src/XMPPConnection.h ================================================================== --- src/XMPPConnection.h +++ src/XMPPConnection.h @@ -161,10 +161,13 @@ - (void)setDelegate: (id )delegate; - (id )delegate; - (XMPPRoster*)roster; - (void)XMPP_startStream; +- (void)XMPP_handleStream: (OFXMLElement*)elem; +- (void)XMPP_handleTLS: (OFXMLElement*)elem; +- (void)XMPP_handleSASL: (OFXMLElement*)elem; - (void)XMPP_handleStanza: (OFXMLElement*)elem; - (void)XMPP_sendAuth: (OFString*)name; - (void)XMPP_handleIQ: (XMPPIQ*)iq; - (void)XMPP_handleMessage: (XMPPMessage*)msg; - (void)XMPP_handlePresence: (XMPPPresence*)pres; Index: src/XMPPConnection.m ================================================================== --- src/XMPPConnection.m +++ src/XMPPConnection.m @@ -295,11 +295,21 @@ [elem setPrefix: @"stream" forNamespace: XMPP_NS_STREAM]; of_log(@"In: %@", elem); - [self XMPP_handleStanza: elem]; + if ([[elem namespace] isEqual: XMPP_NS_CLIENT]) + [self XMPP_handleStanza: elem]; + + if ([[elem namespace] isEqual: XMPP_NS_STREAM]) + [self XMPP_handleStream: elem]; + + if ([[elem namespace] isEqual: XMPP_NS_STARTTLS]) + [self XMPP_handleTLS: elem]; + + if ([[elem namespace] isEqual: XMPP_NS_SASL]) + [self XMPP_handleSASL: elem]; [pool release]; } - (void)XMPP_startStream @@ -311,115 +321,115 @@ @"version='1.0'>", server]; } - (void)XMPP_handleStanza: (OFXMLElement*)elem { - if ([[elem namespace] isEqual: XMPP_NS_CLIENT]) { - if ([[elem name] isEqual: @"iq"]) { - [self XMPP_handleIQ: [XMPPIQ stanzaWithElement: elem]]; - return; - } - - if ([[elem name] isEqual: @"message"]) { - [self XMPP_handleMessage: - [XMPPMessage stanzaWithElement: elem]]; - return; - } - - if ([[elem name] isEqual: @"presence"]) { - [self XMPP_handlePresence: - [XMPPPresence stanzaWithElement: elem]]; - return; - } - - assert(0); - } - - if ([[elem namespace] isEqual: XMPP_NS_STREAM]) { - if ([[elem name] isEqual: @"features"]) { - [self XMPP_handleFeatures: elem]; - return; - } - - assert(0); - } - - if ([[elem namespace] isEqual: XMPP_NS_STARTTLS]) { - if ([[elem name] isEqual: @"proceed"]) { - /* FIXME: Catch errors here */ - SSLSocket *newSock; - - if ([delegate respondsToSelector: - @selector(connectionWillUpgradeToTLS:)]) - [delegate connectionWillUpgradeToTLS: self]; - - newSock = [[SSLSocket alloc] initWithSocket: sock]; - [sock release]; - sock = newSock; - - if ([delegate respondsToSelector: - @selector(connectionDidUpgradeToTLS:)]) - [delegate connectionDidUpgradeToTLS: self]; - - /* Stream restart */ - [parser setDelegate: self]; - [self XMPP_startStream]; - return; - } - - if ([[elem name] isEqual: @"failure"]) - /* TODO: Find/create an exception to throw here */ - @throw [OFException newWithClass: isa]; - - assert(0); - } - - if ([[elem namespace] isEqual: XMPP_NS_SASL]) { - if ([[elem name] isEqual: @"challenge"]) { - OFXMLElement *responseTag; - OFDataArray *challenge = - [OFDataArray dataArrayWithBase64EncodedString: - [elem stringValue]]; - OFDataArray *response = [authModule - calculateResponseWithChallenge: challenge]; - - responseTag = [OFXMLElement - elementWithName: @"response" - namespace: XMPP_NS_SASL]; - [responseTag addChild: - [OFXMLElement elementWithCharacters: - [response stringByBase64Encoding]]]; - - [self sendStanza: responseTag]; - return; - } - - if ([[elem name] isEqual: @"success"]) { - [authModule parseServerFinalMessage: - [OFDataArray dataArrayWithBase64EncodedString: - [elem stringValue]]]; - - if ([delegate respondsToSelector: - @selector(connectionWasAuthenticated:)]) - [delegate connectionWasAuthenticated: self]; - - /* Stream restart */ - [parser setDelegate: self]; - [self XMPP_startStream]; - return; - } - - if ([[elem name] isEqual: @"failure"]) { - of_log(@"Auth failed!"); - // FIXME: Do more parsing/handling - @throw [XMPPAuthFailedException - newWithClass: isa - connection: self - reason: [elem XMLString]]; - } - - assert(0); + if ([[elem name] isEqual: @"iq"]) { + [self XMPP_handleIQ: [XMPPIQ stanzaWithElement: elem]]; + return; + } + + if ([[elem name] isEqual: @"message"]) { + [self XMPP_handleMessage: + [XMPPMessage stanzaWithElement: elem]]; + return; + } + + if ([[elem name] isEqual: @"presence"]) { + [self XMPP_handlePresence: + [XMPPPresence stanzaWithElement: elem]]; + return; + } + + assert(0); +} + + +- (void)XMPP_handleStream: (OFXMLElement*)elem +{ + if ([[elem name] isEqual: @"features"]) { + [self XMPP_handleFeatures: elem]; + return; + } + + assert(0); +} + +- (void)XMPP_handleTLS: (OFXMLElement*)elem +{ + if ([[elem name] isEqual: @"proceed"]) { + /* FIXME: Catch errors here */ + SSLSocket *newSock; + + if ([delegate respondsToSelector: + @selector(connectionWillUpgradeToTLS:)]) + [delegate connectionWillUpgradeToTLS: self]; + + newSock = [[SSLSocket alloc] initWithSocket: sock]; + [sock release]; + sock = newSock; + + if ([delegate respondsToSelector: + @selector(connectionDidUpgradeToTLS:)]) + [delegate connectionDidUpgradeToTLS: self]; + + /* Stream restart */ + [parser setDelegate: self]; + [self XMPP_startStream]; + return; + } + + if ([[elem name] isEqual: @"failure"]) + /* TODO: Find/create an exception to throw here */ + @throw [OFException newWithClass: isa]; + + assert(0); +} + +- (void)XMPP_handleSASL: (OFXMLElement*)elem +{ + if ([[elem name] isEqual: @"challenge"]) { + OFXMLElement *responseTag; + OFDataArray *challenge = + [OFDataArray dataArrayWithBase64EncodedString: + [elem stringValue]]; + OFDataArray *response = [authModule + calculateResponseWithChallenge: challenge]; + + responseTag = [OFXMLElement + elementWithName: @"response" + namespace: XMPP_NS_SASL]; + [responseTag addChild: + [OFXMLElement elementWithCharacters: + [response stringByBase64Encoding]]]; + + [self sendStanza: responseTag]; + return; + } + + if ([[elem name] isEqual: @"success"]) { + [authModule parseServerFinalMessage: + [OFDataArray dataArrayWithBase64EncodedString: + [elem stringValue]]]; + + if ([delegate respondsToSelector: + @selector(connectionWasAuthenticated:)]) + [delegate connectionWasAuthenticated: self]; + + /* Stream restart */ + [parser setDelegate: self]; + [self XMPP_startStream]; + return; + } + + if ([[elem name] isEqual: @"failure"]) { + of_log(@"Auth failed!"); + // FIXME: Do more parsing/handling + @throw [XMPPAuthFailedException + newWithClass: isa + connection: self + reason: [elem XMLString]]; } assert(0); }