about summary refs log tree commit diff
path: root/LibMatrix.EventTypes
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-05-24 18:00:45 +0200
committerRory& <root@rory.gay>2025-05-24 18:00:45 +0200
commit8ade05a37f0f4d283d80d7f8d844787a4487c51e (patch)
tree37ac5e1ba555491345915b714fcce53c888bedbc /LibMatrix.EventTypes
parentClean up some debug code in policy events (diff)
downloadLibMatrix-8ade05a37f0f4d283d80d7f8d844787a4487c51e.tar.xz
Support takedown policies and hashed entities
Diffstat (limited to 'LibMatrix.EventTypes')
-rw-r--r--LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs40
1 files changed, 33 insertions, 7 deletions
diff --git a/LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs b/LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs

index a68de8d..d75b19f 100644 --- a/LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs +++ b/LibMatrix.EventTypes/Spec/State/Policy/PolicyRuleStateEventContent.cs
@@ -82,6 +82,10 @@ public abstract class PolicyRuleEventContent : EventContent { } } + [JsonPropertyName("org.matrix.msc4205.hashes")] + [TableHide] + public PolicyHash? Hashes { get; set; } + public string GetDraupnir2StateKey() => Convert.ToBase64String(SHA256.HashData($"{Entity}{Recommendation}".AsBytes().ToArray())); public Regex? GetEntityRegex() => Entity is null ? null : new(Entity.Replace(".", "\\.").Replace("*", ".*").Replace("?", ".")); @@ -90,19 +94,34 @@ public abstract class PolicyRuleEventContent : EventContent { !string.IsNullOrWhiteSpace(Entity) && (Entity.Contains('*') || Entity.Contains('?')); - public bool EntityMatches(string entity) => - !string.IsNullOrWhiteSpace(entity) - && !string.IsNullOrWhiteSpace(Entity) - && ( + public bool EntityMatches(string entity) { + if (string.IsNullOrWhiteSpace(entity)) return false; + + if (!string.IsNullOrWhiteSpace(Entity)) { // Check if entity is equal regardless of glob check - Entity == entity - || (IsGlobRule() && GetEntityRegex()!.IsMatch(entity)) - ); + var match = Entity == entity + || (IsGlobRule() && GetEntityRegex()!.IsMatch(entity)); + if (match) return match; + } + + if (Hashes is not null) { + if (!string.IsNullOrWhiteSpace(Hashes.Sha256)) { + var hash = SHA256.HashData(entity.AsBytes().ToArray()); + var match = Convert.ToBase64String(hash) == Hashes.Sha256; + if (match) return match; + } + } + + return false; + } public string? GetNormalizedRecommendation() { if (Recommendation is "m.ban" or "org.matrix.mjolnir.ban") return PolicyRecommendationTypes.Ban; + if (Recommendation is "m.takedown" or "org.matrix.msc4204.takedown") + return "m.takedown"; + return Recommendation; } } @@ -117,6 +136,13 @@ public static class PolicyRecommendationTypes { /// Mute this user /// </summary> public static string Mute = "support.feline.policy.recommendation_mute"; //stable prefix: m.mute, msc pending + + public static string Takedown = "m.takedown"; //unstable prefix: org.matrix.msc4204.takedown +} + +public class PolicyHash { + [JsonPropertyName("sha256")] + public string? Sha256 { get; set; } } // public class PolicySchemaDefinition {