2023-02-23 21:46:49 +00:00
|
|
|
|
using SharpChat.EventStorage;
|
2022-08-30 15:00:58 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2024-05-10 15:24:43 +00:00
|
|
|
|
namespace SharpChat.Packet {
|
2024-05-10 18:29:48 +00:00
|
|
|
|
// this entire class is disgusting
|
2022-08-30 15:00:58 +00:00
|
|
|
|
public class ContextMessagePacket : ServerPacket {
|
2024-05-10 18:29:48 +00:00
|
|
|
|
private readonly StoredEventInfo Event;
|
|
|
|
|
private readonly bool Notify;
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2023-02-23 21:46:49 +00:00
|
|
|
|
public ContextMessagePacket(StoredEventInfo evt, bool notify = false) {
|
2022-08-30 15:00:58 +00:00
|
|
|
|
Event = evt ?? throw new ArgumentNullException(nameof(evt));
|
|
|
|
|
Notify = notify;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 17:28:52 +00:00
|
|
|
|
private const string V1_CHATBOT = "-1\tChatBot\tinherit\t";
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2024-05-10 15:24:43 +00:00
|
|
|
|
public override string Pack() {
|
2024-05-09 21:31:19 +00:00
|
|
|
|
bool isAction = Event.Flags.HasFlag(StoredEventFlags.Action);
|
|
|
|
|
bool isBroadcast = Event.Flags.HasFlag(StoredEventFlags.Broadcast);
|
|
|
|
|
bool isPrivate = Event.Flags.HasFlag(StoredEventFlags.Private);
|
|
|
|
|
|
2023-02-07 15:01:56 +00:00
|
|
|
|
StringBuilder sb = new();
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2024-05-10 17:28:52 +00:00
|
|
|
|
sb.AppendFormat("7\t1\t{0}\t", Event.Created.ToUnixTimeSeconds());
|
2022-08-30 15:00:58 +00:00
|
|
|
|
|
2023-02-23 21:46:49 +00:00
|
|
|
|
switch(Event.Type) {
|
|
|
|
|
case "msg:add":
|
|
|
|
|
case "SharpChat.Events.ChatMessage":
|
2024-05-09 21:31:19 +00:00
|
|
|
|
if(isBroadcast) {
|
2023-07-23 21:31:13 +00:00
|
|
|
|
sb.Append(V1_CHATBOT);
|
2024-05-09 21:31:19 +00:00
|
|
|
|
} else {
|
2024-05-10 17:28:52 +00:00
|
|
|
|
sb.AppendFormat(
|
|
|
|
|
"{0}\t{1}\t{2}\t{3} {4} {5} {6} {7}",
|
|
|
|
|
Event.Sender.UserId,
|
|
|
|
|
Event.Sender.LegacyNameWithStatus,
|
|
|
|
|
Event.Sender.Colour,
|
|
|
|
|
Event.Sender.Rank,
|
|
|
|
|
Event.Sender.Can(ChatUserPermissions.KickUser) ? 1 : 0,
|
|
|
|
|
Event.Sender.Can(ChatUserPermissions.ViewLogs) ? 1 : 0,
|
|
|
|
|
Event.Sender.Can(ChatUserPermissions.SetOwnNickname) ? 1 : 0,
|
|
|
|
|
Event.Sender.Can(ChatUserPermissions.CreateChannel | ChatUserPermissions.SetChannelPermanent, true) ? 2 : (
|
|
|
|
|
Event.Sender.Can(ChatUserPermissions.CreateChannel) ? 1 : 0
|
|
|
|
|
)
|
|
|
|
|
);
|
2023-07-23 21:31:13 +00:00
|
|
|
|
}
|
2024-05-09 21:31:19 +00:00
|
|
|
|
|
2024-05-10 17:28:52 +00:00
|
|
|
|
sb.Append('\t');
|
|
|
|
|
|
|
|
|
|
if(isBroadcast)
|
|
|
|
|
sb.Append("0\fsay\f");
|
2024-05-09 21:31:19 +00:00
|
|
|
|
|
2024-05-10 17:28:52 +00:00
|
|
|
|
string body = Event.Data.RootElement.GetProperty("text").GetString().Replace("<", "<").Replace(">", ">").Replace("\n", " <br/> ").Replace("\t", " ");
|
2024-05-09 21:31:19 +00:00
|
|
|
|
if(isAction)
|
2024-05-10 17:28:52 +00:00
|
|
|
|
body = string.Format("<i>{0}</i>", body);
|
|
|
|
|
|
|
|
|
|
sb.Append(body);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2023-02-23 21:46:49 +00:00
|
|
|
|
case "user:connect":
|
|
|
|
|
case "SharpChat.Events.UserConnectEvent":
|
2024-05-10 17:28:52 +00:00
|
|
|
|
sb.AppendFormat("{0}\t0\fjoin\f{1}", V1_CHATBOT, Event.Sender.LegacyName);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2023-02-23 21:46:49 +00:00
|
|
|
|
case "chan:join":
|
|
|
|
|
case "SharpChat.Events.UserChannelJoinEvent":
|
2024-05-10 17:28:52 +00:00
|
|
|
|
sb.AppendFormat("{0}\t0\fjchan\f{1}", V1_CHATBOT, Event.Sender.LegacyName);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2023-02-23 21:46:49 +00:00
|
|
|
|
case "chan:leave":
|
|
|
|
|
case "SharpChat.Events.UserChannelLeaveEvent":
|
2024-05-10 17:28:52 +00:00
|
|
|
|
sb.AppendFormat("{0}\t0\flchan\f{1}", V1_CHATBOT, Event.Sender.LegacyName);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2023-02-23 21:46:49 +00:00
|
|
|
|
case "user:disconnect":
|
|
|
|
|
case "SharpChat.Events.UserDisconnectEvent":
|
2024-05-10 17:28:52 +00:00
|
|
|
|
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
|
|
|
|
|
);
|
2022-08-30 15:00:58 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sb.AppendFormat(
|
2024-05-10 17:28:52 +00:00
|
|
|
|
"\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
|
2022-08-30 15:00:58 +00:00
|
|
|
|
);
|
|
|
|
|
|
2024-05-10 15:24:43 +00:00
|
|
|
|
return sb.ToString();
|
2022-08-30 15:00:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|