Request session and send initial presence in tests.

This commit is contained in:
Jonathan Schleifer 2011-03-21 16:15:35 +01:00
parent 4eb4d6bc9c
commit ae01053f1c
3 changed files with 74 additions and 18 deletions

View file

@ -69,6 +69,7 @@
BOOL useTLS; BOOL useTLS;
id <XMPPConnectionDelegate, OFObject> delegate; id <XMPPConnectionDelegate, OFObject> delegate;
XMPPAuthenticator *authModule; XMPPAuthenticator *authModule;
BOOL needsSession;
} }
@property (copy) OFString *username; @property (copy) OFString *username;

View file

@ -42,6 +42,7 @@
#define NS_CLIENT @"jabber:client" #define NS_CLIENT @"jabber:client"
#define NS_SASL @"urn:ietf:params:xml:ns:xmpp-sasl" #define NS_SASL @"urn:ietf:params:xml:ns:xmpp-sasl"
#define NS_STARTTLS @"urn:ietf:params:xml:ns:xmpp-tls" #define NS_STARTTLS @"urn:ietf:params:xml:ns:xmpp-tls"
#define NS_SESSION @"urn:ietf:params:xml:ns:xmpp-session"
#define NS_STREAM @"http://etherx.jabber.org/streams" #define NS_STREAM @"http://etherx.jabber.org/streams"
@implementation XMPPConnection @implementation XMPPConnection
@ -195,8 +196,6 @@
@selector(connectionWasClosed:)]) @selector(connectionWasClosed:)])
[delegate connectionWasClosed: self]; [delegate connectionWasClosed: self];
[of_stdout writeNBytes: len
fromBuffer: buf];
[parser parseBuffer: buf [parser parseBuffer: buf
withSize: len]; withSize: len];
} }
@ -204,6 +203,7 @@
- (void)sendStanza: (OFXMLElement*)elem - (void)sendStanza: (OFXMLElement*)elem
{ {
of_log(@"Out: %@", elem);
[sock writeString: [elem stringValue]]; [sock writeString: [elem stringValue]];
} }
@ -258,21 +258,43 @@
[self sendStanza: iq]; [self sendStanza: iq];
} }
- (void)_sendSession
{
XMPPIQ *iq = [XMPPIQ IQWithType: @"set"
ID: @"session0"];
[iq addChild: [OFXMLElement elementWithName: @"session"
namespace: NS_SESSION]];
[self sendStanza: iq];
}
- (void)_handleResourceBind: (XMPPIQ*)iq - (void)_handleResourceBind: (XMPPIQ*)iq
{ {
OFXMLElement *bindElem = iq.children.firstObject; OFXMLElement *bindElem = iq.children.firstObject;
OFXMLElement *jidElem;
if ([bindElem.name isEqual: @"bind"] && if (![bindElem.name isEqual: @"bind"] ||
[bindElem.namespace isEqual: NS_BIND]) { ![bindElem.namespace isEqual: NS_BIND])
OFXMLElement *jidElem = bindElem.children.firstObject; assert(0);
jidElem = bindElem.children.firstObject;
JID = [[XMPPJID alloc] initWithString: JID = [[XMPPJID alloc] initWithString:
[jidElem.children.firstObject stringValue]]; [jidElem.children.firstObject stringValue]];
if ([delegate respondsToSelector: if (needsSession) {
@selector(connection:wasBoundToJID:)]) [self _sendSession];
return;
}
if ([delegate respondsToSelector: @selector(connection:wasBoundToJID:)])
[delegate connection: self [delegate connection: self
wasBoundToJID: JID]; wasBoundToJID: JID];
} }
- (void)_handleSession
{
if ([delegate respondsToSelector: @selector(connection:wasBoundToJID:)])
[delegate connection: self
wasBoundToJID: JID];
} }
- (void)_handleFeatures: (OFXMLElement*)elem - (void)_handleFeatures: (OFXMLElement*)elem
@ -282,14 +304,19 @@
namespace: NS_STARTTLS].firstObject; namespace: NS_STARTTLS].firstObject;
OFXMLElement *bind = [elem elementsForName: @"bind" OFXMLElement *bind = [elem elementsForName: @"bind"
namespace: NS_BIND].firstObject; namespace: NS_BIND].firstObject;
OFXMLElement *session = [elem elementsForName: @"session"
namespace: NS_SESSION].firstObject;
OFArray *mechs = [elem elementsForName: @"mechanisms" OFArray *mechs = [elem elementsForName: @"mechanisms"
namespace: NS_SASL]; namespace: NS_SASL];
OFMutableArray *mechanisms = [OFMutableArray array]; OFMutableArray *mechanisms = [OFMutableArray array];
if (starttls != nil) if (starttls != nil) {
[self sendStanza: [OFXMLElement elementWithName: @"starttls" [self sendStanza: [OFXMLElement elementWithName: @"starttls"
namespace: NS_STARTTLS]]; namespace: NS_STARTTLS]];
else if ([mechs count]) { return;
}
if ([mechs count] > 0) {
for (OFXMLElement *mech in [mechs.firstObject children]) for (OFXMLElement *mech in [mechs.firstObject children])
[mechanisms addObject: [mechanisms addObject:
[mech.children.firstObject stringValue]]; [mech.children.firstObject stringValue]];
@ -300,14 +327,29 @@
password: password password: password
hash: [OFSHA1Hash class]]; hash: [OFSHA1Hash class]];
[self _sendAuth: @"SCRAM-SHA-1"]; [self _sendAuth: @"SCRAM-SHA-1"];
} else if ([mechanisms containsObject: @"PLAIN"]) { return;
}
if ([mechanisms containsObject: @"PLAIN"]) {
authModule = [[XMPPPLAINAuth alloc] authModule = [[XMPPPLAINAuth alloc]
initWithAuthcid: username initWithAuthcid: username
password: password]; password: password];
[self _sendAuth: @"PLAIN"]; [self _sendAuth: @"PLAIN"];
return;
} }
} else if (bind != nil)
assert(0);
}
if (session != nil)
needsSession = YES;
if (bind != nil) {
[self _sendResourceBind]; [self _sendResourceBind];
return;
}
assert(0);
} }
- (void)_handleIQ: (XMPPIQ*)iq - (void)_handleIQ: (XMPPIQ*)iq
@ -318,6 +360,11 @@
return; return;
} }
if ([iq.ID isEqual: @"session0"] && [iq.type isEqual: @"result"]) {
[self _handleSession];
return;
}
if ([delegate respondsToSelector: @selector(connection:didReceiveIQ:)]) if ([delegate respondsToSelector: @selector(connection:didReceiveIQ:)])
[delegate connection: self [delegate connection: self
didReceiveIQ: iq]; didReceiveIQ: iq];
@ -346,6 +393,8 @@
[elem setPrefix: @"stream" [elem setPrefix: @"stream"
forNamespace: NS_STREAM]; forNamespace: NS_STREAM];
of_log(@"In: %@", elem);
if ([elem.namespace isEqual: NS_CLIENT]) { if ([elem.namespace isEqual: NS_CLIENT]) {
if ([elem.name isEqual: @"iq"]) { if ([elem.name isEqual: @"iq"]) {
[self _handleIQ: [XMPPIQ stanzaWithElement: elem]]; [self _handleIQ: [XMPPIQ stanzaWithElement: elem]];

View file

@ -33,9 +33,6 @@
#import "XMPPPresence.h" #import "XMPPPresence.h"
@interface AppDelegate: OFObject <OFApplicationDelegate, XMPPConnectionDelegate> @interface AppDelegate: OFObject <OFApplicationDelegate, XMPPConnectionDelegate>
{
XMPPConnection *conn;
}
@end @end
OF_APPLICATION_DELEGATE(AppDelegate) OF_APPLICATION_DELEGATE(AppDelegate)
@ -43,6 +40,7 @@ OF_APPLICATION_DELEGATE(AppDelegate)
@implementation AppDelegate @implementation AppDelegate
- (void)applicationDidFinishLaunching - (void)applicationDidFinishLaunching
{ {
XMPPConnection *conn;
OFArray *arguments = [OFApplication arguments]; OFArray *arguments = [OFApplication arguments];
XMPPPresence *pres = [XMPPPresence presence]; XMPPPresence *pres = [XMPPPresence presence];
@ -113,7 +111,15 @@ OF_APPLICATION_DELEGATE(AppDelegate)
- (void)connection: (XMPPConnection*)conn - (void)connection: (XMPPConnection*)conn
wasBoundToJID: (XMPPJID*)jid wasBoundToJID: (XMPPJID*)jid
{ {
XMPPPresence *pres;
of_log(@"Bound to JID: %@", [jid fullJID]); of_log(@"Bound to JID: %@", [jid fullJID]);
pres = [XMPPPresence presence];
[pres addPriority: 10];
[pres addStatus: @"ObjXMPP test is working!"];
[conn sendStanza: pres];
} }
- (void)connection: (XMPPConnection*)conn - (void)connection: (XMPPConnection*)conn