diff --git a/src/XMPPConnection.m b/src/XMPPConnection.m index fc5a280..cebe86d 100644 --- a/src/XMPPConnection.m +++ b/src/XMPPConnection.m @@ -574,23 +574,8 @@ if (!handled && ![[iq type] isEqual: @"error"] && ![[iq type] isEqual: @"result"]) { - XMPPJID *from = [iq from]; - XMPPJID *to = [iq to]; - 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]; + [self sendStanza: [iq errorIQWithType: @"cancel" + condition: @"service-unavailable"]]; } } diff --git a/src/XMPPIQ.h b/src/XMPPIQ.h index 1ac38d6..a629d7a 100644 --- a/src/XMPPIQ.h +++ b/src/XMPPIQ.h @@ -46,4 +46,33 @@ */ - initWithType: (OFString*)type 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 diff --git a/src/XMPPIQ.m b/src/XMPPIQ.m index 2cb15bf..fba20ca 100644 --- a/src/XMPPIQ.m +++ b/src/XMPPIQ.m @@ -21,6 +21,7 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#import "namespaces.h" #import "XMPPIQ.h" @implementation XMPPIQ @@ -50,4 +51,48 @@ 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 diff --git a/src/XMPPRoster.m b/src/XMPPRoster.m index 1da2ffc..94d1073 100644 --- a/src/XMPPRoster.m +++ b/src/XMPPRoster.m @@ -162,10 +162,7 @@ } if (isPush) { - XMPPIQ *response = [XMPPIQ IQWithType: @"result" - ID: [iq ID]]; - [response setTo: [iq from]]; - [connection sendStanza: response]; + [connection sendStanza: [iq resultIQ]]; } else { if ([[connection delegate] respondsToSelector: @selector(connectionDidReceiveRoster:)])