sharp-chat/SharpChat/Packet/AuthFailPacket.cs

58 lines
1.6 KiB
C#
Raw Normal View History

2022-08-30 15:00:58 +00:00
using System;
using System.Collections.Generic;
using System.Text;
namespace SharpChat.Packet {
public enum AuthFailReason {
AuthInvalid,
MaxSessions,
Banned,
}
public class AuthFailPacket : ServerPacket {
public AuthFailReason Reason { get; private set; }
public DateTimeOffset Expires { get; private set; }
public AuthFailPacket(AuthFailReason reason, DateTimeOffset? expires = null) {
Reason = reason;
if (reason == AuthFailReason.Banned) {
if (!expires.HasValue)
throw new ArgumentNullException(nameof(expires));
Expires = expires.Value;
}
}
public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.UserConnect);
sb.Append("\tn\t");
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) {
sb.Append('\t');
if (Expires == DateTimeOffset.MaxValue)
sb.Append(@"-1");
else
sb.Append(Expires.ToUnixTimeSeconds());
}
yield return sb.ToString();
}
}
}