ObjIRC  Diff

Differences From Artifact [b8818aed8f]:

To Artifact [42cd345c04]:


16
17
18
19
20
21
22


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

































































56
57
58
59
60
61
62
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */



#include <stdarg.h>

#import <ObjFW/OFString.h>
#import <ObjFW/OFArray.h>
#import <ObjFW/OFMutableDictionary.h>
#import <ObjFW/OFTCPSocket.h>
#import <ObjFW/OFAutoreleasePool.h>

#import <ObjFW/OFInvalidEncodingException.h>

#import <ObjFW/macros.h>

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

@implementation IRCConnection
@synthesize server, port, nickname, username, realname, delegate, sock;

- init
{
	self = [super init];

	@try {
		channels = [[OFMutableDictionary alloc] init];
		port = 6667;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}


































































- (void)connect
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

	sock = [[OFTCPSocket alloc] init];
	[sock connectToHost: server







>
>

















<
<














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







16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#define IRC_CONNECTION_M

#include <stdarg.h>

#import <ObjFW/OFString.h>
#import <ObjFW/OFArray.h>
#import <ObjFW/OFMutableDictionary.h>
#import <ObjFW/OFTCPSocket.h>
#import <ObjFW/OFAutoreleasePool.h>

#import <ObjFW/OFInvalidEncodingException.h>

#import <ObjFW/macros.h>

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

@implementation IRCConnection


- init
{
	self = [super init];

	@try {
		channels = [[OFMutableDictionary alloc] init];
		port = 6667;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)setServer: (OFString*)server_
{
	OF_SETTER(server, server_, YES, YES)
}

- (OFString*)server
{
	OF_GETTER(server, YES)
}

- (void)setPort: (uint16_t)port_
{
	port = port_;
}

- (uint16_t)port
{
	return port;
}

- (void)setNickname: (OFString*)nickname_
{
	OF_SETTER(nickname, nickname_, YES, YES)
}

- (OFString*)nickname
{
	OF_GETTER(nickname, YES)
}

- (void)setUsername: (OFString*)username_
{
	OF_SETTER(username, username_, YES, YES)
}

- (OFString*)username
{
	OF_GETTER(username, YES)
}

- (void)setRealname: (OFString*)realname_
{
	OF_SETTER(realname, realname_, YES, YES)
}

- (OFString*)realname
{
	OF_GETTER(realname, YES)
}

- (void)setDelegate: (id <IRCConnectionDelegate>)delegate_
{
	delegate = delegate_;
}

- (id <IRCConnectionDelegate>)delegate
{
	return delegate;
}

- (OFTCPSocket*)socket
{
	OF_GETTER(sock, YES)
}

- (void)connect
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

	sock = [[OFTCPSocket alloc] init];
	[sock connectToHost: server
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
		withReason: nil];
}

- (void)leaveChannel: (IRCChannel*)channel
          withReason: (OFString*)reason
{
	if (reason == nil)
		[self sendLineWithFormat: @"PART %@", channel.name];
	else
		[self sendLineWithFormat: @"PART %@ :%@", channel.name, reason];


	[channels removeObjectForKey: channel.name];
}

- (void)sendLine: (OFString*)line
{
	if ([delegate respondsToSelector: @selector(connection:didSendLine:)])
		[delegate connection: self
			 didSendLine: line];

	[sock writeLine: line];
}

- (void)sendLineWithFormat: (OFConstantString*)format, ...
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];







|

|
>

|




<
|
|







157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

174
175
176
177
178
179
180
181
182
		withReason: nil];
}

- (void)leaveChannel: (IRCChannel*)channel
          withReason: (OFString*)reason
{
	if (reason == nil)
		[self sendLineWithFormat: @"PART %@", [channel name]];
	else
		[self sendLineWithFormat: @"PART %@ :%@",
					  [channel name], reason];

	[channels removeObjectForKey: [channel name]];
}

- (void)sendLine: (OFString*)line
{

	[delegate connection: self
		 didSendLine: line];

	[sock writeLine: line];
}

- (void)sendLineWithFormat: (OFConstantString*)format, ...
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236


237
238

239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255

256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344

345


346
347
348
349
350
351
352
353
354

355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371


372
373
374
375
376
377
378
379
380
381
382
383
384

385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410

411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450

451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481

482

483



484

485

486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518

	[pool release];
}

- (void)sendMessage: (OFString*)msg
	  toChannel: (IRCChannel*)channel
{
	[self sendLineWithFormat: @"PRIVMSG %@ :%@", channel.name, msg];
}

- (void)sendMessage: (OFString*)msg
	     toUser: (OFString*)user
{
	[self sendLineWithFormat: @"PRIVMSG %@ :%@", user, msg];
}

- (void)sendNotice: (OFString*)notice
	    toUser: (OFString*)user
{
	[self sendLineWithFormat: @"NOTICE %@ :%@", user, notice];
}

- (void)sendNotice: (OFString*)notice
	 toChannel: (IRCChannel*)channel
{
	[self sendLineWithFormat: @"NOTICE %@ :%@", channel.name, notice];
}

- (void)kickUser: (OFString*)user
     fromChannel: (IRCChannel*)channel
      withReason: (OFString*)reason
{
	[self sendLineWithFormat: @"KICK %@ %@ :%@",
				  channel.name, user, reason];
}

- (void)changeNicknameTo: (OFString*)nickname_
{
	[self sendLineWithFormat: @"NICK %@", nickname_];
}

- (void)processLine: (OFString*)line
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFArray *split;
	OFString *action = nil;

	if ([delegate respondsToSelector:
	    @selector(connection:didReceiveLine:)])
		[delegate connection: self
		      didReceiveLine: line];

	split = [line componentsSeparatedByString: @" "];

	/* PING */

	if (split.count == 2 && [split.firstObject isEqual: @"PING"]) {
		OFMutableString *s = [[line mutableCopy] autorelease];
		[s replaceOccurrencesOfString: @"PING"
				   withString: @"PONG"];
		[self sendLine: s];

		return;
	}

	action = [[split objectAtIndex: 1] uppercaseString];

	/* Connected */
	if ([action isEqual: @"001"] && split.count >= 4) {
		if ([delegate respondsToSelector:
		    @selector(connectionWasEstablished:)])
			[delegate connectionWasEstablished: self];

		[pool release];
		return;
	}

	/* JOIN */
	if ([action isEqual: @"JOIN"] && split.count == 3) {
		OFString *who = [split objectAtIndex: 0];
		OFString *where = [split objectAtIndex: 2];
		IRCUser *user;
		IRCChannel *channel;

		who = [who substringWithRange: of_range(1, who.length - 1)];
		user = [IRCUser IRCUserWithString: who];

		if ([who hasPrefix: [nickname stringByAppendingString: @"!"]]) {
			channel = [IRCChannel channelWithName: where];
			[channels setObject: channel
				     forKey: where];
		} else
			channel = [channels objectForKey: where];

		[channel IRC_addUser: user.nickname];

		if ([delegate respondsToSelector:
		    @selector(connection:didSeeUser:joinChannel:)])
			[delegate connection: self
				  didSeeUser: user
				 joinChannel: channel];

		[pool release];
		return;
	}

	/* NAMES reply */
	if ([action isEqual: @"353"] && split.count >= 6) {
		IRCChannel *channel;
		OFArray *users;
		size_t pos;



		channel = [channels objectForKey: [split objectAtIndex: 4]];

		if (channel == nil) {
			/* We did not request that */
			[pool release];
			return;
		}

		pos = [[split objectAtIndex: 0] length] +
		    [[split objectAtIndex: 1] length] +
		    [[split objectAtIndex: 2] length] +
		    [[split objectAtIndex: 3] length] +
		    [[split objectAtIndex: 4] length] + 6;

		users = [[line substringWithRange:
		    of_range(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)];

			[channel IRC_addUser: user];
		}

		if ([delegate respondsToSelector: @selector(connection:
		    didReceiveNamesForChannel:)])
			[delegate	   connection: self
			    didReceiveNamesForChannel: channel];

		[pool release];
		return;
	}

	/* PART */
	if ([action isEqual: @"PART"] && split.count >= 3) {
		OFString *who = [split objectAtIndex: 0];
		OFString *where = [split objectAtIndex: 2];
		IRCUser *user;
		IRCChannel *channel;
		OFString *reason = nil;
		size_t pos = who.length + 1 +
		    [[split objectAtIndex: 1] length] + 1 + where.length;

		who = [who substringWithRange: of_range(1, who.length - 1)];
		user = [IRCUser IRCUserWithString: who];
		channel = [channels objectForKey: where];

		if (split.count > 3)
			reason = [line substringWithRange:
			    of_range(pos + 2, line.length - pos - 2)];

		[channel IRC_removeUser: user.nickname];

		if ([delegate respondsToSelector:
		    @selector(connection:didSeeUser:leaveChannel:
		    withReason:)])
			[delegate connection: self
				  didSeeUser: user
				leaveChannel: channel
				  withReason: reason];

		[pool release];
		return;
	}

	/* KICK */
	if ([action isEqual: @"KICK"] && split.count >= 4) {
		OFString *who = [split objectAtIndex: 0];
		OFString *where = [split objectAtIndex: 2];
		OFString *whom = [split objectAtIndex: 3];
		IRCUser *user;
		IRCChannel *channel;
		OFString *reason = nil;
		size_t pos = who.length + 1 +
		    [[split objectAtIndex: 1] length] + 1 + where.length + 1 +
		    whom.length;

		who = [who substringWithRange: of_range(1, who.length - 1)];
		user = [IRCUser IRCUserWithString: who];
		channel = [channels objectForKey: where];

		if (split.count > 4)
			reason = [line substringWithRange:
			    of_range(pos + 2, line.length - pos - 2)];

		[channel IRC_removeUser: user.nickname];

		if ([delegate respondsToSelector:
		    @selector(connection:didSeeUser:kickUser:
		    fromChannel:withReason:)])
			[delegate connection: self
				  didSeeUser: user
				    kickUser: whom
				 fromChannel: channel
				  withReason: reason];

		[pool release];
		return;
	}

	/* QUIT */
	if ([action isEqual: @"QUIT"] && split.count >= 2) {
		OFString *who = [split objectAtIndex: 0];
		IRCUser *user;
		OFString *reason = nil;

		size_t pos = who.length + 1 + [[split objectAtIndex: 1] length];



		who = [who substringWithRange: of_range(1, who.length - 1)];
		user = [IRCUser IRCUserWithString: who];

		if (split.count > 2)
			reason = [line substringWithRange:
			    of_range(pos + 2, line.length - pos - 2)];

		for (IRCChannel *channel in channels)

			[channel IRC_removeUser: user.nickname];

		if ([delegate respondsToSelector:
		    @selector(connection:didSeeUserQuit:withReason:)])
			[delegate connection: self
			      didSeeUserQuit: user
				  withReason: reason];

		[pool release];
		return;
	}

	/* NICK */
	if ([action isEqual: @"NICK"] && split.count == 3) {
		OFString *who = [split objectAtIndex: 0];
		OFString *newNickname = [split objectAtIndex: 2];
		IRCUser *user;



		who = [who substringWithRange: of_range(1, who.length - 1)];
		newNickname = [newNickname substringWithRange:
		    of_range(1, newNickname.length - 1)];

		user = [IRCUser IRCUserWithString: who];

		if ([user.nickname isEqual: nickname]) {
			[nickname release];
			nickname = [user.nickname copy];
		}

		for (IRCChannel *channel in channels) {

			if ([channel.users containsObject: user.nickname]) {
				[channel IRC_removeUser: user.nickname];
				[channel IRC_addUser: newNickname];
			}
		}

		if ([delegate respondsToSelector:
		    @selector(connection:didSeeUser:changeNicknameTo:)])
			[delegate connection: self
				  didSeeUser: user
			    changeNicknameTo: newNickname];

		[pool release];
		return;
	}

	/* PRIVMSG */
	if ([action isEqual: @"PRIVMSG"] && split.count >= 4) {
		OFString *from = [split objectAtIndex: 0];
		OFString *to = [split objectAtIndex: 2];
		IRCUser *user;
		OFString *msg;
		size_t pos = from.length + 1 +
		    [[split objectAtIndex: 1] length] + 1 + to.length;

		from = [from substringWithRange: of_range(1, from.length - 1)];

		msg = [line substringWithRange:
		    of_range(pos + 2, line.length - pos - 2)];
		user = [IRCUser IRCUserWithString: from];

		if (![to isEqual: nickname]) {
			IRCChannel *channel;

			channel = [channels objectForKey: to];

			if ([delegate respondsToSelector:
			    @selector(connection:didReceiveMessage:
			    fromUser:inChannel:)])
				[delegate connection: self
				   didReceiveMessage: msg
					    fromUser: user
					   inChannel: channel];
		} else {
			if ([delegate respondsToSelector:
			     @selector(connection:
			     didReceivePrivateMessage:fromUser:)])
				[delegate
						  connection: self
				    didReceivePrivateMessage: msg
						    fromUser: user];
		}

		[pool release];
		return;
	}

	/* NOTICE */
	if ([action isEqual: @"NOTICE"] && split.count >= 4) {
		OFString *from = [split objectAtIndex: 0];
		OFString *to = [split objectAtIndex: 2];
		IRCUser *user = nil;
		OFString *notice;
		size_t pos = from.length + 1 +
		    [[split objectAtIndex: 1] length] + 1 + to.length;

		from = [from substringWithRange: of_range(1, from.length - 1)];

		notice = [line substringWithRange:
		    of_range(pos + 2, line.length - pos - 2)];

		if (![from containsString: @"!"] || [to isEqual: @"*"]) {
			/* System message - ignore for now */
			[pool release];
			return;
		}

		user = [IRCUser IRCUserWithString: from];

		if (![to isEqual: nickname]) {
			IRCChannel *channel;

			channel = [channels objectForKey: to];

			if ([delegate respondsToSelector:
			    @selector(connection:didReceiveNotice:
			    fromUser:inChannel:)])
				[delegate connection: self
				    didReceiveNotice: notice
					    fromUser: user
					   inChannel: channel];
		} else {
			if ([delegate respondsToSelector:
			    @selector(connection:didReceiveNotice:
			    fromUser:)])
				[delegate connection: self
				    didReceiveNotice: notice
					    fromUser: user];
		}



		[pool release];



		return;

	}

}

-	  (BOOL)connection: (OFTCPSocket*)connection
    didReceiveISO88591Line: (OFString*)line
		   context: (id)context
		 exception: (OFException*)exception
{
	if (line != nil) {
		[self processLine: line];
		[sock asyncReadLineWithTarget: self
				     selector: @selector(connection:
						   didReceiveLine:context:
						   exception:)
				      context: nil];
	}

	return NO;
}

- (BOOL)connection: (OFTCPSocket*)connection
    didReceiveLine: (OFString*)line
	   context: (id)context
	 exception: (OFException*)exception
{
	if (line != nil) {
		[self processLine: line];
		return YES;
	}

	if ([exception isKindOfClass: [OFInvalidEncodingException class]])
		[sock asyncReadLineWithEncoding: OF_STRING_ENCODING_ISO_8859_1
					 target: self
				       selector: @selector(connection:







|

















|







|







|

<
|


<
<
|
|

|


>
|








|


|
<
<
|
<
<




|
|
|



|









|

<
<
|
|
|

<




|



>
>

|
>


<



|
|
|
|
|


|


|
>



|




<
<
|
|

<




|
|
|



|
|

|



|

|

|

<
<
<
|
|
|
|

<




|
|
|
|



|
|
|

|



|

|

|

<
<
<
|
|
|
|
|

<




|
|


>
|
>
>

|


|

|

|
>
|

<
<
|
|
|

<




|
|
|

>
>

|

|



|

|


|
>
|
|




<
<
|
|
|

<




|
|
|


|
|

|
>

|







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




|
|
|


|
|

|
>

|



<










<
<
<
|
|
|
|
|
<
<
<
|
|
|
|
>
|
>
|
>
>
>
|
>
|
>








|
















|







192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

235
236
237


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257


258


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280


281
282
283
284

285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322


323
324
325

326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348



349
350
351
352
353

354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378



379
380
381
382
383
384

385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408


409
410
411
412

413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442


443
444
445
446

447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469



470
471
472
473
474




475
476
477
478


479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497

498
499
500
501
502
503
504
505
506
507



508
509
510
511
512



513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560

	[pool release];
}

- (void)sendMessage: (OFString*)msg
	  toChannel: (IRCChannel*)channel
{
	[self sendLineWithFormat: @"PRIVMSG %@ :%@", [channel name], msg];
}

- (void)sendMessage: (OFString*)msg
	     toUser: (OFString*)user
{
	[self sendLineWithFormat: @"PRIVMSG %@ :%@", user, msg];
}

- (void)sendNotice: (OFString*)notice
	    toUser: (OFString*)user
{
	[self sendLineWithFormat: @"NOTICE %@ :%@", user, notice];
}

- (void)sendNotice: (OFString*)notice
	 toChannel: (IRCChannel*)channel
{
	[self sendLineWithFormat: @"NOTICE %@ :%@", [channel name], notice];
}

- (void)kickUser: (OFString*)user
     fromChannel: (IRCChannel*)channel
      withReason: (OFString*)reason
{
	[self sendLineWithFormat: @"KICK %@ %@ :%@",
				  [channel name], user, reason];
}

- (void)changeNicknameTo: (OFString*)nickname_
{
	[self sendLineWithFormat: @"NICK %@", nickname_];
}

- (void)IRC_processLine: (OFString*)line
{

	OFArray *components;
	OFString *action = nil;



	[delegate connection: self
	      didReceiveLine: line];

	components = [line componentsSeparatedByString: @" "];

	/* PING */
	if ([components count] == 2 &&
	    [[components firstObject] isEqual: @"PING"]) {
		OFMutableString *s = [[line mutableCopy] autorelease];
		[s replaceOccurrencesOfString: @"PING"
				   withString: @"PONG"];
		[self sendLine: s];

		return;
	}

	action = [[components objectAtIndex: 1] uppercaseString];

	/* Connected */
	if ([action isEqual: @"001"] && [components count] >= 4) {


		[delegate connectionWasEstablished: self];


		return;
	}

	/* JOIN */
	if ([action isEqual: @"JOIN"] && [components count] == 3) {
		OFString *who = [components objectAtIndex: 0];
		OFString *where = [components objectAtIndex: 2];
		IRCUser *user;
		IRCChannel *channel;

		who = [who substringWithRange: of_range(1, [who length] - 1)];
		user = [IRCUser IRCUserWithString: who];

		if ([who hasPrefix: [nickname stringByAppendingString: @"!"]]) {
			channel = [IRCChannel channelWithName: where];
			[channels setObject: channel
				     forKey: where];
		} else
			channel = [channels objectForKey: where];

		[channel IRC_addUser: [user nickname]];



		[delegate connection: self
			  didSeeUser: user
			 joinChannel: channel];


		return;
	}

	/* NAMES reply */
	if ([action isEqual: @"353"] && [components count] >= 6) {
		IRCChannel *channel;
		OFArray *users;
		size_t pos;
		OFEnumerator *enumerator;
		OFString *user;

		channel = [channels
		    objectForKey: [components objectAtIndex: 4]];
		if (channel == nil) {
			/* We did not request that */

			return;
		}

		pos = [[components objectAtIndex: 0] length] +
		    [[components objectAtIndex: 1] length] +
		    [[components objectAtIndex: 2] length] +
		    [[components objectAtIndex: 3] length] +
		    [[components objectAtIndex: 4] length] + 6;

		users = [[line substringWithRange:
		    of_range(pos, [line length] - pos)]
		    componentsSeparatedByString: @" "];

		enumerator = [users objectEnumerator];
		while ((user = [enumerator nextObject]) != nil) {
			if ([user hasPrefix: @"@"] || [user hasPrefix: @"+"] ||
			    [user hasPrefix: @"%"] || [user hasPrefix: @"*"])
				user = [user substringWithRange:
				    of_range(1, [user length] - 1)];

			[channel IRC_addUser: user];
		}



		[delegate	   connection: self
		    didReceiveNamesForChannel: channel];


		return;
	}

	/* PART */
	if ([action isEqual: @"PART"] && [components count] >= 3) {
		OFString *who = [components objectAtIndex: 0];
		OFString *where = [components objectAtIndex: 2];
		IRCUser *user;
		IRCChannel *channel;
		OFString *reason = nil;
		size_t pos = [who length] + 1 +
		    [[components objectAtIndex: 1] length] + 1 + [where length];

		who = [who substringWithRange: of_range(1, [who length] - 1)];
		user = [IRCUser IRCUserWithString: who];
		channel = [channels objectForKey: where];

		if ([components count] > 3)
			reason = [line substringWithRange:
			    of_range(pos + 2, [line length] - pos - 2)];

		[channel IRC_removeUser: [user nickname]];




		[delegate connection: self
			  didSeeUser: user
			leaveChannel: channel
			  withReason: reason];


		return;
	}

	/* KICK */
	if ([action isEqual: @"KICK"] && [components count] >= 4) {
		OFString *who = [components objectAtIndex: 0];
		OFString *where = [components objectAtIndex: 2];
		OFString *whom = [components objectAtIndex: 3];
		IRCUser *user;
		IRCChannel *channel;
		OFString *reason = nil;
		size_t pos = [who length] + 1 +
		    [[components objectAtIndex: 1] length] + 1 +
		    [where length] + 1 + [whom length];

		who = [who substringWithRange: of_range(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)];

		[channel IRC_removeUser: [user nickname]];




		[delegate connection: self
			  didSeeUser: user
			    kickUser: whom
			 fromChannel: channel
			  withReason: reason];


		return;
	}

	/* QUIT */
	if ([action isEqual: @"QUIT"] && [components count] >= 2) {
		OFString *who = [components objectAtIndex: 0];
		IRCUser *user;
		OFString *reason = nil;
		size_t pos = [who length] + 1 +
		    [[components objectAtIndex: 1] length];
		OFEnumerator *enumerator;
		IRCChannel *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 keyEnumerator];
		while ((channel = [enumerator nextObject]) != nil)
			[channel IRC_removeUser: [user nickname]];



		[delegate connection: self
		      didSeeUserQuit: user
			  withReason: reason];


		return;
	}

	/* NICK */
	if ([action isEqual: @"NICK"] && [components count] == 3) {
		OFString *who = [components objectAtIndex: 0];
		OFString *newNickname = [components objectAtIndex: 2];
		IRCUser *user;
		OFEnumerator *enumerator;
		IRCChannel *channel;

		who = [who substringWithRange: of_range(1, [who length] - 1)];
		newNickname = [newNickname substringWithRange:
		    of_range(1, [newNickname length] - 1)];

		user = [IRCUser IRCUserWithString: who];

		if ([[user nickname] isEqual: nickname]) {
			[nickname release];
			nickname = [[user nickname] copy];
		}

		enumerator = [channels keyEnumerator];
		while ((channel = [enumerator nextObject]) != nil) {
			if ([[channel users] containsObject: [user nickname]]) {
				[channel IRC_removeUser: [user nickname]];
				[channel IRC_addUser: newNickname];
			}
		}



		[delegate connection: self
			  didSeeUser: user
		    changeNicknameTo: newNickname];


		return;
	}

	/* PRIVMSG */
	if ([action isEqual: @"PRIVMSG"] && [components count] >= 4) {
		OFString *from = [components objectAtIndex: 0];
		OFString *to = [components objectAtIndex: 2];
		IRCUser *user;
		OFString *msg;
		size_t pos = [from length] + 1 +
		    [[components objectAtIndex: 1] length] + 1 + [to length];

		from = [from substringWithRange:
		    of_range(1, [from length] - 1)];
		msg = [line substringWithRange:
		    of_range(pos + 2, [line length] - pos - 2)];
		user = [IRCUser IRCUserWithString: from];

		if (![to isEqual: nickname]) {
			IRCChannel *channel;

			channel = [channels objectForKey: to];




			[delegate connection: self
			   didReceiveMessage: msg
				    fromUser: user
				   inChannel: channel];
		} else




			[delegate	  connection: self
			    didReceivePrivateMessage: msg
					    fromUser: user];



		return;
	}

	/* NOTICE */
	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];

		from = [from substringWithRange:
		    of_range(1, [from length] - 1)];
		notice = [line substringWithRange:
		    of_range(pos + 2, [line length] - pos - 2)];

		if (![from containsString: @"!"] || [to isEqual: @"*"]) {
			/* System message - ignore for now */

			return;
		}

		user = [IRCUser IRCUserWithString: from];

		if (![to isEqual: nickname]) {
			IRCChannel *channel;

			channel = [channels objectForKey: to];




			[delegate connection: self
			    didReceiveNotice: notice
				    fromUser: user
				   inChannel: channel];
		} else



			[delegate connection: self
			    didReceiveNotice: notice
				    fromUser: user];

		return;
	}
}

- (void)processLine: (OFString*)line
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

	[self IRC_processLine: line];

	[pool release];
}

-	  (BOOL)connection: (OFTCPSocket*)connection
    didReceiveISO88591Line: (OFString*)line
		   context: (id)context
		 exception: (OFException*)exception
{
	if (line != nil) {
		[self IRC_processLine: line];
		[sock asyncReadLineWithTarget: self
				     selector: @selector(connection:
						   didReceiveLine:context:
						   exception:)
				      context: nil];
	}

	return NO;
}

- (BOOL)connection: (OFTCPSocket*)connection
    didReceiveLine: (OFString*)line
	   context: (id)context
	 exception: (OFException*)exception
{
	if (line != nil) {
		[self IRC_processLine: line];
		return YES;
	}

	if ([exception isKindOfClass: [OFInvalidEncodingException class]])
		[sock asyncReadLineWithEncoding: OF_STRING_ENCODING_ISO_8859_1
					 target: self
				       selector: @selector(connection:
537
538
539
540
541
542
543
544
545




















































































	[server release];
	[nickname release];
	[username release];
	[realname release];
	[channels release];

	[super dealloc];
}
@end





























































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
	[server release];
	[nickname release];
	[username release];
	[realname release];
	[channels release];

	[super dealloc];
}
@end

@implementation OFObject (IRCConnectionDelegate)
- (void)connection: (IRCConnection*)connection
    didReceiveLine: (OFString*)line
{
}

- (void)connection: (IRCConnection*)connection
       didSendLine: (OFString*)line
{
}

- (void)connectionWasEstablished: (IRCConnection*)connection
{
}

- (void)connection: (IRCConnection*)connection
	didSeeUser: (IRCUser*)user
       joinChannel: (IRCChannel*)channel
{
}

- (void)connection: (IRCConnection*)connection
	didSeeUser: (IRCUser*)user
      leaveChannel: (IRCChannel*)channel
	withReason: (OFString*)reason
{
}

- (void)connection: (IRCConnection*)connection
        didSeeUser: (IRCUser*)user
  changeNicknameTo: (OFString*)nickname
{
}

- (void)connection: (IRCConnection*)connection
	didSeeUser: (IRCUser*)user
	  kickUser: (OFString*)kickedUser
       fromChannel: (IRCChannel*)channel
	withReason: (OFString*)reason
{
}

- (void)connection: (IRCConnection*)connection
    didSeeUserQuit: (IRCUser*)user
	withReason: (OFString*)reason
{
}

-  (void)connection: (IRCConnection*)connection
  didReceiveMessage: (OFString*)msg
	   fromUser: (IRCUser*)user
	  inChannel: (IRCChannel*)channel
{
}

-	  (void)connection: (IRCConnection*)connection
  didReceivePrivateMessage: (OFString*)msg
		  fromUser: (IRCUser*)user
{
}

- (void)connection: (IRCConnection*)connection
  didReceiveNotice: (OFString*)notice
	  fromUser: (IRCUser*)user
{
}

- (void)connection: (IRCConnection*)connection
  didReceiveNotice: (OFString*)notice
	  fromUser: (IRCUser*)user
	 inChannel: (IRCChannel*)channel
{
}

-	   (void)connection: (IRCConnection*)connection
  didReceiveNamesForChannel: (IRCChannel*)channel
{
}

- (void)connectionWasClosed: (IRCConnection*)connection
{
}
@end