ObjIRC  Check-in [3bf621892c]

Overview
Comment:Adjust to ObjFW changes
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA3-256: 3bf621892c7234955008d682211b3985069b4116cc13739081e96c86514ccab2
User & Date: js on 2021-04-29 00:48:45
Other Links: manifest | tags
Context
2021-04-29
00:48
Adjust to ObjFW changes Leaf check-in: 3bf621892c user: js tags: trunk
2018-12-17
21:04
Adjust to ObjFW changes check-in: 42243ac2f7 user: js tags: trunk
Changes

Modified src/IRCConnection.h from [a48a25139f] to [19041e4476].

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

@protocol IRCConnectionDelegate <OFObject>
@optional
- (void)connection: (IRCConnection *)connection
   didCreateSocket: (OF_KINDOF(OFTCPSocket *))socket;
- (void)connection: (IRCConnection *)connection
    didReceiveLine: (OFString *)line;
- (void)connection: (IRCConnection *)connection
       didSendLine: (OFString *)line;
- (void)connectionWasEstablished: (IRCConnection *)connection;
-	       (void)connection: (IRCConnection *)connection
  didFailToConnectWithException: (id)exception;
- (void)connection: (IRCConnection *)connection
	didSeeUser: (IRCUser *)user
       joinChannel: (OFString *)channel;
- (void)connection: (IRCConnection *)connection







|
<







30
31
32
33
34
35
36
37

38
39
40
41
42
43
44

@protocol IRCConnectionDelegate <OFObject>
@optional
- (void)connection: (IRCConnection *)connection
   didCreateSocket: (OF_KINDOF(OFTCPSocket *))socket;
- (void)connection: (IRCConnection *)connection
    didReceiveLine: (OFString *)line;
- (void)connection: (IRCConnection *)connection didSendLine: (OFString *)line;

- (void)connectionWasEstablished: (IRCConnection *)connection;
-	       (void)connection: (IRCConnection *)connection
  didFailToConnectWithException: (id)exception;
- (void)connection: (IRCConnection *)connection
	didSeeUser: (IRCUser *)user
       joinChannel: (OFString *)channel;
- (void)connection: (IRCConnection *)connection
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
128
129
	OF_KINDOF(OFTCPSocket *) _Nullable _socket;
	OFString *_Nullable _server;
	uint16_t _port;
	OFString *_Nullable _nickname, *_Nullable _username;
	OFString *_Nullable _realname;
	OFMutableDictionary OF_GENERIC(OFString *, OFMutableSet *) *_channels;
	id <IRCConnectionDelegate> _Nullable _delegate;
	of_string_encoding_t _fallbackEncoding;
	of_time_interval_t _pingInterval, _pingTimeout;
	OFString *_Nullable _pingData;
	OFTimer *_Nullable _pingTimer;
	bool _fallbackEncodingUsed;
}

@property (readonly, nonatomic) Class socketClass;
@property OF_NULLABLE_PROPERTY (copy, nonatomic) OFString *server;
@property (nonatomic) uint16_t port;
@property OF_NULLABLE_PROPERTY (copy, nonatomic)
    OFString *nickname, *username, *realname;
@property OF_NULLABLE_PROPERTY (assign, nonatomic)
    id <IRCConnectionDelegate> delegate;
@property OF_NULLABLE_PROPERTY (readonly, nonatomic)
    OF_KINDOF(OFTCPSocket *) socket;
@property (nonatomic) of_string_encoding_t fallbackEncoding;
@property (nonatomic) of_time_interval_t pingInterval, pingTimeout;

+ (instancetype)connection;
- (void)sendLine: (OFString *)line;
- (void)sendLineWithFormat: (OFConstantString *)line, ...;
- (void)connect;
- (void)disconnect;
- (void)disconnectWithReason: (nullable OFString *)reason;
- (void)joinChannel: (OFString *)channelName;
- (void)leaveChannel: (OFString *)channel;
- (void)leaveChannel: (OFString *)channel
	      reason: (nullable OFString *)reason;
- (void)sendMessage: (OFString *)message
		 to: (OFString *)to;
- (void)sendNotice: (OFString *)notice
		to: (OFString *)to;
- (void)kickUser: (OFString *)user
	 channel: (OFString *)channel
	  reason: (nullable OFString *)reason;
- (void)changeNicknameTo: (OFString *)nickname;
- (nullable OFSet OF_GENERIC(OFString *) *)usersInChannel: (OFString *)channel;
@end

OF_ASSUME_NONNULL_END







|
|














|
|









|
<
|
<
|
<








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
	OF_KINDOF(OFTCPSocket *) _Nullable _socket;
	OFString *_Nullable _server;
	uint16_t _port;
	OFString *_Nullable _nickname, *_Nullable _username;
	OFString *_Nullable _realname;
	OFMutableDictionary OF_GENERIC(OFString *, OFMutableSet *) *_channels;
	id <IRCConnectionDelegate> _Nullable _delegate;
	OFStringEncoding _fallbackEncoding;
	OFTimeInterval _pingInterval, _pingTimeout;
	OFString *_Nullable _pingData;
	OFTimer *_Nullable _pingTimer;
	bool _fallbackEncodingUsed;
}

@property (readonly, nonatomic) Class socketClass;
@property OF_NULLABLE_PROPERTY (copy, nonatomic) OFString *server;
@property (nonatomic) uint16_t port;
@property OF_NULLABLE_PROPERTY (copy, nonatomic)
    OFString *nickname, *username, *realname;
@property OF_NULLABLE_PROPERTY (assign, nonatomic)
    id <IRCConnectionDelegate> delegate;
@property OF_NULLABLE_PROPERTY (readonly, nonatomic)
    OF_KINDOF(OFTCPSocket *) socket;
@property (nonatomic) OFStringEncoding fallbackEncoding;
@property (nonatomic) OFTimeInterval pingInterval, pingTimeout;

+ (instancetype)connection;
- (void)sendLine: (OFString *)line;
- (void)sendLineWithFormat: (OFConstantString *)line, ...;
- (void)connect;
- (void)disconnect;
- (void)disconnectWithReason: (nullable OFString *)reason;
- (void)joinChannel: (OFString *)channelName;
- (void)leaveChannel: (OFString *)channel;
- (void)leaveChannel: (OFString *)channel reason: (nullable OFString *)reason;

- (void)sendMessage: (OFString *)message to: (OFString *)to;

- (void)sendNotice: (OFString *)notice to: (OFString *)to;

- (void)kickUser: (OFString *)user
	 channel: (OFString *)channel
	  reason: (nullable OFString *)reason;
- (void)changeNicknameTo: (OFString *)nickname;
- (nullable OFSet OF_GENERIC(OFString *) *)usersInChannel: (OFString *)channel;
@end

OF_ASSUME_NONNULL_END

Modified src/IRCConnection.m from [45ed4ab7ae] to [67857e94b2].

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{
	self = [super init];

	@try {
		_socketClass = [OFTCPSocket class];
		_channels = [[OFMutableDictionary alloc] init];
		_port = 6667;
		_fallbackEncoding = OF_STRING_ENCODING_ISO_8859_1;
		_pingInterval = 120;
		_pingTimeout = 30;
	} @catch (id e) {
		[self release];
		@throw e;
	}








|







57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{
	self = [super init];

	@try {
		_socketClass = [OFTCPSocket class];
		_channels = [[OFMutableDictionary alloc] init];
		_port = 6667;
		_fallbackEncoding = OFStringEncodingISO8859_1;
		_pingInterval = 120;
		_pingTimeout = 30;
	} @catch (id e) {
		[self release];
		@throw e;
	}

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








|
<




















|
<







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

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
	objc_autoreleasePoolPop(pool);
}

- (void)joinChannel: (OFString *)channel
{
	void *pool = objc_autoreleasePoolPush();

	channel = [[channel componentsSeparatedByString: @"\n"] firstObject];

	[self sendLineWithFormat: @"JOIN %@", channel];

	objc_autoreleasePoolPop(pool);
}

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

- (void)leaveChannel: (OFString *)channel
	      reason: (OFString *)reason
{
	void *pool = objc_autoreleasePoolPush();

	channel = [[channel componentsSeparatedByString: @"\n"] firstObject];
	reason = [[reason componentsSeparatedByString: @"\n"] firstObject];

	if (reason == nil)
		[self sendLineWithFormat: @"PART %@", channel];
	else
		[self sendLineWithFormat: @"PART %@ :%@", channel, reason];

	[_channels removeObjectForKey: channel];

	objc_autoreleasePoolPop(pool);
}

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

	[_socket writeLine: line];
}

- (void)sendLineWithFormat: (OFConstantString *)format, ...
{
	void *pool = objc_autoreleasePoolPush();
	OFString *line;
	va_list args;

	va_start(args, format);
	line = [[[OFString alloc] initWithFormat: format
				       arguments: args] autorelease];
	va_end(args);

	[self sendLine: line];

	objc_autoreleasePoolPop(pool);
}

- (void)sendMessage: (OFString *)message
		 to: (OFString *)to
{
	void *pool = objc_autoreleasePoolPush();

	for (OFString *line in [message componentsSeparatedByString: @"\n"])
		[self sendLineWithFormat: @"PRIVMSG %@ :%@", to, line];

	objc_autoreleasePoolPop(pool);
}

- (void)sendNotice: (OFString *)notice
		to: (OFString *)to
{
	void *pool = objc_autoreleasePoolPush();

	for (OFString *line in [notice componentsSeparatedByString: @"\n"])
		[self sendLineWithFormat: @"NOTICE %@ :%@", to, line];

	objc_autoreleasePoolPop(pool);







|








|
<


|
<



|
|














|
<




















|
<









|
<







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
	objc_autoreleasePoolPop(pool);
}

- (void)joinChannel: (OFString *)channel
{
	void *pool = objc_autoreleasePoolPush();

	channel = [channel componentsSeparatedByString: @"\n"].firstObject;

	[self sendLineWithFormat: @"JOIN %@", channel];

	objc_autoreleasePoolPop(pool);
}

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

}

- (void)leaveChannel: (OFString *)channel reason: (OFString *)reason

{
	void *pool = objc_autoreleasePoolPush();

	channel = [channel componentsSeparatedByString: @"\n"].firstObject;
	reason = [reason componentsSeparatedByString: @"\n"].firstObject;

	if (reason == nil)
		[self sendLineWithFormat: @"PART %@", channel];
	else
		[self sendLineWithFormat: @"PART %@ :%@", channel, reason];

	[_channels removeObjectForKey: channel];

	objc_autoreleasePoolPop(pool);
}

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


	[_socket writeLine: line];
}

- (void)sendLineWithFormat: (OFConstantString *)format, ...
{
	void *pool = objc_autoreleasePoolPush();
	OFString *line;
	va_list args;

	va_start(args, format);
	line = [[[OFString alloc] initWithFormat: format
				       arguments: args] autorelease];
	va_end(args);

	[self sendLine: line];

	objc_autoreleasePoolPop(pool);
}

- (void)sendMessage: (OFString *)message to: (OFString *)to

{
	void *pool = objc_autoreleasePoolPush();

	for (OFString *line in [message componentsSeparatedByString: @"\n"])
		[self sendLineWithFormat: @"PRIVMSG %@ :%@", to, line];

	objc_autoreleasePoolPop(pool);
}

- (void)sendNotice: (OFString *)notice to: (OFString *)to

{
	void *pool = objc_autoreleasePoolPush();

	for (OFString *line in [notice componentsSeparatedByString: @"\n"])
		[self sendLineWithFormat: @"NOTICE %@ :%@", to, line];

	objc_autoreleasePoolPop(pool);
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
	objc_autoreleasePoolPop(pool);
}

- (void)changeNicknameTo: (OFString *)nickname
{
	void *pool = objc_autoreleasePoolPush();

	nickname = [[nickname componentsSeparatedByString: @"\n"]
	    firstObject];

	[self sendLineWithFormat: @"NICK %@", nickname];

	objc_autoreleasePoolPop(pool);
}

- (void)irc_processLine: (OFString *)line
{
	OFArray *components;
	OFString *action = nil;

	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: of_range(0, 4)
				 withString: @"PONG"];
		[self sendLine: s];

		return;
	}

	/* PONG */
	if ([components count] == 4 &&
	    [[components objectAtIndex: 1] isEqual: @"PONG"] &&
	    [[components objectAtIndex: 3] isEqual: _pingData]) {
		[_pingTimer invalidate];

		[_pingData release];
		[_pingTimer release];

		_pingData = nil;
		_pingTimer = nil;
	}

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

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

		[OFTimer scheduledTimerWithTimeInterval: _pingInterval
						 target: self
					       selector: @selector(irc_sendPing)
						repeats: true];

		return;
	}

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

		if ([_delegate respondsToSelector:
		    @selector(connection:didSeeUser:joinChannel:)])
			[_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;

		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] +
		    [[components objectAtIndex: 3] length] +
		    [[components 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 addObject: user];
		}

		if ([_delegate respondsToSelector:
		    @selector(connection:didReceiveNamesForChannel:)])
			[_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]];

		if ([_delegate respondsToSelector:
		    @selector(connection:didSeeUser:leaveChannel:reason:)])
			[_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]];

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

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

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

		user = [IRCUser IRCUserWithString: who];

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

		for (OFMutableSet *channel in _channels) {
			if ([channel containsObject: [user nickname]]) {
				[channel removeObject: [user nickname]];
				[channel addObject: nickname];
			}
		}

		if ([_delegate respondsToSelector:
		    @selector(connection:didSeeUser:changeNicknameTo:)])
			[_delegate connection: self
				   didSeeUser: user
			     changeNicknameTo: nickname];

		return;
	}

	/* PRIVMSG */
	if ([action isEqual: @"PRIVMSG"] && [components count] >= 4) {
		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:
		    of_range(1, [from length] - 1)];
		message = [line substringWithRange:
		    of_range(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







|
<













|
<







|







|














|













|





|





|
<



|











|



















|






|



















|
|

|



|

|

|












|






|

|

|





|

|













|



|


|




|



|











|




|

|



|





|
|














|




|
|


|

|







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
	objc_autoreleasePoolPop(pool);
}

- (void)changeNicknameTo: (OFString *)nickname
{
	void *pool = objc_autoreleasePoolPush();

	nickname = [nickname componentsSeparatedByString: @"\n"].firstObject;


	[self sendLineWithFormat: @"NICK %@", nickname];

	objc_autoreleasePoolPop(pool);
}

- (void)irc_processLine: (OFString *)line
{
	OFArray *components;
	OFString *action = nil;

	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 */
	if (components.count == 4 &&
	    [[components objectAtIndex: 1] isEqual: @"PONG"] &&
	    [[components objectAtIndex: 3] isEqual: _pingData]) {
		[_pingTimer invalidate];

		[_pingData release];
		[_pingTimer release];

		_pingData = nil;
		_pingTimer = nil;
	}

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

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

		[OFTimer scheduledTimerWithTimeInterval: _pingInterval
						 target: self
					       selector: @selector(irc_sendPing)
						repeats: true];

		return;
	}

	/* 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
			channel = [_channels objectForKey: where];

		[channel addObject: user.nickname];

		if ([_delegate respondsToSelector:
		    @selector(connection:didSeeUser:joinChannel:)])
			[_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;

		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] +
		    [[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
			    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: 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
				 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: 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
				     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];

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

		for (OFMutableSet *channel in _channels) {
			if ([channel containsObject: user.nickname]) {
				[channel removeObject: user.nickname];
				[channel addObject: nickname];
			}
		}

		if ([_delegate respondsToSelector:
		    @selector(connection:didSeeUser:changeNicknameTo:)])
			[_delegate connection: self
				   didSeeUser: user
			     changeNicknameTo: nickname];

		return;
	}

	/* PRIVMSG */
	if ([action isEqual: @"PRIVMSG"] && components.count >= 4) {
		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
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
							user: 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];







|




|
|


|

|







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
							user: 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:
		    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];
571
572
573
574
575
576
577


578
579
580
581
582
583
584
	}
}

- (void)irc_sendPing
{
	[_pingData release];
	[_pingTimer release];



	_pingData = [[OFString alloc] initWithFormat: @":%d", rand()];
	[_socket writeFormat: @"PING %@\r\n", _pingData];

	_pingTimer = [[OFTimer
	    scheduledTimerWithTimeInterval: _pingTimeout
				    target: self







>
>







561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
	}
}

- (void)irc_sendPing
{
	[_pingData release];
	[_pingTimer release];
	_pingData = nil;
	_pingTimer = nil;

	_pingData = [[OFString alloc] initWithFormat: @":%d", rand()];
	[_socket writeFormat: @"PING %@\r\n", _pingData];

	_pingTimer = [[OFTimer
	    scheduledTimerWithTimeInterval: _pingTimeout
				    target: self
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
	}

	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







|
<











611
612
613
614
615
616
617
618

619
620
621
622
623
624
625
626
627
628
629
	}

	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

Modified src/IRCUser.m from [61a450e638] to [d20841002e].

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
	char *tmp2 = NULL;

	self = [super init];

	@try {
		char *tmp;

		if ((tmp2 = strdup([string UTF8String])) == NULL)
			@throw [OFOutOfMemoryException
			     exceptionWithRequestedSize:
			     [string UTF8StringLength]];

		if ((tmp = strchr(tmp2, '@')) == NULL)
			@throw [OFInvalidFormatException exception];

		*tmp = '\0';
		_hostname = [[OFString alloc] initWithUTF8String: tmp + 1];








<
<
<
|







46
47
48
49
50
51
52



53
54
55
56
57
58
59
60
	char *tmp2 = NULL;

	self = [super init];

	@try {
		char *tmp;




		tmp2 = OFStrDup(string.UTF8String);

		if ((tmp = strchr(tmp2, '@')) == NULL)
			@throw [OFInvalidFormatException exception];

		*tmp = '\0';
		_hostname = [[OFString alloc] initWithUTF8String: tmp + 1];

Modified tests/tests.m from [e0efa0bc26] to [8932bff494].

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
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
OF_APPLICATION_DELEGATE(TestApp)

@implementation TestApp
- (void)applicationDidFinishLaunching
{
	IRCConnection *connection = [[IRCConnection alloc] init];

	[connection setServer: @"irc.freenode.net"];
	[connection setNickname: @"ObjIRC"];
	[connection setUsername: @"ObjIRC"];
	[connection setRealname: @"ObjIRC"];
	[connection setDelegate: self];

	[connection connect];
}

- (void)connection: (IRCConnection*)connection
    didReceiveLine: (OFString*)line
{
	[of_stderr writeFormat: @"> %@\n", line];
}

- (void)connection: (IRCConnection*)connection
       didSendLine: (OFString*)line
{
	[of_stderr writeFormat: @"< %@\n", line];
}

- (void)connectionWasEstablished: (IRCConnection*)connection
{
	[connection joinChannel: @"#objfw"];
}

-	       (void)connection: (IRCConnection *)connection
  didFailToConnectWithException: (id)exception
{
	[of_stderr writeFormat: @"Failed to connect: %@\n", exception];

	[OFApplication terminateWithStatus: 1];
}

- (void)connection: (IRCConnection*)connection
	didSeeUser: (IRCUser*)user
       joinChannel: (OFString*)channel
{
	of_log(@"%@ joined %@.", user, channel);
}

- (void)connection: (IRCConnection*)connection
	didSeeUser: (IRCUser*)user
      leaveChannel: (OFString*)channel
	    reason: (OFString*)reason
{
	of_log(@"%@ left %@ (%@).", user, channel, reason);
}

-    (void)connection: (IRCConnection*)connection
	   didSeeUser: (IRCUser*)user
	     kickUser: (OFString*)kickedUser
	      channel: (OFString*)channel
	       reason: (OFString*)reason
{
	of_log(@"%@ kicked %@ from %@: %@", user, kickedUser, channel, reason);
}

- (void)connection: (IRCConnection*)connection
    didSeeUserQuit: (IRCUser*)user
	    reason: (OFString*)reason
{
	of_log(@"%@ quit (%@).", user, reason);
}

- (void)connection: (IRCConnection*)connection
	didSeeUser: (IRCUser*)user
  changeNicknameTo: (OFString *)nickname
{
	of_log(@"%@ changed nick to %@.", user, nickname);
}

-  (void)connection: (IRCConnection*)connection
  didReceiveMessage: (OFString*)msg
	    channel: (OFString*)channel
	       user: (IRCUser*)user
{
	of_log(@"[%@] %@: %@", channel, [user nickname], msg);
}

-	  (void)connection: (IRCConnection*)connection
  didReceivePrivateMessage: (OFString*)msg
		      user: (IRCUser*)user
{
	of_log(@"(%@): %@", user, msg);
}

- (void)connection: (IRCConnection*)connection
  didReceiveNotice: (OFString*)notice
	   channel: (OFString*)channel
	      user: (IRCUser*)user
{
	of_log(@"NOTICE: [%@] %@: %@", channel, [user nickname], notice);
}

- (void)connection: (IRCConnection*)connection
  didReceiveNotice: (OFString*)notice
	      user: (IRCUser*)user
{
	of_log(@"NOTICE: (%@): %@", user, notice);
}

-	   (void)connection: (IRCConnection*)connection
  didReceiveNamesForChannel: (OFString*)channel
{
	of_log(@"Users in %@: %@", channel,
	    [connection usersInChannel: channel]);
}

- (void)connectionWasClosed: (IRCConnection*)connection
{
	of_log(@"Disconnected!");

	[OFApplication terminate];
}
@end







|
|
|
|
|




|
<

|


|
<

|










|








|







|








|






|






|







|






|







|






|





|





|




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
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
OF_APPLICATION_DELEGATE(TestApp)

@implementation TestApp
- (void)applicationDidFinishLaunching
{
	IRCConnection *connection = [[IRCConnection alloc] init];

	connection.server = @"irc.freenode.net";
	connection.nickname = @"ObjIRC";
	connection.username = @"ObjIRC";
	connection.realname = @"ObjIRC";
	connection.delegate = self;

	[connection connect];
}

- (void)connection: (IRCConnection*)connection didReceiveLine: (OFString*)line

{
	[OFStdErr writeFormat: @"> %@\n", line];
}

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

{
	[OFStdErr writeFormat: @"< %@\n", line];
}

- (void)connectionWasEstablished: (IRCConnection*)connection
{
	[connection joinChannel: @"#objfw"];
}

-	       (void)connection: (IRCConnection *)connection
  didFailToConnectWithException: (id)exception
{
	[OFStdErr writeFormat: @"Failed to connect: %@\n", exception];

	[OFApplication terminateWithStatus: 1];
}

- (void)connection: (IRCConnection*)connection
	didSeeUser: (IRCUser*)user
       joinChannel: (OFString*)channel
{
	OFLog(@"%@ joined %@.", user, channel);
}

- (void)connection: (IRCConnection*)connection
	didSeeUser: (IRCUser*)user
      leaveChannel: (OFString*)channel
	    reason: (OFString*)reason
{
	OFLog(@"%@ left %@ (%@).", user, channel, reason);
}

-    (void)connection: (IRCConnection*)connection
	   didSeeUser: (IRCUser*)user
	     kickUser: (OFString*)kickedUser
	      channel: (OFString*)channel
	       reason: (OFString*)reason
{
	OFLog(@"%@ kicked %@ from %@: %@", user, kickedUser, channel, reason);
}

- (void)connection: (IRCConnection*)connection
    didSeeUserQuit: (IRCUser*)user
	    reason: (OFString*)reason
{
	OFLog(@"%@ quit (%@).", user, reason);
}

- (void)connection: (IRCConnection*)connection
	didSeeUser: (IRCUser*)user
  changeNicknameTo: (OFString *)nickname
{
	OFLog(@"%@ changed nick to %@.", user, nickname);
}

-  (void)connection: (IRCConnection*)connection
  didReceiveMessage: (OFString*)msg
	    channel: (OFString*)channel
	       user: (IRCUser*)user
{
	OFLog(@"[%@] %@: %@", channel, [user nickname], msg);
}

-	  (void)connection: (IRCConnection*)connection
  didReceivePrivateMessage: (OFString*)msg
		      user: (IRCUser*)user
{
	OFLog(@"(%@): %@", user, msg);
}

- (void)connection: (IRCConnection*)connection
  didReceiveNotice: (OFString*)notice
	   channel: (OFString*)channel
	      user: (IRCUser*)user
{
	OFLog(@"NOTICE: [%@] %@: %@", channel, [user nickname], notice);
}

- (void)connection: (IRCConnection*)connection
  didReceiveNotice: (OFString*)notice
	      user: (IRCUser*)user
{
	OFLog(@"NOTICE: (%@): %@", user, notice);
}

-	   (void)connection: (IRCConnection*)connection
  didReceiveNamesForChannel: (OFString*)channel
{
	OFLog(@"Users in %@: %@", channel,
	    [connection usersInChannel: channel]);
}

- (void)connectionWasClosed: (IRCConnection*)connection
{
	OFLog(@"Disconnected!");

	[OFApplication terminate];
}
@end