Even slightly lesser aggressive question mark

This commit is contained in:
flash 2023-02-19 23:47:53 +01:00
parent 86a46539f2
commit 8de3ba8dbb
4 changed files with 84 additions and 79 deletions

View file

@ -87,7 +87,7 @@ namespace SharpChat.PacketHandlers {
#if DEBUG #if DEBUG
throw; throw;
#else #else
return; return;
#endif #endif
} }
@ -98,36 +98,41 @@ namespace SharpChat.PacketHandlers {
return; return;
} }
ChatUser user = ctx.Chat.Users.FirstOrDefault(u => u.UserId == fai.UserId); await ctx.Chat.ContextAccess.WaitAsync();
try {
ChatUser user = ctx.Chat.Users.FirstOrDefault(u => u.UserId == fai.UserId);
if(user == null) if(user == null)
user = new ChatUser(fai); user = new ChatUser(fai);
else { else {
user.ApplyAuth(fai); user.ApplyAuth(fai);
if(user.CurrentChannel != null) if(user.CurrentChannel != null)
ctx.Chat.SendTo(user.CurrentChannel, new UserUpdatePacket(user)); ctx.Chat.SendTo(user.CurrentChannel, new UserUpdatePacket(user));
}
// Enforce a maximum amount of connections per user
if(ctx.Chat.Connections.Count(conn => conn.User == user) >= MaxConnections) {
ctx.Connection.Send(new AuthFailPacket(AuthFailReason.MaxSessions));
ctx.Connection.Dispose();
return;
}
ctx.Connection.BumpPing();
ctx.Connection.User = user;
ctx.Connection.Send(new LegacyCommandResponse(LCR.WELCOME, false, $"Welcome to Flashii Chat, {user.Username}!"));
if(File.Exists("welcome.txt")) {
IEnumerable<string> lines = File.ReadAllLines("welcome.txt").Where(x => !string.IsNullOrWhiteSpace(x));
string line = lines.ElementAtOrDefault(RNG.Next(lines.Count()));
if(!string.IsNullOrWhiteSpace(line))
ctx.Connection.Send(new LegacyCommandResponse(LCR.WELCOME, false, line));
}
ctx.Chat.HandleJoin(user, DefaultChannel, ctx.Connection, MaxMessageLength);
} finally {
ctx.Chat.ContextAccess.Release();
} }
// Enforce a maximum amount of connections per user
if(ctx.Chat.Connections.Count(conn => conn.User == user) >= MaxConnections) {
ctx.Connection.Send(new AuthFailPacket(AuthFailReason.MaxSessions));
ctx.Connection.Dispose();
return;
}
ctx.Connection.BumpPing();
ctx.Connection.User = user;
ctx.Connection.Send(new LegacyCommandResponse(LCR.WELCOME, false, $"Welcome to Flashii Chat, {user.Username}!"));
if(File.Exists("welcome.txt")) {
IEnumerable<string> lines = File.ReadAllLines("welcome.txt").Where(x => !string.IsNullOrWhiteSpace(x));
string line = lines.ElementAtOrDefault(RNG.Next(lines.Count()));
if(!string.IsNullOrWhiteSpace(line))
ctx.Connection.Send(new LegacyCommandResponse(LCR.WELCOME, false, line));
}
ctx.Chat.HandleJoin(user, DefaultChannel, ctx.Connection, MaxMessageLength);
}).Wait(); }).Wait();
} }
} }

View file

@ -1,7 +1,6 @@
using SharpChat.Misuzu; using SharpChat.Misuzu;
using SharpChat.Packet; using SharpChat.Packet;
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -9,7 +8,6 @@ namespace SharpChat.PacketHandlers {
public class PingHandler : IChatPacketHandler { public class PingHandler : IChatPacketHandler {
private readonly MisuzuClient Misuzu; private readonly MisuzuClient Misuzu;
private readonly object BumpAccess = new();
private readonly TimeSpan BumpInterval = TimeSpan.FromMinutes(1); private readonly TimeSpan BumpInterval = TimeSpan.FromMinutes(1);
private DateTimeOffset LastBump = DateTimeOffset.MinValue; private DateTimeOffset LastBump = DateTimeOffset.MinValue;
@ -30,7 +28,8 @@ namespace SharpChat.PacketHandlers {
ctx.Connection.BumpPing(); ctx.Connection.BumpPing();
ctx.Connection.Send(new PongPacket(ctx.Connection.LastPing)); ctx.Connection.Send(new PongPacket(ctx.Connection.LastPing));
lock(BumpAccess) { ctx.Chat.ContextAccess.Wait();
try {
if(LastBump < DateTimeOffset.UtcNow - BumpInterval) { if(LastBump < DateTimeOffset.UtcNow - BumpInterval) {
(string, string)[] bumpList = ctx.Chat.Users (string, string)[] bumpList = ctx.Chat.Users
.Where(u => u.Status == ChatUserStatus.Online && ctx.Chat.Connections.Any(c => c.User == u)) .Where(u => u.Status == ChatUserStatus.Online && ctx.Chat.Connections.Any(c => c.User == u))
@ -44,6 +43,8 @@ namespace SharpChat.PacketHandlers {
LastBump = DateTimeOffset.UtcNow; LastBump = DateTimeOffset.UtcNow;
} }
} finally {
ctx.Chat.ContextAccess.Release();
} }
} }
} }

View file

@ -42,60 +42,66 @@ namespace SharpChat.PacketHandlers {
// Extra validation step, not necessary at all but enforces proper formatting in SCv1. // Extra validation step, not necessary at all but enforces proper formatting in SCv1.
if(!long.TryParse(args[1], out long mUserId) || user.UserId != mUserId) if(!long.TryParse(args[1], out long mUserId) || user.UserId != mUserId)
return; return;
ChatChannel channel = user.CurrentChannel;
if(channel == null ctx.Chat.ContextAccess.Wait();
|| !ctx.Chat.IsInChannel(user, channel) try {
/*|| (user.IsSilenced && !user.Can(ChatUserPermissions.SilenceUser))*/) ChatChannel channel = user.CurrentChannel;
return;
if(user.Status != ChatUserStatus.Online) { if(channel == null
user.Status = ChatUserStatus.Online; || !ctx.Chat.IsInChannel(user, channel)
ctx.Chat.SendTo(channel, new UserUpdatePacket(user)); /*|| (user.IsSilenced && !user.Can(ChatUserPermissions.SilenceUser))*/)
} return;
int maxMsgLength = MaxMessageLength; if(user.Status != ChatUserStatus.Online) {
if(messageText.Length > maxMsgLength) user.Status = ChatUserStatus.Online;
messageText = messageText[..maxMsgLength]; ctx.Chat.SendTo(channel, new UserUpdatePacket(user));
}
messageText = messageText.Trim(); int maxMsgLength = MaxMessageLength;
if(messageText.Length > maxMsgLength)
messageText = messageText[..maxMsgLength];
messageText = messageText.Trim();
#if DEBUG #if DEBUG
Logger.Write($"<{ctx.Connection.Id} {user.Username}> {messageText}"); Logger.Write($"<{ctx.Connection.Id} {user.Username}> {messageText}");
#endif #endif
IChatMessage message = null; IChatMessage message = null;
if(messageText.StartsWith("/")) { if(messageText.StartsWith("/")) {
ChatCommandContext context = new(messageText, ctx.Chat, user, ctx.Connection, channel); ChatCommandContext context = new(messageText, ctx.Chat, user, ctx.Connection, channel);
IChatCommand command = null; IChatCommand command = null;
foreach(IChatCommand cmd in Commands) foreach(IChatCommand cmd in Commands)
if(cmd.IsMatch(context)) { if(cmd.IsMatch(context)) {
command = cmd; command = cmd;
break; break;
} }
if(command != null) { if(command != null) {
if(command is ActionCommand actionCommand) if(command is ActionCommand actionCommand)
message = actionCommand.ActionDispatch(context); message = actionCommand.ActionDispatch(context);
else { else {
command.Dispatch(context); command.Dispatch(context);
return; return;
}
} }
} }
message ??= new ChatMessage {
ChannelName = channel.Name,
DateTime = DateTimeOffset.UtcNow,
Sender = user,
Text = messageText,
};
ctx.Chat.Events.AddEvent(message);
ctx.Chat.SendTo(channel, new ChatMessageAddPacket(message));
} finally {
ctx.Chat.ContextAccess.Release();
} }
message ??= new ChatMessage {
ChannelName = channel.Name,
DateTime = DateTimeOffset.UtcNow,
Sender = user,
Text = messageText,
};
ctx.Chat.Events.AddEvent(message);
ctx.Chat.SendTo(channel, new ChatMessageAddPacket(message));
} }
} }
} }

View file

@ -203,14 +203,7 @@ namespace SharpChat {
? GuestHandlers.FirstOrDefault(h => h.IsMatch(context)) ? GuestHandlers.FirstOrDefault(h => h.IsMatch(context))
: AuthedHandlers.FirstOrDefault(h => h.IsMatch(context)); : AuthedHandlers.FirstOrDefault(h => h.IsMatch(context));
if(handler is not null) { handler?.Handle(context);
Context.ContextAccess.Wait();
try {
handler.Handle(context);
} finally {
Context.ContextAccess.Release();
}
}
} }
private bool IsDisposed; private bool IsDisposed;