about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2024-01-11 07:31:09 +0100
committerEmma [it/its]@Rory& <root@rory.gay>2024-01-11 07:31:09 +0100
commit8dadf547033d71480fd7756809992c0f32549f59 (patch)
treea23631cc840047e3ad9e32dda9c043511af1a583
parentTry-create factory for RemoteHomeserver, more reliable RemoteHomeserver creation (diff)
downloadLibMatrix-8dadf547033d71480fd7756809992c0f32549f59.tar.xz
Cleanup, more message formatters, messagebuilder start
m---------ArcaneLibs0
-rw-r--r--ExampleBots/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs6
-rw-r--r--ExampleBots/ModerationBot/Commands/DbgAniRainbowTest.cs48
-rw-r--r--ExampleBots/ModerationBot/Commands/JoinSpaceMembersCommand.cs6
-rw-r--r--ExampleBots/ModerationBot/Commands/ReloadPoliciesCommand.cs37
-rw-r--r--ExampleBots/ModerationBot/ModerationBot.cs32
-rw-r--r--ExampleBots/ModerationBot/PolicyEngine.cs6
-rw-r--r--LibMatrix.EventTypes/LibMatrix.EventTypes.csproj13
-rw-r--r--LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs54
-rw-r--r--LibMatrix/Extensions/HttpClientExtensions.cs4
-rw-r--r--LibMatrix/Helpers/MessageBuilder.cs40
-rw-r--r--LibMatrix/Helpers/MessageFormatter.cs24
-rw-r--r--LibMatrix/LibMatrix.csproj1
-rw-r--r--LibMatrix/StateEvent.cs38
14 files changed, 280 insertions, 29 deletions
diff --git a/ArcaneLibs b/ArcaneLibs
-Subproject 28c0e5a7c7fdf306901ad20bb324efa697db222
+Subproject f69bb51f21002870b0f2dd1c135209cabc83549
diff --git a/ExampleBots/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs b/ExampleBots/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs
index f578f53..cd0bf6b 100644
--- a/ExampleBots/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs
+++ b/ExampleBots/ModerationBot/Commands/DbgAllRoomsArePolicyListsCommand.cs
@@ -14,9 +14,9 @@ public class DbgAllRoomsArePolicyListsCommand
     private GenericRoom logRoom { get; set; }
 
     public async Task<bool> CanInvoke(CommandContext ctx) {
-#if !DEBUG
-        return false;
-#endif
+// #if !DEBUG
+//         return false;
+// #endif
 
         //check if user is admin in control room
         var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
diff --git a/ExampleBots/ModerationBot/Commands/DbgAniRainbowTest.cs b/ExampleBots/ModerationBot/Commands/DbgAniRainbowTest.cs
new file mode 100644
index 0000000..b2216d1
--- /dev/null
+++ b/ExampleBots/ModerationBot/Commands/DbgAniRainbowTest.cs
@@ -0,0 +1,48 @@
+using System.Diagnostics;
+using LibMatrix.EventTypes.Spec;
+using LibMatrix.Helpers;
+using LibMatrix.RoomTypes;
+using LibMatrix.Services;
+using LibMatrix.Utilities.Bot.Interfaces;
+using ModerationBot.AccountData;
+
+namespace ModerationBot.Commands;
+
+public class DbgAniRainbowTest(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand {
+    public string Name { get; } = "dbg-ani-rainbow";
+    public string Description { get; } = "[Debug] animated rainbow :)";
+    private GenericRoom logRoom { get; set; }
+
+    public async Task<bool> CanInvoke(CommandContext ctx) {
+        return ctx.Room.RoomId == "!DoHEdFablOLjddKWIp:rory.gay";
+    }
+
+    public async Task Invoke(CommandContext ctx) {
+        //255 long string
+        // var rainbow = "🟥🟧🟨🟩🟦🟪";
+        var rainbow = "M";
+        var chars = rainbow;
+        for (var i = 0; i < 76; i++) {
+            chars += rainbow[i%rainbow.Length];
+        }
+
+        var msg = new MessageBuilder(msgType: "m.notice").WithRainbowString(chars).Build();
+        var msgEvent = await ctx.Room.SendMessageEventAsync(msg);
+        
+        Task.Run(async () => {
+
+            int i = 0;
+            while (true) {
+                msg = new MessageBuilder(msgType: "m.notice").WithRainbowString(chars, offset: i+=5).Build();
+                    // .SetReplaceRelation<RoomMessageEventContent>(msgEvent.EventId);
+                // msg.Body = "";
+                // msg.FormattedBody = "";
+                var sw = Stopwatch.StartNew();
+                await ctx.Room.SendMessageEventAsync(msg);
+                await Task.Delay(sw.Elapsed);
+            }
+            
+        });
+
+    }
+}
\ No newline at end of file
diff --git a/ExampleBots/ModerationBot/Commands/JoinSpaceMembersCommand.cs b/ExampleBots/ModerationBot/Commands/JoinSpaceMembersCommand.cs
index da77b05..6564e71 100644
--- a/ExampleBots/ModerationBot/Commands/JoinSpaceMembersCommand.cs
+++ b/ExampleBots/ModerationBot/Commands/JoinSpaceMembersCommand.cs
@@ -30,6 +30,7 @@ public class JoinSpaceMembersCommand(IServiceProvider services, HomeserverProvid
     public async Task Invoke(CommandContext ctx) {
         var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
         logRoom = ctx.Homeserver.GetRoom(botData.LogRoom ?? botData.ControlRoom);
+        var currentRooms = (await ctx.Homeserver.GetJoinedRooms()).Select(x=>x.RoomId).ToList();
 
         await logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Joining space children of {ctx.Args[0]} with reason: {string.Join(' ', ctx.Args[1..])}"));
         var roomId = ctx.Args[0];
@@ -47,6 +48,7 @@ public class JoinSpaceMembersCommand(IServiceProvider services, HomeserverProvid
         var room = ctx.Homeserver.GetRoom(roomId);
         var tasks = new List<Task<bool>>();
         await foreach (var memberRoom in room.AsSpace.GetChildrenAsync()) {
+            if (currentRooms.Contains(memberRoom.RoomId)) continue;
             servers.Add(room.RoomId.Split(':', 2)[1]);
             servers = servers.Distinct().ToList();
             tasks.Add(JoinRoom(memberRoom, string.Join(' ', ctx.Args[1..]), servers));
@@ -59,8 +61,8 @@ public class JoinSpaceMembersCommand(IServiceProvider services, HomeserverProvid
 
     private async Task<bool> JoinRoom(GenericRoom memberRoom, string reason, List<string> servers) {
         try {
-            await memberRoom.JoinAsync(servers.ToArray(), reason);
-            await logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Joined room {memberRoom.RoomId}"));
+            var resp = await memberRoom.JoinAsync(servers.ToArray(), reason, checkIfAlreadyMember: false);
+            await logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Joined room {memberRoom.RoomId} (resp={resp.RoomId})"));
         }
         catch (Exception e) {
             await logRoom.SendMessageEventAsync(MessageFormatter.FormatException($"Failed to join {memberRoom.RoomId}", e));
diff --git a/ExampleBots/ModerationBot/Commands/ReloadPoliciesCommand.cs b/ExampleBots/ModerationBot/Commands/ReloadPoliciesCommand.cs
new file mode 100644
index 0000000..b876145
--- /dev/null
+++ b/ExampleBots/ModerationBot/Commands/ReloadPoliciesCommand.cs
@@ -0,0 +1,37 @@
+using LibMatrix.EventTypes.Spec;
+using LibMatrix.Helpers;
+using LibMatrix.Services;
+using LibMatrix.Utilities.Bot.Interfaces;
+using ModerationBot.AccountData;
+
+namespace ModerationBot.Commands;
+
+public class ReloadPoliciesCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver, PolicyEngine engine) : ICommand {
+    public string Name { get; } = "reloadpolicies";
+    public string Description { get; } = "Reload policies";
+
+    public async Task<bool> CanInvoke(CommandContext ctx) {
+        if (ctx.MessageEvent.Sender == "@cadence:cadence.moe") return true;
+        //check if user is admin in control room
+        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
+        var controlRoom = ctx.Homeserver.GetRoom(botData.ControlRoom);
+        var isAdmin = (await controlRoom.GetPowerLevelsAsync())!.UserHasStatePermission(ctx.MessageEvent.Sender, "m.room.ban");
+        if (!isAdmin) {
+            // await ctx.Reply("You do not have permission to use this command!");
+            await ctx.Homeserver.GetRoom(botData.LogRoom!).SendMessageEventAsync(
+                new RoomMessageEventContent(body: $"User {ctx.MessageEvent.Sender} tried to use command {Name} but does not have permission!", messageType: "m.text"));
+        }
+
+        return isAdmin;
+    }
+
+    public async Task Invoke(CommandContext ctx) {
+
+        var botData = await ctx.Homeserver.GetAccountDataAsync<BotData>("gay.rory.moderation_bot_data");
+        var policyRoom = ctx.Homeserver.GetRoom(botData.DefaultPolicyRoom ?? botData.ControlRoom);
+        var logRoom = ctx.Homeserver.GetRoom(botData.LogRoom ?? botData.ControlRoom);
+        
+        await logRoom.SendMessageEventAsync(MessageFormatter.FormatSuccess($"Reloading policy lists due to manual invocation!!!!"));
+        await engine.ReloadActivePolicyLists();
+    }
+}
diff --git a/ExampleBots/ModerationBot/ModerationBot.cs b/ExampleBots/ModerationBot/ModerationBot.cs
index 7c95229..2424eee 100644
--- a/ExampleBots/ModerationBot/ModerationBot.cs
+++ b/ExampleBots/ModerationBot/ModerationBot.cs
@@ -1,5 +1,6 @@
 using ArcaneLibs.Extensions;
 using LibMatrix;
+using LibMatrix.EventTypes;
 using LibMatrix.EventTypes.Spec;
 using LibMatrix.EventTypes.Spec.State;
 using LibMatrix.EventTypes.Spec.State.Policy;
@@ -29,6 +30,7 @@ public class ModerationBot(AuthenticatedHomeserverGeneric hs, ILogger<Moderation
     }
 
     private async Task Run(CancellationToken cancellationToken) {
+        return;
         if (Directory.Exists("bot_data/cache"))
             Directory.GetFiles("bot_data/cache").ToList().ForEach(File.Delete);
 
@@ -112,8 +114,10 @@ public class ModerationBot(AuthenticatedHomeserverGeneric hs, ILogger<Moderation
                 if (@event != null && (
                         @event.MappedType.IsAssignableTo(typeof(BasePolicy))
                         || @event.MappedType.IsAssignableTo(typeof(PolicyRuleEventContent))
-                    ))
+                    )) {
+                    await LogPolicyChange(@event);
                     await engine.ReloadActivePolicyListById(@event.RoomId);
+                }
 
                 var rules = await engine.GetMatchingPolicies(@event);
                 foreach (var matchedRule in rules) {
@@ -264,6 +268,32 @@ public class ModerationBot(AuthenticatedHomeserverGeneric hs, ILogger<Moderation
         await syncHelper.RunSyncLoopAsync();
     }
 
+    private async Task LogPolicyChange(StateEventResponse changeEvent) {
+        var room = hs.GetRoom(changeEvent.RoomId!);
+        var message = MessageFormatter.FormatWarning($"Policy change detected in {MessageFormatter.HtmlFormatMessageLink(changeEvent.RoomId, changeEvent.EventId, [hs.ServerName], await room.GetNameOrFallbackAsync())}!");
+        message = message.ConcatLine(new RoomMessageEventContent(body: $"Policy type: {changeEvent.Type} -> {changeEvent.MappedType.Name}") {
+            FormattedBody = $"Policy type: {changeEvent.Type} -> {changeEvent.MappedType.Name}"
+        });
+        var isUpdated = changeEvent.Unsigned.PrevContent is { Count: > 0 };
+        var isRemoved = changeEvent.RawContent is not { Count: > 0 };
+        // if (isUpdated) {
+        //     message = message.ConcatLine(MessageFormatter.FormatSuccess("Rule updated!"));
+        //     message = message.ConcatLine(MessageFormatter.FormatSuccessJson("Old rule:", changeEvent.Unsigned.PrevContent!));
+        // }
+        // else if (isRemoved) {
+        //     message = message.ConcatLine(MessageFormatter.FormatWarningJson("Rule removed!", changeEvent.Unsigned.PrevContent!));
+        // }
+        // else {
+        //     message = message.ConcatLine(MessageFormatter.FormatSuccess("New rule added!"));
+        // }
+        message = message.ConcatLine(MessageFormatter.FormatSuccessJson($"{(isUpdated ? "Updated" : isRemoved ? "Removed" : "New")} rule: {changeEvent.StateKey}", changeEvent.RawContent!));
+        if (isRemoved || isUpdated) {
+            message = message.ConcatLine(MessageFormatter.FormatSuccessJson("Old content: ", changeEvent.Unsigned.PrevContent!));
+        }
+        
+        await _logRoom.SendMessageEventAsync(message);
+    }
+
     /// <summary>Triggered when the application host is performing a graceful shutdown.</summary>
     /// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param>
     public async Task StopAsync(CancellationToken cancellationToken) {
diff --git a/ExampleBots/ModerationBot/PolicyEngine.cs b/ExampleBots/ModerationBot/PolicyEngine.cs
index 114b90d..0d0ed65 100644
--- a/ExampleBots/ModerationBot/PolicyEngine.cs
+++ b/ExampleBots/ModerationBot/PolicyEngine.cs
@@ -62,12 +62,12 @@ public class PolicyEngine(AuthenticatedHomeserverGeneric hs, ILogger<ModerationB
         await foreach (var policyList in loadTasks.ToAsyncEnumerable()) {
             policyLists.Add(policyList);
 
-            if (policyList.Policies.Count >= 256 || policyLists.Count == PolicyListAccountData.Count) {
+            if (false || policyList.Policies.Count >= 256 || policyLists.Count == PolicyListAccountData.Count) {
                 var progressMsgContent = MessageFormatter.FormatSuccess($"{policyLists.Count}/{PolicyListAccountData.Count} policy lists loaded, " +
                                                                         $"{policyLists.Sum(x => x.Policies.Count)} policies total, {sw.Elapsed} elapsed.")
                     .SetReplaceRelation<RoomMessageEventContent>(progressMessage.EventId);
 
-                _logRoom?.SendMessageEventAsync(progressMsgContent);
+                await _logRoom?.SendMessageEventAsync(progressMsgContent);
             }
         }
 
@@ -253,7 +253,7 @@ public class PolicyEngine(AuthenticatedHomeserverGeneric hs, ILogger<ModerationB
         string raw = "Count | State type | Mapped type", html = "<table><tr><th>Count</th><th>State type</th><th>Mapped type</th></tr>";
         var groupedStates = states.GroupBy(x => x.Type).ToDictionary(x => x.Key, x => x.ToList()).OrderByDescending(x => x.Value.Count);
         foreach (var (type, stateGroup) in groupedStates) {
-            raw += $"{stateGroup.Count} | {type} | {stateGroup[0].MappedType.Name}";
+            raw += $"\n{stateGroup.Count} | {type} | {stateGroup[0].MappedType.Name}";
             html += $"<tr><td>{stateGroup.Count}</td><td>{type}</td><td>{stateGroup[0].MappedType.Name}</td></tr>";
         }
 
diff --git a/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj b/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj
index 3a63532..a242125 100644
--- a/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj
+++ b/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj
@@ -5,5 +5,18 @@
         <ImplicitUsings>enable</ImplicitUsings>
         <Nullable>enable</Nullable>
     </PropertyGroup>
+    
+    <ItemGroup>
+        <ProjectReference Condition="Exists('..\ArcaneLibs\ArcaneLibs\ArcaneLibs.csproj')" Include="..\ArcaneLibs\ArcaneLibs\ArcaneLibs.csproj"/>
+        <!-- This is dangerous, but eases development since locking the version will drift out of sync without noticing,
+                which causes build errors due to missing functions.
+                Using the NuGet version in development is annoying due to delays between pushing and being able to consume.
+                If you want to use a time-appropriate version of the library, recursively clone https://cgit.rory.gay/matrix/MatrixRoomUtils.git
+                instead, since this will be locked by the MatrixRoomUtils project, which contains both LibMatrix and ArcaneLibs as a submodule. -->
+        <PackageReference Condition="!Exists('..\ArcaneLibs\ArcaneLibs\ArcaneLibs.csproj')" Include="ArcaneLibs" Version="*-preview*"/>
+    </ItemGroup>
 
+    <Target Name="ArcaneLibsNugetWarning" AfterTargets="AfterBuild">
+        <Warning Text="ArcaneLibs is being referenced from NuGet, which is dangerous. Please read the warning in LibMatrix.csproj!" Condition="!Exists('..\ArcaneLibs\ArcaneLibs\ArcaneLibs.csproj')"/>
+    </Target>
 </Project>
diff --git a/LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs b/LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs
index d3ab8cb..5293082 100644
--- a/LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs
+++ b/LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs
@@ -1,53 +1,80 @@
 using System.Text.Json.Serialization;
+using ArcaneLibs.Attributes;
 
 namespace LibMatrix.EventTypes.Spec.State.Policy;
 
 //spec
-[MatrixEvent(EventName = EventId)] //spec
-[MatrixEvent(EventName = "m.room.rule.server")] //???
-[MatrixEvent(EventName = "org.matrix.mjolnir.rule.server")] //legacy
+[MatrixEvent(EventName = EventId)]                                         //spec
+[MatrixEvent(EventName = "m.room.rule.server", Legacy = true)]             //???
+[MatrixEvent(EventName = "org.matrix.mjolnir.rule.server", Legacy = true)] //legacy
+[FriendlyName(Name = "Server policy", NamePlural = "Server policies")]
 public class ServerPolicyRuleEventContent : PolicyRuleEventContent {
     public const string EventId = "m.policy.rule.server";
 }
 
-[MatrixEvent(EventName = EventId)] //spec
-[MatrixEvent(EventName = "m.room.rule.user")] //???
-[MatrixEvent(EventName = "org.matrix.mjolnir.rule.user")] //legacy
+[MatrixEvent(EventName = EventId)]                                       //spec
+[MatrixEvent(EventName = "m.room.rule.user", Legacy = true)]             //???
+[MatrixEvent(EventName = "org.matrix.mjolnir.rule.user", Legacy = true)] //legacy
+[FriendlyName(Name = "User policy", NamePlural = "User policies")]
 public class UserPolicyRuleEventContent : PolicyRuleEventContent {
     public const string EventId = "m.policy.rule.user";
 }
 
-[MatrixEvent(EventName = EventId)] //spec
-[MatrixEvent(EventName = "m.room.rule.room")] //???
-[MatrixEvent(EventName = "org.matrix.mjolnir.rule.room")] //legacy
+[MatrixEvent(EventName = EventId)]                                       //spec
+[MatrixEvent(EventName = "m.room.rule.room", Legacy = true)]             //???
+[MatrixEvent(EventName = "org.matrix.mjolnir.rule.room", Legacy = true)] //legacy
+[FriendlyName(Name = "Room policy", NamePlural = "Room policies")]
 public class RoomPolicyRuleEventContent : PolicyRuleEventContent {
     public const string EventId = "m.policy.rule.room";
 }
 
 public abstract class PolicyRuleEventContent : EventContent {
+    public PolicyRuleEventContent() {
+        Console.WriteLine($"init policy {GetType().Name}");
+    }
+    private string? _reason;
+
     /// <summary>
     ///     Entity this ban applies to, can use * and ? as globs.
     ///     Policy is invalid if entity is null
     /// </summary>
     [JsonPropertyName("entity")]
+    [FriendlyName(Name = "Entity")]
     public string? Entity { get; set; }
 
+
+    private bool init;
     /// <summary>
     ///     Reason this user is banned
     /// </summary>
     [JsonPropertyName("reason")]
-    public string? Reason { get; set; }
+    [FriendlyName(Name = "Reason")]
+    public virtual string? Reason {
+        get {
+            // Console.WriteLine($"Read policy reason: {_reason}");
+            return _reason;
+        }
+        set {
+            // Console.WriteLine($"Set policy reason: {value}");
+            // if(init)
+                // Console.WriteLine(string.Join('\n', Environment.StackTrace.Split('\n')[..5]));
+            // init = true;
+            _reason = value;
+        }
+    }
 
     /// <summary>
     ///     Suggested action to take
     /// </summary>
     [JsonPropertyName("recommendation")]
+    [FriendlyName(Name = "Recommendation")]
     public string? Recommendation { get; set; }
 
     /// <summary>
     ///     Expiry time in milliseconds since the unix epoch, or null if the ban has no expiry.
     /// </summary>
     [JsonPropertyName("support.feline.policy.expiry.rev.2")] //stable prefix: expiry, msc pending
+    [TableHide]
     public long? Expiry { get; set; }
 
     //utils
@@ -55,6 +82,7 @@ public abstract class PolicyRuleEventContent : EventContent {
     ///     Readable expiry time, provided for easy interaction
     /// </summary>
     [JsonPropertyName("gay.rory.matrix_room_utils.readable_expiry_time_utc")]
+    [FriendlyName(Name = "Expires at")]
     public DateTime? ExpiryDateTime {
         get => Expiry == null ? null : DateTimeOffset.FromUnixTimeMilliseconds(Expiry.Value).DateTime;
         set => Expiry = ((DateTimeOffset)value).ToUnixTimeMilliseconds();
@@ -72,3 +100,9 @@ public static class PolicyRecommendationTypes {
     /// </summary>
     public static string Mute = "support.feline.policy.recommendation_mute"; //stable prefix: m.mute, msc pending
 }
+
+// public class PolicySchemaDefinition {
+//     public required string Name { get; set; }
+//     public required bool Optional { get; set; }
+//     
+// }
\ No newline at end of file
diff --git a/LibMatrix/Extensions/HttpClientExtensions.cs b/LibMatrix/Extensions/HttpClientExtensions.cs
index 71bb0e5..c0366fb 100644
--- a/LibMatrix/Extensions/HttpClientExtensions.cs
+++ b/LibMatrix/Extensions/HttpClientExtensions.cs
@@ -135,8 +135,8 @@ public class MatrixHttpClient : HttpClient {
         var request = new HttpRequestMessage(HttpMethod.Put, requestUri);
         request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
         Console.WriteLine($"Sending PUT {requestUri}");
-        Console.WriteLine($"Content: {JsonSerializer.Serialize(value, value.GetType(), options)}");
-        Console.WriteLine($"Type: {value.GetType().FullName}");
+        // Console.WriteLine($"Content: {JsonSerializer.Serialize(value, value.GetType(), options)}");
+        // Console.WriteLine($"Type: {value.GetType().FullName}");
         request.Content = new StringContent(JsonSerializer.Serialize(value, value.GetType(), options),
             Encoding.UTF8, "application/json");
         return await SendAsync(request, cancellationToken);
diff --git a/LibMatrix/Helpers/MessageBuilder.cs b/LibMatrix/Helpers/MessageBuilder.cs
new file mode 100644
index 0000000..7715462
--- /dev/null
+++ b/LibMatrix/Helpers/MessageBuilder.cs
@@ -0,0 +1,40 @@
+using ArcaneLibs;
+using LibMatrix.EventTypes.Spec;
+
+namespace LibMatrix.Helpers;
+
+public class MessageBuilder(string msgType = "m.text", string format = "org.matrix.custom.html") {
+    private RoomMessageEventContent Content { get; set; } = new() {
+        MessageType = msgType,
+        Format = format
+    };
+    
+    public RoomMessageEventContent Build() => Content;
+    
+    public MessageBuilder WithColoredBody(string color, string body) {
+        Content.Body += body;
+        Content.FormattedBody += $"<font color=\"{color}\">{body}</font>";
+        return this;
+    }
+    
+    public MessageBuilder WithColoredBody(string color, Action<MessageBuilder> bodyBuilder) {
+        Content.FormattedBody += $"<font color=\"{color}\">";
+        bodyBuilder(this);
+        Content.FormattedBody += "</font>";
+        return this;
+    }
+
+    public MessageBuilder WithRainbowString(string text, byte skip = 1, int offset = 0, double lengthFactor = 255.0, bool useLength = true) {
+        if (useLength) {
+            lengthFactor = text.Length;
+        }
+        RainbowEnumerator enumerator = new(skip, offset, lengthFactor);
+        for (int i = 0; i < text.Length; i++) {
+            var (r, g, b) = enumerator.Next();
+            Content.FormattedBody += $"<font color=\"#{r:X2}{g:X2}{b:X2}\">{text[i]}</font>";
+        }
+
+        return this;
+    }
+    
+}
\ No newline at end of file
diff --git a/LibMatrix/Helpers/MessageFormatter.cs b/LibMatrix/Helpers/MessageFormatter.cs
index b2dda61..b7c6975 100644
--- a/LibMatrix/Helpers/MessageFormatter.cs
+++ b/LibMatrix/Helpers/MessageFormatter.cs
@@ -13,7 +13,7 @@ public static class MessageFormatter {
 
     public static RoomMessageEventContent FormatException(string error, Exception e) {
         return new RoomMessageEventContent(body: $"{error}: {e.Message}", messageType: "m.text") {
-            FormattedBody = $"<font color=\"#EE4444\">{error}: <pre>{e.Message}</pre></font>",
+            FormattedBody = $"<font color=\"#EE4444\">{error}: <pre><code>{e.Message}</code></pre></font>",
             Format = "org.matrix.custom.html"
         };
     }
@@ -27,7 +27,7 @@ public static class MessageFormatter {
 
     public static RoomMessageEventContent FormatSuccessJson(string text, object data) {
         return new RoomMessageEventContent(body: text, messageType: "m.text") {
-            FormattedBody = $"<font color=\"#00FF00\">{text}: <pre>{data.ToJson(ignoreNull: true)}</pre></font>",
+            FormattedBody = $"<font color=\"#00FF00\">{text}: <pre><code>{data.ToJson(ignoreNull: true)}</code></pre></font>",
             Format = "org.matrix.custom.html"
         };
     }
@@ -53,4 +53,24 @@ public static class MessageFormatter {
             Format = "org.matrix.custom.html"
         };
     }
+    
+    public static RoomMessageEventContent FormatWarningJson(string warning, object data) {
+        return new RoomMessageEventContent(body: warning, messageType: "m.text") {
+            FormattedBody = $"<font color=\"#FFFF00\">{warning}: <pre><code>{data.ToJson(ignoreNull: true)}</code></pre></font>",
+            Format = "org.matrix.custom.html"
+        };
+    }
+    
+    public static RoomMessageEventContent Concat(this RoomMessageEventContent a, RoomMessageEventContent b) {
+        return new RoomMessageEventContent(body: $"{a.Body}{b.Body}", messageType: a.MessageType) {
+            FormattedBody = $"{a.FormattedBody}{b.FormattedBody}",
+            Format = a.Format
+        };
+    }
+    public static RoomMessageEventContent ConcatLine(this RoomMessageEventContent a, RoomMessageEventContent b) {
+        return new RoomMessageEventContent(body: $"{a.Body}\n{b.Body}", messageType: "m.text") {
+            FormattedBody = $"{a.FormattedBody}<br/>{b.FormattedBody}",
+            Format = "org.matrix.custom.html"
+        };
+    }
 }
diff --git a/LibMatrix/LibMatrix.csproj b/LibMatrix/LibMatrix.csproj
index 57d194d..a2ee327 100644
--- a/LibMatrix/LibMatrix.csproj
+++ b/LibMatrix/LibMatrix.csproj
@@ -11,6 +11,7 @@
     </PropertyGroup>
 
     <ItemGroup>
+        <PackageReference Include="Castle.Core" Version="5.1.1" />
         <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
         <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
     </ItemGroup>
diff --git a/LibMatrix/StateEvent.cs b/LibMatrix/StateEvent.cs
index 4a0adbd..1a8df11 100644
--- a/LibMatrix/StateEvent.cs
+++ b/LibMatrix/StateEvent.cs
@@ -6,7 +6,9 @@ using System.Text.Json;
 using System.Text.Json.Nodes;
 using System.Text.Json.Serialization;
 using ArcaneLibs;
+using ArcaneLibs.Attributes;
 using ArcaneLibs.Extensions;
+using Castle.DynamicProxy;
 using LibMatrix.EventTypes;
 using LibMatrix.Extensions;
 
@@ -14,7 +16,7 @@ namespace LibMatrix;
 
 public class StateEvent {
     public static FrozenSet<Type> KnownStateEventTypes { get; } = new ClassCollector<EventContent>().ResolveFromAllAccessibleAssemblies().ToFrozenSet();
-
+    
     public static FrozenDictionary<string, Type> KnownStateEventTypesByName { get; } = KnownStateEventTypes.Aggregate(
         new Dictionary<string, Type>(),
         (dict, type) => {
@@ -22,14 +24,24 @@ public class StateEvent {
             foreach (var attr in attrs) {
                 dict[attr.EventName] = type;
             }
+
             return dict;
         }).ToFrozenDictionary();
 
     public static Type GetStateEventType(string type) => KnownStateEventTypesByName.GetValueOrDefault(type) ?? typeof(UnknownEventContent);
-    
+
     [JsonIgnore]
     public Type MappedType => GetStateEventType(Type);
 
+    [JsonIgnore]
+    public bool IsLegacyType => MappedType.GetCustomAttributes<MatrixEventAttribute>().FirstOrDefault(x => x.EventName == Type)?.Legacy ?? false;
+
+    [JsonIgnore]
+    public string FriendlyTypeName => MappedType.GetFriendlyNameOrNull() ?? Type;
+
+    [JsonIgnore]
+    public string FriendlyTypeNamePlural => MappedType.GetFriendlyNamePluralOrNull() ?? Type;
+
     private static readonly JsonSerializerOptions TypedContentSerializerOptions = new() {
         Converters = {
             new JsonFloatStringConverter(),
@@ -38,15 +50,30 @@ public class StateEvent {
         }
     };
 
+    private class EventContentInterceptor : IInterceptor {
+        public void Intercept(IInvocation invocation) {
+            Console.WriteLine($"Intercepting {invocation.Method.Name}");
+            // if (invocation.Method.Name == "ToString") {
+            //     invocation.ReturnValue = "EventContent";
+            //     return;
+            // }
+
+            invocation.Proceed();
+        }
+    }
+    
     [JsonIgnore]
     [SuppressMessage("ReSharper", "PropertyCanBeMadeInitOnly.Global")]
     public EventContent? TypedContent {
         get {
             // if (Type == "m.receipt") {
-                // return null;
+            // return null;
             // }
             try {
-                return (EventContent)RawContent.Deserialize(GetStateEventType(Type), TypedContentSerializerOptions)!;
+                var c= (EventContent)RawContent.Deserialize(GetStateEventType(Type), TypedContentSerializerOptions)!;
+                // c = (EventContent)new ProxyGenerator().CreateClassProxyWithTarget(GetStateEventType(Type), c, new EventContentInterceptor());
+                // Console.WriteLine(c.GetType().Name + ": " + string.Join(", ", c.GetType().GetRuntimeProperties().Select(x=>x.Name)));
+                return c;
             }
             catch (JsonException e) {
                 Console.WriteLine(e);
@@ -127,7 +154,6 @@ public class StateEvent {
     public string InternalContentTypeName => TypedContent?.GetType().Name ?? "null";
 }
 
-
 public class StateEventResponse : StateEvent {
     [JsonPropertyName("origin_server_ts")]
     public ulong? OriginServerTs { get; set; }
@@ -213,4 +239,4 @@ public class StateEventContentPolymorphicTypeInfoResolver : DefaultJsonTypeInfoR
 }
 */
 
-#endregion
+#endregion
\ No newline at end of file