ObjIRC  Diff

Differences From Artifact [67857e94b2]:

To Artifact [2f9d06f05e]:


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 * 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/OFInvalidEncodingException.h>

#import <ObjFW/macros.h>

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

@interface IRCConnection () <OFTCPSocketDelegate>
@end








|
<
<
<
<
<
<
<







21
22
23
24
25
26
27
28







29
30
31
32
33
34
35
 * POSSIBILITY OF SUCH DAMAGE.
 */

#define IRC_CONNECTION_M

#include <stdarg.h>

#import <ObjFW/ObjFW.h>








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

@interface IRCConnection () <OFTCPSocketDelegate>
@end

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
}

- (void)connect
{
	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);
}







|







80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
}

- (void)connect
{
	void *pool = objc_autoreleasePoolPush();

	if (_socket != nil)
		@throw [OFAlreadyOpenException exceptionWithObject: self];

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

	objc_autoreleasePoolPop(pool);
}
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
	if ([_delegate respondsToSelector:
	    @selector(connection:didReceiveLine:)])
		[_delegate connection: self didReceiveLine: line];

	components = [line componentsSeparatedByString: @" "];

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

		return;
	}

	/* PONG */







|
|

|







245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
	if ([_delegate respondsToSelector:
	    @selector(connection:didReceiveLine:)])
		[_delegate connection: self didReceiveLine: line];

	components = [line componentsSeparatedByString: @" "];

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

		return;
	}

	/* PONG */
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
	/* 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: OFRangeMake(1, who.length - 1)];
		user = [IRCUser IRCUserWithString: who];

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







|







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;
		OFMutableSet *channel;

		who = [who substringFromIndex: 1];
		user = [IRCUser IRCUserWithString: who];

		if ([who hasPrefix:
		    [_nickname stringByAppendingString: @"!"]]) {
			channel = [OFMutableSet set];
			[_channels setObject: channel forKey: where];
		} else
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
		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:
		    OFRangeMake(pos, line.length - pos)]
		    componentsSeparatedByString: @" "];

		for (OFString *user in users) {
			if ([user hasPrefix: @"@"] || [user hasPrefix: @"+"] ||
			    [user hasPrefix: @"%"] || [user hasPrefix: @"*"])
				user = [user substringWithRange:
				    OFRangeMake(1, user.length - 1)];

			[channel addObject: user];
		}

		if ([_delegate respondsToSelector:
		    @selector(connection:didReceiveNamesForChannel:)])
			[_delegate	   connection: self







|





|
<







333
334
335
336
337
338
339
340
341
342
343
344
345
346

347
348
349
350
351
352
353
		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:
		    OFMakeRange(pos, line.length - pos)]
		    componentsSeparatedByString: @" "];

		for (OFString *user in users) {
			if ([user hasPrefix: @"@"] || [user hasPrefix: @"+"] ||
			    [user hasPrefix: @"%"] || [user hasPrefix: @"*"])
				user = [user substringFromIndex: 1];


			[channel addObject: user];
		}

		if ([_delegate respondsToSelector:
		    @selector(connection:didReceiveNamesForChannel:)])
			[_delegate	   connection: self
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
		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: OFRangeMake(1, who.length - 1)];
		user = [IRCUser IRCUserWithString: who];
		channel = [_channels objectForKey: where];

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

		[channel removeObject: user.nickname];

		if ([_delegate respondsToSelector:
		    @selector(connection:didSeeUser:leaveChannel:reason:)])
			[_delegate connection: self
				   didSeeUser: user







|




|
<







362
363
364
365
366
367
368
369
370
371
372
373
374

375
376
377
378
379
380
381
		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 substringFromIndex: 1];
		user = [IRCUser IRCUserWithString: who];
		channel = [_channels objectForKey: where];

		if (components.count > 3)
			reason = [line substringFromIndex: pos + 2];


		[channel removeObject: user.nickname];

		if ([_delegate respondsToSelector:
		    @selector(connection:didSeeUser:leaveChannel:reason:)])
			[_delegate connection: self
				   didSeeUser: user
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
		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: OFRangeMake(1, who.length - 1)];
		user = [IRCUser IRCUserWithString: who];
		channel = [_channels objectForKey: where];

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

		[channel removeObject: user.nickname];

		if ([_delegate respondsToSelector:
		    @selector(connection:didSeeUser:kickUser:channel:reason:)])
			[_delegate connection: self
				   didSeeUser: user







|



|
|
<







393
394
395
396
397
398
399
400
401
402
403
404
405

406
407
408
409
410
411
412
		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 substringFromIndex: 1];
		user = [IRCUser IRCUserWithString: who];
		channel = [_channels objectForKey: where];

		if (components.count > 4)
			reason = [line substringFromIndex: pos + 2];


		[channel removeObject: user.nickname];

		if ([_delegate respondsToSelector:
		    @selector(connection:didSeeUser:kickUser:channel:reason:)])
			[_delegate connection: self
				   didSeeUser: user
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
	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];

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

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

		for (OFString *channel in _channels)
			[[_channels objectForKey: channel]
			    removeObject: user.nickname];

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

		return;
	}

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

		who = [who substringWithRange: OFRangeMake(1, who.length - 1)];
		nickname = [nickname substringWithRange:
		    OFRangeMake(1, nickname.length - 1)];

		user = [IRCUser IRCUserWithString: who];

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







|



|
<




















|
|
<







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

		who = [who substringFromIndex: 1];
		user = [IRCUser IRCUserWithString: who];

		if ([components count] > 2)
			reason = [line substringFromIndex: pos + 2];


		for (OFString *channel in _channels)
			[[_channels objectForKey: channel]
			    removeObject: user.nickname];

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

		return;
	}

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

		who = [who substringFromIndex: 1];
		nickname = [nickname substringFromIndex: 1];


		user = [IRCUser IRCUserWithString: who];

		if ([user.nickname isEqual: _nickname]) {
			[_nickname release];
			_nickname = [nickname copy];
		}
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
		OFString *from = [components objectAtIndex: 0];
		OFString *to = [components objectAtIndex: 2];
		IRCUser *user;
		OFString *message;
		size_t pos = from.length + 1 +
		    [[components objectAtIndex: 1] length] + 1 + to.length;

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

		if (![to isEqual: _nickname]) {
			if ([_delegate respondsToSelector: @selector(connection:
			    didReceiveMessage:channel:user:)])
				[_delegate connection: self
				    didReceiveMessage: message







|
<
|
<







481
482
483
484
485
486
487
488

489

490
491
492
493
494
495
496
		OFString *from = [components objectAtIndex: 0];
		OFString *to = [components objectAtIndex: 2];
		IRCUser *user;
		OFString *message;
		size_t pos = from.length + 1 +
		    [[components objectAtIndex: 1] length] + 1 + to.length;

		from = [from substringFromIndex: 1];

		message = [line substringFromIndex: pos + 2];

		user = [IRCUser IRCUserWithString: from];

		if (![to isEqual: _nickname]) {
			if ([_delegate respondsToSelector: @selector(connection:
			    didReceiveMessage:channel:user:)])
				[_delegate connection: self
				    didReceiveMessage: message
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
		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:
		    OFRangeMake(1, from.length - 1)];
		notice = [line substringWithRange:
		    OFRangeMake(pos + 2, line.length - pos - 2)];

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

		user = [IRCUser IRCUserWithString: from];







|
<
|
<







512
513
514
515
516
517
518
519

520

521
522
523
524
525
526
527
		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 substringFromIndex: 1];

		notice = [line substringFromIndex: pos + 2];


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

		user = [IRCUser IRCUserWithString: from];