about summary refs log tree commit diff
path: root/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs
blob: 601e598b017132bc39a74c80efb835656da2a356 (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
using System.Reflection.Metadata;
using ArcaneLibs.Extensions;
using LibMatrix.EventTypes.Spec;
using LibMatrix.EventTypes.Spec.State;
using LibMatrix.Filters;
using LibMatrix.Helpers;
using LibMatrix.Homeservers;
using LibMatrix.Utilities.Bot.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace LibMatrix.Utilities.Bot.Services;

public class CommandListenerHostedService : IHostedService {
    private readonly AuthenticatedHomeserverGeneric _hs;
    private readonly ILogger<CommandListenerHostedService> _logger;
    private readonly IEnumerable<ICommand> _commands;
    private readonly LibMatrixBotConfiguration _config;
    private readonly Func<CommandResult, Task>? _commandResultHandler;

    private Task? _listenerTask;

    public CommandListenerHostedService(AuthenticatedHomeserverGeneric hs, ILogger<CommandListenerHostedService> logger, IServiceProvider services,
        LibMatrixBotConfiguration config, Func<CommandResult, Task>? commandResultHandler = null) {
        logger.LogInformation("{} instantiated!", GetType().Name);
        _hs = hs;
        _logger = logger;
        _config = config;
        _commandResultHandler = commandResultHandler;
        _logger.LogInformation("Getting commands...");
        _commands = services.GetServices<ICommand>();
        _logger.LogInformation("Got {} commands!", _commands.Count());
    }

    /// <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 Task StartAsync(CancellationToken cancellationToken) {
        _listenerTask = Run(cancellationToken);
        _logger.LogInformation("Command listener started (StartAsync)!");
        return Task.CompletedTask;
    }

    private async Task? Run(CancellationToken cancellationToken) {
        _logger.LogInformation("Starting command listener!");
        var filter = await _hs.NamedCaches.FilterCache.GetOrSetValueAsync("gay.rory.libmatrix.utilities.bot.command_listener_syncfilter.dev2", new SyncFilter() {
            AccountData = new SyncFilter.EventFilter(notTypes: ["*"], limit: 1),
            Presence = new SyncFilter.EventFilter(notTypes: ["*"]),
            Room = new SyncFilter.RoomFilter() {
                AccountData = new SyncFilter.RoomFilter.StateFilter(notTypes: ["*"]),
                Ephemeral = new SyncFilter.RoomFilter.StateFilter(notTypes: ["*"]),
                State = new SyncFilter.RoomFilter.StateFilter(notTypes: ["*"]),
                Timeline = new SyncFilter.RoomFilter.StateFilter(types: ["m.room.message"], notSenders: [_hs.WhoAmI.UserId]),
            }
        });

        var syncHelper = new SyncHelper(_hs, _logger) {
            Timeout = 300_000,
            FilterId = filter
        };

        syncHelper.TimelineEventHandlers.Add(async @event => {
            try {
                var room = _hs.GetRoom(@event.RoomId);
                // _logger.LogInformation(eventResponse.ToJson(indent: false));
                if (@event is { Type: "m.room.message", TypedContent: RoomMessageEventContent message })
                    if (message is { MessageType: "m.text" }) {
                        var usedPrefix = await GetUsedPrefix(@event);
                        if (usedPrefix is null) return;
                        var res = await InvokeCommand(@event, usedPrefix);
                        await (_commandResultHandler?.Invoke(res) ?? HandleResult(res));
                    }
            }
            catch (Exception e) {
                _logger.LogError(e, "Error in command listener!");
            }
        });
        await syncHelper.RunSyncLoopAsync(cancellationToken: cancellationToken);
    }

    /// <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 command listener!");
        if (_listenerTask is null) {
            _logger.LogError("Could not shut down command listener task because it was null!");
            return;
        }

        await _listenerTask.WaitAsync(cancellationToken);
    }

    private async Task<string?> GetUsedPrefix(StateEventResponse evt) {
        var messageContent = evt.TypedContent as RoomMessageEventContent;
        var message = messageContent!.BodyWithoutReplyFallback;
        var prefix = _config.Prefixes.OrderByDescending(x => x.Length).FirstOrDefault(message.StartsWith);
        if (prefix is null && _config.MentionPrefix) {
            var profile = await _hs.GetProfileAsync(_hs.WhoAmI.UserId);
            var roomProfile = await _hs.GetRoom(evt.RoomId!).GetStateAsync<RoomMemberEventContent>(RoomMemberEventContent.EventId, _hs.WhoAmI.UserId);
            if (message.StartsWith(_hs.WhoAmI.UserId + ": ")) prefix = profile.DisplayName + ": ";    // `@bot:server.xyz: `
            else if (message.StartsWith(_hs.WhoAmI.UserId + " ")) prefix = profile.DisplayName + " "; // `@bot:server.xyz `
            else if (!string.IsNullOrWhiteSpace(roomProfile?.DisplayName) && message.StartsWith(roomProfile.DisplayName + ": "))
                prefix = roomProfile.DisplayName + ": "; // `local bot: `
            else if (!string.IsNullOrWhiteSpace(roomProfile?.DisplayName) && message.StartsWith(roomProfile.DisplayName + " "))
                prefix = roomProfile.DisplayName + " ";                                                                                                      // `local bot `
            else if (!string.IsNullOrWhiteSpace(profile.DisplayName) && message.StartsWith(profile.DisplayName + ": ")) prefix = profile.DisplayName + ": "; // `bot: `
            else if (!string.IsNullOrWhiteSpace(profile.DisplayName) && message.StartsWith(profile.DisplayName + " ")) prefix = profile.DisplayName + " ";   // `bot `
        }

        return prefix;
    }

    private async Task<CommandResult> InvokeCommand(StateEventResponse evt, string usedPrefix) {
        var message = evt.TypedContent as RoomMessageEventContent;
        var room = _hs.GetRoom(evt.RoomId!);

        var commandWithoutPrefix = message.BodyWithoutReplyFallback[usedPrefix.Length..].Trim();
        var ctx = new CommandContext {
            Room = room,
            MessageEvent = @evt,
            Homeserver = _hs,
            Args = commandWithoutPrefix.Split(' ').Length == 1 ? [] : commandWithoutPrefix.Split(' ')[1..],
            CommandName = commandWithoutPrefix.Split(' ')[0]
        };
        try {
            var command = _commands.SingleOrDefault(x => x.Name == commandWithoutPrefix.Split(' ')[0] || x.Aliases?.Contains(commandWithoutPrefix.Split(' ')[0]) == true);
            if (command == null) {
                await room.SendMessageEventAsync(
                    new RoomMessageEventContent("m.notice", $"Command \"{ctx.CommandName}\" not found!"));
                return new() {
                    Success = false,
                    Result = CommandResult.CommandResultType.Failure_InvalidCommand,
                    Context = ctx
                };
            }

            if (await command.CanInvoke(ctx))
                try {
                    await command.Invoke(ctx);
                }
                catch (Exception e) {
                    return new CommandResult() {
                        Context = ctx,
                        Result = CommandResult.CommandResultType.Failure_Exception,
                        Success = false,
                        Exception = e
                    };
                    // await room.SendMessageEventAsync(
                    // MessageFormatter.FormatException("An error occurred during the execution of this command", e));
                }
            else
                return new CommandResult() {
                    Context = ctx,
                    Result = CommandResult.CommandResultType.Failure_NoPermission,
                    Success = false
                };
            // await room.SendMessageEventAsync(
            // new RoomMessageEventContent("m.notice", "You do not have permission to run this command!"));

            return new CommandResult() {
                Context = ctx,
                Success = true,
                Result = CommandResult.CommandResultType.Success
            };
        }
        catch (Exception e) {
            return new CommandResult() {
                Context = ctx,
                Result = CommandResult.CommandResultType.Failure_Exception,
                Success = false,
                Exception = e
            };
        }
    }

    private async Task HandleResult(CommandResult res) {
        if (res.Success) return;
        var room = res.Context.Room;
        var msg = res.Result switch {
            CommandResult.CommandResultType.Failure_Exception => MessageFormatter.FormatException("An error occurred during the execution of this command", res.Exception!),
            CommandResult.CommandResultType.Failure_NoPermission => new RoomMessageEventContent("m.notice", "You do not have permission to run this command!"),
            CommandResult.CommandResultType.Failure_InvalidCommand => new RoomMessageEventContent("m.notice", $"Command \"{res.Context.CommandName}\" not found!"),
            _ => throw new ArgumentOutOfRangeException()
        };

        await room.SendMessageEventAsync(msg);
    }
}