@@ -56,36 +56,49 @@ #import #define BUFFER_LENGTH 512 -OF_ASSUME_NONNULL_BEGIN - @interface XMPPConnection () -- (void)XMPP_startStream; -- (void)XMPP_handleStream: (OFXMLElement *)element; -- (void)XMPP_handleTLS: (OFXMLElement *)element; -- (void)XMPP_handleSASL: (OFXMLElement *)element; -- (void)XMPP_handleStanza: (OFXMLElement *)element; -- (void)XMPP_sendAuth: (OFString *)authName; -- (void)XMPP_sendResourceBind; -- (void)XMPP_sendStreamError: (OFString *)condition - text: (nullable OFString *)text; -- (void)XMPP_handleIQ: (XMPPIQ *)IQ; -- (void)XMPP_handleMessage: (XMPPMessage *)message; -- (void)XMPP_handlePresence: (XMPPPresence *)presence; -- (void)XMPP_handleFeatures: (OFXMLElement *)element; -- (void)XMPP_handleResourceBindForConnection: (XMPPConnection *)connection - IQ: (XMPPIQ *)IQ; -- (void)XMPP_sendSession; -- (void)XMPP_handleSessionForConnection: (XMPPConnection *)connection - IQ: (XMPPIQ *)IQ; -- (OFString *)XMPP_IDNAToASCII: (OFString *)domain; -- (XMPPMulticastDelegate *)XMPP_delegates; -@end - -OF_ASSUME_NONNULL_END +- (void)xmpp_socketDidConnect: (OFTCPSocket *)socket + context: (OFArray *)nextSRVRecords + exception: (id)exception; +- (void)xmpp_tryNextSRVRecord: (OFArray *)SRVRecords; +- (void)xmpp_resolver: (OFDNSResolver *)resolver + didResolveDomainName: (OFString *)domainName + answerRecords: (OFDictionary *)answerRecords + authorityRecords: (OFDictionary *)authorityRecords + additionalRecords: (OFDictionary *)additionalRecords + context: (OFString *)domainToASCII + exception: (id)exception; +- (bool)xmpp_parseBuffer: (const void *)buffer + length: (size_t)length; +- (bool)xmpp_stream: (OFStream *)stream + didReadIntoBuffer: (char *)buffer + length: (size_t)length + exception: (OFException *)exception; +- (void)xmpp_startStream; +- (void)xmpp_handleStanza: (OFXMLElement *)element; +- (void)xmpp_handleStream: (OFXMLElement *)element; +- (void)xmpp_handleTLS: (OFXMLElement *)element; +- (void)xmpp_handleSASL: (OFXMLElement *)element; +- (void)xmpp_handleIQ: (XMPPIQ *)IQ; +- (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_handleResourceBindForConnection: (XMPPConnection *)connection + IQ: (XMPPIQ *)IQ; +- (void)xmpp_sendSession; +- (void)xmpp_handleSessionForConnection: (XMPPConnection *)connection + IQ: (XMPPIQ *)IQ; +- (OFString *)xmpp_IDNAToASCII: (OFString *)domain; +- (XMPPMulticastDelegate *)xmpp_delegates; +@end @implementation XMPPConnection @synthesize username = _username, resource = _resource, server = _server; @synthesize domain = _domain, password = _password, language = _language; @synthesize privateKeyFile = _privateKeyFile; @@ -97,11 +110,11 @@ + (instancetype)connection { return [[[self alloc] init] autorelease]; } -- init +- (instancetype)init { self = [super init]; @try { _port = 5222; @@ -190,11 +203,11 @@ - (void)setServer: (OFString *)server { OFString *old = _server; if (server != nil) - _server = [self XMPP_IDNAToASCII: server]; + _server = [self xmpp_IDNAToASCII: server]; else _server = nil; [old release]; } @@ -219,11 +232,11 @@ _domain = [[OFString alloc] initWithUTF8String: srv]; } @finally { free(srv); } - _domainToASCII = [self XMPP_IDNAToASCII: _domain]; + _domainToASCII = [self xmpp_IDNAToASCII: _domain]; } else { _domain = nil; _domainToASCII = nil; } @@ -255,19 +268,19 @@ _password = nil; [old release]; } -- (void)XMPP_socketDidConnect: (OFTCPSocket *)socket +- (void)xmpp_socketDidConnect: (OFTCPSocket *)socket context: (OFArray *)nextSRVRecords exception: (id)exception { char *buffer; if (exception != nil) { if (nextSRVRecords != nil) { - [self XMPP_tryNextSRVRecord: nextSRVRecords]; + [self xmpp_tryNextSRVRecord: nextSRVRecords]; return; } [_delegates broadcastSelector: @selector(connection:didThrowException:) @@ -274,22 +287,22 @@ withObject: self withObject: exception]; return; } - [self XMPP_startStream]; + [self xmpp_startStream]; buffer = [self allocMemoryWithSize: BUFFER_LENGTH]; [_socket asyncReadIntoBuffer: buffer length: BUFFER_LENGTH target: self - selector: @selector(XMPP_stream:didReadIntoBuffer: + selector: @selector(xmpp_stream:didReadIntoBuffer: length:exception:) context: nil]; } -- (void)XMPP_tryNextSRVRecord: (OFArray *)SRVRecords +- (void)xmpp_tryNextSRVRecord: (OFArray *)SRVRecords { OFSRVDNSResourceRecord *record = [SRVRecords objectAtIndex: 0]; SRVRecords = [SRVRecords objectsInRange: of_range(1, [SRVRecords count] - 1)]; @@ -297,16 +310,16 @@ SRVRecords = nil; [_socket asyncConnectToHost: [record target] port: [record port] target: self - selector: @selector(XMPP_socketDidConnect: + selector: @selector(xmpp_socketDidConnect: context:exception:) context: SRVRecords]; } -- (void)XMPP_resolver: (OFDNSResolver *)resolver +- (void)xmpp_resolver: (OFDNSResolver *)resolver didResolveDomainName: (OFString *)domainName answerRecords: (OFDictionary *)answerRecords authorityRecords: (OFDictionary *)authorityRecords additionalRecords: (OFDictionary *)additionalRecords context: (OFString *)domainToASCII @@ -333,17 +346,17 @@ if ([records count] == 0) { /* Fall back to A / AAA record. */ [_socket asyncConnectToHost: domainToASCII port: _port target: self - selector: @selector(XMPP_socketDidConnect: + selector: @selector(xmpp_socketDidConnect: context:exception:) context: nil]; return; } - [self XMPP_tryNextSRVRecord: records]; + [self xmpp_tryNextSRVRecord: records]; } - (void)asyncConnect { void *pool = objc_autoreleasePoolPush(); @@ -355,27 +368,27 @@ if (_server != nil) [_socket asyncConnectToHost: _server port: _port target: self - selector: @selector(XMPP_socketDidConnect: + selector: @selector(xmpp_socketDidConnect: context:exception:) context: nil]; else [[OFThread DNSResolver] asyncResolveHost: _domainToASCII target: self - selector: @selector(XMPP_resolver: + selector: @selector(xmpp_resolver: didResolveDomainName:answerRecords: authorityRecords:additionalRecords: context:exception:) context: _domainToASCII]; objc_autoreleasePoolPop(pool); } -- (bool)XMPP_parseBuffer: (const void *)buffer +- (bool)xmpp_parseBuffer: (const void *)buffer length: (size_t)length { if ([_socket isAtEndOfStream]) { [_delegates broadcastSelector: @selector(connectionWasClosed:) withObject: self]; @@ -384,11 +397,11 @@ @try { [_parser parseBuffer: buffer length: length]; } @catch (OFMalformedXMLException *e) { - [self XMPP_sendStreamError: @"bad-format" + [self xmpp_sendStreamError: @"bad-format" text: nil]; [self close]; return false; } @@ -396,21 +409,21 @@ } - (void)parseBuffer: (const void *)buffer length: (size_t)length { - [self XMPP_parseBuffer: buffer + [self xmpp_parseBuffer: buffer length: length]; [_oldParser release]; [_oldElementBuilder release]; _oldParser = nil; _oldElementBuilder = nil; } -- (bool)XMPP_stream: (OFStream *)stream +- (bool)xmpp_stream: (OFStream *)stream didReadIntoBuffer: (char *)buffer length: (size_t)length exception: (OFException *)exception { if (exception != nil) { @@ -421,11 +434,11 @@ [self close]; return false; } @try { - if (![self XMPP_parseBuffer: buffer + if (![self xmpp_parseBuffer: buffer length: length]) return false; } @catch (id e) { [_delegates broadcastSelector: @selector(connection: didThrowException:) @@ -443,11 +456,11 @@ _oldElementBuilder = nil; [_socket asyncReadIntoBuffer: buffer length: BUFFER_LENGTH target: self - selector: @selector(XMPP_stream: + selector: @selector(xmpp_stream: didReadIntoBuffer:length: exception:) context: nil]; return false; @@ -581,31 +594,31 @@ [_socket close]; return; } if (![prefix isEqual: @"stream"]) { - [self XMPP_sendStreamError: @"bad-namespace-prefix" + [self xmpp_sendStreamError: @"bad-namespace-prefix" text: nil]; return; } if (![NS isEqual: XMPP_NS_STREAM]) { - [self XMPP_sendStreamError: @"invalid-namespace" + [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" + [self xmpp_sendStreamError: @"invalid-from" text: nil]; return; } if ([[attribute name] isEqual: @"version"] && ![[attribute stringValue] isEqual: @"1.0"]) { - [self XMPP_sendStreamError: @"unsupported-version" + [self xmpp_sendStreamError: @"unsupported-version" text: nil]; return; } } @@ -626,20 +639,20 @@ [_delegates broadcastSelector: @selector(connection:didReceiveElement:) withObject: self withObject: element]; if ([[element namespace] isEqual: XMPP_NS_CLIENT]) - [self XMPP_handleStanza: element]; + [self xmpp_handleStanza: element]; if ([[element namespace] isEqual: XMPP_NS_STREAM]) - [self XMPP_handleStream: element]; + [self xmpp_handleStream: element]; if ([[element namespace] isEqual: XMPP_NS_STARTTLS]) - [self XMPP_handleTLS: element]; + [self xmpp_handleTLS: element]; if ([[element namespace] isEqual: XMPP_NS_SASL]) - [self XMPP_handleSASL: element]; + [self xmpp_handleSASL: element]; } - (void)elementBuilder: (OFXMLElementBuilder *)builder didNotExpectCloseTag: (OFString *)name prefix: (OFString *)prefix @@ -651,11 +664,11 @@ else { [self close]; } } -- (void)XMPP_startStream +- (void)xmpp_startStream { OFString *langString = @""; /* Make sure we don't get any old events */ [_parser setDelegate: nil]; @@ -706,38 +719,38 @@ _streamOpen = _needsSession = _encrypted = false; _supportsRosterVersioning = _supportsStreamManagement = false; _lastID = 0; } -- (void)XMPP_handleStanza: (OFXMLElement *)element +- (void)xmpp_handleStanza: (OFXMLElement *)element { if ([[element name] isEqual: @"iq"]) { - [self XMPP_handleIQ: [XMPPIQ stanzaWithElement: element]]; + [self xmpp_handleIQ: [XMPPIQ stanzaWithElement: element]]; return; } if ([[element name] isEqual: @"message"]) { - [self XMPP_handleMessage: + [self xmpp_handleMessage: [XMPPMessage stanzaWithElement: element]]; return; } if ([[element name] isEqual: @"presence"]) { - [self XMPP_handlePresence: + [self xmpp_handlePresence: [XMPPPresence stanzaWithElement: element]]; return; } - [self XMPP_sendStreamError: @"unsupported-stanza-type" + [self xmpp_sendStreamError: @"unsupported-stanza-type" text: nil]; } -- (void)XMPP_handleStream: (OFXMLElement *)element +- (void)xmpp_handleStream: (OFXMLElement *)element { if ([[element name] isEqual: @"features"]) { - [self XMPP_handleFeatures: element]; + [self xmpp_handleFeatures: element]; return; } if ([[element name] isEqual: @"error"]) { OFString *condition, *reason; @@ -834,11 +847,11 @@ } assert(0); } -- (void)XMPP_handleTLS: (OFXMLElement *)element +- (void)xmpp_handleTLS: (OFXMLElement *)element { if ([[element name] isEqual: @"proceed"]) { /* FIXME: Catch errors here */ SSLSocket *newSock; @@ -863,11 +876,11 @@ [_delegates broadcastSelector: @selector( connectionDidUpgradeToTLS:) withObject: self]; /* Stream restart */ - [self XMPP_startStream]; + [self xmpp_startStream]; return; } if ([[element name] isEqual: @"failure"]) @@ -875,11 +888,11 @@ @throw [OFException exception]; assert(0); } -- (void)XMPP_handleSASL: (OFXMLElement *)element +- (void)xmpp_handleSASL: (OFXMLElement *)element { if ([[element name] isEqual: @"challenge"]) { OFXMLElement *responseTag; OFData *challenge = [OFData dataWithBase64EncodedString: [element stringValue]]; @@ -906,11 +919,11 @@ [_delegates broadcastSelector: @selector( connectionWasAuthenticated:) withObject: self]; /* Stream restart */ - [self XMPP_startStream]; + [self xmpp_startStream]; return; } if ([[element name] isEqual: @"failure"]) { @@ -921,11 +934,11 @@ } assert(0); } -- (void)XMPP_handleIQ: (XMPPIQ *)IQ +- (void)xmpp_handleIQ: (XMPPIQ *)IQ { bool handled = false; XMPPCallback *callback; OFString *key; @@ -952,25 +965,25 @@ [self sendStanza: [IQ errorIQWithType: @"cancel" condition: @"service-unavailable"]]; } } -- (void)XMPP_handleMessage: (XMPPMessage *)message +- (void)xmpp_handleMessage: (XMPPMessage *)message { [_delegates broadcastSelector: @selector(connection:didReceiveMessage:) withObject: self withObject: message]; } -- (void)XMPP_handlePresence: (XMPPPresence *)presence +- (void)xmpp_handlePresence: (XMPPPresence *)presence { [_delegates broadcastSelector: @selector(connection:didReceivePresence:) withObject: self withObject: presence]; } -- (void)XMPP_handleFeatures: (OFXMLElement *)element +- (void)xmpp_handleFeatures: (OFXMLElement *)element { OFXMLElement *startTLS = [element elementForName: @"starttls" namespace: XMPP_NS_STARTTLS]; OFXMLElement *bind = [element elementForName: @"bind" namespace: XMPP_NS_BIND]; @@ -998,21 +1011,17 @@ if ([element elementForName: @"sm" namespace: XMPP_NS_SM] != nil) _supportsStreamManagement = true; if (mechs != nil) { - OFEnumerator *enumerator; - OFXMLElement *mech; - - enumerator = [[mechs children] objectEnumerator]; - while ((mech = [enumerator nextObject]) != nil) + for (OFXMLElement *mech in [mechs children]) [mechanisms addObject: [mech stringValue]]; if (_privateKeyFile != nil && _certificateFile != nil && [mechanisms containsObject: @"EXTERNAL"]) { _authModule = [[XMPPEXTERNALAuth alloc] init]; - [self XMPP_sendAuth: @"EXTERNAL"]; + [self xmpp_sendAuth: @"EXTERNAL"]; return; } if ([mechanisms containsObject: @"SCRAM-SHA-1-PLUS"]) { _authModule = [[XMPPSCRAMAuth alloc] @@ -1019,11 +1028,11 @@ initWithAuthcid: _username password: _password connection: self hash: [OFSHA1Hash class] plusAvailable: true]; - [self XMPP_sendAuth: @"SCRAM-SHA-1-PLUS"]; + [self xmpp_sendAuth: @"SCRAM-SHA-1-PLUS"]; return; } if ([mechanisms containsObject: @"SCRAM-SHA-1"]) { _authModule = [[XMPPSCRAMAuth alloc] @@ -1030,19 +1039,19 @@ initWithAuthcid: _username password: _password connection: self hash: [OFSHA1Hash class] plusAvailable: false]; - [self XMPP_sendAuth: @"SCRAM-SHA-1"]; + [self xmpp_sendAuth: @"SCRAM-SHA-1"]; return; } if ([mechanisms containsObject: @"PLAIN"] && _encrypted) { _authModule = [[XMPPPLAINAuth alloc] initWithAuthcid: _username password: _password]; - [self XMPP_sendAuth: @"PLAIN"]; + [self xmpp_sendAuth: @"PLAIN"]; return; } assert(0); } @@ -1050,18 +1059,18 @@ if (session != nil && [session elementForName: @"optional" namespace: XMPP_NS_SESSION] == nil) _needsSession = true; if (bind != nil) { - [self XMPP_sendResourceBind]; + [self xmpp_sendResourceBind]; return; } assert(0); } -- (void)XMPP_sendAuth: (OFString *)authName +- (void)xmpp_sendAuth: (OFString *)authName { OFXMLElement *authTag; OFData *initialMessage = [_authModule initialMessage]; authTag = [OFXMLElement elementWithName: @"auth" @@ -1077,11 +1086,11 @@ } [self sendStanza: authTag]; } -- (void)XMPP_sendResourceBind +- (void)xmpp_sendResourceBind { XMPPIQ *IQ; OFXMLElement *bind; IQ = [XMPPIQ IQWithType: @"set" @@ -1097,15 +1106,15 @@ [IQ addChild: bind]; [self sendIQ: IQ callbackTarget: self - selector: @selector(XMPP_handleResourceBindForConnection: + selector: @selector(xmpp_handleResourceBindForConnection: IQ:)]; } -- (void)XMPP_sendStreamError: (OFString *)condition +- (void)xmpp_sendStreamError: (OFString *)condition text: (OFString *)text { OFXMLElement *error = [OFXMLElement elementWithName: @"error" namespace: XMPP_NS_STREAM]; @@ -1121,11 +1130,11 @@ [_parser setDelegate: nil]; [self sendStanza: error]; [self close]; } -- (void)XMPP_handleResourceBindForConnection: (XMPPConnection *)connection +- (void)xmpp_handleResourceBindForConnection: (XMPPConnection *)connection IQ: (XMPPIQ *)IQ { OFXMLElement *bindElement, *JIDElement; assert([[IQ type] isEqual: @"result"]); @@ -1138,33 +1147,33 @@ JIDElement = [bindElement elementForName: @"jid" namespace: XMPP_NS_BIND]; _JID = [[XMPPJID alloc] initWithString: [JIDElement stringValue]]; if (_needsSession) { - [self XMPP_sendSession]; + [self xmpp_sendSession]; return; } [_delegates broadcastSelector: @selector(connection:wasBoundToJID:) withObject: self withObject: _JID]; } -- (void)XMPP_sendSession +- (void)xmpp_sendSession { XMPPIQ *IQ = [XMPPIQ IQWithType: @"set" ID: [self generateStanzaID]]; [IQ addChild: [OFXMLElement elementWithName: @"session" namespace: XMPP_NS_SESSION]]; [self sendIQ: IQ callbackTarget: self - selector: @selector(XMPP_handleSessionForConnection:IQ:)]; + selector: @selector(xmpp_handleSessionForConnection:IQ:)]; } -- (void)XMPP_handleSessionForConnection: (XMPPConnection *)connection +- (void)xmpp_handleSessionForConnection: (XMPPConnection *)connection IQ: (XMPPIQ *)IQ { if (![[IQ type] isEqual: @"result"]) OF_ENSURE(0); @@ -1171,11 +1180,11 @@ [_delegates broadcastSelector: @selector(connection:wasBoundToJID:) withObject: self withObject: _JID]; } -- (OFString *)XMPP_IDNAToASCII: (OFString *)domain +- (OFString *)xmpp_IDNAToASCII: (OFString *)domain { OFString *ret; char *cDomain; Idna_rc rc; @@ -1217,10 +1226,10 @@ - (void)removeDelegate: (id )delegate { [_delegates removeDelegate: delegate]; } -- (XMPPMulticastDelegate *)XMPP_delegates +- (XMPPMulticastDelegate *)xmpp_delegates { return _delegates; } @end