ObjIRC  Diff

Differences From Artifact [5544578885]:

To Artifact [002f1423f2]:


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

#import <ObjFW/OFInvalidEncodingException.h>

#import <ObjFW/macros.h>

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

@implementation IRCConnection
- init
{
	self = [super init];

	@try {







<







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
- init
{
	self = [super init];

	@try {
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
{
	if (reason == nil)
		[self sendLine: @"QUIT"];
	else
		[self sendLineWithFormat: @"QUIT :%@", reason];
}

- (void)joinChannel: (OFString*)channelName
{
	[self sendLineWithFormat: @"JOIN %@", channelName];
}

- (void)leaveChannel: (IRCChannel*)channel
{
	[self leaveChannel: channel
		    reason: nil];
}

- (void)leaveChannel: (IRCChannel*)channel
	      reason: (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];








|

|


|





|



|

|
<

|







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
{
	if (reason == nil)
		[self sendLine: @"QUIT"];
	else
		[self sendLineWithFormat: @"QUIT :%@", reason];
}

- (void)joinChannel: (OFString*)channel
{
	[self sendLineWithFormat: @"JOIN %@", channel];
}

- (void)leaveChannel: (OFString*)channel
{
	[self leaveChannel: channel
		    reason: nil];
}

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


	[channels removeObjectForKey: channel];
}

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

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

	[self sendLine: line];

	[pool release];
}

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

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

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

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

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

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








<
<
<
<
<
<
|

|



|

|


<
<
<
<
<
<

|


|
<







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

	[self sendLine: line];

	[pool release];
}

- (void)sendMessage: (OFString*)msg






		 to: (OFString*)to
{
	[self sendLineWithFormat: @"PRIVMSG %@ :%@", to, msg];
}

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







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

}

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

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
	}

	/* 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] +







|





|





|



|






>
|





<
|
|
>







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
	}

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

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

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

		[channel addObject: [user nickname]];

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

		return;
	}

	/* NAMES reply */
	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 */
			return;
		}

		pos = [[components objectAtIndex: 0] length] +
		    [[components objectAtIndex: 1] length] +
		    [[components objectAtIndex: 2] length] +
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
		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
			      reason: 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
			     channel: channel
			      reason: 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
			      reason: 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];








|



|









|












|



|











|













|




|













|








|

|














|














|
|
|







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
		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 addObject: user];
		}

		[delegate	   connection: self
		    didReceiveNamesForChannel: where];

		return;
	}

	/* PART */
	if ([action isEqual: @"PART"] && [components count] >= 3) {
		OFString *who = [components objectAtIndex: 0];
		OFString *where = [components objectAtIndex: 2];
		IRCUser *user;
		OFMutableSet *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 removeObject: [user nickname]];

		[delegate connection: self
			  didSeeUser: user
			leaveChannel: where
			      reason: 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;
		OFMutableSet *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 removeObject: [user nickname]];

		[delegate connection: self
			  didSeeUser: user
			    kickUser: whom
			     channel: where
			      reason: 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;
		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)
			[channel removeObject: [user nickname]];

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

		return;
	}

	/* NICK */
	if ([action isEqual: @"NICK"] && [components count] == 3) {
		OFString *who = [components objectAtIndex: 0];
		OFString *newNickname = [components objectAtIndex: 2];
		IRCUser *user;
		OFEnumerator *enumerator;
		OFMutableSet *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 containsObject: [user nickname]]) {
				[channel removeObject: [user nickname]];
				[channel addObject: newNickname];
			}
		}

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

458
459
460
461
462
463
464
465
466
467
468
469
470
471

472
473
474
475
476
477
478
479
480
481

		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

					user: user
				     channel: channel];
		} else
			[delegate	  connection: self
			    didReceivePrivateMessage: msg
						user: user];

		return;
	}








|
<
<
<
<


>
|
<
|







444
445
446
447
448
449
450
451




452
453
454
455

456
457
458
459
460
461
462
463

		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])




			[delegate connection: self
			   didReceiveMessage: msg
				     channel: to
					user: user];

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

		return;
	}

496
497
498
499
500
501
502
503
504
505
506
507
508
509

510
511
512
513
514
515
516
517
518
519
		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

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

		return;
	}
}







|
<
<
<
<


>
|
<
|







478
479
480
481
482
483
484
485




486
487
488
489

490
491
492
493
494
495
496
497
		if (![from containsString: @"!"] || [to isEqual: @"*"]) {
			/* System message - ignore for now */
			return;
		}

		user = [IRCUser IRCUserWithString: from];

		if (![to isEqual: nickname])




			[delegate connection: self
			    didReceiveNotice: notice
				     channel: to
					user: user];

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

		return;
	}
}
563
564
565
566
567
568
569





570
571
572
573
574
575
576

- (void)handleConnection
{
	[sock asyncReadLineWithTarget: self
			     selector: @selector(connection:didReceiveLine:
					   exception:)];
}






- (void)dealloc
{
	[sock release];
	[server release];
	[nickname release];
	[username release];







>
>
>
>
>







541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559

- (void)handleConnection
{
	[sock asyncReadLineWithTarget: self
			     selector: @selector(connection:didReceiveLine:
					   exception:)];
}

- (OFSet*)usersInChannel: (OFString*)channel
{
	return [[[channels objectForKey: channel] copy] autorelease];
}

- (void)dealloc
{
	[sock release];
	[server release];
	[nickname release];
	[username release];
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

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

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

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

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

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

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

-  (void)connection: (IRCConnection*)connection
  didReceiveMessage: (OFString*)msg

	   fromUser: (IRCUser*)user
	    channel: (IRCChannel*)channel
{
}

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

- (void)connection: (IRCConnection*)connection
  didReceiveNotice: (OFString*)notice

	      user: (IRCUser*)user
{
}

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

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

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







|





|













|












>
|
<











>







<




|







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

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

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

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

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

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

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

-  (void)connection: (IRCConnection*)connection
  didReceiveMessage: (OFString*)msg
	    channel: (OFString*)channel
	       user: (IRCUser*)user

{
}

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

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

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

{
}

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

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