65 lines
2.6 KiB
C#
65 lines
2.6 KiB
C#
using SharpChat.PacketsS2C;
|
|
using System.Linq;
|
|
|
|
namespace SharpChat.Commands {
|
|
public class ChannelCreateCommand : ISockChatClientCommand {
|
|
public bool IsMatch(SockChatClientCommandContext ctx) {
|
|
return ctx.NameEquals("create");
|
|
}
|
|
|
|
public void Dispatch(SockChatClientCommandContext ctx) {
|
|
if(!ctx.User.Permissions.HasFlag(UserPermissions.CreateChannel)) {
|
|
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorS2CPacket(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 CommandFormatErrorS2CPacket());
|
|
return;
|
|
}
|
|
|
|
int createChanHierarchy = 0;
|
|
if(createChanHasHierarchy)
|
|
if(!int.TryParse(firstArg, out createChanHierarchy))
|
|
createChanHierarchy = 0;
|
|
|
|
if(createChanHierarchy > ctx.User.Rank) {
|
|
ctx.Chat.SendTo(ctx.User, new ChannelRankTooHighErrorS2CPacket());
|
|
return;
|
|
}
|
|
|
|
string createChanName = string.Join('_', ctx.Args.Skip(createChanHasHierarchy ? 1 : 0));
|
|
|
|
if(!SockChatUtility.CheckChannelName(createChanName)) {
|
|
ctx.Chat.SendTo(ctx.User, new ChannelNameFormatErrorS2CPacket());
|
|
return;
|
|
}
|
|
|
|
if(ctx.Chat.Channels.Get(createChanName, SockChatUtility.SanitiseChannelName) != null) {
|
|
ctx.Chat.SendTo(ctx.User, new ChannelNameInUseErrorS2CPacket(createChanName));
|
|
return;
|
|
}
|
|
|
|
ChannelInfo createChan = new(
|
|
createChanName,
|
|
isTemporary: !ctx.User.Permissions.HasFlag(UserPermissions.SetChannelPermanent),
|
|
rank: createChanHierarchy,
|
|
ownerId: ctx.User.UserId
|
|
);
|
|
|
|
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 ChannelCreateS2CPacket(
|
|
ctx.Channel.Name,
|
|
ctx.Channel.HasPassword,
|
|
ctx.Channel.IsTemporary
|
|
));
|
|
|
|
ctx.Chat.SwitchChannel(ctx.User, createChan, createChan.Password);
|
|
ctx.Chat.SendTo(ctx.User, new ChannelCreateResponseS2CPacket(createChan.Name));
|
|
}
|
|
}
|
|
}
|