2023-02-16 20:34:59 +00:00
|
|
|
|
using SharpChat.Packet;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
|
|
|
|
namespace SharpChat.Commands {
|
2024-05-19 02:17:51 +00:00
|
|
|
|
public class WhoisCommand : IUserCommand {
|
|
|
|
|
public bool IsMatch(UserCommandContext ctx) {
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return ctx.NameEquals("ip")
|
|
|
|
|
|| ctx.NameEquals("whois");
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-19 02:17:51 +00:00
|
|
|
|
public void Dispatch(UserCommandContext ctx) {
|
|
|
|
|
if(!ctx.User.Permissions.HasFlag(UserPermissions.SeeIPAddress)) {
|
2024-05-14 19:11:09 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-14 22:17:25 +00:00
|
|
|
|
string ipUserStr = ctx.Args.FirstOrDefault() ?? string.Empty;
|
2024-05-19 02:17:51 +00:00
|
|
|
|
UserInfo? ipUser;
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
2024-05-19 21:02:17 +00:00
|
|
|
|
(string name, UsersContext.NameTarget target) = SockChatUtility.ExplodeUserName(ipUserStr);
|
|
|
|
|
if(string.IsNullOrWhiteSpace(name) || (ipUser = ctx.Chat.Users.Get(name: name, nameTarget: target)) == null) {
|
2024-05-14 22:17:25 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new UserNotFoundErrorPacket(ipUserStr));
|
2023-02-19 22:27:08 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
2023-02-16 22:33:48 +00:00
|
|
|
|
foreach(IPAddress ip in ctx.Chat.GetRemoteAddresses(ipUser))
|
2024-05-14 19:11:09 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new WhoisResponsePacket(ipUser.UserName, ip.ToString()));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|