Keep track of users in a channel.

FossilOrigin-Name: 5f51d55981a74610c7d36038d540cc689b15d41194b00b2e8b0c7428bf102341
This commit is contained in:
Jonathan Schleifer 2011-10-05 19:40:33 +00:00
parent 77e2dba178
commit 318f4a2c1b
5 changed files with 83 additions and 1 deletions

View file

@ -21,16 +21,21 @@
*/
#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

View file

@ -23,7 +23,7 @@
#import "IRCChannel.h"
@implementation IRCChannel
@synthesize name;
@synthesize name, users;
+ channelWithName: (OFString*)name
{
@ -36,6 +36,7 @@
@try {
name = [name_ copy];
users = [[OFMutableSet alloc] init];
} @catch (id e) {
[self release];
@throw e;
@ -47,6 +48,7 @@
- (void)dealloc
{
[name release];
[users release];
[super dealloc];
}
@ -55,4 +57,14 @@
{
return name;
}
- (void)IRC_addUser: (OFString*)user
{
[users addObject: user];
}
- (void)IRC_removeUser: (OFString*)user
{
[users removeObject: user];
}
@end

View file

@ -68,6 +68,8 @@
didReceiveNotice: (OFString*)notice
fromUser: (IRCUser*)user
inChannel: (IRCChannel*)channel;
- (void)connection: (IRCConnection*)connection
didReceiveNamesForChannel: (IRCChannel*)channel;
@end
@interface IRCConnection: OFObject

View file

@ -226,6 +226,8 @@
} else
channel = [channels objectForKey: where];
[channel IRC_addUser: user.nickname];
if ([delegate respondsToSelector:
@selector(connection:didSeeUser:joinChannel:)])
[delegate connection: self
@ -236,6 +238,47 @@
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];
@ -254,6 +297,8 @@
reason = [line substringWithRange:
of_range(pos + 2, line.length - pos - 2)];
[channel IRC_removeUser: user.nickname];
if ([delegate respondsToSelector:
@selector(connection:didSeeUser:leaveChannel:
withReason:)])
@ -286,6 +331,8 @@
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:)])
@ -313,6 +360,9 @@
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
@ -340,6 +390,13 @@
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