Use server start timestamp for welcome MOTD message and MOTD file last write for the other one.

This commit is contained in:
flash 2024-05-20 02:27:41 +00:00
parent 1d781bd72c
commit fa8c416b77
4 changed files with 25 additions and 7 deletions

View file

@ -1,8 +1,10 @@
namespace SharpChat.Packet {
using System;
namespace SharpChat.Packet {
public class MOTDPacket : SockChatTimedS2CPacket {
private readonly string Body;
public MOTDPacket(string body) {
public MOTDPacket(DateTimeOffset timeStamp, string body) : base(timeStamp) {
Body = body;
}

View file

@ -9,17 +9,22 @@ using System.Threading.Tasks;
namespace SharpChat.PacketHandlers {
public class AuthHandler : IPacketHandler {
public const string MOTD_FILE = @"welcome.txt";
private readonly DateTimeOffset Started;
private readonly MisuzuClient Misuzu;
private readonly ChannelInfo DefaultChannel;
private readonly CachedValue<int> MaxMessageLength;
private readonly CachedValue<int> MaxConnections;
public AuthHandler(
DateTimeOffset started,
MisuzuClient msz,
ChannelInfo? defaultChannel,
CachedValue<int> maxMsgLength,
CachedValue<int> maxConns
) {
Started = started;
Misuzu = msz;
DefaultChannel = defaultChannel ?? throw new ArgumentNullException(nameof(defaultChannel));
MaxMessageLength = maxMsgLength;
@ -144,14 +149,14 @@ namespace SharpChat.PacketHandlers {
ctx.Connection.BumpPing();
ctx.Connection.User = user;
ctx.Connection.Send(new MOTDPacket($"Welcome to Flashii Chat, {user.UserName}!"));
ctx.Connection.Send(new MOTDPacket(Started, $"Welcome to Flashii Chat, {user.UserName}!"));
if(File.Exists("welcome.txt")) {
IEnumerable<string> lines = File.ReadAllLines("welcome.txt").Where(x => !string.IsNullOrWhiteSpace(x));
if(File.Exists(MOTD_FILE)) {
IEnumerable<string> lines = File.ReadAllLines(MOTD_FILE).Where(x => !string.IsNullOrWhiteSpace(x));
string? line = lines.ElementAtOrDefault(RNG.Next(lines.Count()));
if(!string.IsNullOrWhiteSpace(line))
ctx.Connection.Send(new MOTDPacket(line));
ctx.Connection.Send(new MOTDPacket(File.GetLastWriteTimeUtc(MOTD_FILE), line));
}
ctx.Chat.HandleJoin(user, DefaultChannel, ctx.Connection, MaxMessageLength);

View file

@ -39,6 +39,7 @@ namespace SharpChat {
public SockChatServer(HttpClient httpClient, MisuzuClient msz, IEventStorage evtStore, IConfig config) {
Logger.Write("Initialising Sock Chat server...");
DateTimeOffset started = DateTimeOffset.UtcNow;
HttpClient = httpClient;
Misuzu = msz;
@ -71,7 +72,13 @@ namespace SharpChat {
if(Context.Channels.PublicCount < 1)
Context.Channels.Add(new ChannelInfo("Default"));
GuestHandlers.Add(new AuthHandler(Misuzu, Context.Channels.MainChannel, MaxMessageLength, MaxConnections));
GuestHandlers.Add(new AuthHandler(
started,
Misuzu,
Context.Channels.MainChannel,
MaxMessageLength,
MaxConnections
));
AuthedHandlers.AddRange(new IPacketHandler[] {
new PingHandler(Misuzu),

View file

@ -8,6 +8,10 @@ namespace SharpChat {
TimeStamp = DateTimeOffset.UtcNow;
}
public SockChatTimedS2CPacket(DateTimeOffset timeStamp) : base() {
TimeStamp = timeStamp;
}
public SockChatTimedS2CPacket(long messageId, DateTimeOffset timeStamp) : base(messageId) {
TimeStamp = timeStamp;
}