Created more discrete error/response packets.

This commit is contained in:
flash 2024-05-14 19:11:09 +00:00
parent 38f17c325a
commit 7bcf5acb7e
21 changed files with 104 additions and 41 deletions

View file

@ -285,7 +285,7 @@ namespace SharpChat {
if(!user.Can(ChatUserPermissions.JoinAnyChannel) && chan.IsOwner(user)) {
if(chan.Rank > user.Rank) {
SendTo(user, new LegacyCommandResponse(LCR.CHANNEL_INSUFFICIENT_HIERARCHY, true, chan.Name));
SendTo(user, new LegacyCommandResponse(LCR.CHANNEL_INSUFFICIENT_RANK, true, chan.Name));
ForceChannel(user);
return;
}

View file

@ -19,7 +19,7 @@ namespace SharpChat.Commands {
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.BanUser | ChatUserPermissions.KickUser)) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}

View file

@ -11,7 +11,7 @@ namespace SharpChat.Commands {
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.Broadcast)) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}

View file

@ -9,7 +9,7 @@ namespace SharpChat.Commands {
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.CreateChannel)) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}
@ -17,7 +17,7 @@ namespace SharpChat.Commands {
bool createChanHasHierarchy;
if(!ctx.Args.Any() || (createChanHasHierarchy = firstArg.All(char.IsDigit) && ctx.Args.Length < 2)) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket());
return;
}
@ -27,7 +27,7 @@ namespace SharpChat.Commands {
createChanHierarchy = 0;
if(createChanHierarchy > ctx.User.Rank) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.INSUFFICIENT_HIERARCHY));
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.INSUFFICIENT_RANK));
return;
}

View file

@ -12,7 +12,7 @@ namespace SharpChat.Commands {
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.Args.Any() || string.IsNullOrWhiteSpace(ctx.Args.FirstOrDefault())) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket());
return;
}

View file

@ -16,14 +16,14 @@ namespace SharpChat.Commands
bool deleteAnyMessage = ctx.User.Can(ChatUserPermissions.DeleteAnyMessage);
if(!deleteAnyMessage && !ctx.User.Can(ChatUserPermissions.DeleteOwnMessage)) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}
string? firstArg = ctx.Args.FirstOrDefault();
if(string.IsNullOrWhiteSpace(firstArg) || !firstArg.All(char.IsDigit) || !long.TryParse(firstArg, out long delSeqId)) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket());
return;
}

View file

@ -21,7 +21,7 @@ namespace SharpChat.Commands {
bool isBanning = ctx.NameEquals("ban");
if(!ctx.User.Can(isBanning ? ChatUserPermissions.BanUser : ChatUserPermissions.KickUser)) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}
@ -43,7 +43,7 @@ namespace SharpChat.Commands {
TimeSpan duration = isBanning ? TimeSpan.MaxValue : TimeSpan.Zero;
if(!string.IsNullOrWhiteSpace(banDurationStr) && double.TryParse(banDurationStr, out double durationSeconds)) {
if(durationSeconds < 0) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket());
return;
}

View file

@ -11,7 +11,7 @@ namespace SharpChat.Commands {
bool setOthersNick = ctx.User.Can(ChatUserPermissions.SetOthersNickname);
if(!setOthersNick && !ctx.User.Can(ChatUserPermissions.SetOwnNickname)) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}
@ -26,7 +26,7 @@ namespace SharpChat.Commands {
targetUser ??= ctx.User;
if(ctx.Args.Length < offset) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket());
return;
}

View file

@ -20,13 +20,13 @@ namespace SharpChat.Commands {
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.BanUser | ChatUserPermissions.KickUser)) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}
string? unbanAddrTarget = ctx.Args.FirstOrDefault();
if(string.IsNullOrWhiteSpace(unbanAddrTarget) || !IPAddress.TryParse(unbanAddrTarget, out IPAddress? unbanAddr)) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket());
return;
}

View file

@ -19,14 +19,14 @@ namespace SharpChat.Commands {
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.BanUser | ChatUserPermissions.KickUser)) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}
bool unbanUserTargetIsName = true;
string? unbanUserTarget = ctx.Args.FirstOrDefault();
if(string.IsNullOrWhiteSpace(unbanUserTarget)) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket());
return;
}

View file

@ -9,7 +9,7 @@ namespace SharpChat.Commands {
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.SetChannelPassword) || ctx.Channel.IsOwner(ctx.User)) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}

View file

@ -11,17 +11,17 @@ namespace SharpChat.Commands {
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.SetChannelHierarchy) || ctx.Channel.IsOwner(ctx.User)) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}
if(!ctx.Args.Any() || !int.TryParse(ctx.Args.First(), out int chanHierarchy) || chanHierarchy > ctx.User.Rank) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.INSUFFICIENT_HIERARCHY));
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.INSUFFICIENT_RANK));
return;
}
ctx.Chat.UpdateChannel(ctx.Channel, minRank: chanHierarchy);
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.CHANNEL_HIERARCHY_CHANGED, false));
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.CHANNEL_RANK_CHANGED, false));
}
}
}

View file

@ -19,7 +19,7 @@ namespace SharpChat.Commands {
public void Dispatch(ChatCommandContext ctx) {
if(ctx.User.UserId != 1) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, $"/{ctx.Name}"));
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}

View file

@ -12,7 +12,7 @@ namespace SharpChat.Commands {
public void Dispatch(ChatCommandContext ctx) {
if(ctx.Args.Length < 2) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_FORMAT_ERROR));
ctx.Chat.SendTo(ctx.User, new CommandFormatErrorPacket());
return;
}

View file

@ -3,7 +3,7 @@ using System.Linq;
using System.Net;
namespace SharpChat.Commands {
public class RemoteAddressCommand : IChatCommand {
public class WhoisCommand : IChatCommand {
public bool IsMatch(ChatCommandContext ctx) {
return ctx.NameEquals("ip")
|| ctx.NameEquals("whois");
@ -11,7 +11,7 @@ namespace SharpChat.Commands {
public void Dispatch(ChatCommandContext ctx) {
if(!ctx.User.Can(ChatUserPermissions.SeeIPAddress)) {
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.COMMAND_NOT_ALLOWED, true, "/ip"));
ctx.Chat.SendTo(ctx.User, new CommandNotAllowedErrorPacket(ctx.Name));
return;
}
@ -24,7 +24,7 @@ namespace SharpChat.Commands {
}
foreach(IPAddress ip in ctx.Chat.GetRemoteAddresses(ipUser))
ctx.Chat.SendTo(ctx.User, new LegacyCommandResponse(LCR.IP_ADDRESS, false, ipUser.UserName, ip));
ctx.Chat.SendTo(ctx.User, new WhoisResponsePacket(ipUser.UserName, ip.ToString()));
}
}
}

View file

@ -0,0 +1,15 @@
using System;
namespace SharpChat.Packet {
public class CommandFormatErrorPacket : ServerPacket {
private readonly long Timestamp;
public CommandFormatErrorPacket() {
Timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
}
public override string Pack() {
return string.Format("2\t{0}\t-1\t1\fcmdna\t{1}\t10010", Timestamp, SequenceId);
}
}
}

View file

@ -0,0 +1,17 @@
using System;
namespace SharpChat.Packet {
public class CommandNotAllowedErrorPacket : ServerPacket {
private readonly long Timestamp;
private readonly string CommandName;
public CommandNotAllowedErrorPacket(string commandName) {
Timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
CommandName = commandName;
}
public override string Pack() {
return string.Format("2\t{0}\t-1\t1\fcmdna\f/{1}\t{2}\t10010", Timestamp, CommandName, SequenceId);
}
}
}

View file

@ -0,0 +1,15 @@
using System;
namespace SharpChat.Packet {
public class FloodWarningPacket : ServerPacket {
private readonly long Timestamp;
public FloodWarningPacket() {
Timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
}
public override string Pack() {
return string.Format("2\t{0}\t-1\t0\fflwarn\t{1}\t10010", Timestamp, SequenceId);
}
}
}

View file

@ -38,29 +38,26 @@ namespace SharpChat.Packet {
// Abbreviated class name because otherwise shit gets wide
public static class LCR {
public const string COMMAND_NOT_ALLOWED = "cmdna";
public const string COMMAND_FORMAT_ERROR = "cmderr";
public const string IP_ADDRESS = "ipaddr";
public const string CHANNEL_CREATED = "crchan";
public const string CHANNEL_DELETED = "delchan";
public const string CHANNEL_PASSWORD_CHANGED = "cpwdchan";
public const string CHANNEL_RANK_CHANGED = "cprivchan";
public const string USERS_LISTING_CHANNEL = "whochan";
public const string USERS_LISTING_SERVER = "who";
public const string USER_UNBANNED = "unban";
public const string USER_NOT_FOUND = "usernf";
public const string NAME_IN_USE = "nameinuse";
public const string CHANNEL_INSUFFICIENT_HIERARCHY = "ipchan";
public const string CHANNEL_INSUFFICIENT_RANK = "ipchan";
public const string CHANNEL_INVALID_PASSWORD = "ipwchan";
public const string CHANNEL_NOT_FOUND = "nochan";
public const string CHANNEL_ALREADY_EXISTS = "nischan";
public const string CHANNEL_NAME_INVALID = "inchan";
public const string CHANNEL_CREATED = "crchan";
public const string CHANNEL_DELETE_FAILED = "ndchan";
public const string CHANNEL_DELETED = "delchan";
public const string CHANNEL_PASSWORD_CHANGED = "cpwdchan";
public const string CHANNEL_HIERARCHY_CHANGED = "cprivchan";
public const string USERS_LISTING_ERROR = "whoerr";
public const string USERS_LISTING_CHANNEL = "whochan";
public const string USERS_LISTING_SERVER = "who";
public const string INSUFFICIENT_HIERARCHY = "rankerr";
public const string INSUFFICIENT_RANK = "rankerr";
public const string MESSAGE_DELETE_ERROR = "delerr";
public const string KICK_NOT_ALLOWED = "kickna";
public const string USER_NOT_BANNED = "notban";
public const string USER_UNBANNED = "unban";
public const string FLOOD_WARN = "flwarn";
}
}

View file

@ -0,0 +1,19 @@
using System;
namespace SharpChat.Packet {
public class WhoisResponsePacket : ServerPacket {
private readonly long Timestamp;
private readonly string UserName;
private readonly string RemoteAddress;
public WhoisResponsePacket(string userName, string remoteAddress) {
Timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
UserName = userName;
RemoteAddress = remoteAddress;
}
public override string Pack() {
return string.Format("2\t{0}\t-1\t0\fipaddr\f{1}\f{2}\t{3}\t10010", Timestamp, UserName, RemoteAddress, SequenceId);
}
}
}

View file

@ -99,7 +99,7 @@ namespace SharpChat {
new PardonUserCommand(msz),
new PardonAddressCommand(msz),
new BanListCommand(msz),
new RemoteAddressCommand(),
new WhoisCommand(),
});
ushort port = config.SafeReadValue("port", DEFAULT_PORT);
@ -184,7 +184,7 @@ namespace SharpChat {
if(banUser is not null) {
if(banDuration == TimeSpan.MinValue) {
Context.SendTo(conn.User, new LegacyCommandResponse(LCR.FLOOD_WARN, false));
Context.SendTo(conn.User, new FloodWarningPacket());
} else {
Context.BanUser(conn.User, banDuration, UserDisconnectReason.Flood);