Extracted all log packets into their own ones.
This commit is contained in:
parent
549c80740d
commit
c490dcf128
5 changed files with 158 additions and 91 deletions
|
@ -203,13 +203,38 @@ namespace SharpChat {
|
|||
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 == null ? string.Empty : SockChatUtility.GetUserName(msg.Sender)),
|
||||
"msg:add" => new MessageAddLogPacket(
|
||||
msg.Id,
|
||||
msg.Created,
|
||||
msg.Sender?.UserId ?? -1,
|
||||
msg.Sender == null ? "ChatBot" : SockChatUtility.GetUserName(msg.Sender),
|
||||
msg.Sender?.Colour ?? Colour.None,
|
||||
msg.Sender?.Rank ?? 0,
|
||||
msg.Sender?.Permissions ?? 0,
|
||||
msg.Data.RootElement.GetProperty("text").GetString() ?? string.Empty,
|
||||
msg.Flags.HasFlag(StoredEventFlags.Action),
|
||||
msg.Flags.HasFlag(StoredEventFlags.Private),
|
||||
msg.Flags.HasFlag(StoredEventFlags.Broadcast),
|
||||
false
|
||||
),
|
||||
"user:connect" => new UserConnectLogPacket(
|
||||
msg.Created,
|
||||
msg.Sender == null ? string.Empty : SockChatUtility.GetUserName(msg.Sender)
|
||||
),
|
||||
"user:disconnect" => new UserDisconnectLogPacket(
|
||||
msg.Created,
|
||||
msg.Sender == null ? string.Empty : SockChatUtility.GetUserNameWithStatus(msg.Sender),
|
||||
(UserDisconnectReason)msg.Data.RootElement.GetProperty("reason").GetByte()
|
||||
),
|
||||
_ => new MessagePopulatePacket(msg),
|
||||
"chan:join" => new UserChannelJoinLogPacket(
|
||||
msg.Created,
|
||||
msg.Sender == null ? string.Empty : SockChatUtility.GetUserName(msg.Sender)
|
||||
),
|
||||
"chan:leave" => new UserChannelLeaveLogPacket(
|
||||
msg.Created,
|
||||
msg.Sender == null ? string.Empty : SockChatUtility.GetUserName(msg.Sender)
|
||||
),
|
||||
_ => throw new Exception($"Unsupported backlog type: {msg.Type}"),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
81
SharpChat/Packet/MessageAddLogPacket.cs
Normal file
81
SharpChat/Packet/MessageAddLogPacket.cs
Normal file
|
@ -0,0 +1,81 @@
|
|||
using System;
|
||||
|
||||
namespace SharpChat.Packet {
|
||||
public class MessageAddLogPacket : ServerPacket {
|
||||
private readonly long Created;
|
||||
private readonly long UserId;
|
||||
private readonly string UserName;
|
||||
private readonly Colour UserColour;
|
||||
private readonly int UserRank;
|
||||
private readonly UserPermissions UserPerms;
|
||||
private readonly string Body;
|
||||
private readonly bool IsAction;
|
||||
private readonly bool IsPrivate;
|
||||
private readonly bool IsBroadcast; // this should be MessageBroadcastLogPacket
|
||||
private readonly bool Notify;
|
||||
|
||||
public MessageAddLogPacket(
|
||||
long msgId,
|
||||
DateTimeOffset created,
|
||||
long userId,
|
||||
string userName,
|
||||
Colour userColour,
|
||||
int userRank,
|
||||
UserPermissions userPerms,
|
||||
string body,
|
||||
bool isAction,
|
||||
bool isPrivate,
|
||||
bool isBroadcast,
|
||||
bool notify
|
||||
) : base(msgId) {
|
||||
Created = created.ToUnixTimeSeconds();
|
||||
UserId = userId < 0 ? -1 : userId;
|
||||
UserName = userName;
|
||||
UserColour = userColour;
|
||||
UserRank = userRank;
|
||||
UserPerms = userPerms;
|
||||
Body = body;
|
||||
IsAction = isAction;
|
||||
IsPrivate = isPrivate;
|
||||
IsBroadcast = isBroadcast;
|
||||
Notify = notify;
|
||||
}
|
||||
|
||||
public override string Pack() {
|
||||
string body = SockChatUtility.SanitiseMessageBody(Body);
|
||||
if(IsAction)
|
||||
body = string.Format("<i>{0}</i>", body);
|
||||
|
||||
if(IsBroadcast)
|
||||
body = "0\fsay\f" + body;
|
||||
|
||||
string userPerms = UserId < 0 ? string.Empty : string.Format(
|
||||
"{0} {1} {2} {3} {4}",
|
||||
UserRank,
|
||||
UserPerms.HasFlag(UserPermissions.KickUser) == true ? 1 : 0,
|
||||
UserPerms.HasFlag(UserPermissions.ViewLogs) == true ? 1 : 0,
|
||||
UserPerms.HasFlag(UserPermissions.SetOwnNickname) == true ? 1 : 0,
|
||||
UserPerms.HasFlag(UserPermissions.CreateChannel) == true ? (
|
||||
UserPerms.HasFlag(UserPermissions.SetChannelPermanent) == true ? 2 : 1
|
||||
) : 0
|
||||
);
|
||||
|
||||
return string.Format(
|
||||
"7\t1\t{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}{9}{10}{11}{12}",
|
||||
Created,
|
||||
UserId,
|
||||
UserName,
|
||||
UserColour,
|
||||
userPerms,
|
||||
body,
|
||||
SequenceId,
|
||||
Notify ? 1 : 0,
|
||||
1,
|
||||
IsAction ? 1 : 0,
|
||||
0,
|
||||
IsAction ? 0 : 1,
|
||||
IsPrivate ? 1 : 0
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,89 +0,0 @@
|
|||
using SharpChat.EventStorage;
|
||||
using System.Text;
|
||||
|
||||
namespace SharpChat.Packet {
|
||||
// this entire class is disgusting
|
||||
public class MessagePopulatePacket : ServerPacket {
|
||||
private readonly StoredEventInfo Event;
|
||||
private readonly bool Notify;
|
||||
|
||||
public MessagePopulatePacket(StoredEventInfo evt, bool notify = false) {
|
||||
Event = evt;
|
||||
Notify = notify;
|
||||
}
|
||||
|
||||
private const string V1_CHATBOT = "-1\tChatBot\tinherit\t";
|
||||
|
||||
public override string Pack() {
|
||||
bool isAction = Event.Flags.HasFlag(StoredEventFlags.Action);
|
||||
bool isBroadcast = Event.Flags.HasFlag(StoredEventFlags.Broadcast);
|
||||
bool isPrivate = Event.Flags.HasFlag(StoredEventFlags.Private);
|
||||
|
||||
StringBuilder sb = new();
|
||||
|
||||
sb.AppendFormat("7\t1\t{0}\t", Event.Created.ToUnixTimeSeconds());
|
||||
|
||||
switch(Event.Type) {
|
||||
case "msg:add":
|
||||
if(isBroadcast) {
|
||||
sb.Append(V1_CHATBOT);
|
||||
} else {
|
||||
sb.AppendFormat(
|
||||
"{0}\t{1}\t{2}\t{3} {4} {5} {6} {7}",
|
||||
Event.Sender?.UserId,
|
||||
Event.Sender == null ? string.Empty : SockChatUtility.GetUserNameWithStatus(Event.Sender),
|
||||
Event.Sender?.Colour,
|
||||
Event.Sender?.Rank,
|
||||
Event.Sender?.Permissions.HasFlag(UserPermissions.KickUser) == true ? 1 : 0,
|
||||
Event.Sender?.Permissions.HasFlag(UserPermissions.ViewLogs) == true ? 1 : 0,
|
||||
Event.Sender?.Permissions.HasFlag(UserPermissions.SetOwnNickname) == true ? 1 : 0,
|
||||
Event.Sender?.Permissions.HasFlag(UserPermissions.CreateChannel) == true ? (
|
||||
Event.Sender?.Permissions.HasFlag(UserPermissions.SetChannelPermanent) == true ? 2 : 1
|
||||
) : 0
|
||||
);
|
||||
}
|
||||
|
||||
sb.Append('\t');
|
||||
|
||||
if(isBroadcast)
|
||||
sb.Append("0\fsay\f");
|
||||
|
||||
string body = SockChatUtility.SanitiseMessageBody(Event.Data.RootElement.GetProperty("text").GetString());
|
||||
if(isAction)
|
||||
body = string.Format("<i>{0}</i>", body);
|
||||
|
||||
sb.Append(body);
|
||||
break;
|
||||
|
||||
case "chan:join":
|
||||
sb.AppendFormat(
|
||||
"{0}\t0\fjchan\f{1}",
|
||||
V1_CHATBOT,
|
||||
Event.Sender == null ? string.Empty : SockChatUtility.GetUserName(Event.Sender)
|
||||
);
|
||||
break;
|
||||
|
||||
case "chan:leave":
|
||||
sb.AppendFormat(
|
||||
"{0}\t0\flchan\f{1}",
|
||||
V1_CHATBOT,
|
||||
Event.Sender == null ? string.Empty : SockChatUtility.GetUserName(Event.Sender)
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
sb.AppendFormat(
|
||||
"\t{0}\t{1}\t{2}{3}{4}{5}{6}",
|
||||
Event.Id < 1 ? SequenceId : Event.Id,
|
||||
Notify ? 1 : 0,
|
||||
1,
|
||||
isAction ? 1 : 0,
|
||||
0,
|
||||
isAction ? 0 : 1,
|
||||
isPrivate ? 1 : 0
|
||||
);
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
25
SharpChat/Packet/UserChannelJoinLogPacket.cs
Normal file
25
SharpChat/Packet/UserChannelJoinLogPacket.cs
Normal file
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
|
||||
namespace SharpChat.Packet {
|
||||
public class UserChannelJoinLogPacket : ServerPacket {
|
||||
private readonly long Timestamp;
|
||||
private readonly string UserName;
|
||||
|
||||
public UserChannelJoinLogPacket(
|
||||
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\fjchan\f{1}\t{2}\t0\t10010",
|
||||
Timestamp,
|
||||
UserName,
|
||||
SequenceId
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
25
SharpChat/Packet/UserChannelLeaveLogPacket.cs
Normal file
25
SharpChat/Packet/UserChannelLeaveLogPacket.cs
Normal file
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
|
||||
namespace SharpChat.Packet {
|
||||
public class UserChannelLeaveLogPacket : ServerPacket {
|
||||
private readonly long Timestamp;
|
||||
private readonly string UserName;
|
||||
|
||||
public UserChannelLeaveLogPacket(
|
||||
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\flchan\f{1}\t{2}\t0\t10010",
|
||||
Timestamp,
|
||||
UserName,
|
||||
SequenceId
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue