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;
id <XMPPConnectionDelegate, OFObject> delegate;
XMPPAuthenticator *authModule;
BOOL needsSession;
}
@property (copy) OFString *username;

View file

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

View file

@ -33,9 +33,6 @@
#import "XMPPPresence.h"
@interface AppDelegate: OFObject <OFApplicationDelegate, XMPPConnectionDelegate>
{
XMPPConnection *conn;
}
@end
OF_APPLICATION_DELEGATE(AppDelegate)
@ -43,6 +40,7 @@ OF_APPLICATION_DELEGATE(AppDelegate)
@implementation AppDelegate
- (void)applicationDidFinishLaunching
{
XMPPConnection *conn;
OFArray *arguments = [OFApplication arguments];
XMPPPresence *pres = [XMPPPresence presence];
@ -113,7 +111,15 @@ OF_APPLICATION_DELEGATE(AppDelegate)
- (void)connection: (XMPPConnection*)conn
wasBoundToJID: (XMPPJID*)jid
{
XMPPPresence *pres;
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