From 9e71cf9de705503059499c970ec4516ebd4b79c9 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Fri, 12 Jul 2013 14:12:16 +0000 Subject: [PATCH] Adjust to ObjFW API and fix a disconnect bug. FossilOrigin-Name: 18137016905f0581bc493e61961522356acab11c15d53f561447045acc6e202c --- src/IRCConnection.h | 1 + src/IRCConnection.m | 48 ++++++++++++++++++++++++++++++--------------- src/IRCUser.h | 2 +- src/IRCUser.m | 18 ++++++++--------- 4 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/IRCConnection.h b/src/IRCConnection.h index e4b9ca6..ce18ecf 100644 --- a/src/IRCConnection.h +++ b/src/IRCConnection.h @@ -89,6 +89,7 @@ @property (readonly, retain) OFTCPSocket *socket; #endif ++ (instancetype)connection; - (void)setServer: (OFString*)server; - (OFString*)server; - (void)setPort: (uint16_t)port; diff --git a/src/IRCConnection.m b/src/IRCConnection.m index 46574d5..8ce7c67 100644 --- a/src/IRCConnection.m +++ b/src/IRCConnection.m @@ -38,6 +38,11 @@ #import "IRCUser.h" @implementation IRCConnection ++ (instancetype)connection +{ + return [[[self alloc] init] autorelease]; +} + - init { self = [super init]; @@ -67,12 +72,12 @@ - (void)setServer: (OFString*)server { - OF_SETTER(_server, server, YES, YES) + OF_SETTER(_server, server, true, 1) } - (OFString*)server { - OF_GETTER(_server, YES) + OF_GETTER(_server, true) } - (void)setPort: (uint16_t)port @@ -87,32 +92,32 @@ - (void)setNickname: (OFString*)nickname { - OF_SETTER(_nickname, nickname, YES, YES) + OF_SETTER(_nickname, nickname, true, 1) } - (OFString*)nickname { - OF_GETTER(_nickname, YES) + OF_GETTER(_nickname, true) } - (void)setUsername: (OFString*)username { - OF_SETTER(_username, username, YES, YES) + OF_SETTER(_username, username, true, 1) } - (OFString*)username { - OF_GETTER(_username, YES) + OF_GETTER(_username, true) } - (void)setRealname: (OFString*)realname { - OF_SETTER(_realname, realname, YES, YES) + OF_SETTER(_realname, realname, true, 1) } - (OFString*)realname { - OF_GETTER(_realname, YES) + OF_GETTER(_realname, true) } - (void)setDelegate: (id )delegate @@ -122,18 +127,21 @@ - (id )delegate { - OF_GETTER(_delegate, NO) + OF_GETTER(_delegate, false) } - (OFTCPSocket*)socket { - OF_GETTER(_socket, YES) + OF_GETTER(_socket, true) } - (void)connect { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + if (_socket != nil) + @throw [OFAlreadyConnectedException exception]; + _socket = [[OFTCPSocket alloc] init]; [_socket connectToHost: _server port: _port]; @@ -568,7 +576,7 @@ [pool release]; } -- (BOOL)socket: (OFTCPSocket*)socket +- (bool)socket: (OFTCPSocket*)socket didReceiveISO88591Line: (OFString*)line exception: (OFException*)exception { @@ -580,26 +588,34 @@ exception:)]; } - return NO; + return false; } -- (BOOL)socket: (OFTCPSocket*)socket +- (bool)socket: (OFTCPSocket*)socket didReceiveLine: (OFString*)line exception: (OFException*)exception { if (line != nil) { [self IRC_processLine: line]; - return YES; + return true; } - if ([exception isKindOfClass: [OFInvalidEncodingException class]]) + if ([exception isKindOfClass: [OFInvalidEncodingException class]]) { [socket asyncReadLineWithEncoding: OF_STRING_ENCODING_ISO_8859_1 target: self selector: @selector(socket: didReceiveISO88591Line: exception:)]; + return false; + } - return NO; + if ([_delegate respondsToSelector: @selector(connectionWasClosed:)]) { + [_delegate connectionWasClosed: self]; + [_socket release]; + _socket = nil; + } + + return false; } - (void)handleConnection diff --git a/src/IRCUser.h b/src/IRCUser.h index 5db2906..89b4965 100644 --- a/src/IRCUser.h +++ b/src/IRCUser.h @@ -31,7 +31,7 @@ @property (copy, readonly) OFString *nickname, *username, *hostname; #endif -+ IRCUserWithString: (OFString*)string; ++ (instancetype)IRCUserWithString: (OFString*)string; - initWithString: (OFString*)string; - (OFString*)nickname; - (OFString*)username; diff --git a/src/IRCUser.m b/src/IRCUser.m index 1f98352..f47342a 100644 --- a/src/IRCUser.m +++ b/src/IRCUser.m @@ -33,7 +33,7 @@ #import "IRCUser.h" @implementation IRCUser -+ IRCUserWithString: (OFString*)string ++ (instancetype)IRCUserWithString: (OFString*)string { return [[[self alloc] initWithString: string] autorelease]; } @@ -49,19 +49,17 @@ if ((tmp2 = strdup([string UTF8String])) == NULL) @throw [OFOutOfMemoryException - exceptionWithClass: [self class] - requestedSize: [string UTF8StringLength]]; + exceptionWithRequestedSize: + [string UTF8StringLength]]; if ((tmp = strchr(tmp2, '@')) == NULL) - @throw [OFInvalidFormatException - exceptionWithClass: [self class]]; + @throw [OFInvalidFormatException exception]; *tmp = '\0'; _hostname = [[OFString alloc] initWithUTF8String: tmp + 1]; if ((tmp = strchr(tmp2, '!')) == NULL) - @throw [OFInvalidFormatException - exceptionWithClass: [self class]]; + @throw [OFInvalidFormatException exception]; *tmp = '\0'; _username = [[OFString alloc] initWithUTF8String: tmp + 1]; @@ -89,17 +87,17 @@ - (OFString*)username { - OF_GETTER(_username, YES) + OF_GETTER(_username, true) } - (OFString*)nickname { - OF_GETTER(_nickname, YES) + OF_GETTER(_nickname, true) } - (OFString*)hostname { - OF_GETTER(_hostname, YES) + OF_GETTER(_hostname, true) } - copy