2023-02-16 20:34:59 +00:00
|
|
|
|
using SharpChat.Misuzu;
|
|
|
|
|
using SharpChat.Packet;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SharpChat.Commands {
|
2024-05-19 02:17:51 +00:00
|
|
|
|
public class PardonAddressCommand : IUserCommand {
|
2023-02-16 20:34:59 +00:00
|
|
|
|
private readonly MisuzuClient Misuzu;
|
|
|
|
|
|
|
|
|
|
public PardonAddressCommand(MisuzuClient msz) {
|
2024-05-19 01:53:14 +00:00
|
|
|
|
Misuzu = msz;
|
2023-02-16 20:34:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-19 02:17:51 +00:00
|
|
|
|
public bool IsMatch(UserCommandContext ctx) {
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return ctx.NameEquals("pardonip")
|
|
|
|
|
|| ctx.NameEquals("unbanip");
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-19 02:17:51 +00:00
|
|
|
|
public void Dispatch(UserCommandContext ctx) {
|
|
|
|
|
if(!ctx.User.Permissions.HasFlag(UserPermissions.KickUser)
|
|
|
|
|
&& !ctx.User.Permissions.HasFlag(UserPermissions.BanUser)) {
|
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-10 19:18:55 +00:00
|
|
|
|
string? unbanAddrTarget = ctx.Args.FirstOrDefault();
|
|
|
|
|
if(string.IsNullOrWhiteSpace(unbanAddrTarget) || !IPAddress.TryParse(unbanAddrTarget, out IPAddress? unbanAddr)) {
|
2024-05-14 19:11:09 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket());
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unbanAddrTarget = unbanAddr.ToString();
|
|
|
|
|
|
|
|
|
|
Task.Run(async () => {
|
2024-05-10 19:18:55 +00:00
|
|
|
|
MisuzuBanInfo? banInfo = await Misuzu.CheckBanAsync(ipAddr: unbanAddrTarget);
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
if(banInfo?.IsBanned != true || banInfo.HasExpired) {
|
2024-05-14 22:17:25 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new KickBanNoRecordErrorPacket(unbanAddrTarget));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool wasBanned = await Misuzu.RevokeBanAsync(banInfo, MisuzuClient.BanRevokeKind.RemoteAddress);
|
|
|
|
|
if(wasBanned)
|
2024-05-14 22:17:25 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new PardonResponsePacket(unbanAddrTarget));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
else
|
2024-05-14 22:17:25 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new KickBanNoRecordErrorPacket(unbanAddrTarget));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
}).Wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|