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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -7,7 +7,7 @@
<ItemGroup>
<PackageReference Include="Fleck" Version="1.2.0" />
<PackageReference Include="MySqlConnector" Version="1.3.11" />
<PackageReference Include="MySqlConnector" Version="2.2.5" />
</ItemGroup>
</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');
if(args.Length < 1 || !Enum.TryParse(args[0], out SockChatClientPacket opCode))
if(args.Length < 1)
return;
switch(opCode) {
case SockChatClientPacket.Ping:
switch(args[0]) {
case "0":
if(!int.TryParse(args[1], out int pTime))
break;
@ -164,11 +164,13 @@ namespace SharpChat {
sess.Send(new PongPacket(sess.LastPing));
break;
case SockChatClientPacket.Authenticate:
case "1":
if(sess.User != null)
break;
DateTimeOffset aBanned = Context.Bans.Check(sess.RemoteAddress);
DateTimeOffset aBanned;
lock(Context.BansAccess)
aBanned = Context.Bans.Check(sess.RemoteAddress);
if(aBanned > DateTimeOffset.UtcNow) {
sess.Send(new AuthFailPacket(AuthFailReason.Banned, aBanned));
@ -209,7 +211,8 @@ namespace SharpChat {
aUser.Channel?.Send(new UserUpdatePacket(aUser));
}
aBanned = Context.Bans.Check(aUser);
lock(Context.BansAccess)
aBanned = Context.Bans.Check(aUser);
if(aBanned > DateTimeOffset.Now) {
sess.Send(new AuthFailPacket(AuthFailReason.Banned, aBanned));
@ -243,7 +246,7 @@ namespace SharpChat {
});
break;
case SockChatClientPacket.MessageSend:
case "2":
if(args.Length < 3)
break;
@ -301,32 +304,6 @@ namespace SharpChat {
Context.Events.Add(message);
mChannel.Send(new ChatMessageAddPacket(message));
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;
}
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));
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;
if(parts.Length > 2) {
@ -700,14 +683,17 @@ namespace SharpChat {
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) {
user.Send(new LegacyCommandResponse(LCR.USER_NOT_BANNED, true, unbanUser?.Username ?? parts[1]));
break;
}
Context.Bans.Remove(unbanUser);
lock(Context.BansAccess)
Context.Bans.Remove(unbanUser);
user.Send(new LegacyCommandResponse(LCR.USER_UNBANNED, false, unbanUser));
break;
@ -723,12 +709,14 @@ namespace SharpChat {
break;
}
if(Context.Bans.Check(unbanIP) <= DateTimeOffset.Now) {
user.Send(new LegacyCommandResponse(LCR.USER_NOT_BANNED, true, unbanIP));
break;
}
lock(Context.BansAccess) {
if(Context.Bans.Check(unbanIP) <= DateTimeOffset.Now) {
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));
break;
@ -739,7 +727,8 @@ namespace SharpChat {
break;
}
user.Send(new BanListPacket(Context.Bans.All()));
lock(Context.BansAccess)
user.Send(new BanListPacket(Context.Bans.All()));
break;
case @"silence": // silence a user
if(!user.Can(ChatUserPermissions.SilenceUser)) {