sharp-chat/SharpChat.Protocol.SockChat/Packets/AuthFailPacket.cs

57 lines
1.6 KiB
C#
Raw Permalink Normal View History

2022-08-30 15:05:29 +00:00
using SharpChat.Bans;
using System;
2022-08-30 15:00:58 +00:00
using System.Text;
2022-08-30 15:05:29 +00:00
namespace SharpChat.Protocol.SockChat.Packets {
2022-08-30 15:00:58 +00:00
public enum AuthFailReason {
AuthInvalid,
MaxSessions,
Banned,
}
public class AuthFailPacket : ServerPacket {
public AuthFailReason Reason { get; private set; }
2022-08-30 15:05:29 +00:00
public IBanRecord BanInfo { get; private set; }
2022-08-30 15:00:58 +00:00
2022-08-30 15:05:29 +00:00
public AuthFailPacket(AuthFailReason reason, IBanRecord banInfo = null) {
2022-08-30 15:00:58 +00:00
Reason = reason;
2022-08-30 15:05:29 +00:00
if(reason == AuthFailReason.Banned)
BanInfo = banInfo ?? throw new ArgumentNullException(nameof(banInfo));
2022-08-30 15:00:58 +00:00
}
2022-08-30 15:05:29 +00:00
protected override string DoPack() {
StringBuilder sb = new();
2022-08-30 15:00:58 +00:00
2022-08-30 15:05:29 +00:00
sb.Append((int)ServerPacketId.UserConnect);
sb.Append(IServerPacket.SEPARATOR);
sb.Append('n');
sb.Append(IServerPacket.SEPARATOR);
2022-08-30 15:00:58 +00:00
switch (Reason) {
case AuthFailReason.AuthInvalid:
default:
sb.Append(@"authfail");
break;
case AuthFailReason.MaxSessions:
sb.Append(@"sockfail");
break;
case AuthFailReason.Banned:
sb.Append(@"joinfail");
break;
}
if (Reason == AuthFailReason.Banned) {
2022-08-30 15:05:29 +00:00
sb.Append(IServerPacket.SEPARATOR);
2022-08-30 15:00:58 +00:00
2022-08-30 15:05:29 +00:00
if (BanInfo.IsPermanent)
2022-08-30 15:00:58 +00:00
sb.Append(@"-1");
else
2022-08-30 15:05:29 +00:00
sb.Append(BanInfo.Expires.ToUnixTimeSeconds());
2022-08-30 15:00:58 +00:00
}
2022-08-30 15:05:29 +00:00
return sb.ToString();
2022-08-30 15:00:58 +00:00
}
}
}