Removed protocol enums.
This commit is contained in:
parent
513539319f
commit
c9cc5ff23a
27 changed files with 77 additions and 159 deletions
|
@ -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,6 +37,7 @@ namespace SharpChat {
|
|||
}
|
||||
|
||||
public void Update() {
|
||||
lock(BansAccess)
|
||||
Bans.RemoveExpired();
|
||||
CheckPings();
|
||||
}
|
||||
|
@ -44,9 +48,11 @@ namespace SharpChat {
|
|||
|
||||
if (until.HasValue) {
|
||||
user.Send(new ForceDisconnectPacket(ForceDisconnectReason.Banned, until.Value));
|
||||
|
||||
lock(BansAccess) {
|
||||
Bans.Add(user, until.Value);
|
||||
|
||||
if (banIPs) {
|
||||
if(banIPs)
|
||||
foreach(IPAddress ip in user.RemoteAddresses)
|
||||
Bans.Add(ip, until.Value);
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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());
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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());
|
||||
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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());
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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());
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
}
|
|
@ -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,6 +211,7 @@ namespace SharpChat {
|
|||
aUser.Channel?.Send(new UserUpdatePacket(aUser));
|
||||
}
|
||||
|
||||
lock(Context.BansAccess)
|
||||
aBanned = Context.Bans.Check(aUser);
|
||||
|
||||
if(aBanned > DateTimeOffset.Now) {
|
||||
|
@ -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,7 +647,13 @@ 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;
|
||||
}
|
||||
|
@ -700,13 +683,16 @@ 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;
|
||||
}
|
||||
|
||||
lock(Context.BansAccess)
|
||||
Context.Bans.Remove(unbanUser);
|
||||
|
||||
user.Send(new LegacyCommandResponse(LCR.USER_UNBANNED, false, unbanUser));
|
||||
|
@ -723,12 +709,14 @@ namespace SharpChat {
|
|||
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);
|
||||
}
|
||||
|
||||
user.Send(new LegacyCommandResponse(LCR.USER_UNBANNED, false, unbanIP));
|
||||
break;
|
||||
|
@ -739,6 +727,7 @@ namespace SharpChat {
|
|||
break;
|
||||
}
|
||||
|
||||
lock(Context.BansAccess)
|
||||
user.Send(new BanListPacket(Context.Bans.All()));
|
||||
break;
|
||||
case @"silence": // silence a user
|
||||
|
|
Loading…
Reference in a new issue