@@ -57,12 +57,11 @@ #import @interface XMPPConnection () - (void)xmpp_tryNextSRVRecord; -- (bool)xmpp_parseBuffer: (const void *)buffer - length: (size_t)length; +- (bool)xmpp_parseBuffer: (const void *)buffer length: (size_t)length; - (void)xmpp_startStream; - (void)xmpp_handleStanza: (OFXMLElement *)element; - (void)xmpp_handleStream: (OFXMLElement *)element; - (void)xmpp_handleTLS: (OFXMLElement *)element; - (void)xmpp_handleSASL: (OFXMLElement *)element; @@ -70,12 +69,11 @@ - (void)xmpp_handleMessage: (XMPPMessage *)message; - (void)xmpp_handlePresence: (XMPPPresence *)presence; - (void)xmpp_handleFeatures: (OFXMLElement *)element; - (void)xmpp_sendAuth: (OFString *)authName; - (void)xmpp_sendResourceBind; -- (void)xmpp_sendStreamError: (OFString *)condition - text: (OFString *)text; +- (void)xmpp_sendStreamError: (OFString *)condition text: (OFString *)text; - (void)xmpp_handleResourceBindForConnection: (XMPPConnection *)connection IQ: (XMPPIQ *)IQ; - (void)xmpp_sendSession; - (void)xmpp_handleSessionForConnection: (XMPPConnection *)connection IQ: (XMPPIQ *)IQ; @@ -276,11 +274,11 @@ } [self xmpp_startStream]; [_socket asyncReadIntoBuffer: _buffer - length: XMPP_CONNECTION_BUFFER_LENGTH]; + length: XMPPConnectionBufferLength]; } - (void)xmpp_tryNextSRVRecord { OFSRVDNSResourceRecord *record = @@ -289,12 +287,11 @@ if (_nextSRVRecords.count == 0) { [_nextSRVRecords release]; _nextSRVRecords = nil; } - [_socket asyncConnectToHost: record.target - port: record.port]; + [_socket asyncConnectToHost: record.target port: record.port]; } - (void)resolver: (OFDNSResolver *)resolver didPerformQuery: (OFString *)domainName response: (OFDNSResponse *)response @@ -313,12 +310,11 @@ /* TODO: Sort records */ [records makeImmutable]; if (records.count == 0) { /* Fall back to A / AAAA record. */ - [_socket asyncConnectToHost: _domainToASCII - port: _port]; + [_socket asyncConnectToHost: _domainToASCII port: _port]; return; } [_nextSRVRecords release]; _nextSRVRecords = nil; @@ -335,12 +331,11 @@ _socket = [[OFTCPSocket alloc] init]; [_socket setDelegate: self]; if (_server != nil) - [_socket asyncConnectToHost: _server - port: _port]; + [_socket asyncConnectToHost: _server port: _port]; else { OFString *SRVDomain = [_domainToASCII stringByPrependingString: @"_xmpp-client._tcp."]; OFDNSQuery *query = [OFDNSQuery queryWithDomainName: SRVDomain @@ -363,27 +358,23 @@ withObject: nil]; return false; } @try { - [_parser parseBuffer: buffer - length: length]; + [_parser parseBuffer: buffer length: length]; } @catch (OFMalformedXMLException *e) { - [self xmpp_sendStreamError: @"bad-format" - text: nil]; + [self xmpp_sendStreamError: @"bad-format" text: nil]; [self close]; return false; } return true; } -- (void)parseBuffer: (const void *)buffer - length: (size_t)length +- (void)parseBuffer: (const void *)buffer length: (size_t)length { - [self xmpp_parseBuffer: buffer - length: length]; + [self xmpp_parseBuffer: buffer length: length]; [_oldParser release]; [_oldElementBuilder release]; _oldParser = nil; @@ -403,12 +394,11 @@ [self close]; return false; } @try { - if (![self xmpp_parseBuffer: buffer - length: length]) + if (![self xmpp_parseBuffer: buffer length: length]) return false; } @catch (id e) { [_delegates broadcastSelector: @selector(connection: didThrowException:) withObject: self @@ -423,11 +413,11 @@ _oldParser = nil; _oldElementBuilder = nil; [_socket asyncReadIntoBuffer: _buffer - length: XMPP_CONNECTION_BUFFER_LENGTH]; + length: XMPPConnectionBufferLength]; return false; } return true; } @@ -500,23 +490,20 @@ key = _JID.bareJID; if (key == nil) // Only happens for resource bind key = @"bind"; key = [key stringByAppendingString: ID]; - callback = [XMPPCallback callbackWithTarget: target - selector: selector]; - [_callbacks setObject: callback - forKey: key]; + callback = [XMPPCallback callbackWithTarget: target selector: selector]; + [_callbacks setObject: callback forKey: key]; objc_autoreleasePoolPop(pool); [self sendStanza: IQ]; } #ifdef OF_HAVE_BLOCKS -- (void)sendIQ: (XMPPIQ *)IQ - callbackBlock: (xmpp_callback_block_t)block +- (void)sendIQ: (XMPPIQ *)IQ callbackBlock: (XMPPCallbackBlock)block { void *pool = objc_autoreleasePoolPush(); XMPPCallback *callback; OFString *ID, *key; @@ -530,12 +517,11 @@ if (key == nil) // Connection not yet bound, can't send stanzas @throw [OFInvalidArgumentException exception]; key = [key stringByAppendingString: ID]; callback = [XMPPCallback callbackWithBlock: block]; - [_callbacks setObject: callback - forKey: key]; + [_callbacks setObject: callback forKey: key]; objc_autoreleasePoolPop(pool); [self sendStanza: IQ]; } @@ -558,26 +544,23 @@ [_socket close]; return; } if (![prefix isEqual: @"stream"]) { - [self xmpp_sendStreamError: @"bad-namespace-prefix" - text: nil]; + [self xmpp_sendStreamError: @"bad-namespace-prefix" text: nil]; return; } - if (![namespace isEqual: XMPP_NS_STREAM]) { - [self xmpp_sendStreamError: @"invalid-namespace" - text: nil]; + if (![namespace isEqual: XMPPStreamNS]) { + [self xmpp_sendStreamError: @"invalid-namespace" text: nil]; return; } for (OFXMLAttribute *attribute in attributes) { if ([attribute.name isEqual: @"from"] && ![attribute.stringValue isEqual: _domain]) { - [self xmpp_sendStreamError: @"invalid-from" - text: nil]; + [self xmpp_sendStreamError: @"invalid-from" text: nil]; return; } if ([attribute.name isEqual: @"version"] && ![attribute.stringValue isEqual: @"1.0"]) { [self xmpp_sendStreamError: @"unsupported-version" @@ -594,38 +577,37 @@ { /* Ignore whitespace elements */ if (element.name == nil) return; - element.defaultNamespace = XMPP_NS_CLIENT; - [element setPrefix: @"stream" - forNamespace: XMPP_NS_STREAM]; + element.defaultNamespace = XMPPClientNS; + [element setPrefix: @"stream" forNamespace: XMPPStreamNS]; [_delegates broadcastSelector: @selector(connection:didReceiveElement:) withObject: self withObject: element]; - if ([element.namespace isEqual: XMPP_NS_CLIENT]) + if ([element.namespace isEqual: XMPPClientNS]) [self xmpp_handleStanza: element]; - if ([element.namespace isEqual: XMPP_NS_STREAM]) + if ([element.namespace isEqual: XMPPStreamNS]) [self xmpp_handleStream: element]; - if ([element.namespace isEqual: XMPP_NS_STARTTLS]) + if ([element.namespace isEqual: XMPPStartTLSNS]) [self xmpp_handleTLS: element]; - if ([element.namespace isEqual: XMPP_NS_SASL]) + if ([element.namespace isEqual: XMPPSASLNS]) [self xmpp_handleSASL: element]; } - (void)elementBuilder: (OFXMLElementBuilder *)builder didNotExpectCloseTag: (OFString *)name prefix: (OFString *)prefix namespace: (OFString *)ns { if (![name isEqual: @"stream"] || ![prefix isEqual: @"stream"] || - ![ns isEqual: XMPP_NS_STREAM]) + ![ns isEqual: XMPPStreamNS]) @throw [OFMalformedXMLException exception]; else [self close]; } @@ -654,12 +636,12 @@ langString = [OFString stringWithFormat: @"xml:lang='%@' ", _language]; [_socket writeFormat: @"\n" @"", _domain, langString]; _streamOpen = true; } @@ -700,12 +682,11 @@ [self xmpp_handlePresence: [XMPPPresence stanzaWithElement: element]]; return; } - [self xmpp_sendStreamError: @"unsupported-stanza-type" - text: nil]; + [self xmpp_sendStreamError: @"unsupported-stanza-type" text: nil]; } - (void)xmpp_handleStream: (OFXMLElement *)element { @@ -720,19 +701,18 @@ [_delegates broadcastSelector: @selector(connectionWasClosed:) withObject: self withObject: element]; - condition = [[element elementsForNamespace: - XMPP_NS_XMPP_STREAM].firstObject name]; + condition = [[element elementsForNamespace: XMPPXMPPStreamNS] + .firstObject name]; if (condition == nil) condition = @"undefined"; - reason = [element - elementForName: @"text" - namespace: XMPP_NS_XMPP_STREAM].stringValue; + reason = [element elementForName: @"text" + namespace: XMPPXMPPStreamNS].stringValue; @throw [XMPPStreamErrorException exceptionWithConnection: self condition: condition reason: reason]; @@ -791,11 +771,11 @@ OFData *challenge = [OFData dataWithBase64EncodedString: element.stringValue]; OFData *response = [_authModule continueWithData: challenge]; responseTag = [OFXMLElement elementWithName: @"response" - namespace: XMPP_NS_SASL]; + namespace: XMPPSASLNS]; if (response) { if (response.count == 0) responseTag.stringValue = @"="; else responseTag.stringValue = @@ -841,12 +821,11 @@ if (key == nil) // Only happens for resource bind key = @"bind"; key = [key stringByAppendingString: IQ.ID]; if ((callback = [_callbacks objectForKey: key])) { - [callback runWithIQ: IQ - connection: self]; + [callback runWithIQ: IQ connection: self]; [_callbacks removeObjectForKey: key]; return; } handled = [_delegates broadcastSelector: @selector( @@ -876,36 +855,34 @@ } - (void)xmpp_handleFeatures: (OFXMLElement *)element { OFXMLElement *startTLS = [element elementForName: @"starttls" - namespace: XMPP_NS_STARTTLS]; + namespace: XMPPStartTLSNS]; OFXMLElement *bind = [element elementForName: @"bind" - namespace: XMPP_NS_BIND]; + namespace: XMPPBindNS]; OFXMLElement *session = [element elementForName: @"session" - namespace: XMPP_NS_SESSION]; + namespace: XMPPSessionNS]; OFXMLElement *mechs = [element elementForName: @"mechanisms" - namespace: XMPP_NS_SASL]; + namespace: XMPPSASLNS]; OFMutableSet *mechanisms = [OFMutableSet set]; if (!_encrypted && startTLS != nil) { [self sendStanza: [OFXMLElement elementWithName: @"starttls" - namespace: XMPP_NS_STARTTLS]]; + namespace: XMPPStartTLSNS]]; return; } if (_encryptionRequired && !_encrypted) /* TODO: Find/create an exception to throw here */ @throw [OFException exception]; - if ([element elementForName: @"ver" - namespace: XMPP_NS_ROSTERVER] != nil) + if ([element elementForName: @"ver" namespace: XMPPRosterVerNS] != nil) _supportsRosterVersioning = true; - if ([element elementForName: @"sm" - namespace: XMPP_NS_SM] != nil) + if ([element elementForName: @"sm" namespace: XMPPSMNS] != nil) _supportsStreamManagement = true; if (mechs != nil) { for (OFXMLElement *mech in mechs.children) [mechanisms addObject: mech.stringValue]; @@ -964,11 +941,11 @@ reason: @"No supported auth mechanism"]; } if (session != nil && [session elementForName: @"optional" - namespace: XMPP_NS_SESSION] == nil) + namespace: XMPPSessionNS] == nil) _needsSession = true; if (bind != nil) { [self xmpp_sendResourceBind]; return; @@ -980,14 +957,12 @@ - (void)xmpp_sendAuth: (OFString *)authName { OFXMLElement *authTag; OFData *initialMessage = [_authModule initialMessage]; - authTag = [OFXMLElement elementWithName: @"auth" - namespace: XMPP_NS_SASL]; - [authTag addAttributeWithName: @"mechanism" - stringValue: authName]; + authTag = [OFXMLElement elementWithName: @"auth" namespace: XMPPSASLNS]; + [authTag addAttributeWithName: @"mechanism" stringValue: authName]; if (initialMessage != nil) { if (initialMessage.count == 0) authTag.stringValue = @"="; else authTag.stringValue = @@ -1000,19 +975,17 @@ - (void)xmpp_sendResourceBind { XMPPIQ *IQ; OFXMLElement *bind; - IQ = [XMPPIQ IQWithType: @"set" - ID: [self generateStanzaID]]; + IQ = [XMPPIQ IQWithType: @"set" ID: [self generateStanzaID]]; - bind = [OFXMLElement elementWithName: @"bind" - namespace: XMPP_NS_BIND]; + bind = [OFXMLElement elementWithName: @"bind" namespace: XMPPBindNS]; if (_resource != nil) [bind addChild: [OFXMLElement elementWithName: @"resource" - namespace: XMPP_NS_BIND + namespace: XMPPBindNS stringValue: _resource]]; [IQ addChild: bind]; [self sendIQ: IQ @@ -1024,19 +997,18 @@ - (void)xmpp_sendStreamError: (OFString *)condition text: (OFString *)text { OFXMLElement *error = [OFXMLElement elementWithName: @"error" - namespace: XMPP_NS_STREAM]; - [error setPrefix: @"stream" - forNamespace: XMPP_NS_STREAM]; + namespace: XMPPStreamNS]; + [error setPrefix: @"stream" forNamespace: XMPPStreamNS]; [error addChild: [OFXMLElement elementWithName: condition - namespace: XMPP_NS_XMPP_STREAM]]; + namespace: XMPPXMPPStreamNS]]; if (text) [error addChild: [OFXMLElement elementWithName: @"text" - namespace: XMPP_NS_XMPP_STREAM + namespace: XMPPXMPPStreamNS stringValue: text]]; _parser.delegate = nil; [self sendStanza: error]; [self close]; } @@ -1046,17 +1018,15 @@ { OFXMLElement *bindElement, *JIDElement; assert([IQ.type isEqual: @"result"]); - bindElement = [IQ elementForName: @"bind" - namespace: XMPP_NS_BIND]; + bindElement = [IQ elementForName: @"bind" namespace: XMPPBindNS]; assert(bindElement != nil); - JIDElement = [bindElement elementForName: @"jid" - namespace: XMPP_NS_BIND]; + JIDElement = [bindElement elementForName: @"jid" namespace: XMPPBindNS]; _JID = [[XMPPJID alloc] initWithString: JIDElement.stringValue]; if (_needsSession) { [self xmpp_sendSession]; return; @@ -1067,15 +1037,14 @@ withObject: _JID]; } - (void)xmpp_sendSession { - XMPPIQ *IQ = [XMPPIQ IQWithType: @"set" - ID: [self generateStanzaID]]; + XMPPIQ *IQ = [XMPPIQ IQWithType: @"set" ID: [self generateStanzaID]]; [IQ addChild: [OFXMLElement elementWithName: @"session" - namespace: XMPP_NS_SESSION]]; + namespace: XMPPSessionNS]]; [self sendIQ: IQ callbackTarget: self selector: @selector(xmpp_handleSessionForConnection:IQ:)]; }