sharp-chat/SharpChat/Packet/ForceDisconnectPacket.cs

42 lines
1.2 KiB
C#

using System;
using System.Text;
namespace SharpChat.Packet {
public enum ForceDisconnectReason {
Kicked = 0,
Banned = 1,
}
public class ForceDisconnectPacket : ServerPacket {
public ForceDisconnectReason Reason { get; private set; }
public DateTimeOffset Expires { get; private set; }
public ForceDisconnectPacket(ForceDisconnectReason reason, DateTimeOffset? expires = null) {
Reason = reason;
if(reason == ForceDisconnectReason.Banned) {
if(!expires.HasValue)
throw new ArgumentNullException(nameof(expires));
Expires = expires.Value;
}
}
public override string Pack() {
StringBuilder sb = new();
sb.Append('9');
sb.Append('\t');
sb.Append((int)Reason);
if(Reason == ForceDisconnectReason.Banned) {
sb.Append('\t');
if(Expires.Year >= 2100)
sb.Append("-1");
else
sb.Append(Expires.ToUnixTimeSeconds());
}
return sb.ToString();
}
}
}