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
throw;
#else
return;
return;
#endif
}
@ -98,36 +98,41 @@ namespace SharpChat.PacketHandlers {
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)
user = new ChatUser(fai);
else {
user.ApplyAuth(fai);
if(user.CurrentChannel != null)
ctx.Chat.SendTo(user.CurrentChannel, new UserUpdatePacket(user));
if(user == null)
user = new ChatUser(fai);
else {
user.ApplyAuth(fai);
if(user.CurrentChannel != null)
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();
}
}

View file

@ -1,7 +1,6 @@
using SharpChat.Misuzu;
using SharpChat.Packet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@ -9,7 +8,6 @@ namespace SharpChat.PacketHandlers {
public class PingHandler : IChatPacketHandler {
private readonly MisuzuClient Misuzu;
private readonly object BumpAccess = new();
private readonly TimeSpan BumpInterval = TimeSpan.FromMinutes(1);
private DateTimeOffset LastBump = DateTimeOffset.MinValue;
@ -30,7 +28,8 @@ namespace SharpChat.PacketHandlers {
ctx.Connection.BumpPing();
ctx.Connection.Send(new PongPacket(ctx.Connection.LastPing));
lock(BumpAccess) {
ctx.Chat.ContextAccess.Wait();
try {
if(LastBump < DateTimeOffset.UtcNow - BumpInterval) {
(string, string)[] bumpList = ctx.Chat.Users
.Where(u => u.Status == ChatUserStatus.Online && ctx.Chat.Connections.Any(c => c.User == u))
@ -44,6 +43,8 @@ namespace SharpChat.PacketHandlers {
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.
if(!long.TryParse(args[1], out long mUserId) || user.UserId != mUserId)
return;
ChatChannel channel = user.CurrentChannel;
if(channel == null
|| !ctx.Chat.IsInChannel(user, channel)
/*|| (user.IsSilenced && !user.Can(ChatUserPermissions.SilenceUser))*/)
return;
ctx.Chat.ContextAccess.Wait();
try {
ChatChannel channel = user.CurrentChannel;
if(user.Status != ChatUserStatus.Online) {
user.Status = ChatUserStatus.Online;
ctx.Chat.SendTo(channel, new UserUpdatePacket(user));
}
if(channel == null
|| !ctx.Chat.IsInChannel(user, channel)
/*|| (user.IsSilenced && !user.Can(ChatUserPermissions.SilenceUser))*/)
return;
int maxMsgLength = MaxMessageLength;
if(messageText.Length > maxMsgLength)
messageText = messageText[..maxMsgLength];
if(user.Status != ChatUserStatus.Online) {
user.Status = ChatUserStatus.Online;
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
Logger.Write($"<{ctx.Connection.Id} {user.Username}> {messageText}");
Logger.Write($"<{ctx.Connection.Id} {user.Username}> {messageText}");
#endif
IChatMessage message = null;
IChatMessage message = null;
if(messageText.StartsWith("/")) {
ChatCommandContext context = new(messageText, ctx.Chat, user, ctx.Connection, channel);
if(messageText.StartsWith("/")) {
ChatCommandContext context = new(messageText, ctx.Chat, user, ctx.Connection, channel);
IChatCommand command = null;
IChatCommand command = null;
foreach(IChatCommand cmd in Commands)
if(cmd.IsMatch(context)) {
command = cmd;
break;
}
foreach(IChatCommand cmd in Commands)
if(cmd.IsMatch(context)) {
command = cmd;
break;
}
if(command != null) {
if(command is ActionCommand actionCommand)
message = actionCommand.ActionDispatch(context);
else {
command.Dispatch(context);
return;
if(command != null) {
if(command is ActionCommand actionCommand)
message = actionCommand.ActionDispatch(context);
else {
command.Dispatch(context);
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))
: AuthedHandlers.FirstOrDefault(h => h.IsMatch(context));
if(handler is not null) {
Context.ContextAccess.Wait();
try {
handler.Handle(context);
} finally {
Context.ContextAccess.Release();
}
}
handler?.Handle(context);
}
private bool IsDisposed;