Removed protocol enums.

This commit is contained in:
flash 2023-02-07 15:34:31 +01:00
parent 513539319f
commit c9cc5ff23a
27 changed files with 77 additions and 159 deletions

View file

@ -15,7 +15,10 @@ namespace SharpChat {
public SockChatServer Server { get; } public SockChatServer Server { get; }
public Timer BumpTimer { get; } public Timer BumpTimer { get; }
public BanManager Bans { get; } public BanManager Bans { get; }
public readonly object BansAccess = new();
public ChannelManager Channels { get; } public ChannelManager Channels { get; }
public UserManager Users { get; } public UserManager Users { get; }
public ChatEventManager Events { get; } public ChatEventManager Events { get; }
@ -34,7 +37,8 @@ namespace SharpChat {
} }
public void Update() { public void Update() {
Bans.RemoveExpired(); lock(BansAccess)
Bans.RemoveExpired();
CheckPings(); CheckPings();
} }
@ -44,11 +48,13 @@ namespace SharpChat {
if (until.HasValue) { if (until.HasValue) {
user.Send(new ForceDisconnectPacket(ForceDisconnectReason.Banned, until.Value)); user.Send(new ForceDisconnectPacket(ForceDisconnectReason.Banned, until.Value));
Bans.Add(user, until.Value);
if (banIPs) { lock(BansAccess) {
foreach (IPAddress ip in user.RemoteAddresses) Bans.Add(user, until.Value);
Bans.Add(ip, until.Value);
if(banIPs)
foreach(IPAddress ip in user.RemoteAddresses)
Bans.Add(ip, until.Value);
} }
} else } else
user.Send(new ForceDisconnectPacket(ForceDisconnectReason.Kicked)); user.Send(new ForceDisconnectPacket(ForceDisconnectReason.Kicked));

View file

@ -26,7 +26,7 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.UserConnect); sb.Append('1');
sb.Append("\tn\t"); sb.Append("\tn\t");
switch (Reason) { switch (Reason) {

View file

@ -17,7 +17,7 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.UserConnect); sb.Append('1');
sb.Append("\ty\t"); sb.Append("\ty\t");
sb.Append(User.Pack()); sb.Append(User.Pack());
sb.Append('\t'); sb.Append('\t');

View file

@ -14,7 +14,7 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.MessageAdd); sb.Append('2');
sb.Append('\t'); sb.Append('\t');
sb.Append(DateTimeOffset.Now.ToUnixTimeSeconds()); sb.Append(DateTimeOffset.Now.ToUnixTimeSeconds());
sb.Append("\t-1\t0\fbanlist\f"); sb.Append("\t-1\t0\fbanlist\f");

View file

@ -12,9 +12,9 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.ChannelEvent); sb.Append('4');
sb.Append('\t'); sb.Append('\t');
sb.Append((int)SockChatServerChannelPacket.Create); sb.Append('0');
sb.Append('\t'); sb.Append('\t');
sb.Append(Channel.Pack()); sb.Append(Channel.Pack());

View file

@ -13,9 +13,9 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.ChannelEvent); sb.Append('4');
sb.Append('\t'); sb.Append('\t');
sb.Append((int)SockChatServerChannelPacket.Delete); sb.Append('2');
sb.Append('\t'); sb.Append('\t');
sb.Append(Channel.Name); sb.Append(Channel.Name);

View file

@ -14,9 +14,9 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.ChannelEvent); sb.Append('4');
sb.Append('\t'); sb.Append('\t');
sb.Append((int)SockChatServerChannelPacket.Update); sb.Append('1');
sb.Append('\t'); sb.Append('\t');
sb.Append(PreviousName); sb.Append(PreviousName);
sb.Append('\t'); sb.Append('\t');

View file

@ -17,7 +17,7 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.MessageAdd); sb.Append('2');
sb.Append('\t'); sb.Append('\t');
sb.Append(Message.DateTime.ToUnixTimeSeconds()); sb.Append(Message.DateTime.ToUnixTimeSeconds());

View file

@ -12,7 +12,7 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.MessageDelete); sb.Append('6');
sb.Append('\t'); sb.Append('\t');
sb.Append(EventId); sb.Append(EventId);

View file

@ -14,9 +14,9 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.ContextPopulate); sb.Append('7');
sb.Append('\t'); sb.Append('\t');
sb.Append((int)SockChatServerContextPacket.Channels); sb.Append('2');
sb.Append('\t'); sb.Append('\t');
sb.Append(Channels.Count()); sb.Append(Channels.Count());

View file

@ -25,7 +25,7 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.ContextClear); sb.Append('8');
sb.Append('\t'); sb.Append('\t');
sb.Append((int)Mode); sb.Append((int)Mode);

View file

@ -18,9 +18,9 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.ContextPopulate); sb.Append('7');
sb.Append('\t'); sb.Append('\t');
sb.Append((int)SockChatServerContextPacket.Message); sb.Append('1');
sb.Append('\t'); sb.Append('\t');
sb.Append(Event.DateTime.ToUnixTimeSeconds()); sb.Append(Event.DateTime.ToUnixTimeSeconds());
sb.Append('\t'); sb.Append('\t');

View file

@ -14,9 +14,9 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.ContextPopulate); sb.Append('7');
sb.Append('\t'); sb.Append('\t');
sb.Append((int)SockChatServerContextPacket.Users); sb.Append('0');
sb.Append('\t'); sb.Append('\t');
sb.Append(Users.Count()); sb.Append(Users.Count());

View file

@ -7,7 +7,7 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.MessageAdd); sb.Append('2');
sb.Append('\t'); sb.Append('\t');
sb.Append(DateTimeOffset.Now.ToUnixTimeSeconds()); sb.Append(DateTimeOffset.Now.ToUnixTimeSeconds());
sb.Append("\t-1\t0\fflwarn\t"); sb.Append("\t-1\t0\fflwarn\t");

View file

@ -25,7 +25,7 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.BAKA); sb.Append('9');
sb.Append('\t'); sb.Append('\t');
sb.Append((int)Reason); sb.Append((int)Reason);

View file

@ -23,14 +23,14 @@ namespace SharpChat.Packet {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (StringId == LCR.WELCOME) { if (StringId == LCR.WELCOME) {
sb.Append((int)SockChatServerPacket.ContextPopulate); sb.Append('7');
sb.Append('\t'); sb.Append('\t');
sb.Append((int)SockChatServerContextPacket.Message); sb.Append('1');
sb.Append('\t'); sb.Append('\t');
sb.Append(DateTimeOffset.Now.ToUnixTimeSeconds()); sb.Append(DateTimeOffset.Now.ToUnixTimeSeconds());
sb.Append("\t-1\tChatBot\tinherit\t\t"); sb.Append("\t-1\tChatBot\tinherit\t\t");
} else { } else {
sb.Append((int)SockChatServerPacket.MessageAdd); sb.Append('2');
sb.Append('\t'); sb.Append('\t');
sb.Append(DateTimeOffset.Now.ToUnixTimeSeconds()); sb.Append(DateTimeOffset.Now.ToUnixTimeSeconds());
sb.Append("\t-1\t"); sb.Append("\t-1\t");

View file

@ -13,7 +13,7 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.Pong); sb.Append('0');
sb.Append('\t'); sb.Append('\t');
sb.Append(PongTime.ToUnixTimeSeconds()); sb.Append(PongTime.ToUnixTimeSeconds());

View file

@ -1,29 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace SharpChat.Packet {
public class TypingPacket : ServerPacket {
public ChatChannel Channel { get; }
public ChatChannelTyping TypingInfo { get; }
public TypingPacket(ChatChannel channel, ChatChannelTyping typingInfo) {
Channel = channel;
TypingInfo = typingInfo ?? throw new ArgumentNullException(nameof(typingInfo));
}
public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.Typing);
sb.Append('\t');
sb.Append(Channel?.TargetName ?? string.Empty);
sb.Append('\t');
sb.Append(TypingInfo.User.UserId);
sb.Append('\t');
sb.Append(TypingInfo.Started.ToUnixTimeSeconds());
yield return sb.ToString();
}
}
}

View file

@ -13,9 +13,9 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.UserSwitch); sb.Append('5');
sb.Append('\t'); sb.Append('\t');
sb.Append((int)SockChatServerMovePacket.ForcedMove); sb.Append('2');
sb.Append('\t'); sb.Append('\t');
sb.Append(Channel.Name); sb.Append(Channel.Name);

View file

@ -13,9 +13,9 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.UserSwitch); sb.Append('5');
sb.Append('\t'); sb.Append('\t');
sb.Append((int)SockChatServerMovePacket.UserJoined); sb.Append('0');
sb.Append('\t'); sb.Append('\t');
sb.Append(User.Pack()); sb.Append(User.Pack());
sb.Append('\t'); sb.Append('\t');

View file

@ -13,9 +13,9 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.UserSwitch); sb.Append('5');
sb.Append('\t'); sb.Append('\t');
sb.Append((int)SockChatServerMovePacket.UserLeft); sb.Append('1');
sb.Append('\t'); sb.Append('\t');
sb.Append(User.UserId); sb.Append(User.UserId);
sb.Append('\t'); sb.Append('\t');

View file

@ -15,7 +15,7 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.UserConnect); sb.Append('1');
sb.Append('\t'); sb.Append('\t');
sb.Append(Joined.ToUnixTimeSeconds()); sb.Append(Joined.ToUnixTimeSeconds());
sb.Append('\t'); sb.Append('\t');

View file

@ -24,7 +24,7 @@ namespace SharpChat.Packet {
public override IEnumerable<string> Pack() { public override IEnumerable<string> Pack() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.Append((int)SockChatServerPacket.UserDisconnect); sb.Append('3');
sb.Append('\t'); sb.Append('\t');
sb.Append(User.UserId); sb.Append(User.UserId);
sb.Append('\t'); sb.Append('\t');

View file

@ -18,7 +18,7 @@ namespace SharpChat.Packet {
bool isSilent = string.IsNullOrEmpty(PreviousName); bool isSilent = string.IsNullOrEmpty(PreviousName);
if (!isSilent) { if (!isSilent) {
sb.Append((int)SockChatServerPacket.MessageAdd); sb.Append('2');
sb.Append('\t'); sb.Append('\t');
sb.Append(DateTimeOffset.Now.ToUnixTimeSeconds()); sb.Append(DateTimeOffset.Now.ToUnixTimeSeconds());
sb.Append("\t-1\t0\fnick\f"); sb.Append("\t-1\t0\fnick\f");
@ -32,7 +32,7 @@ namespace SharpChat.Packet {
sb.Clear(); sb.Clear();
} }
sb.Append((int)SockChatServerPacket.UserUpdate); sb.Append("10");
sb.Append('\t'); sb.Append('\t');
sb.Append(User.Pack()); sb.Append(User.Pack());

View file

@ -7,7 +7,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Fleck" Version="1.2.0" /> <PackageReference Include="Fleck" Version="1.2.0" />
<PackageReference Include="MySqlConnector" Version="1.3.11" /> <PackageReference Include="MySqlConnector" Version="2.2.5" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -1,48 +0,0 @@
namespace SharpChat {
public enum SockChatClientPacket {
// Version 1
Ping = 0,
Authenticate = 1,
MessageSend = 2,
// Version 2
FocusChannel = 3,
Typing = 4,
}
public enum SockChatServerPacket {
// Version 1
Pong = 0,
UserConnect = 1,
MessageAdd = 2,
UserDisconnect = 3,
ChannelEvent = 4,
UserSwitch = 5,
MessageDelete = 6,
ContextPopulate = 7,
ContextClear = 8,
BAKA = 9,
UserUpdate = 10,
// Version 2
Typing = 11,
}
public enum SockChatServerChannelPacket {
Create = 0,
Update = 1,
Delete = 2,
}
public enum SockChatServerMovePacket {
UserJoined = 0,
UserLeft = 1,
ForcedMove = 2,
}
public enum SockChatServerContextPacket {
Users = 0,
Message = 1,
Channels = 2,
}
}

View file

@ -152,11 +152,11 @@ namespace SharpChat {
string[] args = msg.Split('\t'); string[] args = msg.Split('\t');
if(args.Length < 1 || !Enum.TryParse(args[0], out SockChatClientPacket opCode)) if(args.Length < 1)
return; return;
switch(opCode) { switch(args[0]) {
case SockChatClientPacket.Ping: case "0":
if(!int.TryParse(args[1], out int pTime)) if(!int.TryParse(args[1], out int pTime))
break; break;
@ -164,11 +164,13 @@ namespace SharpChat {
sess.Send(new PongPacket(sess.LastPing)); sess.Send(new PongPacket(sess.LastPing));
break; break;
case SockChatClientPacket.Authenticate: case "1":
if(sess.User != null) if(sess.User != null)
break; break;
DateTimeOffset aBanned = Context.Bans.Check(sess.RemoteAddress); DateTimeOffset aBanned;
lock(Context.BansAccess)
aBanned = Context.Bans.Check(sess.RemoteAddress);
if(aBanned > DateTimeOffset.UtcNow) { if(aBanned > DateTimeOffset.UtcNow) {
sess.Send(new AuthFailPacket(AuthFailReason.Banned, aBanned)); sess.Send(new AuthFailPacket(AuthFailReason.Banned, aBanned));
@ -209,7 +211,8 @@ namespace SharpChat {
aUser.Channel?.Send(new UserUpdatePacket(aUser)); aUser.Channel?.Send(new UserUpdatePacket(aUser));
} }
aBanned = Context.Bans.Check(aUser); lock(Context.BansAccess)
aBanned = Context.Bans.Check(aUser);
if(aBanned > DateTimeOffset.Now) { if(aBanned > DateTimeOffset.Now) {
sess.Send(new AuthFailPacket(AuthFailReason.Banned, aBanned)); sess.Send(new AuthFailPacket(AuthFailReason.Banned, aBanned));
@ -243,7 +246,7 @@ namespace SharpChat {
}); });
break; break;
case SockChatClientPacket.MessageSend: case "2":
if(args.Length < 3) if(args.Length < 3)
break; break;
@ -301,32 +304,6 @@ namespace SharpChat {
Context.Events.Add(message); Context.Events.Add(message);
mChannel.Send(new ChatMessageAddPacket(message)); mChannel.Send(new ChatMessageAddPacket(message));
break; break;
case SockChatClientPacket.FocusChannel:
if(sess.User == null || args.Length < 2)
break;
ChatChannel fChannel = Context.Channels.Get(args[1]);
if(fChannel == null || sess.User.CurrentChannel == fChannel)
break;
sess.User.FocusChannel(fChannel);
break;
case SockChatClientPacket.Typing:
if(!ENABLE_TYPING_EVENT || sess.User == null)
break;
ChatChannel tChannel = sess.User.CurrentChannel;
if(tChannel == null || !tChannel.CanType(sess.User))
break;
ChatChannelTyping tInfo = tChannel.RegisterTyping(sess.User);
if(tInfo == null)
return;
tChannel.Send(new TypingPacket(tChannel, tInfo));
break;
} }
} }
@ -670,11 +647,17 @@ namespace SharpChat {
break; break;
} }
if(banUser == user || banUser.Rank >= user.Rank || Context.Bans.Check(banUser) > DateTimeOffset.Now) { if(banUser == user || banUser.Rank >= user.Rank) {
user.Send(new LegacyCommandResponse(LCR.KICK_NOT_ALLOWED, true, banUser.DisplayName)); user.Send(new LegacyCommandResponse(LCR.KICK_NOT_ALLOWED, true, banUser.DisplayName));
break; break;
} }
lock(Context.BansAccess)
if(Context.Bans.Check(banUser) > DateTimeOffset.Now) {
user.Send(new LegacyCommandResponse(LCR.KICK_NOT_ALLOWED, true, banUser.DisplayName));
break;
}
DateTimeOffset? banUntil = isBanning ? (DateTimeOffset?)DateTimeOffset.MaxValue : null; DateTimeOffset? banUntil = isBanning ? (DateTimeOffset?)DateTimeOffset.MaxValue : null;
if(parts.Length > 2) { if(parts.Length > 2) {
@ -700,14 +683,17 @@ namespace SharpChat {
break; break;
} }
BannedUser unbanUser = Context.Bans.GetUser(parts[1]); BannedUser unbanUser;
lock(Context.BansAccess)
unbanUser = Context.Bans.GetUser(parts[1]);
if(unbanUser == null || unbanUser.Expires <= DateTimeOffset.Now) { if(unbanUser == null || unbanUser.Expires <= DateTimeOffset.Now) {
user.Send(new LegacyCommandResponse(LCR.USER_NOT_BANNED, true, unbanUser?.Username ?? parts[1])); user.Send(new LegacyCommandResponse(LCR.USER_NOT_BANNED, true, unbanUser?.Username ?? parts[1]));
break; break;
} }
Context.Bans.Remove(unbanUser); lock(Context.BansAccess)
Context.Bans.Remove(unbanUser);
user.Send(new LegacyCommandResponse(LCR.USER_UNBANNED, false, unbanUser)); user.Send(new LegacyCommandResponse(LCR.USER_UNBANNED, false, unbanUser));
break; break;
@ -723,12 +709,14 @@ namespace SharpChat {
break; break;
} }
if(Context.Bans.Check(unbanIP) <= DateTimeOffset.Now) { lock(Context.BansAccess) {
user.Send(new LegacyCommandResponse(LCR.USER_NOT_BANNED, true, unbanIP)); if(Context.Bans.Check(unbanIP) <= DateTimeOffset.Now) {
break; user.Send(new LegacyCommandResponse(LCR.USER_NOT_BANNED, true, unbanIP));
} break;
}
Context.Bans.Remove(unbanIP); Context.Bans.Remove(unbanIP);
}
user.Send(new LegacyCommandResponse(LCR.USER_UNBANNED, false, unbanIP)); user.Send(new LegacyCommandResponse(LCR.USER_UNBANNED, false, unbanIP));
break; break;
@ -739,7 +727,8 @@ namespace SharpChat {
break; break;
} }
user.Send(new BanListPacket(Context.Bans.All())); lock(Context.BansAccess)
user.Send(new BanListPacket(Context.Bans.All()));
break; break;
case @"silence": // silence a user case @"silence": // silence a user
if(!user.Can(ChatUserPermissions.SilenceUser)) { if(!user.Can(ChatUserPermissions.SilenceUser)) {