sharp-chat/SharpChat.Protocol.SockChat/Commands/PardonIPCommand.cs

45 lines
1.6 KiB
C#
Raw Normal View History

2022-08-30 15:05:29 +00:00
using SharpChat.Bans;
using SharpChat.Protocol.SockChat.Packets;
using SharpChat.Users;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
namespace SharpChat.Protocol.SockChat.Commands {
public class PardonIPCommand : ICommand {
private BanManager Bans { get; }
private IUser Sender { get; }
public PardonIPCommand(BanManager bans, IUser sender) {
Bans = bans ?? throw new ArgumentNullException(nameof(bans));
Sender = sender ?? throw new ArgumentNullException(nameof(sender));
}
public bool IsCommandMatch(string name, IEnumerable<string> args)
=> name is @"pardonip" or @"unbanip";
public bool DispatchCommand(CommandContext ctx) {
if(!ctx.User.Can(UserPermissions.BanUser | UserPermissions.KickUser)) {
ctx.Connection.SendPacket(new CommandNotAllowedErrorPacket(Sender, ctx.Args));
return true;
}
string ipAddress = ctx.Args.ElementAtOrDefault(1);
if(!IPAddress.TryParse(ipAddress, out IPAddress ipAddr)) {
ctx.Connection.SendPacket(new NotBannedErrorPacket(Sender, ipAddr?.ToString() ?? @"::"));
return true;
}
Bans.RemoveBan(ipAddr, success => {
if(success)
ctx.Connection.SendPacket(new PardonResponsePacket(Sender, ipAddr));
else
ctx.Connection.SendPacket(new NotBannedErrorPacket(Sender, ipAddr.ToString()));
}, ex => ctx.Connection.SendPacket(new GenericErrorPacket(Sender)));
return true;
}
}
}