Index: src/IRCConnection.h ================================================================== --- src/IRCConnection.h +++ src/IRCConnection.h @@ -32,12 +32,11 @@ @optional - (void)connection: (IRCConnection *)connection didCreateSocket: (OF_KINDOF(OFTCPSocket *))socket; - (void)connection: (IRCConnection *)connection didReceiveLine: (OFString *)line; -- (void)connection: (IRCConnection *)connection - didSendLine: (OFString *)line; +- (void)connection: (IRCConnection *)connection didSendLine: (OFString *)line; - (void)connectionWasEstablished: (IRCConnection *)connection; - (void)connection: (IRCConnection *)connection didFailToConnectWithException: (id)exception; - (void)connection: (IRCConnection *)connection didSeeUser: (IRCUser *)user @@ -84,12 +83,12 @@ uint16_t _port; OFString *_Nullable _nickname, *_Nullable _username; OFString *_Nullable _realname; OFMutableDictionary OF_GENERIC(OFString *, OFMutableSet *) *_channels; id _Nullable _delegate; - of_string_encoding_t _fallbackEncoding; - of_time_interval_t _pingInterval, _pingTimeout; + OFStringEncoding _fallbackEncoding; + OFTimeInterval _pingInterval, _pingTimeout; OFString *_Nullable _pingData; OFTimer *_Nullable _pingTimer; bool _fallbackEncodingUsed; } @@ -100,30 +99,27 @@ OFString *nickname, *username, *realname; @property OF_NULLABLE_PROPERTY (assign, nonatomic) id delegate; @property OF_NULLABLE_PROPERTY (readonly, nonatomic) OF_KINDOF(OFTCPSocket *) socket; -@property (nonatomic) of_string_encoding_t fallbackEncoding; -@property (nonatomic) of_time_interval_t pingInterval, pingTimeout; +@property (nonatomic) OFStringEncoding fallbackEncoding; +@property (nonatomic) OFTimeInterval pingInterval, pingTimeout; + (instancetype)connection; - (void)sendLine: (OFString *)line; - (void)sendLineWithFormat: (OFConstantString *)line, ...; - (void)connect; - (void)disconnect; - (void)disconnectWithReason: (nullable OFString *)reason; - (void)joinChannel: (OFString *)channelName; - (void)leaveChannel: (OFString *)channel; -- (void)leaveChannel: (OFString *)channel - reason: (nullable OFString *)reason; -- (void)sendMessage: (OFString *)message - to: (OFString *)to; -- (void)sendNotice: (OFString *)notice - to: (OFString *)to; +- (void)leaveChannel: (OFString *)channel reason: (nullable OFString *)reason; +- (void)sendMessage: (OFString *)message to: (OFString *)to; +- (void)sendNotice: (OFString *)notice to: (OFString *)to; - (void)kickUser: (OFString *)user channel: (OFString *)channel reason: (nullable OFString *)reason; - (void)changeNicknameTo: (OFString *)nickname; - (nullable OFSet OF_GENERIC(OFString *) *)usersInChannel: (OFString *)channel; @end OF_ASSUME_NONNULL_END Index: src/IRCConnection.m ================================================================== --- src/IRCConnection.m +++ src/IRCConnection.m @@ -59,11 +59,11 @@ @try { _socketClass = [OFTCPSocket class]; _channels = [[OFMutableDictionary alloc] init]; _port = 6667; - _fallbackEncoding = OF_STRING_ENCODING_ISO_8859_1; + _fallbackEncoding = OFStringEncodingISO8859_1; _pingInterval = 120; _pingTimeout = 30; } @catch (id e) { [self release]; @throw e; @@ -93,12 +93,11 @@ if (_socket != nil) @throw [OFAlreadyConnectedException exception]; _socket = [[_socketClass alloc] init]; [_socket setDelegate: self]; - [_socket asyncConnectToHost: _server - port: _port]; + [_socket asyncConnectToHost: _server port: _port]; objc_autoreleasePoolPop(pool); } - (void)socket: (OF_KINDOF(OFTCPSocket *))socket @@ -115,12 +114,11 @@ return; } if ([_delegate respondsToSelector: @selector(connection:didCreateSocket:)]) - [_delegate connection: self - didCreateSocket: _socket]; + [_delegate connection: self didCreateSocket: _socket]; [self sendLineWithFormat: @"NICK %@", _nickname]; [self sendLineWithFormat: @"USER %@ * 0 :%@", _username, _realname]; [socket asyncReadLine]; @@ -147,30 +145,28 @@ - (void)joinChannel: (OFString *)channel { void *pool = objc_autoreleasePoolPush(); - channel = [[channel componentsSeparatedByString: @"\n"] firstObject]; + channel = [channel componentsSeparatedByString: @"\n"].firstObject; [self sendLineWithFormat: @"JOIN %@", channel]; objc_autoreleasePoolPop(pool); } - (void)leaveChannel: (OFString *)channel { - [self leaveChannel: channel - reason: nil]; + [self leaveChannel: channel reason: nil]; } -- (void)leaveChannel: (OFString *)channel - reason: (OFString *)reason +- (void)leaveChannel: (OFString *)channel reason: (OFString *)reason { void *pool = objc_autoreleasePoolPush(); - channel = [[channel componentsSeparatedByString: @"\n"] firstObject]; - reason = [[reason componentsSeparatedByString: @"\n"] firstObject]; + channel = [channel componentsSeparatedByString: @"\n"].firstObject; + reason = [reason componentsSeparatedByString: @"\n"].firstObject; if (reason == nil) [self sendLineWithFormat: @"PART %@", channel]; else [self sendLineWithFormat: @"PART %@ :%@", channel, reason]; @@ -181,12 +177,11 @@ } - (void)sendLine: (OFString *)line { if ([_delegate respondsToSelector: @selector(connection:didSendLine:)]) - [_delegate connection: self - didSendLine: line]; + [_delegate connection: self didSendLine: line]; [_socket writeLine: line]; } - (void)sendLineWithFormat: (OFConstantString *)format, ... @@ -203,23 +198,21 @@ [self sendLine: line]; objc_autoreleasePoolPop(pool); } -- (void)sendMessage: (OFString *)message - to: (OFString *)to +- (void)sendMessage: (OFString *)message to: (OFString *)to { void *pool = objc_autoreleasePoolPush(); for (OFString *line in [message componentsSeparatedByString: @"\n"]) [self sendLineWithFormat: @"PRIVMSG %@ :%@", to, line]; objc_autoreleasePoolPop(pool); } -- (void)sendNotice: (OFString *)notice - to: (OFString *)to +- (void)sendNotice: (OFString *)notice to: (OFString *)to { void *pool = objc_autoreleasePoolPush(); for (OFString *line in [notice componentsSeparatedByString: @"\n"]) [self sendLineWithFormat: @"NOTICE %@ :%@", to, line]; @@ -242,12 +235,11 @@ - (void)changeNicknameTo: (OFString *)nickname { void *pool = objc_autoreleasePoolPush(); - nickname = [[nickname componentsSeparatedByString: @"\n"] - firstObject]; + nickname = [nickname componentsSeparatedByString: @"\n"].firstObject; [self sendLineWithFormat: @"NICK %@", nickname]; objc_autoreleasePoolPop(pool); } @@ -257,28 +249,27 @@ OFArray *components; OFString *action = nil; if ([_delegate respondsToSelector: @selector(connection:didReceiveLine:)]) - [_delegate connection: self - didReceiveLine: line]; + [_delegate connection: self didReceiveLine: line]; components = [line componentsSeparatedByString: @" "]; /* PING */ if ([components count] == 2 && [[components firstObject] isEqual: @"PING"]) { OFMutableString *s = [[line mutableCopy] autorelease]; - [s replaceCharactersInRange: of_range(0, 4) + [s replaceCharactersInRange: OFRangeMake(0, 4) withString: @"PONG"]; [self sendLine: s]; return; } /* PONG */ - if ([components count] == 4 && + if (components.count == 4 && [[components objectAtIndex: 1] isEqual: @"PONG"] && [[components objectAtIndex: 3] isEqual: _pingData]) { [_pingTimer invalidate]; [_pingData release]; @@ -289,11 +280,11 @@ } action = [[components objectAtIndex: 1] uppercaseString]; /* Connected */ - if ([action isEqual: @"001"] && [components count] >= 4) { + if ([action isEqual: @"001"] && components.count >= 4) { if ([_delegate respondsToSelector: @selector(connectionWasEstablished:)]) [_delegate connectionWasEstablished: self]; [OFTimer scheduledTimerWithTimeInterval: _pingInterval @@ -303,28 +294,27 @@ return; } /* JOIN */ - if ([action isEqual: @"JOIN"] && [components count] == 3) { + if ([action isEqual: @"JOIN"] && components.count == 3) { OFString *who = [components objectAtIndex: 0]; OFString *where = [components objectAtIndex: 2]; IRCUser *user; OFMutableSet *channel; - who = [who substringWithRange: of_range(1, [who length] - 1)]; + who = [who substringWithRange: OFRangeMake(1, who.length - 1)]; user = [IRCUser IRCUserWithString: who]; if ([who hasPrefix: [_nickname stringByAppendingString: @"!"]]) { channel = [OFMutableSet set]; - [_channels setObject: channel - forKey: where]; + [_channels setObject: channel forKey: where]; } else channel = [_channels objectForKey: where]; - [channel addObject: [user nickname]]; + [channel addObject: user.nickname]; if ([_delegate respondsToSelector: @selector(connection:didSeeUser:joinChannel:)]) [_delegate connection: self didSeeUser: user @@ -332,11 +322,11 @@ return; } /* NAMES reply */ - if ([action isEqual: @"353"] && [components count] >= 6) { + if ([action isEqual: @"353"] && components.count >= 6) { OFString *where; OFMutableSet *channel; OFArray *users; size_t pos; @@ -352,18 +342,18 @@ [[components objectAtIndex: 2] length] + [[components objectAtIndex: 3] length] + [[components objectAtIndex: 4] length] + 6; users = [[line substringWithRange: - of_range(pos, [line length] - pos)] + OFRangeMake(pos, line.length - pos)] componentsSeparatedByString: @" "]; for (OFString *user in users) { if ([user hasPrefix: @"@"] || [user hasPrefix: @"+"] || [user hasPrefix: @"%"] || [user hasPrefix: @"*"]) user = [user substringWithRange: - of_range(1, [user length] - 1)]; + OFRangeMake(1, user.length - 1)]; [channel addObject: user]; } if ([_delegate respondsToSelector: @@ -379,22 +369,22 @@ OFString *who = [components objectAtIndex: 0]; OFString *where = [components objectAtIndex: 2]; IRCUser *user; OFMutableSet *channel; OFString *reason = nil; - size_t pos = [who length] + 1 + - [[components objectAtIndex: 1] length] + 1 + [where length]; + size_t pos = who.length + 1 + + [[components objectAtIndex: 1] length] + 1 + where.length; - who = [who substringWithRange: of_range(1, [who length] - 1)]; + who = [who substringWithRange: OFRangeMake(1, who.length - 1)]; user = [IRCUser IRCUserWithString: who]; channel = [_channels objectForKey: where]; - if ([components count] > 3) + if (components.count > 3) reason = [line substringWithRange: - of_range(pos + 2, [line length] - pos - 2)]; + OFRangeMake(pos + 2, line.length - pos - 2)]; - [channel removeObject: [user nickname]]; + [channel removeObject: user.nickname]; if ([_delegate respondsToSelector: @selector(connection:didSeeUser:leaveChannel:reason:)]) [_delegate connection: self didSeeUser: user @@ -403,30 +393,30 @@ return; } /* KICK */ - if ([action isEqual: @"KICK"] && [components count] >= 4) { + if ([action isEqual: @"KICK"] && components.count >= 4) { OFString *who = [components objectAtIndex: 0]; OFString *where = [components objectAtIndex: 2]; OFString *whom = [components objectAtIndex: 3]; IRCUser *user; OFMutableSet *channel; OFString *reason = nil; - size_t pos = [who length] + 1 + + size_t pos = who.length + 1 + [[components objectAtIndex: 1] length] + 1 + - [where length] + 1 + [whom length]; + where.length + 1 + whom.length; - who = [who substringWithRange: of_range(1, [who length] - 1)]; + who = [who substringWithRange: OFRangeMake(1, who.length - 1)]; user = [IRCUser IRCUserWithString: who]; channel = [_channels objectForKey: where]; if ([components count] > 4) reason = [line substringWithRange: - of_range(pos + 2, [line length] - pos - 2)]; + OFRangeMake(pos + 2, line.length - pos - 2)]; - [channel removeObject: [user nickname]]; + [channel removeObject: user.nickname]; if ([_delegate respondsToSelector: @selector(connection:didSeeUser:kickUser:channel:reason:)]) [_delegate connection: self didSeeUser: user @@ -436,27 +426,27 @@ return; } /* QUIT */ - if ([action isEqual: @"QUIT"] && [components count] >= 2) { + if ([action isEqual: @"QUIT"] && components.count >= 2) { OFString *who = [components objectAtIndex: 0]; IRCUser *user; OFString *reason = nil; - size_t pos = [who length] + 1 + + size_t pos = who.length + 1 + [[components objectAtIndex: 1] length]; - who = [who substringWithRange: of_range(1, [who length] - 1)]; + who = [who substringWithRange: OFRangeMake(1, who.length - 1)]; user = [IRCUser IRCUserWithString: who]; if ([components count] > 2) reason = [line substringWithRange: - of_range(pos + 2, [line length] - pos - 2)]; + OFRangeMake(pos + 2, line.length - pos - 2)]; for (OFString *channel in _channels) [[_channels objectForKey: channel] - removeObject: [user nickname]]; + removeObject: user.nickname]; if ([_delegate respondsToSelector: @selector(connection:didSeeUserQuit:reason:)]) [_delegate connection: self didSeeUserQuit: user @@ -464,29 +454,29 @@ return; } /* NICK */ - if ([action isEqual: @"NICK"] && [components count] == 3) { + if ([action isEqual: @"NICK"] && components.count == 3) { OFString *who = [components objectAtIndex: 0]; OFString *nickname = [components objectAtIndex: 2]; IRCUser *user; - who = [who substringWithRange: of_range(1, [who length] - 1)]; + who = [who substringWithRange: OFRangeMake(1, who.length - 1)]; nickname = [nickname substringWithRange: - of_range(1, [nickname length] - 1)]; + OFRangeMake(1, nickname.length - 1)]; user = [IRCUser IRCUserWithString: who]; - if ([[user nickname] isEqual: _nickname]) { + if ([user.nickname isEqual: _nickname]) { [_nickname release]; _nickname = [nickname copy]; } for (OFMutableSet *channel in _channels) { - if ([channel containsObject: [user nickname]]) { - [channel removeObject: [user nickname]]; + if ([channel containsObject: user.nickname]) { + [channel removeObject: user.nickname]; [channel addObject: nickname]; } } if ([_delegate respondsToSelector: @@ -497,22 +487,22 @@ return; } /* PRIVMSG */ - if ([action isEqual: @"PRIVMSG"] && [components count] >= 4) { + if ([action isEqual: @"PRIVMSG"] && components.count >= 4) { OFString *from = [components objectAtIndex: 0]; OFString *to = [components objectAtIndex: 2]; IRCUser *user; OFString *message; - size_t pos = [from length] + 1 + - [[components objectAtIndex: 1] length] + 1 + [to length]; + size_t pos = from.length + 1 + + [[components objectAtIndex: 1] length] + 1 + to.length; from = [from substringWithRange: - of_range(1, [from length] - 1)]; + OFRangeMake(1, from.length - 1)]; message = [line substringWithRange: - of_range(pos + 2, [line length] - pos - 2)]; + OFRangeMake(pos + 2, line.length - pos - 2)]; user = [IRCUser IRCUserWithString: from]; if (![to isEqual: _nickname]) { if ([_delegate respondsToSelector: @selector(connection: didReceiveMessage:channel:user:)]) @@ -530,22 +520,22 @@ return; } /* NOTICE */ - if ([action isEqual: @"NOTICE"] && [components count] >= 4) { + if ([action isEqual: @"NOTICE"] && components.count >= 4) { OFString *from = [components objectAtIndex: 0]; OFString *to = [components objectAtIndex: 2]; IRCUser *user = nil; OFString *notice; - size_t pos = [from length] + 1 + - [[components objectAtIndex: 1] length] + 1 + [to length]; + size_t pos = from.length + 1 + + [[components objectAtIndex: 1] length] + 1 + to.length; from = [from substringWithRange: - of_range(1, [from length] - 1)]; + OFRangeMake(1, from.length - 1)]; notice = [line substringWithRange: - of_range(pos + 2, [line length] - pos - 2)]; + OFRangeMake(pos + 2, line.length - pos - 2)]; if (![from containsString: @"!"] || [to isEqual: @"*"]) { /* System message - ignore for now */ return; } @@ -573,10 +563,12 @@ - (void)irc_sendPing { [_pingData release]; [_pingTimer release]; + _pingData = nil; + _pingTimer = nil; _pingData = [[OFString alloc] initWithFormat: @":%d", rand()]; [_socket writeFormat: @"PING %@\r\n", _pingData]; _pingTimer = [[OFTimer @@ -621,12 +613,11 @@ if ([_delegate respondsToSelector: @selector(connectionWasClosed:)]) [_delegate connectionWasClosed: self]; [_pingTimer invalidate]; - [_socket performSelector: @selector(cancelAsyncRequests) - afterDelay: 0]; + [_socket performSelector: @selector(cancelAsyncRequests) afterDelay: 0]; [_socket release]; _socket = nil; return false; } Index: src/IRCUser.m ================================================================== --- src/IRCUser.m +++ src/IRCUser.m @@ -48,14 +48,11 @@ self = [super init]; @try { char *tmp; - if ((tmp2 = strdup([string UTF8String])) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: - [string UTF8StringLength]]; + tmp2 = OFStrDup(string.UTF8String); if ((tmp = strchr(tmp2, '@')) == NULL) @throw [OFInvalidFormatException exception]; *tmp = '\0'; Index: tests/tests.m ================================================================== --- tests/tests.m +++ tests/tests.m @@ -35,29 +35,27 @@ @implementation TestApp - (void)applicationDidFinishLaunching { IRCConnection *connection = [[IRCConnection alloc] init]; - [connection setServer: @"irc.freenode.net"]; - [connection setNickname: @"ObjIRC"]; - [connection setUsername: @"ObjIRC"]; - [connection setRealname: @"ObjIRC"]; - [connection setDelegate: self]; + connection.server = @"irc.freenode.net"; + connection.nickname = @"ObjIRC"; + connection.username = @"ObjIRC"; + connection.realname = @"ObjIRC"; + connection.delegate = self; [connection connect]; } -- (void)connection: (IRCConnection*)connection - didReceiveLine: (OFString*)line +- (void)connection: (IRCConnection*)connection didReceiveLine: (OFString*)line { - [of_stderr writeFormat: @"> %@\n", line]; + [OFStdErr writeFormat: @"> %@\n", line]; } -- (void)connection: (IRCConnection*)connection - didSendLine: (OFString*)line +- (void)connection: (IRCConnection*)connection didSendLine: (OFString*)line { - [of_stderr writeFormat: @"< %@\n", line]; + [OFStdErr writeFormat: @"< %@\n", line]; } - (void)connectionWasEstablished: (IRCConnection*)connection { [connection joinChannel: @"#objfw"]; @@ -64,92 +62,92 @@ } - (void)connection: (IRCConnection *)connection didFailToConnectWithException: (id)exception { - [of_stderr writeFormat: @"Failed to connect: %@\n", exception]; + [OFStdErr writeFormat: @"Failed to connect: %@\n", exception]; [OFApplication terminateWithStatus: 1]; } - (void)connection: (IRCConnection*)connection didSeeUser: (IRCUser*)user joinChannel: (OFString*)channel { - of_log(@"%@ joined %@.", user, channel); + OFLog(@"%@ joined %@.", user, channel); } - (void)connection: (IRCConnection*)connection didSeeUser: (IRCUser*)user leaveChannel: (OFString*)channel reason: (OFString*)reason { - of_log(@"%@ left %@ (%@).", user, channel, reason); + OFLog(@"%@ left %@ (%@).", user, channel, reason); } - (void)connection: (IRCConnection*)connection didSeeUser: (IRCUser*)user kickUser: (OFString*)kickedUser channel: (OFString*)channel reason: (OFString*)reason { - of_log(@"%@ kicked %@ from %@: %@", user, kickedUser, channel, reason); + OFLog(@"%@ kicked %@ from %@: %@", user, kickedUser, channel, reason); } - (void)connection: (IRCConnection*)connection didSeeUserQuit: (IRCUser*)user reason: (OFString*)reason { - of_log(@"%@ quit (%@).", user, reason); + OFLog(@"%@ quit (%@).", user, reason); } - (void)connection: (IRCConnection*)connection didSeeUser: (IRCUser*)user changeNicknameTo: (OFString *)nickname { - of_log(@"%@ changed nick to %@.", user, nickname); + OFLog(@"%@ changed nick to %@.", user, nickname); } - (void)connection: (IRCConnection*)connection didReceiveMessage: (OFString*)msg channel: (OFString*)channel user: (IRCUser*)user { - of_log(@"[%@] %@: %@", channel, [user nickname], msg); + OFLog(@"[%@] %@: %@", channel, [user nickname], msg); } - (void)connection: (IRCConnection*)connection didReceivePrivateMessage: (OFString*)msg user: (IRCUser*)user { - of_log(@"(%@): %@", user, msg); + OFLog(@"(%@): %@", user, msg); } - (void)connection: (IRCConnection*)connection didReceiveNotice: (OFString*)notice channel: (OFString*)channel user: (IRCUser*)user { - of_log(@"NOTICE: [%@] %@: %@", channel, [user nickname], notice); + OFLog(@"NOTICE: [%@] %@: %@", channel, [user nickname], notice); } - (void)connection: (IRCConnection*)connection didReceiveNotice: (OFString*)notice user: (IRCUser*)user { - of_log(@"NOTICE: (%@): %@", user, notice); + OFLog(@"NOTICE: (%@): %@", user, notice); } - (void)connection: (IRCConnection*)connection didReceiveNamesForChannel: (OFString*)channel { - of_log(@"Users in %@: %@", channel, + OFLog(@"Users in %@: %@", channel, [connection usersInChannel: channel]); } - (void)connectionWasClosed: (IRCConnection*)connection { - of_log(@"Disconnected!"); + OFLog(@"Disconnected!"); [OFApplication terminate]; } @end