30 lines
1.1 KiB
C#
30 lines
1.1 KiB
C#
using SharpChat.Packet;
|
|
using System.Linq;
|
|
using System.Net;
|
|
|
|
namespace SharpChat.Commands {
|
|
public class WhoisCommand : IUserCommand {
|
|
public bool IsMatch(UserCommandContext ctx) {
|
|
return ctx.NameEquals("ip")
|
|
|| ctx.NameEquals("whois");
|
|
}
|
|
|
|
public void Dispatch(UserCommandContext ctx) {
|
|
if(!ctx.User.Permissions.HasFlag(UserPermissions.SeeIPAddress)) {
|
|
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
|
|
return;
|
|
}
|
|
|
|
string ipUserStr = ctx.Args.FirstOrDefault() ?? string.Empty;
|
|
UserInfo? ipUser;
|
|
|
|
if(string.IsNullOrWhiteSpace(ipUserStr) || (ipUser = ctx.Chat.Users.Values.FirstOrDefault(u => u.NameEquals(ipUserStr))) == null) {
|
|
ctx.Chat.SendTo(ctx.User, new UserNotFoundErrorPacket(ipUserStr));
|
|
return;
|
|
}
|
|
|
|
foreach(IPAddress ip in ctx.Chat.GetRemoteAddresses(ipUser))
|
|
ctx.Chat.SendTo(ctx.User, new WhoisResponsePacket(ipUser.UserName, ip.ToString()));
|
|
}
|
|
}
|
|
}
|