@@ -26,20 +26,23 @@ #import #import #import #import -#import #import #import #import "IRCConnection.h" #import "IRCUser.h" @implementation IRCConnection +@synthesize server = _server, port = _port; +@synthesize nickname = _nickname, username = _username, realname = _realname; +@synthesize delegate = _delegate, socket = _socket; + + (instancetype)connection { return [[[self alloc] init] autorelease]; } @@ -68,78 +71,13 @@ [_channels release]; [super dealloc]; } -- (void)setServer: (OFString*)server -{ - OF_SETTER(_server, server, true, 1) -} - -- (OFString*)server -{ - OF_GETTER(_server, true) -} - -- (void)setPort: (uint16_t)port -{ - _port = port; -} - -- (uint16_t)port -{ - return _port; -} - -- (void)setNickname: (OFString*)nickname -{ - OF_SETTER(_nickname, nickname, true, 1) -} - -- (OFString*)nickname -{ - OF_GETTER(_nickname, true) -} - -- (void)setUsername: (OFString*)username -{ - OF_SETTER(_username, username, true, 1) -} - -- (OFString*)username -{ - OF_GETTER(_username, true) -} - -- (void)setRealname: (OFString*)realname -{ - OF_SETTER(_realname, realname, true, 1) -} - -- (OFString*)realname -{ - OF_GETTER(_realname, true) -} - -- (void)setDelegate: (id )delegate -{ - _delegate = delegate; -} - -- (id )delegate -{ - OF_GETTER(_delegate, false) -} - -- (OFTCPSocket*)socket -{ - OF_GETTER(_socket, true) -} - - (void)connect { - OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + void *pool = objc_autoreleasePoolPush(); if (_socket != nil) @throw [OFAlreadyConnectedException exception]; _socket = [[OFTCPSocket alloc] init]; @@ -147,33 +85,41 @@ port: _port]; [self sendLineWithFormat: @"NICK %@", _nickname]; [self sendLineWithFormat: @"USER %@ * 0 :%@", _username, _realname]; - [pool release]; + objc_autoreleasePoolPop(pool); } - (void)disconnect { [self disconnectWithReason: nil]; } - (void)disconnectWithReason: (OFString*)reason { + void *pool = objc_autoreleasePoolPush(); + reason = [[reason componentsSeparatedByString: @"\n"] firstObject]; if (reason == nil) [self sendLine: @"QUIT"]; else [self sendLineWithFormat: @"QUIT :%@", reason]; + + objc_autoreleasePoolPop(pool); } - (void)joinChannel: (OFString*)channel { + void *pool = objc_autoreleasePoolPush(); + channel = [[channel componentsSeparatedByString: @"\n"] firstObject]; [self sendLineWithFormat: @"JOIN %@", channel]; + + objc_autoreleasePoolPop(pool); } - (void)leaveChannel: (OFString*)channel { [self leaveChannel: channel @@ -181,19 +127,23 @@ } - (void)leaveChannel: (OFString*)channel reason: (OFString*)reason { + void *pool = objc_autoreleasePoolPush(); + channel = [[channel componentsSeparatedByString: @"\n"] firstObject]; reason = [[reason componentsSeparatedByString: @"\n"] firstObject]; if (reason == nil) [self sendLineWithFormat: @"PART %@", channel]; else [self sendLineWithFormat: @"PART %@ :%@", channel, reason]; [_channels removeObjectForKey: channel]; + + objc_autoreleasePoolPop(pool); } - (void)sendLine: (OFString*)line { if ([_delegate respondsToSelector: @selector(connection:didSendLine:)]) @@ -203,11 +153,11 @@ [_socket writeLine: line]; } - (void)sendLineWithFormat: (OFConstantString*)format, ... { - OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + void *pool = objc_autoreleasePoolPush(); OFString *line; va_list args; va_start(args, format); line = [[[OFString alloc] initWithFormat: format @@ -214,50 +164,58 @@ arguments: args] autorelease]; va_end(args); [self sendLine: line]; - [pool release]; + objc_autoreleasePoolPop(pool); } - (void)sendMessage: (OFString*)msg to: (OFString*)to { - OFArray *lines = [msg componentsSeparatedByString: @"\n"]; - OFEnumerator *enumerator = [lines objectEnumerator]; - OFString *line; + void *pool = objc_autoreleasePoolPush(); - while ((line = [enumerator nextObject]) != nil) + for (OFString *line in [msg componentsSeparatedByString: @"\n"]) [self sendLineWithFormat: @"PRIVMSG %@ :%@", to, line]; + + objc_autoreleasePoolPop(pool); } - (void)sendNotice: (OFString*)notice to: (OFString*)to { - OFArray *lines = [notice componentsSeparatedByString: @"\n"]; - OFEnumerator *enumerator = [lines objectEnumerator]; - OFString *line; + void *pool = objc_autoreleasePoolPush(); - while ((line = [enumerator nextObject]) != nil) + for (OFString *line in [notice componentsSeparatedByString: @"\n"]) [self sendLineWithFormat: @"NOTICE %@ :%@", to, line]; + + objc_autoreleasePoolPop(pool); } - (void)kickUser: (OFString*)user channel: (OFString*)channel reason: (OFString*)reason { + void *pool = objc_autoreleasePoolPush(); + reason = [[reason componentsSeparatedByString: @"\n"] firstObject]; [self sendLineWithFormat: @"KICK %@ %@ :%@", channel, user, reason]; + + objc_autoreleasePoolPop(pool); } - (void)changeNicknameTo: (OFString*)nickname { + void *pool = objc_autoreleasePoolPush(); + nickname = [[nickname componentsSeparatedByString: @"\n"] firstObject]; [self sendLineWithFormat: @"NICK %@", nickname]; + + objc_autoreleasePoolPop(pool); } - (void)IRC_processLine: (OFString*)line { OFArray *components; @@ -325,12 +283,10 @@ if ([action isEqual: @"353"] && [components count] >= 6) { OFString *where; OFMutableSet *channel; OFArray *users; size_t pos; - OFEnumerator *enumerator; - OFString *user; where = [components objectAtIndex: 4]; if ((channel = [_channels objectForKey: where]) == nil) { /* We did not request that */ @@ -345,12 +301,11 @@ users = [[line substringWithRange: of_range(pos, [line length] - pos)] componentsSeparatedByString: @" "]; - enumerator = [users objectEnumerator]; - while ((user = [enumerator nextObject]) != nil) { + for (OFString *user in users) { if ([user hasPrefix: @"@"] || [user hasPrefix: @"+"] || [user hasPrefix: @"%"] || [user hasPrefix: @"*"]) user = [user substringWithRange: of_range(1, [user length] - 1)]; @@ -433,22 +388,19 @@ OFString *who = [components objectAtIndex: 0]; IRCUser *user; OFString *reason = nil; size_t pos = [who length] + 1 + [[components objectAtIndex: 1] length]; - OFEnumerator *enumerator; - OFMutableSet *channel; who = [who substringWithRange: of_range(1, [who length] - 1)]; user = [IRCUser IRCUserWithString: who]; if ([components count] > 2) reason = [line substringWithRange: of_range(pos + 2, [line length] - pos - 2)]; - enumerator = [_channels objectEnumerator]; - while ((channel = [enumerator nextObject]) != nil) + for (OFMutableSet *channel in _channels) [channel removeObject: [user nickname]]; if ([_delegate respondsToSelector: @selector(connection:didSeeUserQuit:reason:)]) [_delegate connection: self @@ -461,12 +413,10 @@ /* NICK */ if ([action isEqual: @"NICK"] && [components count] == 3) { OFString *who = [components objectAtIndex: 0]; OFString *nickname = [components objectAtIndex: 2]; IRCUser *user; - OFEnumerator *enumerator; - OFMutableSet *channel; who = [who substringWithRange: of_range(1, [who length] - 1)]; nickname = [nickname substringWithRange: of_range(1, [nickname length] - 1)]; @@ -475,12 +425,11 @@ if ([[user nickname] isEqual: _nickname]) { [_nickname release]; _nickname = [nickname copy]; } - enumerator = [_channels objectEnumerator]; - while ((channel = [enumerator nextObject]) != nil) { + for (OFMutableSet *channel in _channels) { if ([channel containsObject: [user nickname]]) { [channel removeObject: [user nickname]]; [channel addObject: nickname]; } } @@ -567,15 +516,15 @@ } } - (void)processLine: (OFString*)line { - OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + void *pool = objc_autoreleasePoolPush(); [self IRC_processLine: line]; - [pool release]; + objc_autoreleasePoolPop(pool); } - (bool)socket: (OFTCPSocket*)socket didReceiveISO88591Line: (OFString*)line exception: (OFException*)exception