Add methods to generate error and result replies for IQ stanzas

This commit is contained in:
Florian Zeitz 2011-05-12 23:04:49 +02:00
parent 39d57946d9
commit 7d3796c62f
4 changed files with 77 additions and 21 deletions

View file

@ -574,23 +574,8 @@
if (!handled && ![[iq type] isEqual: @"error"] if (!handled && ![[iq type] isEqual: @"error"]
&& ![[iq type] isEqual: @"result"]) { && ![[iq type] isEqual: @"result"]) {
XMPPJID *from = [iq from]; [self sendStanza: [iq errorIQWithType: @"cancel"
XMPPJID *to = [iq to]; condition: @"service-unavailable"]];
OFXMLElement *error;
[iq setType: @"error"];
[iq setTo: from];
[iq setFrom: to];
error = [OFXMLElement elementWithName: @"error"];
[error addAttributeWithName: @"type"
stringValue: @"cancel"];
[error addChild:
[OFXMLElement elementWithName: @"service-unavailable"
namespace: XMPP_NS_STANZAS]];
[iq addChild: error];
[self sendStanza: iq];
} }
} }

View file

@ -46,4 +46,33 @@
*/ */
- initWithType: (OFString*)type - initWithType: (OFString*)type
ID: (OFString*)ID; ID: (OFString*)ID;
/**
* Generates a result IQ for the receiving object
*
* \return A new autoreleased XMPPIQ
*/
- (XMPPIQ*)resultIQ;
/**
* Generates a error IQ for the receiving object
*
* \param type A error type as defined by RFC 6120
* \param condition A error condition as defined by RFC 6120
* \param text A descriptive text
* \return A new autoreleased XMPPIQ
*/
- (XMPPIQ*)errorIQWithType: (OFString*)type
condition: (OFString*)condition
text: (OFString*)text;
/**
* Generates a error IQ for the receiving object
*
* \param type A error type as defined by RFC 6120
* \param condition A defined conditions from RFC 6120
* \return A new autoreleased XMPPIQ
*/
- (XMPPIQ*)errorIQWithType: (OFString*)type
condition: (OFString*)condition;
@end @end

View file

@ -21,6 +21,7 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#import "namespaces.h"
#import "XMPPIQ.h" #import "XMPPIQ.h"
@implementation XMPPIQ @implementation XMPPIQ
@ -50,4 +51,48 @@
return self; return self;
} }
- (XMPPIQ*)resultIQ
{
XMPPIQ *ret = [XMPPIQ IQWithType: @"result"
ID: [self ID]];
[ret setTo: [self from]];
[ret setFrom: nil];
return ret;
}
- (XMPPIQ*)errorIQWithType: (OFString*)type_
condition: (OFString*)condition
text: (OFString*)text
{
XMPPIQ *ret = [XMPPIQ IQWithType: @"error"
ID: [self ID]];
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
OFXMLElement *error = [OFXMLElement elementWithName: @"error"
namespace: XMPP_NS_CLIENT];
[error addAttributeWithName: @"type"
stringValue: type_];
[error addChild: [OFXMLElement elementWithName: condition
namespace: XMPP_NS_STANZAS]];
if (text)
[error addChild: [OFXMLElement elementWithName: @"text"
namespace: XMPP_NS_STANZAS
stringValue: text]];
[ret addChild: error];
[ret setTo: [self from]];
[ret setFrom: nil];
[pool release];
return ret;
}
- (XMPPIQ*)errorIQWithType: (OFString*)type_
condition: (OFString*)condition
{
return [self errorIQWithType: type_
condition: condition
text: nil];
}
@end @end

View file

@ -162,10 +162,7 @@
} }
if (isPush) { if (isPush) {
XMPPIQ *response = [XMPPIQ IQWithType: @"result" [connection sendStanza: [iq resultIQ]];
ID: [iq ID]];
[response setTo: [iq from]];
[connection sendStanza: response];
} else { } else {
if ([[connection delegate] respondsToSelector: if ([[connection delegate] respondsToSelector:
@selector(connectionDidReceiveRoster:)]) @selector(connectionDidReceiveRoster:)])