using SharpChat.Packet;
using System.Linq;
using System.Text;
namespace SharpChat.Commands {
public class WhoCommand : IChatCommand {
public bool IsMatch(ChatCommandContext ctx) {
return ctx.NameEquals("who");
}
public void Dispatch(ChatCommandContext ctx) {
StringBuilder whoChanSB = new();
string whoChanStr = ctx.Args.FirstOrDefault();
if(string.IsNullOrEmpty(whoChanStr)) {
lock(ctx.Chat.UsersAccess)
foreach(ChatUser whoUser in ctx.Chat.Users) {
whoChanSB.Append(@"');
whoChanSB.Append(whoUser.DisplayName);
whoChanSB.Append(", ");
}
if(whoChanSB.Length > 2)
whoChanSB.Length -= 2;
ctx.User.Send(new LegacyCommandResponse(LCR.USERS_LISTING_SERVER, false, whoChanSB));
} else {
ChatChannel whoChan;
lock(ctx.Chat.ChannelsAccess)
whoChan = ctx.Chat.Channels.FirstOrDefault(c => c.NameEquals(whoChanStr));
if(whoChan == null) {
ctx.User.Send(new LegacyCommandResponse(LCR.CHANNEL_NOT_FOUND, true, whoChanStr));
return;
}
if(whoChan.Rank > ctx.User.Rank || (whoChan.HasPassword && !ctx.User.Can(ChatUserPermissions.JoinAnyChannel))) {
ctx.User.Send(new LegacyCommandResponse(LCR.USERS_LISTING_ERROR, true, whoChanStr));
return;
}
foreach(ChatUser whoUser in whoChan.GetUsers()) {
whoChanSB.Append(@"');
whoChanSB.Append(whoUser.DisplayName);
whoChanSB.Append(", ");
}
if(whoChanSB.Length > 2)
whoChanSB.Length -= 2;
ctx.User.Send(new LegacyCommandResponse(LCR.USERS_LISTING_CHANNEL, false, whoChan.Name, whoChanSB));
}
}
}
}