ObjIRC  Diff

Differences From Artifact [e49892a8ab]:

To Artifact [45ed4ab7ae]:


32
33
34
35
36
37
38



39
40
41
42
43
44
45

#import <ObjFW/OFInvalidEncodingException.h>

#import <ObjFW/macros.h>

#import "IRCConnection.h"
#import "IRCUser.h"




@implementation IRCConnection
@synthesize socketClass = _socketClass;
@synthesize server = _server, port = _port;
@synthesize nickname = _nickname, username = _username, realname = _realname;
@synthesize delegate = _delegate, socket = _socket;
@synthesize fallbackEncoding = _fallbackEncoding;







>
>
>







32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#import <ObjFW/OFInvalidEncodingException.h>

#import <ObjFW/macros.h>

#import "IRCConnection.h"
#import "IRCUser.h"

@interface IRCConnection () <OFTCPSocketDelegate>
@end

@implementation IRCConnection
@synthesize socketClass = _socketClass;
@synthesize server = _server, port = _port;
@synthesize nickname = _nickname, username = _username, realname = _realname;
@synthesize delegate = _delegate, socket = _socket;
@synthesize fallbackEncoding = _fallbackEncoding;
87
88
89
90
91
92
93





















94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
{
	void *pool = objc_autoreleasePoolPush();

	if (_socket != nil)
		@throw [OFAlreadyConnectedException exception];

	_socket = [[_socketClass alloc] init];





















	if ([_delegate respondsToSelector:
	    @selector(connection:didCreateSocket:)])
		[_delegate connection: self
		      didCreateSocket: _socket];

	[_socket connectToHost: _server
			  port: _port];

	[self sendLineWithFormat: @"NICK %@", _nickname];
	[self sendLineWithFormat: @"USER %@ * 0 :%@", _username, _realname];

	objc_autoreleasePoolPop(pool);
}

- (void)disconnect
{
	[self disconnectWithReason: nil];
}








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





<
<
<



|







90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122



123
124
125
126
127
128
129
130
131
132
133
{
	void *pool = objc_autoreleasePoolPush();

	if (_socket != nil)
		@throw [OFAlreadyConnectedException exception];

	_socket = [[_socketClass alloc] init];
	[_socket setDelegate: self];
	[_socket asyncConnectToHost: _server
			       port: _port];

	objc_autoreleasePoolPop(pool);
}

-     (void)socket: (OF_KINDOF(OFTCPSocket *))socket
  didConnectToHost: (OFString *)host
	      port: (uint16_t)port
	 exception: (id)exception
{
	if (exception != nil) {
		if ([_delegate respondsToSelector:
		    @selector(connection:didFailToConnectWithException:)])
			[_delegate	       connection: self
			    didFailToConnectWithException: exception];

		return;
	}

	if ([_delegate respondsToSelector:
	    @selector(connection:didCreateSocket:)])
		[_delegate connection: self
		      didCreateSocket: _socket];




	[self sendLineWithFormat: @"NICK %@", _nickname];
	[self sendLineWithFormat: @"USER %@ * 0 :%@", _username, _realname];

	[socket asyncReadLine];
}

- (void)disconnect
{
	[self disconnectWithReason: nil];
}

571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600


601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
		[_delegate connectionWasClosed: self];

	[_socket cancelAsyncRequests];
	[_socket release];
	_socket = nil;
}

- (void)processLine: (OFString *)line
{
	void *pool = objc_autoreleasePoolPush();

	[self irc_processLine: line];

	objc_autoreleasePoolPop(pool);
}

-	      (bool)irc_socket: (OFTCPSocket *)socket
  didReceiveWronglyEncodedLine: (OFString *)line
		       context: (id)context
		     exception: (OFException *)exception
{
	if (line != nil) {
		[self irc_processLine: line];
		[socket asyncReadLineWithTarget: self
				       selector: @selector(irc_socket:
						     didReceiveLine:context:
						     exception:)
					context: nil];
	}



	return false;
}

- (bool)irc_socket: (OFTCPSocket *)socket
    didReceiveLine: (OFString *)line
	   context: (id)context
	 exception: (OFException *)exception
{
	if (line != nil) {
		[self irc_processLine: line];
		return true;
	}

	if ([exception isKindOfClass: [OFInvalidEncodingException class]]) {
		[socket
		    asyncReadLineWithEncoding: _fallbackEncoding
				       target: self
				     selector: @selector(irc_socket:
						   didReceiveWronglyEncodedLine:
						   context:exception:)
				      context: nil];
		return false;
	}

	if ([_delegate respondsToSelector: @selector(connectionWasClosed:)])
		[_delegate connectionWasClosed: self];

	[_pingTimer invalidate];

	[_socket performSelector: @selector(cancelAsyncRequests)
		      afterDelay: 0];
	[_socket release];
	_socket = nil;

	return false;
}

- (void)handleConnection
{
	[_socket asyncReadLineWithTarget: self
				selector: @selector(irc_socket:didReceiveLine:
					      context:exception:)
				 context: nil];
}

- (OFSet OF_GENERIC(OFString *) *)usersInChannel: (OFString *)channel
{
	return [[[_channels objectForKey: channel] copy] autorelease];
}
@end







<
<
<
|
<
<
<
<
<
<
|
<
|



<
<
<
<
<
|
|
>
>
|
|

<
<
<
<
<
<
<




|
|
<
<
<
<
<
















<
<
<
<
<
<
<
<





592
593
594
595
596
597
598



599






600

601
602
603
604





605
606
607
608
609
610
611







612
613
614
615
616
617





618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633








634
635
636
637
638
		[_delegate connectionWasClosed: self];

	[_socket cancelAsyncRequests];
	[_socket release];
	_socket = nil;
}




- (bool)stream: (OF_KINDOF(OFStream *))stream






   didReadLine: (OFString *)line

     exception: (OFException *)exception
{
	if (line != nil) {
		[self irc_processLine: line];






		if (_fallbackEncodingUsed) {
			_fallbackEncodingUsed = false;
			[stream asyncReadLine];
			return false;
		}








		return true;
	}

	if ([exception isKindOfClass: [OFInvalidEncodingException class]]) {
		_fallbackEncodingUsed = true;
		[stream asyncReadLineWithEncoding: _fallbackEncoding];





		return false;
	}

	if ([_delegate respondsToSelector: @selector(connectionWasClosed:)])
		[_delegate connectionWasClosed: self];

	[_pingTimer invalidate];

	[_socket performSelector: @selector(cancelAsyncRequests)
		      afterDelay: 0];
	[_socket release];
	_socket = nil;

	return false;
}









- (OFSet OF_GENERIC(OFString *) *)usersInChannel: (OFString *)channel
{
	return [[[_channels objectForKey: channel] copy] autorelease];
}
@end