Improve stanza handling and call delegate for iq, message and presence.
This commit is contained in:
parent
b1439941ba
commit
932d805e95
2 changed files with 96 additions and 16 deletions
|
@ -34,6 +34,8 @@
|
||||||
#import "XMPPStanza.h"
|
#import "XMPPStanza.h"
|
||||||
#import "XMPPJID.h"
|
#import "XMPPJID.h"
|
||||||
#import "XMPPIQ.h"
|
#import "XMPPIQ.h"
|
||||||
|
#import "XMPPMessage.h"
|
||||||
|
#import "XMPPPresence.h"
|
||||||
#import "XMPPExceptions.h"
|
#import "XMPPExceptions.h"
|
||||||
|
|
||||||
#define NS_BIND @"urn:ietf:params:xml:ns:xmpp-bind"
|
#define NS_BIND @"urn:ietf:params:xml:ns:xmpp-bind"
|
||||||
|
@ -303,6 +305,30 @@
|
||||||
[self _sendResourceBind];
|
[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
|
- (void)elementBuilder: (OFXMLElementBuilder*)b
|
||||||
didBuildElement: (OFXMLElement*)elem
|
didBuildElement: (OFXMLElement*)elem
|
||||||
{
|
{
|
||||||
|
@ -310,10 +336,34 @@
|
||||||
[elem setPrefix: @"stream"
|
[elem setPrefix: @"stream"
|
||||||
forNamespace: NS_STREAM];
|
forNamespace: NS_STREAM];
|
||||||
|
|
||||||
if ([elem.name isEqual: @"features"] &&
|
if ([elem.namespace isEqual: NS_CLIENT]) {
|
||||||
[elem.namespace isEqual: NS_STREAM]) {
|
if ([elem.name isEqual: @"iq"]) {
|
||||||
[self _handleFeatures: elem];
|
[self _handleIQ: [XMPPIQ stanzaWithElement: elem]];
|
||||||
return;
|
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.namespace isEqual: NS_STARTTLS]) {
|
||||||
|
@ -324,9 +374,14 @@
|
||||||
/* Stream restart */
|
/* Stream restart */
|
||||||
parser.delegate = self;
|
parser.delegate = self;
|
||||||
[self _startStream];
|
[self _startStream];
|
||||||
} else if ([elem.name isEqual: @"failure"])
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ([elem.name isEqual: @"failure"])
|
||||||
/* TODO: Find/create an exception to throw here */
|
/* TODO: Find/create an exception to throw here */
|
||||||
@throw [OFException newWithClass: isa];
|
@throw [OFException newWithClass: isa];
|
||||||
|
|
||||||
|
assert(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ([elem.namespace isEqual: NS_SASL]) {
|
if ([elem.namespace isEqual: NS_SASL]) {
|
||||||
|
@ -346,7 +401,10 @@
|
||||||
[response stringByBase64Encoding]]];
|
[response stringByBase64Encoding]]];
|
||||||
|
|
||||||
[self sendStanza: responseTag];
|
[self sendStanza: responseTag];
|
||||||
} else if ([elem.name isEqual: @"success"]) {
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ([elem.name isEqual: @"success"]) {
|
||||||
[authModule parseServerFinalMessage:
|
[authModule parseServerFinalMessage:
|
||||||
[OFDataArray dataArrayWithBase64EncodedString:
|
[OFDataArray dataArrayWithBase64EncodedString:
|
||||||
[elem.children.firstObject stringValue]]];
|
[elem.children.firstObject stringValue]]];
|
||||||
|
@ -355,7 +413,10 @@
|
||||||
/* Stream restart */
|
/* Stream restart */
|
||||||
parser.delegate = self;
|
parser.delegate = self;
|
||||||
[self _startStream];
|
[self _startStream];
|
||||||
} else if ([elem.name isEqual: @"failure"]) {
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ([elem.name isEqual: @"failure"]) {
|
||||||
of_log(@"Auth failed!");
|
of_log(@"Auth failed!");
|
||||||
// FIXME: Do more parsing/handling
|
// FIXME: Do more parsing/handling
|
||||||
@throw [XMPPAuthFailedException
|
@throw [XMPPAuthFailedException
|
||||||
|
@ -363,16 +424,11 @@
|
||||||
connection: self
|
connection: self
|
||||||
reason: [elem stringValue]];
|
reason: [elem stringValue]];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ([elem.name isEqual: @"iq"] &&
|
assert(0);
|
||||||
[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];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)elementBuilder: (OFXMLElementBuilder*)b
|
- (void)elementBuilder: (OFXMLElementBuilder*)b
|
||||||
|
|
26
tests/test.m
26
tests/test.m
|
@ -32,7 +32,7 @@
|
||||||
#import "XMPPMessage.h"
|
#import "XMPPMessage.h"
|
||||||
#import "XMPPPresence.h"
|
#import "XMPPPresence.h"
|
||||||
|
|
||||||
@interface AppDelegate: OFObject
|
@interface AppDelegate: OFObject <XMPPConnectionDelegate>
|
||||||
{
|
{
|
||||||
XMPPConnection *conn;
|
XMPPConnection *conn;
|
||||||
}
|
}
|
||||||
|
@ -84,6 +84,7 @@ OF_APPLICATION_DELEGATE(AppDelegate)
|
||||||
isEqual: @"bob@localhost, alice@localhost, get, 42"]));
|
isEqual: @"bob@localhost, alice@localhost, get, 42"]));
|
||||||
|
|
||||||
conn = [[XMPPConnection alloc] init];
|
conn = [[XMPPConnection alloc] init];
|
||||||
|
conn.delegate = self;
|
||||||
|
|
||||||
if (arguments.count != 3) {
|
if (arguments.count != 3) {
|
||||||
of_log(@"Invalid count of command line arguments!");
|
of_log(@"Invalid count of command line arguments!");
|
||||||
|
@ -103,4 +104,27 @@ OF_APPLICATION_DELEGATE(AppDelegate)
|
||||||
of_log(@"%@", 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
|
@end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue