about summary refs log tree commit diff
path: root/ExampleBots/MediaModeratorPoC/MediaModBot.cs
blob: 0aacf61f247727e79e9023c283cc74ae022a51c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using ArcaneLibs.Extensions;
using LibMatrix;
using LibMatrix.EventTypes.Spec;
using LibMatrix.EventTypes.Spec.State;
using LibMatrix.Helpers;
using LibMatrix.Homeservers;
using LibMatrix.Responses;
using LibMatrix.RoomTypes;
using LibMatrix.Services;
using LibMatrix.Utilities.Bot.Interfaces;
using MediaModeratorPoC.AccountData;
using MediaModeratorPoC.StateEventTypes;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace MediaModeratorPoC;

public class MediaModBot(AuthenticatedHomeserverGeneric hs, ILogger<MediaModBot> logger, MediaModBotConfiguration configuration,
    HomeserverResolverService hsResolver) : IHostedService {
    private readonly IEnumerable<ICommand> _commands;

    private Task _listenerTask;

    private GenericRoom _policyRoom;
    private GenericRoom _logRoom;
    private GenericRoom _controlRoom;



    /// <summary>Triggered when the application host is ready to start the service.</summary>
    /// <param name="cancellationToken">Indicates that the start process has been aborted.</param>
    public async Task StartAsync(CancellationToken cancellationToken) {
        _listenerTask = Run(cancellationToken);
        logger.LogInformation("Bot started!");
    }

    private async Task Run(CancellationToken cancellationToken) {
        Directory.GetFiles("bot_data/cache").ToList().ForEach(File.Delete);

        BotData botData;

        try {
            botData = await hs.GetAccountDataAsync<BotData>("gay.rory.modbot_data");
        }
        catch (Exception e) {
            if (e is not MatrixException { ErrorCode: "M_NOT_FOUND" }) {
                logger.LogError("{}", e.ToString());
                throw;
            }

            botData = new BotData();
            var creationContent = CreateRoomRequest.CreatePrivate(hs, name: "Media Moderator PoC - Control room", roomAliasName: "media-moderator-poc-control-room");
            creationContent.Invite = configuration.Admins;
            creationContent.CreationContent["type"] = "gay.rory.media_moderator_poc.control_room";

            botData.ControlRoom = (await hs.CreateRoom(creationContent)).RoomId;

            //set access rules to allow joining via control room
            creationContent.InitialState.Add(new StateEvent {
                Type = "m.room.join_rules",
                StateKey = "",
                TypedContent = new RoomJoinRulesEventContent {
                    JoinRule = "knock_restricted",
                    Allow = new() {
                        new RoomJoinRulesEventContent.AllowEntry {
                            Type = "m.room_membership",
                            RoomId = botData.ControlRoom
                        }
                    }
                }
            });

            creationContent.Name = "Media Moderator PoC - Log room";
            creationContent.RoomAliasName = "media-moderator-poc-log-room";
            creationContent.CreationContent["type"] = "gay.rory.media_moderator_poc.log_room";
            botData.LogRoom = (await hs.CreateRoom(creationContent)).RoomId;

            creationContent.Name = "Media Moderator PoC - Policy room";
            creationContent.RoomAliasName = "media-moderator-poc-policy-room";
            creationContent.CreationContent["type"] = "gay.rory.media_moderator_poc.policy_room";
            botData.PolicyRoom = (await hs.CreateRoom(creationContent)).RoomId;

            await hs.SetAccountData("gay.rory.modbot_data", botData);
        }

        _policyRoom = hs.GetRoom(botData.PolicyRoom ?? botData.ControlRoom);
        _logRoom = hs.GetRoom(botData.LogRoom ?? botData.ControlRoom);
        _controlRoom = hs.GetRoom(botData.ControlRoom);
        var syncHelper = new SyncHelper(hs);

        List<string> admins = new();

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
        Task.Run(async () => {
            while (!cancellationToken.IsCancellationRequested) {
                var controlRoomMembers = _controlRoom.GetMembersAsync();
                await foreach (var member in controlRoomMembers) {
                    if ((member.TypedContent as RoomMemberEventContent)?

                        .Membership == "join") admins.Add(member.UserId);
                }

                await Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);
            }
        }, cancellationToken);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

        syncHelper.InviteReceivedHandlers.Add(async Task (args) => {
            var inviteEvent =
                args.Value.InviteState.Events.FirstOrDefault(x =>
                    x.Type == "m.room.member" && x.StateKey == hs.UserId);
            logger.LogInformation("Got invite to {RoomId} by {Sender} with reason: {Reason}", args.Key, inviteEvent!.Sender, (inviteEvent.TypedContent as RoomMemberEventContent)!.Reason);
            if (inviteEvent.Sender.EndsWith(":rory.gay") || inviteEvent!.Sender.EndsWith(":conduit.rory.gay")) {
                try {
                    var senderProfile = await hs.GetProfileAsync(inviteEvent.Sender);
                    await hs.GetRoom(args.Key).JoinAsync(reason: $"I was invited by {senderProfile.DisplayName ?? inviteEvent.Sender}!");
                }
                catch (Exception e) {
                    logger.LogError("{}", e.ToString());
                    await hs.GetRoom(args.Key).LeaveAsync(reason: "I was unable to join the room: " + e);
                }
            }
        });

        syncHelper.TimelineEventHandlers.Add(async @event => {
            var room = hs.GetRoom(@event.RoomId);
            try {
                logger.LogInformation(
                    "Got timeline event in {}: {}", @event.RoomId, @event.ToJson(indent: true, ignoreNull: true));

                if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent message }) {
                    if (message is { MessageType: "m.image" }) {
                        //check media
                        var matchedPolicy = await CheckMedia(@event);
                        if (matchedPolicy is null) return;
                        var matchedpolicyData = matchedPolicy.TypedContent as MediaPolicyEventContent;
                        var recommendation = matchedpolicyData.Recommendation;
                        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 =
                                    $"<font color=\"#FFFF00\">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: <pre>{matchedPolicy.RawContent!.ToJson(ignoreNull: true)}</pre></font>"
                            });
                        switch (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" +
                                                        $"<font color=\"#FF0000\">User {MessageFormatter.HtmlFormatMention(@event.Sender)} posted a banned image <a href=\"{message.Url}\">{message.Url}</a></font>"
                                    });
                                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 =
                                            $"<font color=\"#FFFF00\">Please be careful when posting this image: {matchedpolicyData.Reason ?? "No reason specified"}</a></font>"
                                    });
                                break;
                            }
                            case "redact": {
                                await room.RedactEventAsync(@event.EventId, matchedpolicyData.Reason ?? "No reason specified");
                                break;
                            }
                            case "spoiler": {
                                // <blockquote>
                                //  <a href=\"https://matrix.to/#/@emma:rory.gay\">@emma:rory.gay</a><br>
                                //  <a href=\"https://codeberg.org/crimsonfork/CN\"></a>
                                //  <font color=\"#dc143c\" data-mx-color=\"#dc143c\">
                                //      <b>CN</b>
                                //  </font>:
                                //  <a href=\"https://the-apothecary.club/_matrix/media/v3/download/rory.gay/sLkdxUhipiQaFwRkXcPSRwdg\">test</a><br>
                                //  <span data-mx-spoiler=\"\"><a href=\"https://the-apothecary.club/_matrix/media/v3/download/rory.gay/sLkdxUhipiQaFwRkXcPSRwdg\">
                                //      <img src=\"mxc://rory.gay/sLkdxUhipiQaFwRkXcPSRwdg\" height=\"69\"></a>
                                //  </span>
                                // </blockquote>
                                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 =
                                            $"<font color=\"#FFFF00\">Please be careful when posting this image: {matchedpolicyData.Reason}, I have spoilered it for you:</a></font>"
                                    });
                                var imageUrl = message.Url;
                                await room.SendMessageEventAsync(
                                    new RoomMessageEventContent(body: $"CN: {imageUrl}",
                                        messageType: "m.text") {
                                        Format = "org.matrix.custom.html",
                                        FormattedBody = $"""
                                                             <blockquote>
                                                                <font color=\"#dc143c\" data-mx-color=\"#dc143c\">
                                                                    <b>CN</b>
                                                                </font>:
                                                                <a href=\"{imageUrl}\">{matchedpolicyData.Reason}</a><br>
                                                                <span data-mx-spoiler=\"\">
                                                                    <a href=\"{imageUrl}\">
                                                                        <img src=\"{imageUrl}\" height=\"69\">
                                                                    </a>
                                                                </span>
                                                             </blockquote>
                                                         """
                                    });
                                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());
                await _controlRoom.SendMessageEventAsync(
                    MessageFormatter.FormatException($"Unable to ban user in {MessageFormatter.HtmlFormatMention(room.RoomId)}", e));
                await _logRoom.SendMessageEventAsync(
                    MessageFormatter.FormatException($"Unable to ban user in {MessageFormatter.HtmlFormatMention(room.RoomId)}", e));
                await using var stream = new MemoryStream(e.ToString().AsBytes().ToArray());
                await _controlRoom.SendFileAsync("error.log.cs", stream);
                await _logRoom.SendFileAsync("error.log.cs", stream);
            }
        });
    }

    /// <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) {
        logger.LogInformation("Shutting down bot!");
    }

    private async Task<StateEventResponse?> CheckMedia(StateEventResponse @event) {
        var stateList = _policyRoom.GetFullStateAsync();
        var hashAlgo = SHA3_256.Create();

        var mxcUri = @event.RawContent["url"].GetValue<string>();
        var resolvedUri = await hsResolver.ResolveMediaUri(mxcUri.Split('/')[2], mxcUri);
        var uriHash = hashAlgo.ComputeHash(mxcUri.AsBytes().ToArray());
        byte[]? fileHash = null;

        try {
            fileHash = await hashAlgo.ComputeHashAsync(await hs._httpClient.GetStreamAsync(resolvedUri));
        }
        catch (Exception ex) {
            await _logRoom.SendMessageEventAsync(
                MessageFormatter.FormatException($"Error calculating file hash for {mxcUri} via {mxcUri.Split('/')[2]} ({resolvedUri}), retrying via {hs.BaseUrl}...",
                    ex));
            try {
                resolvedUri = await hsResolver.ResolveMediaUri(hs.BaseUrl, mxcUri);
                fileHash = await hashAlgo.ComputeHashAsync(await hs._httpClient.GetStreamAsync(resolvedUri));
            }
            catch (Exception ex2) {
                await _logRoom.SendMessageEventAsync(
                    MessageFormatter.FormatException($"Error calculating file hash via {hs.BaseUrl} ({resolvedUri})!", ex2));
            }
        }

        logger.LogInformation("Checking media {url} with hash {hash}", resolvedUri, fileHash);

        await foreach (var state in stateList) {
            if (state.Type != "gay.rory.media_moderator_poc.rule.media" && state.Type != "gay.rory.media_moderator_poc.rule.server") continue;
            if (!state.RawContent.ContainsKey("entity")) {
                logger.LogWarning("Rule {rule} has no entity, this event was probably redacted!", state.StateKey);
                continue;
            }

            logger.LogInformation("Checking rule {rule}: {data}", state.StateKey, state.TypedContent.ToJson(ignoreNull: true, indent: false));
            var rule = state.TypedContent as MediaPolicyEventContent;
            if (state.Type == "gay.rory.media_moderator_poc.rule.server" && rule.ServerEntity is not null) {
                rule.ServerEntity = rule.ServerEntity.Replace("\\*", ".*").Replace("\\?", ".");
                var regex = new Regex($"mxc://({rule.ServerEntity})/.*", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                if (regex.IsMatch(@event.RawContent["url"]!.GetValue<string>())) {
                    logger.LogInformation("{url} matched rule {rule}", @event.RawContent["url"], rule.ToJson(ignoreNull: true));
                    return state;
                }
            }

            if (rule.Entity is not null && uriHash.SequenceEqual(rule.Entity)) {
                logger.LogInformation("{url} matched rule {rule} by uri hash", @event.RawContent["url"], rule.ToJson(ignoreNull: true));
                return state;
            }

            logger.LogInformation("uri hash {uriHash} did not match rule's {ruleUriHash}", Convert.ToBase64String(uriHash), Convert.ToBase64String(rule.Entity));

            if (rule.FileHash is not null && fileHash is not null && rule.FileHash.SequenceEqual(fileHash)) {
                logger.LogInformation("{url} matched rule {rule} by file hash", @event.RawContent["url"], rule.ToJson(ignoreNull: true));
                return state;
            }

            logger.LogInformation("file hash {fileHash} did not match rule's {ruleFileHash}", Convert.ToBase64String(fileHash), Convert.ToBase64String(rule.FileHash));

            //check pixels every 10% of the way through the image using ImageSharp
            // var image = Image.Load(await _hs._httpClient.GetStreamAsync(resolvedUri));
        }

        logger.LogInformation("{url} did not match any rules", @event.RawContent["url"]);

        return null;
    }
}