Index: src/XMPPConnection.m ================================================================== --- src/XMPPConnection.m +++ src/XMPPConnection.m @@ -32,10 +32,12 @@ #import "XMPPSCRAMAuth.h" #import "XMPPPLAINAuth.h" #import "XMPPStanza.h" #import "XMPPJID.h" #import "XMPPIQ.h" +#import "XMPPMessage.h" +#import "XMPPPresence.h" #import "XMPPExceptions.h" #define NS_BIND @"urn:ietf:params:xml:ns:xmpp-bind" #define NS_CLIENT @"jabber:client" #define NS_SASL @"urn:ietf:params:xml:ns:xmpp-sasl" @@ -300,22 +302,70 @@ [self _sendAuth: @"PLAIN"]; } } else if (bind != nil) [self _sendResourceBind]; } + +- (void)_handleIQ: (XMPPIQ*)iq +{ + // FIXME: More checking! + if ([iq.ID isEqual: @"bind0"] && [iq.type isEqual: @"result"]) { + [self _handleResourceBind: iq]; + return; + } + + [delegate connection: self + didReceiveIQ: iq]; +} + +- (void)_handleMessage: (XMPPMessage*)msg +{ + [delegate connection: self + didReceiveMessage: msg]; +} + +- (void)_handlePresence: (XMPPPresence*)pres +{ + [delegate connection: self + didReceivePresence: pres]; +} - (void)elementBuilder: (OFXMLElementBuilder*)b didBuildElement: (OFXMLElement*)elem { elem.defaultNamespace = NS_CLIENT; [elem setPrefix: @"stream" forNamespace: NS_STREAM]; - if ([elem.name isEqual: @"features"] && - [elem.namespace isEqual: NS_STREAM]) { - [self _handleFeatures: elem]; - return; + if ([elem.namespace isEqual: NS_CLIENT]) { + if ([elem.name isEqual: @"iq"]) { + [self _handleIQ: [XMPPIQ stanzaWithElement: elem]]; + return; + } + + if ([elem.name isEqual: @"message"]) { + [self _handleMessage: + [XMPPMessage stanzaWithElement: elem]]; + return; + } + + if ([elem.name isEqual: @"presence"]) { + [self _handlePresence: + [XMPPPresence stanzaWithElement: elem]]; + return; + } + + assert(0); + } + + if ([elem.namespace isEqual: NS_STREAM]) { + if ([elem.name isEqual: @"features"]) { + [self _handleFeatures: elem]; + return; + } + + assert(0); } if ([elem.namespace isEqual: NS_STARTTLS]) { if ([elem.name isEqual: @"proceed"]) { /* FIXME: Catch errors here */ @@ -322,13 +372,18 @@ sock = [[GTLSSocket alloc] initWithSocket: sock]; /* Stream restart */ parser.delegate = self; [self _startStream]; - } else if ([elem.name isEqual: @"failure"]) + return; + } + + if ([elem.name isEqual: @"failure"]) /* TODO: Find/create an exception to throw here */ @throw [OFException newWithClass: isa]; + + assert(0); } if ([elem.namespace isEqual: NS_SASL]) { if ([elem.name isEqual: @"challenge"]) { OFXMLElement *responseTag; @@ -344,37 +399,38 @@ [responseTag addChild: [OFXMLElement elementWithCharacters: [response stringByBase64Encoding]]]; [self sendStanza: responseTag]; - } else if ([elem.name isEqual: @"success"]) { + return; + } + + if ([elem.name isEqual: @"success"]) { [authModule parseServerFinalMessage: [OFDataArray dataArrayWithBase64EncodedString: [elem.children.firstObject stringValue]]]; of_log(@"Auth successful"); /* Stream restart */ parser.delegate = self; [self _startStream]; - } else if ([elem.name isEqual: @"failure"]) { + return; + } + + if ([elem.name isEqual: @"failure"]) { of_log(@"Auth failed!"); // FIXME: Do more parsing/handling @throw [XMPPAuthFailedException newWithClass: isa connection: self reason: [elem stringValue]]; } + + assert(0); } - if ([elem.name isEqual: @"iq"] && - [elem.namespace isEqual: NS_CLIENT]) { - XMPPIQ *iq = [XMPPIQ stanzaWithElement: elem]; - - // FIXME: More checking! - if ([iq.ID isEqual: @"bind0"] && [iq.type isEqual: @"result"]) - [self _handleResourceBind: iq]; - } + assert(0); } - (void)elementBuilder: (OFXMLElementBuilder*)b didNotExpectCloseTag: (OFString*)name withPrefix: (OFString*)prefix Index: tests/test.m ================================================================== --- tests/test.m +++ tests/test.m @@ -30,11 +30,11 @@ #import "XMPPStanza.h" #import "XMPPIQ.h" #import "XMPPMessage.h" #import "XMPPPresence.h" -@interface AppDelegate: OFObject +@interface AppDelegate: OFObject { XMPPConnection *conn; } @end @@ -82,10 +82,11 @@ assert(([[OFString stringWithFormat: @"%@, %@, %@, %@", stanza.from.fullJID, stanza.to.fullJID, stanza.type, stanza.ID] isEqual: @"bob@localhost, alice@localhost, get, 42"])); conn = [[XMPPConnection alloc] init]; + conn.delegate = self; if (arguments.count != 3) { of_log(@"Invalid count of command line arguments!"); [OFApplication terminateWithStatus: 1]; } @@ -101,6 +102,29 @@ [conn handleConnection]; } @catch (id e) { of_log(@"%@", e); } } + +- (void)connectionWasClosed: (XMPPConnection*)conn +{ + of_log(@"Connection was closed!"); +} + +- (void)connection: (XMPPConnection*)conn + didReceiveIQ: (XMPPIQ*)iq +{ + of_log(@"IQ: %@", iq); +} + +- (void)connection: (XMPPConnection*)conn + didReceiveMessage: (XMPPMessage*)msg +{ + of_log(@"Message: %@", msg); +} + +- (void)connection: (XMPPConnection*)conn + didReceivePresence: (XMPPPresence*)pres +{ + of_log(@"Presence: %@", pres); +} @end