36 lines
1.4 KiB
C#
36 lines
1.4 KiB
C#
using SharpChat.PacketsS2C;
|
|
using System.Linq;
|
|
|
|
namespace SharpChat.Commands {
|
|
public class ChannelDeleteCommand : ISockChatClientCommand {
|
|
public bool IsMatch(SockChatClientCommandContext ctx) {
|
|
return ctx.NameEquals("delchan") || (
|
|
ctx.NameEquals("delete")
|
|
&& ctx.Args.FirstOrDefault()?.All(char.IsDigit) == false
|
|
);
|
|
}
|
|
|
|
public void Dispatch(SockChatClientCommandContext ctx) {
|
|
if(!ctx.Args.Any() || string.IsNullOrWhiteSpace(ctx.Args.FirstOrDefault())) {
|
|
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorS2CPacket());
|
|
return;
|
|
}
|
|
|
|
string delChanName = string.Join('_', ctx.Args);
|
|
ChannelInfo? delChan = ctx.Chat.Channels.Get(delChanName, SockChatUtility.SanitiseChannelName);
|
|
|
|
if(delChan == null) {
|
|
ctx.Chat.SendTo(ctx.User, new ChannelNotFoundErrorS2CPacket(delChanName));
|
|
return;
|
|
}
|
|
|
|
if(!ctx.User.Permissions.HasFlag(UserPermissions.DeleteChannel) || !delChan.IsOwner(ctx.User)) {
|
|
ctx.Chat.SendTo(ctx.User, new ChannelDeleteNotAllowedErrorS2CPacket(delChan.Name));
|
|
return;
|
|
}
|
|
|
|
ctx.Chat.RemoveChannel(delChan);
|
|
ctx.Chat.SendTo(ctx.User, new ChannelDeleteResponseS2CPacket(delChan.Name));
|
|
}
|
|
}
|
|
}
|