2023-02-16 20:34:59 +00:00
|
|
|
|
using SharpChat.Misuzu;
|
|
|
|
|
using SharpChat.Packet;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SharpChat.Commands {
|
|
|
|
|
public class KickBanCommand : IChatCommand {
|
|
|
|
|
private readonly MisuzuClient Misuzu;
|
|
|
|
|
|
|
|
|
|
public KickBanCommand(MisuzuClient msz) {
|
|
|
|
|
Misuzu = msz ?? throw new ArgumentNullException(nameof(msz));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsMatch(ChatCommandContext ctx) {
|
|
|
|
|
return ctx.NameEquals("kick")
|
|
|
|
|
|| ctx.NameEquals("ban");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispatch(ChatCommandContext ctx) {
|
|
|
|
|
bool isBanning = ctx.NameEquals("ban");
|
|
|
|
|
|
|
|
|
|
if(!ctx.User.Can(isBanning ? ChatUserPermissions.BanUser : ChatUserPermissions.KickUser)) {
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
string? banUserTarget = ctx.Args.ElementAtOrDefault(0);
|
|
|
|
|
string? banDurationStr = ctx.Args.ElementAtOrDefault(1);
|
2023-02-16 20:34:59 +00:00
|
|
|
|
int banReasonIndex = 1;
|
2024-05-10 19:18:55 +00:00
|
|
|
|
ChatUser? banUser = null;
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
2023-02-19 22:27:08 +00:00
|
|
|
|
if(banUserTarget == null || (banUser = ctx.Chat.Users.FirstOrDefault(u => u.NameEquals(banUserTarget))) == null) {
|
2024-05-10 19:18:55 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.USER_NOT_FOUND, true, banUserTarget ?? string.Empty));
|
2023-02-19 22:27:08 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
2023-11-07 14:50:55 +00:00
|
|
|
|
if(!ctx.User.IsSuper && banUser.Rank >= ctx.User.Rank && banUser != ctx.User) {
|
2023-02-22 00:28:53 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.KICK_NOT_ALLOWED, true, banUser.LegacyName));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TimeSpan duration = isBanning ? TimeSpan.MaxValue : TimeSpan.Zero;
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(banDurationStr) && double.TryParse(banDurationStr, out double durationSeconds)) {
|
|
|
|
|
if(durationSeconds < 0) {
|
2023-02-16 22:33:48 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
duration = TimeSpan.FromSeconds(durationSeconds);
|
|
|
|
|
++banReasonIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(duration <= TimeSpan.Zero) {
|
|
|
|
|
ctx.Chat.BanUser(banUser, duration);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string banReason = string.Join(' ', ctx.Args.Skip(banReasonIndex));
|
|
|
|
|
|
|
|
|
|
Task.Run(async () => {
|
2023-02-16 22:33:48 +00:00
|
|
|
|
string userId = banUser.UserId.ToString();
|
|
|
|
|
string userIp = ctx.Chat.GetRemoteAddresses(banUser).FirstOrDefault()?.ToString() ?? string.Empty;
|
|
|
|
|
|
2023-02-16 20:34:59 +00:00
|
|
|
|
// obviously it makes no sense to only check for one ip address but that's current misuzu limitations
|
2024-05-10 19:18:55 +00:00
|
|
|
|
MisuzuBanInfo? fbi = await Misuzu.CheckBanAsync(userId, userIp);
|
2023-02-16 20:34:59 +00:00
|
|
|
|
|
2024-05-10 19:18:55 +00:00
|
|
|
|
if(fbi != null && fbi.IsBanned && !fbi.HasExpired) {
|
2023-02-22 00:28:53 +00:00
|
|
|
|
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.KICK_NOT_ALLOWED, true, banUser.LegacyName));
|
2023-02-16 20:34:59 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Misuzu.CreateBanAsync(
|
2023-02-16 22:33:48 +00:00
|
|
|
|
userId, userIp,
|
2023-02-16 21:25:41 +00:00
|
|
|
|
ctx.User.UserId.ToString(), ctx.Connection.RemoteAddress.ToString(),
|
2023-02-16 20:34:59 +00:00
|
|
|
|
duration, banReason
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
ctx.Chat.BanUser(banUser, duration);
|
|
|
|
|
}).Wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|