ObjIRC  Check-in [5f51d55981]

Overview
Comment:Keep track of users in a channel.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 5f51d55981a74610c7d36038d540cc689b15d41194b00b2e8b0c7428bf102341
User & Date: js on 2011-10-05 19:40:34
Other Links: manifest | tags
Context
2011-10-24
18:34
Don't retain the delegate to prevent a reference cycle. check-in: c2aa35d109 user: js tags: trunk
2011-10-05
19:40
Keep track of users in a channel. check-in: 5f51d55981 user: js tags: trunk
19:26
Fix adding channels to the list of joined channels. check-in: a515b09ef0 user: js tags: trunk
Changes

Modified src/IRCChannel.h from [58d378b5d3] to [25d9875e0e].

17
18
19
20
21
22
23

24
25
26
27
28
29

30
31
32

33
34
35


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







+






+



+



+
+

 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#import <ObjFW/OFObject.h>
#import <ObjFW/OFSet.h>

@class OFString;

@interface IRCChannel: OFObject
{
	OFString *name;
	OFMutableSet *users;
}

@property (readonly) OFString *name;
@property (copy) OFSet *users;

+ channelWithName: (OFString*)name;
- initWithName: (OFString*)name;
- (void)IRC_addUser: (OFString*)user;
- (void)IRC_removeUser: (OFString*)user;
@end

Modified src/IRCChannel.m from [902ac1c988] to [9e11412db3].

19
20
21
22
23
24
25
26

27
28
29
30
31
32
33
34
35
36
37
38

39
40
41
42
43
44
45
46
47
48
49

50
51
52
53
54
55
56
57










58
19
20
21
22
23
24
25

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70







-
+












+











+








+
+
+
+
+
+
+
+
+
+

 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#import "IRCChannel.h"

@implementation IRCChannel
@synthesize name;
@synthesize name, users;

+ channelWithName: (OFString*)name
{
	return [[[self alloc] initWithName: name] autorelease];
}

- initWithName: (OFString*)name_
{
	self = [super init];

	@try {
		name = [name_ copy];
		users = [[OFMutableSet alloc] init];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[name release];
	[users release];

	[super dealloc];
}

- (OFString*)description
{
	return name;
}

- (void)IRC_addUser: (OFString*)user
{
	[users addObject: user];
}

- (void)IRC_removeUser: (OFString*)user
{
	[users removeObject: user];
}
@end

Modified src/IRCConnection.h from [8fa29103e7] to [db3258a581].

64
65
66
67
68
69
70


71
72
73
74
75
76
77
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79







+
+







- (void)connection: (IRCConnection*)connection
  didReceiveNotice: (OFString*)notice
	  fromUser: (IRCUser*)user;
- (void)connection: (IRCConnection*)connection
  didReceiveNotice: (OFString*)notice
	  fromUser: (IRCUser*)user
	 inChannel: (IRCChannel*)channel;
-	   (void)connection: (IRCConnection*)connection
  didReceiveNamesForChannel: (IRCChannel*)channel;
@end

@interface IRCConnection: OFObject
{
	OFTCPSocket *sock;
	OFString *server;
	uint16_t port;

Modified src/IRCConnection.m from [bf2838c044] to [687bea590f].

222
223
224
225
226
227
228


229
230
231
232
233
234









































235
236
237
238
239
240
241
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







+
+






+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







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

		[channel IRC_addUser: user.nickname];

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

		[pool release];
		return;
	}

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

		channel = [channels objectForKey: [split objectAtIndex: 4]];
		if (channel == nil) {
			/* We did not request that */
			[pool release];
			return;
		}

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

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

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

			[channel IRC_addUser: user];
		}

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

		[pool release];
		return;
	}

	/* PART */
	if ([action isEqual: @"PART"] && split.count >= 3) {
		OFString *who = [split objectAtIndex: 0];
249
250
251
252
253
254
255


256
257
258
259
260
261
262
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307







+
+







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

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

		[channel IRC_removeUser: user.nickname];

		if ([delegate respondsToSelector:
		    @selector(connection:didSeeUser:leaveChannel:
		    withReason:)])
			[delegate connection: self
				  didSeeUser: user
				leaveChannel: channel
282
283
284
285
286
287
288


289
290
291
292
293
294
295
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342







+
+







		user = [IRCUser IRCUserWithString: who];
		channel = [channels objectForKey: where];

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

		[channel IRC_removeUser: user.nickname];

		if ([delegate respondsToSelector:
		    @selector(connection:didSeeUser:kickUser:
		    fromChannel:withReason:)])
			[delegate connection: self
				  didSeeUser: user
				    kickUser: whom
				 fromChannel: channel
309
310
311
312
313
314
315



316
317
318
319
320
321
322
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372







+
+
+







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

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

		for (IRCChannel *channel in channels)
			[channel IRC_removeUser: user.nickname];

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

		[pool release];
335
336
337
338
339
340
341







342
343
344
345
346
347
348
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405







+
+
+
+
+
+
+








		user = [IRCUser IRCUserWithString: who];

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

		for (IRCChannel *channel in channels) {
			if ([channel.users containsObject: user.nickname]) {
				[channel IRC_removeUser: user.nickname];
				[channel IRC_addUser: newNickname];
			}
		}

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

Modified tests/test.m from [3b6f19e207] to [bf66f3f82c].

128
129
130
131
132
133
134






135
128
129
130
131
132
133
134
135
136
137
138
139
140
141







+
+
+
+
+
+

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

-	   (void)connection: (IRCConnection*)connection
  didReceiveNamesForChannel: (IRCChannel*)channel
{
	of_log(@"Users in %@: %@", channel, channel.users);
}
@end