From fdc4b83c27a6e141481bfdd6a0f00fb7e91af1df Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Fri, 1 Dec 2023 12:16:00 +0100 Subject: Cleanup, move ArcaneLibs to submodule instead of parent submodule --- ModerationBot.cs | 257 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 129 insertions(+), 128 deletions(-) (limited to 'ModerationBot.cs') diff --git a/ModerationBot.cs b/ModerationBot.cs index 79b05bf..8a48b61 100644 --- a/ModerationBot.cs +++ b/ModerationBot.cs @@ -10,10 +10,10 @@ using LibMatrix.Responses; using LibMatrix.RoomTypes; using LibMatrix.Services; using LibMatrix.Utilities.Bot.Interfaces; -using ModerationBot.AccountData; -using ModerationBot.StateEventTypes; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using ModerationBot.AccountData; +using ModerationBot.StateEventTypes; using ModerationBot.StateEventTypes.Policies; namespace ModerationBot; @@ -74,9 +74,10 @@ public class ModerationBot(AuthenticatedHomeserverGeneric hs, ILogger { while (!cancellationToken.IsCancellationRequested) { var controlRoomMembers = _controlRoom.GetMembersAsync(); + var pls = await _controlRoom.GetPowerLevelsAsync(); await foreach (var member in controlRoomMembers) { if ((member.TypedContent as RoomMemberEventContent)? - .Membership == "join") admins.Add(member.StateKey); + .Membership == "join" && pls.UserHasTimelinePermission(member.Sender, RoomMessageEventContent.EventId)) admins.Add(member.StateKey); } await Task.Delay(TimeSpan.FromMinutes(5), cancellationToken); @@ -104,7 +105,7 @@ public class ModerationBot(AuthenticatedHomeserverGeneric hs, ILogger { var room = hs.GetRoom(@event.RoomId); try { @@ -116,7 +117,7 @@ public class ModerationBot(AuthenticatedHomeserverGeneric hs, ILoggerUser {MessageFormatter.HtmlFormatMention(@event.Sender)} posted an image in {MessageFormatter.HtmlFormatMention(room.RoomId)} that matched rule {matchedPolicy.StateKey}, applying action {matchedpolicyData.Recommendation}, as described in rule:
{matchedPolicy.RawContent!.ToJson(ignoreNull: true)}
" -// }); -// switch (matchedpolicyData.Recommendation) { -// case "warn_admins": { -// await _controlRoom.SendMessageEventAsync( -// new RoomMessageEventContent( -// body: $"{string.Join(' ', admins)}\nUser {MessageFormatter.HtmlFormatMention(@event.Sender)} posted a banned image {message.Url}", -// messageType: "m.text") { -// Format = "org.matrix.custom.html", -// FormattedBody = $"{string.Join(' ', admins.Select(u => MessageFormatter.HtmlFormatMention(u)))}\n" + -// $"User {MessageFormatter.HtmlFormatMention(@event.Sender)} posted a banned image {message.Url}" -// }); -// break; -// } -// case "warn": { -// await room.SendMessageEventAsync( -// new RoomMessageEventContent( -// body: $"Please be careful when posting this image: {matchedpolicyData.Reason ?? "No reason specified"}", -// messageType: "m.text") { -// Format = "org.matrix.custom.html", -// FormattedBody = -// $"Please be careful when posting this image: {matchedpolicyData.Reason ?? "No reason specified"}" -// }); -// break; -// } -// case "redact": { -// await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason ?? "No reason specified"); -// break; -// } -// case "spoiler": { -// //
-// // @emma:rory.gay
-// // -// // -// // CN -// // : -// // test
-// // -// // -// // -// //
-// await room.SendMessageEventAsync( -// new RoomMessageEventContent( -// body: -// $"Please be careful when posting this image: {matchedpolicyData.Reason}, I have spoilered it for you:", -// messageType: "m.text") { -// Format = "org.matrix.custom.html", -// FormattedBody = -// $"Please be careful when posting this image: {matchedpolicyData.Reason}, I have spoilered it for you:" -// }); -// var imageUrl = message.Url; -// await room.SendMessageEventAsync( -// new RoomMessageEventContent(body: $"CN: {imageUrl}", -// messageType: "m.text") { -// Format = "org.matrix.custom.html", -// FormattedBody = $""" -//
-// -// CN -// : -// {matchedpolicyData.Reason}
-// -// -// -// -// -//
-// """ -// }); -// await room.RedactEventAsync(@event.EventId, "Automatically spoilered: " + matchedpolicyData.Reason); -// break; -// } -// case "mute": { -// await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason); -// //change powerlevel to -1 -// var currentPls = await room.GetPowerLevelsAsync(); -// if (currentPls is null) { -// logger.LogWarning("Unable to get power levels for {room}", room.RoomId); -// await _logRoom.SendMessageEventAsync( -// MessageFormatter.FormatError($"Unable to get power levels for {MessageFormatter.HtmlFormatMention(room.RoomId)}")); -// return; -// } -// -// currentPls.Users ??= new(); -// currentPls.Users[@event.Sender] = -1; -// await room.SendStateEventAsync("m.room.power_levels", currentPls); -// break; -// } -// case "kick": { -// await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason); -// await room.KickAsync(@event.Sender, matchedpolicyData.Reason); -// break; -// } -// case "ban": { -// await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason); -// await room.BanAsync(@event.Sender, matchedpolicyData.Reason); -// break; -// } -// default: { -// throw new ArgumentOutOfRangeException("recommendation", -// $"Unknown response type {matchedpolicyData.Recommendation}!"); -// } -// } -// } -// } + // + // if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent message }) { + // if (message is { MessageType: "m.image" }) { + // //check media + // // var matchedPolicy = await CheckMedia(@event); + // var matchedPolicy = rules.FirstOrDefault(); + // if (matchedPolicy is null) return; + // var matchedpolicyData = matchedPolicy.TypedContent as MediaPolicyEventContent; + // await _logRoom.SendMessageEventAsync( + // new RoomMessageEventContent( + // body: + // $"User {MessageFormatter.HtmlFormatMention(@event.Sender)} posted an image in {MessageFormatter.HtmlFormatMention(room.RoomId)} that matched rule {matchedPolicy.StateKey}, applying action {matchedpolicyData.Recommendation}, as described in rule: {matchedPolicy.RawContent!.ToJson(ignoreNull: true)}", + // messageType: "m.text") { + // Format = "org.matrix.custom.html", + // FormattedBody = + // $"User {MessageFormatter.HtmlFormatMention(@event.Sender)} posted an image in {MessageFormatter.HtmlFormatMention(room.RoomId)} that matched rule {matchedPolicy.StateKey}, applying action {matchedpolicyData.Recommendation}, as described in rule:
{matchedPolicy.RawContent!.ToJson(ignoreNull: true)}
" + // }); + // switch (matchedpolicyData.Recommendation) { + // case "warn_admins": { + // await _controlRoom.SendMessageEventAsync( + // new RoomMessageEventContent( + // body: $"{string.Join(' ', admins)}\nUser {MessageFormatter.HtmlFormatMention(@event.Sender)} posted a banned image {message.Url}", + // messageType: "m.text") { + // Format = "org.matrix.custom.html", + // FormattedBody = $"{string.Join(' ', admins.Select(u => MessageFormatter.HtmlFormatMention(u)))}\n" + + // $"User {MessageFormatter.HtmlFormatMention(@event.Sender)} posted a banned image {message.Url}" + // }); + // break; + // } + // case "warn": { + // await room.SendMessageEventAsync( + // new RoomMessageEventContent( + // body: $"Please be careful when posting this image: {matchedpolicyData.Reason ?? "No reason specified"}", + // messageType: "m.text") { + // Format = "org.matrix.custom.html", + // FormattedBody = + // $"Please be careful when posting this image: {matchedpolicyData.Reason ?? "No reason specified"}" + // }); + // break; + // } + // case "redact": { + // await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason ?? "No reason specified"); + // break; + // } + // case "spoiler": { + // //
+ // // @emma:rory.gay
+ // // + // // + // // CN + // // : + // // test
+ // // + // // + // // + // //
+ // await room.SendMessageEventAsync( + // new RoomMessageEventContent( + // body: + // $"Please be careful when posting this image: {matchedpolicyData.Reason}, I have spoilered it for you:", + // messageType: "m.text") { + // Format = "org.matrix.custom.html", + // FormattedBody = + // $"Please be careful when posting this image: {matchedpolicyData.Reason}, I have spoilered it for you:" + // }); + // var imageUrl = message.Url; + // await room.SendMessageEventAsync( + // new RoomMessageEventContent(body: $"CN: {imageUrl}", + // messageType: "m.text") { + // Format = "org.matrix.custom.html", + // FormattedBody = $""" + //
+ // + // CN + // : + // {matchedpolicyData.Reason}
+ // + // + // + // + // + //
+ // """ + // }); + // await room.RedactEventAsync(@event.EventId, "Automatically spoilered: " + matchedpolicyData.Reason); + // break; + // } + // case "mute": { + // await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason); + // //change powerlevel to -1 + // var currentPls = await room.GetPowerLevelsAsync(); + // if (currentPls is null) { + // logger.LogWarning("Unable to get power levels for {room}", room.RoomId); + // await _logRoom.SendMessageEventAsync( + // MessageFormatter.FormatError($"Unable to get power levels for {MessageFormatter.HtmlFormatMention(room.RoomId)}")); + // return; + // } + // + // currentPls.Users ??= new(); + // currentPls.Users[@event.Sender] = -1; + // await room.SendStateEventAsync("m.room.power_levels", currentPls); + // break; + // } + // case "kick": { + // await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason); + // await room.KickAsync(@event.Sender, matchedpolicyData.Reason); + // break; + // } + // case "ban": { + // await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason); + // await room.BanAsync(@event.Sender, matchedpolicyData.Reason); + // break; + // } + // default: { + // throw new ArgumentOutOfRangeException("recommendation", + // $"Unknown response type {matchedpolicyData.Recommendation}!"); + // } + // } + // } + // } } catch (Exception e) { logger.LogError("{}", e.ToString()); @@ -272,4 +273,4 @@ public class ModerationBot(AuthenticatedHomeserverGeneric hs, ILogger