Moved some things out of the MessagePopulatePacket class.
This commit is contained in:
parent
a6a7e56bd1
commit
322500739e
13 changed files with 102 additions and 50 deletions
|
@ -92,7 +92,7 @@ namespace SharpChat {
|
||||||
|
|
||||||
foreach(ChatUser user in Users)
|
foreach(ChatUser user in Users)
|
||||||
if(!Connections.Any(conn => conn.User == user)) {
|
if(!Connections.Any(conn => conn.User == user)) {
|
||||||
HandleDisconnect(user, UserDisconnectReason.TimeOut);
|
HandleDisconnect(user, ChatUserDisconnectReason.TimeOut);
|
||||||
Logger.Write($"Timed out {user} (no more connections).");
|
Logger.Write($"Timed out {user} (no more connections).");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -205,7 +205,7 @@ namespace SharpChat {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void BanUser(ChatUser user, TimeSpan duration, UserDisconnectReason reason = UserDisconnectReason.Kicked) {
|
public void BanUser(ChatUser user, TimeSpan duration, ChatUserDisconnectReason reason = ChatUserDisconnectReason.Kicked) {
|
||||||
if(duration > TimeSpan.Zero) {
|
if(duration > TimeSpan.Zero) {
|
||||||
DateTimeOffset expires = duration >= TimeSpan.MaxValue ? DateTimeOffset.MaxValue : DateTimeOffset.Now + duration;
|
DateTimeOffset expires = duration >= TimeSpan.MaxValue ? DateTimeOffset.MaxValue : DateTimeOffset.Now + duration;
|
||||||
SendTo(user, new ForceDisconnectPacket(expires));
|
SendTo(user, new ForceDisconnectPacket(expires));
|
||||||
|
@ -220,6 +220,19 @@ namespace SharpChat {
|
||||||
HandleDisconnect(user, reason);
|
HandleDisconnect(user, reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void HandleChannelEventLog(string channelName, Action<IServerPacket> handler) {
|
||||||
|
foreach(StoredEventInfo msg in Events.GetChannelEventLog(channelName))
|
||||||
|
handler(msg.Type switch {
|
||||||
|
"user:connect" => new UserConnectLogPacket(msg.Created, msg.Sender?.LegacyName ?? string.Empty),
|
||||||
|
"user:disconnect" => new UserDisconnectLogPacket(
|
||||||
|
msg.Created,
|
||||||
|
msg.Sender?.LegacyNameWithStatus ?? string.Empty,
|
||||||
|
(ChatUserDisconnectReason)msg.Data.RootElement.GetProperty("reason").GetByte()
|
||||||
|
),
|
||||||
|
_ => new MessagePopulatePacket(msg),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public void HandleJoin(ChatUser user, ChatChannel chan, ChatConnection conn, int maxMsgLength) {
|
public void HandleJoin(ChatUser user, ChatChannel chan, ChatConnection conn, int maxMsgLength) {
|
||||||
if(!IsInChannel(user, chan)) {
|
if(!IsInChannel(user, chan)) {
|
||||||
SendTo(chan, new UserConnectPacket(
|
SendTo(chan, new UserConnectPacket(
|
||||||
|
@ -246,8 +259,7 @@ namespace SharpChat {
|
||||||
user => new UsersPopulatePacket.ListEntry(user.UserId, user.LegacyNameWithStatus, user.Colour, user.Rank, user.Permissions, true)
|
user => new UsersPopulatePacket.ListEntry(user.UserId, user.LegacyNameWithStatus, user.Colour, user.Rank, user.Permissions, true)
|
||||||
).OrderByDescending(user => user.Rank).ToArray()));
|
).OrderByDescending(user => user.Rank).ToArray()));
|
||||||
|
|
||||||
foreach(StoredEventInfo msg in Events.GetChannelEventLog(chan.Name))
|
HandleChannelEventLog(chan.Name, p => conn.Send(p));
|
||||||
conn.Send(new MessagePopulatePacket(msg));
|
|
||||||
|
|
||||||
conn.Send(new ChannelsPopulatePacket(Channels.Where(c => c.Rank <= user.Rank).Select(
|
conn.Send(new ChannelsPopulatePacket(Channels.Where(c => c.Rank <= user.Rank).Select(
|
||||||
channel => new ChannelsPopulatePacket.ListEntry(channel.Name, channel.HasPassword, channel.IsTemporary)
|
channel => new ChannelsPopulatePacket.ListEntry(channel.Name, channel.HasPassword, channel.IsTemporary)
|
||||||
|
@ -259,7 +271,7 @@ namespace SharpChat {
|
||||||
UserLastChannel[user.UserId] = chan;
|
UserLastChannel[user.UserId] = chan;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void HandleDisconnect(ChatUser user, UserDisconnectReason reason = UserDisconnectReason.Leave) {
|
public void HandleDisconnect(ChatUser user, ChatUserDisconnectReason reason = ChatUserDisconnectReason.Leave) {
|
||||||
UpdateUser(user, status: ChatUserStatus.Offline);
|
UpdateUser(user, status: ChatUserStatus.Offline);
|
||||||
Users.Remove(user);
|
Users.Remove(user);
|
||||||
UserLastChannel.Remove(user.UserId);
|
UserLastChannel.Remove(user.UserId);
|
||||||
|
@ -316,9 +328,7 @@ namespace SharpChat {
|
||||||
user => new UsersPopulatePacket.ListEntry(user.UserId, user.LegacyNameWithStatus, user.Colour, user.Rank, user.Permissions, true)
|
user => new UsersPopulatePacket.ListEntry(user.UserId, user.LegacyNameWithStatus, user.Colour, user.Rank, user.Permissions, true)
|
||||||
).OrderByDescending(u => u.Rank).ToArray()));
|
).OrderByDescending(u => u.Rank).ToArray()));
|
||||||
|
|
||||||
foreach(StoredEventInfo msg in Events.GetChannelEventLog(chan.Name))
|
HandleChannelEventLog(chan.Name, p => SendTo(user, p));
|
||||||
SendTo(user, new MessagePopulatePacket(msg));
|
|
||||||
|
|
||||||
ForceChannel(user, chan);
|
ForceChannel(user, chan);
|
||||||
|
|
||||||
ChannelUsers.Remove(new ChannelUserAssoc(user.UserId, oldChan.Name));
|
ChannelUsers.Remove(new ChannelUserAssoc(user.UserId, oldChan.Name));
|
||||||
|
|
8
SharpChat/ChatUserDisconnectReason.cs
Normal file
8
SharpChat/ChatUserDisconnectReason.cs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
namespace SharpChat {
|
||||||
|
public enum ChatUserDisconnectReason {
|
||||||
|
Leave,
|
||||||
|
TimeOut,
|
||||||
|
Kicked,
|
||||||
|
Flood,
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,11 +20,11 @@ namespace SharpChat.Packet {
|
||||||
int maxMsgLength
|
int maxMsgLength
|
||||||
) {
|
) {
|
||||||
UserId = userId;
|
UserId = userId;
|
||||||
UserName = userName ?? throw new ArgumentNullException(nameof(userName));
|
UserName = userName;
|
||||||
UserColour = userColour;
|
UserColour = userColour;
|
||||||
UserRank = userRank;
|
UserRank = userRank;
|
||||||
UserPerms = userPerms;
|
UserPerms = userPerms;
|
||||||
ChannelName = channelName ?? throw new ArgumentNullException(nameof(channelName));
|
ChannelName = channelName;
|
||||||
MaxMessageLength = maxMsgLength;
|
MaxMessageLength = maxMsgLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ namespace SharpChat.Packet {
|
||||||
bool channelHasPassword,
|
bool channelHasPassword,
|
||||||
bool channelIsTemporary
|
bool channelIsTemporary
|
||||||
) {
|
) {
|
||||||
ChannelName = channelName ?? throw new ArgumentNullException(nameof(channelName));
|
ChannelName = channelName;
|
||||||
ChannelHasPassword = channelHasPassword;
|
ChannelHasPassword = channelHasPassword;
|
||||||
ChannelIsTemporary = channelIsTemporary;
|
ChannelIsTemporary = channelIsTemporary;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,8 +13,8 @@ namespace SharpChat.Packet {
|
||||||
bool channelHasPassword,
|
bool channelHasPassword,
|
||||||
bool channelIsTemporary
|
bool channelIsTemporary
|
||||||
) {
|
) {
|
||||||
ChannelNamePrevious = channelNamePrevious ?? throw new ArgumentNullException(nameof(channelNamePrevious));
|
ChannelNamePrevious = channelNamePrevious;
|
||||||
ChannelNameNew = channelNameNew ?? throw new ArgumentNullException(nameof(channelNameNew));
|
ChannelNameNew = channelNameNew;
|
||||||
ChannelHasPassword = channelHasPassword;
|
ChannelHasPassword = channelHasPassword;
|
||||||
ChannelIsTemporary = channelIsTemporary;
|
ChannelIsTemporary = channelIsTemporary;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ namespace SharpChat.Packet {
|
||||||
private readonly ListEntry[] Entries;
|
private readonly ListEntry[] Entries;
|
||||||
|
|
||||||
public ChannelsPopulatePacket(ListEntry[] entries) {
|
public ChannelsPopulatePacket(ListEntry[] entries) {
|
||||||
Entries = entries ?? throw new ArgumentNullException(nameof(entries));
|
Entries = entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Pack() {
|
public override string Pack() {
|
||||||
|
|
|
@ -18,7 +18,7 @@ namespace SharpChat.Packet {
|
||||||
) : base(msgId) {
|
) : base(msgId) {
|
||||||
Created = created.ToUnixTimeSeconds();
|
Created = created.ToUnixTimeSeconds();
|
||||||
UserId = userId < 0 ? -1 : userId;
|
UserId = userId < 0 ? -1 : userId;
|
||||||
Body = body ?? throw new ArgumentNullException(nameof(body));
|
Body = body;
|
||||||
IsAction = isAction;
|
IsAction = isAction;
|
||||||
IsPrivate = isPrivate;
|
IsPrivate = isPrivate;
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,10 +56,6 @@ namespace SharpChat.Packet {
|
||||||
sb.Append(body);
|
sb.Append(body);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "user:connect":
|
|
||||||
sb.AppendFormat("{0}\t0\fjoin\f{1}", V1_CHATBOT, Event.Sender?.LegacyName);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "chan:join":
|
case "chan:join":
|
||||||
sb.AppendFormat("{0}\t0\fjchan\f{1}", V1_CHATBOT, Event.Sender?.LegacyName);
|
sb.AppendFormat("{0}\t0\fjchan\f{1}", V1_CHATBOT, Event.Sender?.LegacyName);
|
||||||
break;
|
break;
|
||||||
|
@ -67,21 +63,6 @@ namespace SharpChat.Packet {
|
||||||
case "chan:leave":
|
case "chan:leave":
|
||||||
sb.AppendFormat("{0}\t0\flchan\f{1}", V1_CHATBOT, Event.Sender?.LegacyName);
|
sb.AppendFormat("{0}\t0\flchan\f{1}", V1_CHATBOT, Event.Sender?.LegacyName);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "user:disconnect":
|
|
||||||
sb.AppendFormat(
|
|
||||||
"{0}\t0\f{1}\f{2}",
|
|
||||||
V1_CHATBOT,
|
|
||||||
(UserDisconnectReason)Event.Data.RootElement.GetProperty("reason").GetByte() switch {
|
|
||||||
UserDisconnectReason.Flood => "flood",
|
|
||||||
UserDisconnectReason.Kicked => "kick",
|
|
||||||
UserDisconnectReason.TimeOut => "timeout",
|
|
||||||
UserDisconnectReason.Leave => "leave",
|
|
||||||
_ => "leave",
|
|
||||||
},
|
|
||||||
Event.Sender?.LegacyName
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.AppendFormat(
|
sb.AppendFormat(
|
||||||
|
|
25
SharpChat/Packet/UserConnectLogPacket.cs
Normal file
25
SharpChat/Packet/UserConnectLogPacket.cs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace SharpChat.Packet {
|
||||||
|
public class UserConnectLogPacket : ServerPacket {
|
||||||
|
private readonly long Timestamp;
|
||||||
|
private readonly string UserName;
|
||||||
|
|
||||||
|
public UserConnectLogPacket(
|
||||||
|
DateTimeOffset timestamp,
|
||||||
|
string userName
|
||||||
|
) {
|
||||||
|
Timestamp = timestamp.ToUnixTimeSeconds();
|
||||||
|
UserName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string Pack() {
|
||||||
|
return string.Format(
|
||||||
|
"7\t1\t{0}\t-1\tChatBot\tinherit\t\t0\fjoin\f{1}\t{2}\t0\t10010",
|
||||||
|
Timestamp,
|
||||||
|
UserName,
|
||||||
|
SequenceId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,7 +19,7 @@ namespace SharpChat.Packet {
|
||||||
) {
|
) {
|
||||||
Joined = joined;
|
Joined = joined;
|
||||||
UserId = userId;
|
UserId = userId;
|
||||||
UserName = userName ?? throw new ArgumentNullException(nameof(userName));
|
UserName = userName;
|
||||||
UserColour = userColour;
|
UserColour = userColour;
|
||||||
UserRank = userRank;
|
UserRank = userRank;
|
||||||
UserPerms = userPerms;
|
UserPerms = userPerms;
|
||||||
|
|
35
SharpChat/Packet/UserDisconnectLogPacket.cs
Normal file
35
SharpChat/Packet/UserDisconnectLogPacket.cs
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace SharpChat.Packet {
|
||||||
|
public class UserDisconnectLogPacket : ServerPacket {
|
||||||
|
private readonly long Timestamp;
|
||||||
|
private readonly string UserName;
|
||||||
|
private readonly ChatUserDisconnectReason Reason;
|
||||||
|
|
||||||
|
public UserDisconnectLogPacket(
|
||||||
|
DateTimeOffset timestamp,
|
||||||
|
string userName,
|
||||||
|
ChatUserDisconnectReason reason
|
||||||
|
) {
|
||||||
|
Timestamp = timestamp.ToUnixTimeSeconds();
|
||||||
|
UserName = userName;
|
||||||
|
Reason = reason;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string Pack() {
|
||||||
|
return string.Format(
|
||||||
|
"7\t1\t{0}\t-1\tChatBot\tinherit\t\t0\f{1}\f{2}\t{3}\t0\t10010",
|
||||||
|
Timestamp,
|
||||||
|
Reason switch {
|
||||||
|
ChatUserDisconnectReason.Leave => "leave",
|
||||||
|
ChatUserDisconnectReason.TimeOut => "timeout",
|
||||||
|
ChatUserDisconnectReason.Kicked => "kick",
|
||||||
|
ChatUserDisconnectReason.Flood => "flood",
|
||||||
|
_ => "leave",
|
||||||
|
},
|
||||||
|
UserName,
|
||||||
|
SequenceId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,28 +1,21 @@
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace SharpChat.Packet {
|
namespace SharpChat.Packet {
|
||||||
public enum UserDisconnectReason {
|
|
||||||
Leave,
|
|
||||||
TimeOut,
|
|
||||||
Kicked,
|
|
||||||
Flood,
|
|
||||||
}
|
|
||||||
|
|
||||||
public class UserDisconnectPacket : ServerPacket {
|
public class UserDisconnectPacket : ServerPacket {
|
||||||
private readonly long Timestamp;
|
private readonly long Timestamp;
|
||||||
private readonly long UserId;
|
private readonly long UserId;
|
||||||
private readonly string UserName;
|
private readonly string UserName;
|
||||||
private readonly UserDisconnectReason Reason;
|
private readonly ChatUserDisconnectReason Reason;
|
||||||
|
|
||||||
public UserDisconnectPacket(
|
public UserDisconnectPacket(
|
||||||
DateTimeOffset timestamp,
|
DateTimeOffset timestamp,
|
||||||
long userId,
|
long userId,
|
||||||
string userName,
|
string userName,
|
||||||
UserDisconnectReason reason
|
ChatUserDisconnectReason reason
|
||||||
) {
|
) {
|
||||||
Timestamp = timestamp.ToUnixTimeSeconds();
|
Timestamp = timestamp.ToUnixTimeSeconds();
|
||||||
UserId = userId;
|
UserId = userId;
|
||||||
UserName = userName ?? throw new ArgumentNullException(nameof(userName));
|
UserName = userName;
|
||||||
Reason = reason;
|
Reason = reason;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,10 +25,10 @@ namespace SharpChat.Packet {
|
||||||
UserId,
|
UserId,
|
||||||
UserName,
|
UserName,
|
||||||
Reason switch {
|
Reason switch {
|
||||||
UserDisconnectReason.Leave => "leave",
|
ChatUserDisconnectReason.Leave => "leave",
|
||||||
UserDisconnectReason.TimeOut => "timeout",
|
ChatUserDisconnectReason.TimeOut => "timeout",
|
||||||
UserDisconnectReason.Kicked => "kick",
|
ChatUserDisconnectReason.Kicked => "kick",
|
||||||
UserDisconnectReason.Flood => "flood",
|
ChatUserDisconnectReason.Flood => "flood",
|
||||||
_ => "leave",
|
_ => "leave",
|
||||||
},
|
},
|
||||||
Timestamp,
|
Timestamp,
|
||||||
|
|
|
@ -186,7 +186,7 @@ namespace SharpChat {
|
||||||
if(banDuration == TimeSpan.MinValue) {
|
if(banDuration == TimeSpan.MinValue) {
|
||||||
Context.SendTo(conn.User, new FloodWarningPacket());
|
Context.SendTo(conn.User, new FloodWarningPacket());
|
||||||
} else {
|
} else {
|
||||||
Context.BanUser(conn.User, banDuration, UserDisconnectReason.Flood);
|
Context.BanUser(conn.User, banDuration, ChatUserDisconnectReason.Flood);
|
||||||
|
|
||||||
if(banDuration > TimeSpan.Zero)
|
if(banDuration > TimeSpan.Zero)
|
||||||
Misuzu.CreateBanAsync(
|
Misuzu.CreateBanAsync(
|
||||||
|
|
Loading…
Add table
Reference in a new issue