2025-04-26 22:47:57 +00:00
|
|
|
using SharpChat.SockChat.S2CPackets;
|
2023-02-16 21:34:59 +01:00
|
|
|
using System.Net;
|
|
|
|
|
2025-04-26 23:15:54 +00:00
|
|
|
namespace SharpChat.ClientCommands;
|
2023-02-16 21:34:59 +01:00
|
|
|
|
2025-04-26 23:15:54 +00:00
|
|
|
public class RemoteAddressClientCommand : ClientCommand {
|
|
|
|
public bool IsMatch(ClientCommandContext ctx) {
|
|
|
|
return ctx.NameEquals("ip")
|
|
|
|
|| ctx.NameEquals("whois");
|
|
|
|
}
|
2025-04-25 20:05:55 +00:00
|
|
|
|
2025-04-26 23:15:54 +00:00
|
|
|
public async Task Dispatch(ClientCommandContext ctx) {
|
|
|
|
long msgId = ctx.Chat.RandomSnowflake.Next();
|
2023-02-16 21:34:59 +01:00
|
|
|
|
2025-04-26 23:15:54 +00:00
|
|
|
if(!ctx.User.Can(UserPermissions.SeeIPAddress)) {
|
|
|
|
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.COMMAND_NOT_ALLOWED, true, "/ip"));
|
|
|
|
return;
|
|
|
|
}
|
2023-02-16 21:34:59 +01:00
|
|
|
|
2025-04-26 23:15:54 +00:00
|
|
|
string? ipUserStr = ctx.Args.FirstOrDefault();
|
|
|
|
User? ipUser = null;
|
2023-02-16 21:34:59 +01:00
|
|
|
|
2025-04-26 23:15:54 +00:00
|
|
|
if(string.IsNullOrWhiteSpace(ipUserStr) || (ipUser = ctx.Chat.Users.FirstOrDefault(u => u.NameEquals(ipUserStr))) == null) {
|
|
|
|
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.USER_NOT_FOUND, true, ipUserStr ?? "User"));
|
|
|
|
return;
|
2023-02-16 21:34:59 +01:00
|
|
|
}
|
2025-04-26 23:15:54 +00:00
|
|
|
|
|
|
|
foreach(IPAddress ip in ctx.Chat.GetRemoteAddresses(ipUser))
|
|
|
|
await ctx.Chat.SendTo(ctx.User, new CommandResponseS2CPacket(msgId, LCR.IP_ADDRESS, false, ipUser.UserName, ip));
|
2023-02-16 21:34:59 +01:00
|
|
|
}
|
|
|
|
}
|