ObjIRC  Diff

Differences From Artifact [590ed4cde0]:

To Artifact [1078b7859d]:


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
}

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

- (void)handleConnection
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFString *line;
	OFArray *split;

	for (;;) {
		OFString *action = nil;

		@try {
			line = [sock readLine];
		} @catch (OFInvalidEncodingException *e) {
			[e dealloc];
			line = [sock readLineWithEncoding:
			    OF_STRING_ENCODING_WINDOWS_1252];
		}

		if (line == nil)
			break;

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

			continue;
		}

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

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


			continue;
		}

		/* 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)];
			where = [where substringWithRange:
			    of_range(1, where.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];

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


			continue;
		}

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

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


			continue;
		}

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

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


			continue;
		}

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

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


			continue;
		}

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

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




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


			continue;
		}

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

				continue;


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


			continue;
		}

		[pool releaseObjects];
	}



	[pool release];
}

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







|




<
<
|

|
|
|
|
|
|
|

|
|

|
|
|
|

|

|
|
|
|
|
|

|
|

|

|
|
|
|
|

>
|
|

|
|
|
|
|
|

|
<
|
|
|

<
|
|
|
|
|
|

|
|
|
|
|

>
|
|

|
|
|
|
|
|
|
|
|
<

|
<
|
|

|
|
|

|
|
|
|
|
|
|

>
|
|

|
|
|
|
|
|
|
|
|
|
|

|
<
|
|

|
|
|

|
|
|
|
|
|
|
|

>
|
|

|
|
|
|
|
|
<

|
<
|

|
|
|

|
|
|
|
|

>
|
|

|
|
|
|
|

|
<
|
|

|

|
|
|
|

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

|
<
|
|
|

|
|

|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

>
|
|

|
|
|
|
|
|
|
|
<

|
<
|
|

|
|
>
|
>

|

|
|

|

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|

>
|
|
|
<
|
>
|
>
|







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
}

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

- (void)process
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFString *line;
	OFArray *split;


	OFString *action = nil;

	@try {
		line = [sock tryReadLine];
	} @catch (OFInvalidEncodingException *e) {
		[e dealloc];
		line = [sock tryReadLineWithEncoding:
		    OF_STRING_ENCODING_WINDOWS_1252];
	}

	if (line == nil)
		return;

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

		where = [where substringWithRange:
		    of_range(1, where.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];

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

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

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

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

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


- (void)handleConnection
{
	while (![sock isAtEndOfStream])
		[self process];
}

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