Use XMPPJID for from and to.

This commit is contained in:
Jonathan Schleifer 2011-02-19 17:40:41 +01:00
parent f3109b1c7a
commit 613400a718
3 changed files with 27 additions and 24 deletions

View file

@ -23,23 +23,25 @@
#import <ObjFW/ObjFW.h> #import <ObjFW/ObjFW.h>
@class XMPPJID;
/** /**
* \brief A class describing an XMPP Stanza. * \brief A class describing an XMPP Stanza.
*/ */
@interface XMPPStanza: OFXMLElement @interface XMPPStanza: OFXMLElement
{ {
/// The value of the stanza's from attribute /// The value of the stanza's from attribute
OFString *from; XMPPJID *from;
/// The value of the stanza's to attribute /// The value of the stanza's to attribute
OFString *to; XMPPJID *to;
/// The value of the stanza's type attribute /// The value of the stanza's type attribute
OFString *type; OFString *type;
/// The value of the stanza's id attribute /// The value of the stanza's id attribute
OFString *ID; OFString *ID;
} }
@property (copy) OFString *from; @property (copy) XMPPJID *from;
@property (copy) OFString *to; @property (copy) XMPPJID *to;
@property (copy) OFString *type; @property (copy) OFString *type;
@property (copy) OFString *ID; @property (copy) OFString *ID;

View file

@ -22,6 +22,7 @@
*/ */
#import "XMPPStanza.h" #import "XMPPStanza.h"
#import "XMPPJID.h"
@implementation XMPPStanza @implementation XMPPStanza
@synthesize from; @synthesize from;
@ -118,14 +119,13 @@
namespace: elem.namespace]; namespace: elem.namespace];
@try { @try {
OFXMLAttribute *attr; for (OFXMLAttribute *attr in elem.attributes) {
OFXMLElement *el;
for (attr in elem.attributes) {
if ([attr.name isEqual: @"from"]) if ([attr.name isEqual: @"from"])
[self setFrom: [attr stringValue]]; [self setFrom: [XMPPJID JIDWithString:
[attr stringValue]]];
else if ([attr.name isEqual: @"to"]) else if ([attr.name isEqual: @"to"])
[self setTo: [attr stringValue]]; [self setTo: [XMPPJID JIDWithString:
[attr stringValue]]];
else if ([attr.name isEqual: @"type"]) else if ([attr.name isEqual: @"type"])
[self setType: [attr stringValue]]; [self setType: [attr stringValue]];
else if ([attr.name isEqual: @"id"]) else if ([attr.name isEqual: @"id"])
@ -134,7 +134,7 @@
[self addAttribute: attr]; [self addAttribute: attr];
} }
for (el in elem.children) for (OFXMLElement *el in elem.children)
[self addChild: el]; [self addChild: el];
} @catch (id e) { } @catch (id e) {
[self release]; [self release];
@ -154,26 +154,26 @@
[super dealloc]; [super dealloc];
} }
- (void)setFrom: (OFString*)from_ - (void)setFrom: (XMPPJID*)from_
{ {
OFString* old = from; XMPPJID *old = from;
from = [from_ copy]; from = [from_ copy];
[old release]; [old release];
[self removeAttributeForName: @"from"]; [self removeAttributeForName: @"from"];
[self addAttributeWithName: @"from" [self addAttributeWithName: @"from"
stringValue: from_]; stringValue: from_.fullJID];
} }
- (void)setTo: (OFString*)to_ - (void)setTo: (XMPPJID*)to_
{ {
OFString* old = to; XMPPJID *old = to;
to = [to_ copy]; to = [to_ copy];
[old release]; [old release];
[self removeAttributeForName: @"to"]; [self removeAttributeForName: @"to"];
[self addAttributeWithName: @"to" [self addAttributeWithName: @"to"
stringValue: to]; stringValue: to_.fullJID];
} }
- (void)setType: (OFString*)type_ - (void)setType: (OFString*)type_

View file

@ -26,6 +26,7 @@
#import <ObjFW/ObjFW.h> #import <ObjFW/ObjFW.h>
#import "XMPPConnection.h" #import "XMPPConnection.h"
#import "XMPPJID.h"
#import "XMPPStanza.h" #import "XMPPStanza.h"
#import "XMPPIQ.h" #import "XMPPIQ.h"
#import "XMPPMessage.h" #import "XMPPMessage.h"
@ -48,8 +49,8 @@ OF_APPLICATION_DELEGATE(AppDelegate)
[pres addShow: @"chat"]; [pres addShow: @"chat"];
[pres addStatus: @"Bored"]; [pres addStatus: @"Bored"];
[pres addPriority: 20]; [pres addPriority: 20];
pres.to = @"alice@example.com"; pres.to = [XMPPJID JIDWithString: @"alice@example.com"];
pres.from = @"bob@example.org"; pres.from = [XMPPJID JIDWithString: @"bob@example.org"];
assert([[pres stringValue] isEqual: @"<presence to='alice@example.com' " assert([[pres stringValue] isEqual: @"<presence to='alice@example.com' "
@"from='bob@example.org'><show>chat</show>" @"from='bob@example.org'><show>chat</show>"
@"<status>Bored</status><priority>20</priority>" @"<status>Bored</status><priority>20</priority>"
@ -57,16 +58,16 @@ OF_APPLICATION_DELEGATE(AppDelegate)
XMPPMessage *msg = [XMPPMessage messageWithType: @"chat"]; XMPPMessage *msg = [XMPPMessage messageWithType: @"chat"];
[msg addBody: @"Hello everyone"]; [msg addBody: @"Hello everyone"];
msg.to = @"jdev@conference.jabber.org"; msg.to = [XMPPJID JIDWithString: @"jdev@conference.jabber.org"];
msg.from = @"alice@example.com"; msg.from = [XMPPJID JIDWithString: @"alice@example.com"];
assert([[msg stringValue] isEqual: @"<message type='chat' " assert([[msg stringValue] isEqual: @"<message type='chat' "
@"to='jdev@conference.jabber.org' " @"to='jdev@conference.jabber.org' "
@"from='alice@example.com'><body>Hello everyone</body>" @"from='alice@example.com'><body>Hello everyone</body>"
@"</message>"]); @"</message>"]);
XMPPIQ *iq = [XMPPIQ IQWithType: @"set" ID: @"128"]; XMPPIQ *iq = [XMPPIQ IQWithType: @"set" ID: @"128"];
iq.to = @"juliet@capulet.lit"; iq.to = [XMPPJID JIDWithString: @"juliet@capulet.lit"];
iq.from = @"romeo@montague.lit"; iq.from = [XMPPJID JIDWithString: @"romeo@montague.lit"];
assert([[iq stringValue] isEqual: @"<iq type='set' id='128' " assert([[iq stringValue] isEqual: @"<iq type='set' id='128' "
@"to='juliet@capulet.lit' " @"to='juliet@capulet.lit' "
@"from='romeo@montague.lit'/>"]); @"from='romeo@montague.lit'/>"]);