sharp-chat/SharpChat/Commands/WhoisCommand.cs

31 lines
1.1 KiB
C#
Raw Normal View History

using SharpChat.Packet;
using System.Linq;
using System.Net;
namespace SharpChat.Commands {
public class WhoisCommand : IChatCommand {
public bool IsMatch(ChatCommandContext ctx) {
return ctx.NameEquals("ip")
|| ctx.NameEquals("whois");
}
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.SeeIPAddress)) {
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}
2024-05-10 19:18:55 +00:00
string? ipUserStr = ctx.Args.FirstOrDefault();
ChatUser? ipUser;
2023-02-19 22:27:08 +00:00
if(string.IsNullOrWhiteSpace(ipUserStr) || (ipUser = ctx.Chat.Users.FirstOrDefault(u => u.NameEquals(ipUserStr))) == null) {
2024-05-10 19:18:55 +00:00
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.USER_NOT_FOUND, true, ipUserStr ?? string.Empty));
2023-02-19 22:27:08 +00:00
return;
}
foreach(IPAddress ip in ctx.Chat.GetRemoteAddresses(ipUser))
ctx.Chat.SendTo(ctx.User, new WhoisResponsePacket(ipUser.UserName, ip.ToString()));
}
}
}