Adjust to ObjFW API and fix a disconnect bug.

FossilOrigin-Name: 18137016905f0581bc493e61961522356acab11c15d53f561447045acc6e202c
This commit is contained in:
Jonathan Schleifer 2013-07-12 14:12:16 +00:00
parent 52fcf8b32d
commit 9e71cf9de7
4 changed files with 42 additions and 27 deletions

View file

@ -89,6 +89,7 @@
@property (readonly, retain) OFTCPSocket *socket; @property (readonly, retain) OFTCPSocket *socket;
#endif #endif
+ (instancetype)connection;
- (void)setServer: (OFString*)server; - (void)setServer: (OFString*)server;
- (OFString*)server; - (OFString*)server;
- (void)setPort: (uint16_t)port; - (void)setPort: (uint16_t)port;

View file

@ -38,6 +38,11 @@
#import "IRCUser.h" #import "IRCUser.h"
@implementation IRCConnection @implementation IRCConnection
+ (instancetype)connection
{
return [[[self alloc] init] autorelease];
}
- init - init
{ {
self = [super init]; self = [super init];
@ -67,12 +72,12 @@
- (void)setServer: (OFString*)server - (void)setServer: (OFString*)server
{ {
OF_SETTER(_server, server, YES, YES) OF_SETTER(_server, server, true, 1)
} }
- (OFString*)server - (OFString*)server
{ {
OF_GETTER(_server, YES) OF_GETTER(_server, true)
} }
- (void)setPort: (uint16_t)port - (void)setPort: (uint16_t)port
@ -87,32 +92,32 @@
- (void)setNickname: (OFString*)nickname - (void)setNickname: (OFString*)nickname
{ {
OF_SETTER(_nickname, nickname, YES, YES) OF_SETTER(_nickname, nickname, true, 1)
} }
- (OFString*)nickname - (OFString*)nickname
{ {
OF_GETTER(_nickname, YES) OF_GETTER(_nickname, true)
} }
- (void)setUsername: (OFString*)username - (void)setUsername: (OFString*)username
{ {
OF_SETTER(_username, username, YES, YES) OF_SETTER(_username, username, true, 1)
} }
- (OFString*)username - (OFString*)username
{ {
OF_GETTER(_username, YES) OF_GETTER(_username, true)
} }
- (void)setRealname: (OFString*)realname - (void)setRealname: (OFString*)realname
{ {
OF_SETTER(_realname, realname, YES, YES) OF_SETTER(_realname, realname, true, 1)
} }
- (OFString*)realname - (OFString*)realname
{ {
OF_GETTER(_realname, YES) OF_GETTER(_realname, true)
} }
- (void)setDelegate: (id <IRCConnectionDelegate>)delegate - (void)setDelegate: (id <IRCConnectionDelegate>)delegate
@ -122,18 +127,21 @@
- (id <IRCConnectionDelegate>)delegate - (id <IRCConnectionDelegate>)delegate
{ {
OF_GETTER(_delegate, NO) OF_GETTER(_delegate, false)
} }
- (OFTCPSocket*)socket - (OFTCPSocket*)socket
{ {
OF_GETTER(_socket, YES) OF_GETTER(_socket, true)
} }
- (void)connect - (void)connect
{ {
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
if (_socket != nil)
@throw [OFAlreadyConnectedException exception];
_socket = [[OFTCPSocket alloc] init]; _socket = [[OFTCPSocket alloc] init];
[_socket connectToHost: _server [_socket connectToHost: _server
port: _port]; port: _port];
@ -568,7 +576,7 @@
[pool release]; [pool release];
} }
- (BOOL)socket: (OFTCPSocket*)socket - (bool)socket: (OFTCPSocket*)socket
didReceiveISO88591Line: (OFString*)line didReceiveISO88591Line: (OFString*)line
exception: (OFException*)exception exception: (OFException*)exception
{ {
@ -580,26 +588,34 @@
exception:)]; exception:)];
} }
return NO; return false;
} }
- (BOOL)socket: (OFTCPSocket*)socket - (bool)socket: (OFTCPSocket*)socket
didReceiveLine: (OFString*)line didReceiveLine: (OFString*)line
exception: (OFException*)exception exception: (OFException*)exception
{ {
if (line != nil) { if (line != nil) {
[self IRC_processLine: line]; [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 [socket asyncReadLineWithEncoding: OF_STRING_ENCODING_ISO_8859_1
target: self target: self
selector: @selector(socket: selector: @selector(socket:
didReceiveISO88591Line: didReceiveISO88591Line:
exception:)]; exception:)];
return false;
}
return NO; if ([_delegate respondsToSelector: @selector(connectionWasClosed:)]) {
[_delegate connectionWasClosed: self];
[_socket release];
_socket = nil;
}
return false;
} }
- (void)handleConnection - (void)handleConnection

View file

@ -31,7 +31,7 @@
@property (copy, readonly) OFString *nickname, *username, *hostname; @property (copy, readonly) OFString *nickname, *username, *hostname;
#endif #endif
+ IRCUserWithString: (OFString*)string; + (instancetype)IRCUserWithString: (OFString*)string;
- initWithString: (OFString*)string; - initWithString: (OFString*)string;
- (OFString*)nickname; - (OFString*)nickname;
- (OFString*)username; - (OFString*)username;

View file

@ -33,7 +33,7 @@
#import "IRCUser.h" #import "IRCUser.h"
@implementation IRCUser @implementation IRCUser
+ IRCUserWithString: (OFString*)string + (instancetype)IRCUserWithString: (OFString*)string
{ {
return [[[self alloc] initWithString: string] autorelease]; return [[[self alloc] initWithString: string] autorelease];
} }
@ -49,19 +49,17 @@
if ((tmp2 = strdup([string UTF8String])) == NULL) if ((tmp2 = strdup([string UTF8String])) == NULL)
@throw [OFOutOfMemoryException @throw [OFOutOfMemoryException
exceptionWithClass: [self class] exceptionWithRequestedSize:
requestedSize: [string UTF8StringLength]]; [string UTF8StringLength]];
if ((tmp = strchr(tmp2, '@')) == NULL) if ((tmp = strchr(tmp2, '@')) == NULL)
@throw [OFInvalidFormatException @throw [OFInvalidFormatException exception];
exceptionWithClass: [self class]];
*tmp = '\0'; *tmp = '\0';
_hostname = [[OFString alloc] initWithUTF8String: tmp + 1]; _hostname = [[OFString alloc] initWithUTF8String: tmp + 1];
if ((tmp = strchr(tmp2, '!')) == NULL) if ((tmp = strchr(tmp2, '!')) == NULL)
@throw [OFInvalidFormatException @throw [OFInvalidFormatException exception];
exceptionWithClass: [self class]];
*tmp = '\0'; *tmp = '\0';
_username = [[OFString alloc] initWithUTF8String: tmp + 1]; _username = [[OFString alloc] initWithUTF8String: tmp + 1];
@ -89,17 +87,17 @@
- (OFString*)username - (OFString*)username
{ {
OF_GETTER(_username, YES) OF_GETTER(_username, true)
} }
- (OFString*)nickname - (OFString*)nickname
{ {
OF_GETTER(_nickname, YES) OF_GETTER(_nickname, true)
} }
- (OFString*)hostname - (OFString*)hostname
{ {
OF_GETTER(_hostname, YES) OF_GETTER(_hostname, true)
} }
- copy - copy