sharp-chat/SharpChat/Commands/ChannelCreateCommand.cs

66 lines
2.5 KiB
C#
Raw Normal View History

using SharpChat.Packet;
using System.Linq;
namespace SharpChat.Commands {
public class ChannelCreateCommand : IUserCommand {
public bool IsMatch(UserCommandContext ctx) {
return ctx.NameEquals("create");
}
public void Dispatch(UserCommandContext ctx) {
if(!ctx.User.Permissions.HasFlag(UserPermissions.CreateChannel)) {
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}
string firstArg = ctx.Args.First();
bool createChanHasHierarchy;
if(!ctx.Args.Any() || (createChanHasHierarchy = firstArg.All(char.IsDigit) && ctx.Args.Length < 2)) {
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket());
return;
}
int createChanHierarchy = 0;
if(createChanHasHierarchy)
if(!int.TryParse(firstArg, out createChanHierarchy))
createChanHierarchy = 0;
if(createChanHierarchy > ctx.User.Rank) {
2024-05-14 22:17:25 +00:00
ctx.Chat.SendTo(ctx.User, new ChannelRankTooHighErrorPacket());
return;
}
string createChanName = string.Join('_', ctx.Args.Skip(createChanHasHierarchy ? 1 : 0));
2024-05-19 21:02:17 +00:00
if(!SockChatUtility.CheckChannelName(createChanName)) {
2024-05-14 22:17:25 +00:00
ctx.Chat.SendTo(ctx.User, new ChannelNameFormatErrorPacket());
return;
}
2024-05-19 21:02:17 +00:00
if(ctx.Chat.Channels.Get(createChanName, SockChatUtility.SanitiseChannelName) != null) {
2024-05-14 22:17:25 +00:00
ctx.Chat.SendTo(ctx.User, new ChannelNameInUseErrorPacket(createChanName));
2023-02-19 22:27:08 +00:00
return;
}
ChannelInfo createChan = new(
createChanName,
isTemporary: !ctx.User.Permissions.HasFlag(UserPermissions.SetChannelPermanent),
rank: createChanHierarchy,
ownerId: ctx.User.UserId
);
2024-05-19 21:02:17 +00:00
ctx.Chat.Channels.Add(createChan, sanitiseName: SockChatUtility.SanitiseChannelName);
foreach(UserInfo ccu in ctx.Chat.Users.GetMany(minRank: ctx.Channel.Rank))
ctx.Chat.SendTo(ccu, new ChannelCreatePacket(
ctx.Channel.Name,
ctx.Channel.HasPassword,
ctx.Channel.IsTemporary
));
2023-02-19 22:27:08 +00:00
ctx.Chat.SwitchChannel(ctx.User, createChan, createChan.Password);
2024-05-14 22:17:25 +00:00
ctx.Chat.SendTo(ctx.User, new ChannelCreateResponsePacket(createChan.Name));
}
}
}