65 lines
2.4 KiB
C#
65 lines
2.4 KiB
C#
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) {
|
|
ctx.Chat.SendTo(ctx.User, new ChannelRankTooHighErrorPacket());
|
|
return;
|
|
}
|
|
|
|
string createChanName = string.Join('_', ctx.Args.Skip(createChanHasHierarchy ? 1 : 0));
|
|
|
|
if(!ChannelInfo.CheckName(createChanName)) {
|
|
ctx.Chat.SendTo(ctx.User, new ChannelNameFormatErrorPacket());
|
|
return;
|
|
}
|
|
|
|
if(ctx.Chat.Channels.Values.Any(c => c.NameEquals(createChanName))) {
|
|
ctx.Chat.SendTo(ctx.User, new ChannelNameInUseErrorPacket(createChanName));
|
|
return;
|
|
}
|
|
|
|
ChannelInfo createChan = new(
|
|
createChanName,
|
|
isTemporary: !ctx.User.Permissions.HasFlag(UserPermissions.SetChannelPermanent),
|
|
rank: createChanHierarchy,
|
|
ownerId: ctx.User.UserId
|
|
);
|
|
|
|
ctx.Chat.Channels.Add(createChan.Name, createChan);
|
|
foreach(UserInfo ccu in ctx.Chat.Users.Values.Where(u => u.Rank >= ctx.Channel.Rank))
|
|
ctx.Chat.SendTo(ccu, new ChannelCreatePacket(
|
|
ctx.Channel.Name,
|
|
ctx.Channel.HasPassword,
|
|
ctx.Channel.IsTemporary
|
|
));
|
|
|
|
ctx.Chat.SwitchChannel(ctx.User, createChan, createChan.Password);
|
|
ctx.Chat.SendTo(ctx.User, new ChannelCreateResponsePacket(createChan.Name));
|
|
}
|
|
}
|
|
}
|