From 1cbcf84174f8fdbd021f8e16466d2784e8fdf38c Mon Sep 17 00:00:00 2001 From: Rory& Date: Fri, 4 Oct 2024 19:46:45 +0200 Subject: Minor cleanups, support for loading access tokens from disk or appservice --- Utilities/LibMatrix.TestDataGenerator/Program.cs | 1 + .../AppServiceConfiguration.cs | 87 ---------------- .../AppServices/AppServiceConfiguration.cs | 111 +++++++++++++++++++++ .../LibMatrix.Utilities.Bot/BotCommandInstaller.cs | 17 +++- .../LibMatrixBotConfiguration.cs | 3 +- 5 files changed, 129 insertions(+), 90 deletions(-) delete mode 100644 Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs create mode 100644 Utilities/LibMatrix.Utilities.Bot/AppServices/AppServiceConfiguration.cs (limited to 'Utilities') diff --git a/Utilities/LibMatrix.TestDataGenerator/Program.cs b/Utilities/LibMatrix.TestDataGenerator/Program.cs index 2583817..f3750a8 100644 --- a/Utilities/LibMatrix.TestDataGenerator/Program.cs +++ b/Utilities/LibMatrix.TestDataGenerator/Program.cs @@ -2,6 +2,7 @@ using LibMatrix.Services; using LibMatrix.Utilities.Bot; +using LibMatrix.Utilities.Bot.AppServices; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using TestDataGenerator.Bot; diff --git a/Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs b/Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs deleted file mode 100644 index afda89e..0000000 --- a/Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs +++ /dev/null @@ -1,87 +0,0 @@ -namespace LibMatrix.Utilities.Bot; - -public class AppServiceConfiguration { - public string Id { get; set; } = null!; - public string? Url { get; set; } = null!; - public string SenderLocalpart { get; set; } = null!; - public string AppserviceToken { get; set; } = null!; - public string HomeserverToken { get; set; } = null!; - public List? Protocols { get; set; } = null!; - public bool? RateLimited { get; set; } = null!; - - public AppserviceNamespaces Namespaces { get; set; } = null!; - - public class AppserviceNamespaces { - public List? Users { get; set; } = null; - public List? Aliases { get; set; } = null; - public List? Rooms { get; set; } = null; - - public class AppserviceNamespace { - public bool Exclusive { get; set; } - public string Regex { get; set; } = null!; - } - } - - /// - /// Please dont look at code, it's horrifying but works - /// - /// - public string ToYaml() { - var yaml = $""" - id: "{Id ?? throw new NullReferenceException("Id is null")}" - url: {(Url is null ? "null" : $"\"{Url}\"")} - as_token: "{AppserviceToken ?? throw new NullReferenceException("AppserviceToken is null")}" - hs_token: "{HomeserverToken ?? throw new NullReferenceException("HomeserverToken is null")}" - sender_localpart: "{SenderLocalpart ?? throw new NullReferenceException("SenderLocalpart is null")}" - - """; - - if (Protocols is not null && Protocols.Count > 0) - yaml += $""" - protocols: - - "{Protocols[0] ?? throw new NullReferenceException("Protocols[0] is null")}" - """; - else - yaml += "protocols: []"; - yaml += "\n"; - if (RateLimited.HasValue) - yaml += $"rate_limited: {RateLimited.Value.ToString().ToLower()}\n"; - else - yaml += "rate_limited: false\n"; - - yaml += "namespaces: \n"; - - if (Namespaces.Users is null || Namespaces.Users.Count == 0) - yaml += " users: []"; - else - Namespaces.Users.ForEach(x => - yaml += $""" - users: - - exclusive: {x.Exclusive.ToString().ToLower()} - regex: "{x.Regex ?? throw new NullReferenceException("x.Regex is null")}" - """); - yaml += "\n"; - - if (Namespaces.Aliases is null || Namespaces.Aliases.Count == 0) - yaml += " aliases: []"; - else - Namespaces.Aliases.ForEach(x => - yaml += $""" - aliases: - - exclusive: {x.Exclusive.ToString().ToLower()} - regex: "{x.Regex ?? throw new NullReferenceException("x.Regex is null")}" - """); - yaml += "\n"; - if (Namespaces.Rooms is null || Namespaces.Rooms.Count == 0) - yaml += " rooms: []"; - else - Namespaces.Rooms.ForEach(x => - yaml += $""" - rooms: - - exclusive: {x.Exclusive.ToString().ToLower()} - regex: "{x.Regex ?? throw new NullReferenceException("x.Regex is null")}" - """); - - return yaml; - } -} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/AppServices/AppServiceConfiguration.cs b/Utilities/LibMatrix.Utilities.Bot/AppServices/AppServiceConfiguration.cs new file mode 100644 index 0000000..2cfcf32 --- /dev/null +++ b/Utilities/LibMatrix.Utilities.Bot/AppServices/AppServiceConfiguration.cs @@ -0,0 +1,111 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.Utilities.Bot.AppServices; + +public class AppServiceConfiguration { + [JsonPropertyName("id")] + public string Id { get; set; } = null!; + + [JsonPropertyName("url")] + public string? Url { get; set; } = null!; + + [JsonPropertyName("sender_localpart")] + public string SenderLocalpart { get; set; } = null!; + + [JsonPropertyName("as_token")] + public string AppserviceToken { get; set; } = null!; + + [JsonPropertyName("hs_token")] + public string HomeserverToken { get; set; } = null!; + + [JsonPropertyName("protocols")] + public List? Protocols { get; set; } = null!; + + [JsonPropertyName("rate_limited")] + public bool? RateLimited { get; set; } = null!; + + [JsonPropertyName("namespaces")] + public AppserviceNamespaces Namespaces { get; set; } = null!; + + public class AppserviceNamespaces { + [JsonPropertyName("users")] + public List? Users { get; set; } = null; + + [JsonPropertyName("aliases")] + public List? Aliases { get; set; } = null; + + [JsonPropertyName("rooms")] + public List? Rooms { get; set; } = null; + + public class AppserviceNamespace { + [JsonPropertyName("exclusive")] + public bool Exclusive { get; set; } + + [JsonPropertyName("regex")] + public string Regex { get; set; } = null!; + } + } + + /// + /// Please dont look at code, it's horrifying but works + /// + /// + public string ToYaml() { + var yaml = $""" + id: "{Id ?? throw new NullReferenceException("Id is null")}" + url: {(Url is null ? "null" : $"\"{Url}\"")} + as_token: "{AppserviceToken ?? throw new NullReferenceException("AppserviceToken is null")}" + hs_token: "{HomeserverToken ?? throw new NullReferenceException("HomeserverToken is null")}" + sender_localpart: "{SenderLocalpart ?? throw new NullReferenceException("SenderLocalpart is null")}" + + """; + + if (Protocols is not null && Protocols.Count > 0) + yaml += $""" + protocols: + - "{Protocols[0] ?? throw new NullReferenceException("Protocols[0] is null")}" + """; + else + yaml += "protocols: []"; + yaml += "\n"; + if (RateLimited.HasValue) + yaml += $"rate_limited: {RateLimited.Value.ToString().ToLower()}\n"; + else + yaml += "rate_limited: false\n"; + + yaml += "namespaces: \n"; + + if (Namespaces.Users is null || Namespaces.Users.Count == 0) + yaml += " users: []"; + else + Namespaces.Users.ForEach(x => + yaml += $""" + users: + - exclusive: {x.Exclusive.ToString().ToLower()} + regex: "{x.Regex ?? throw new NullReferenceException("x.Regex is null")}" + """); + yaml += "\n"; + + if (Namespaces.Aliases is null || Namespaces.Aliases.Count == 0) + yaml += " aliases: []"; + else + Namespaces.Aliases.ForEach(x => + yaml += $""" + aliases: + - exclusive: {x.Exclusive.ToString().ToLower()} + regex: "{x.Regex ?? throw new NullReferenceException("x.Regex is null")}" + """); + yaml += "\n"; + if (Namespaces.Rooms is null || Namespaces.Rooms.Count == 0) + yaml += " rooms: []"; + else + Namespaces.Rooms.ForEach(x => + yaml += $""" + rooms: + - exclusive: {x.Exclusive.ToString().ToLower()} + regex: "{x.Regex ?? throw new NullReferenceException("x.Regex is null")}" + """); + + return yaml; + } +} \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs index 621c1ee..ca6a4d8 100644 --- a/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs +++ b/Utilities/LibMatrix.Utilities.Bot/BotCommandInstaller.cs @@ -1,8 +1,7 @@ using ArcaneLibs; -using LibMatrix.EventTypes.Spec.State; using LibMatrix.Homeservers; -using LibMatrix.Responses; using LibMatrix.Services; +using LibMatrix.Utilities.Bot.AppServices; using LibMatrix.Utilities.Bot.Interfaces; using LibMatrix.Utilities.Bot.Services; using Microsoft.Extensions.DependencyInjection; @@ -22,6 +21,20 @@ public class BotInstaller(IServiceCollection services) { services.AddScoped(x => { var config = x.GetService() ?? throw new Exception("No configuration found!"); var hsProvider = x.GetService() ?? throw new Exception("No homeserver provider found!"); + + if (x.GetService() is AppServiceConfiguration appsvcConfig) + config.AccessToken = appsvcConfig.AppserviceToken; + else if (Environment.GetEnvironmentVariable("LIBMATRIX_ACCESS_TOKEN_PATH") is string path) + config.AccessTokenPath = path; + + if(string.IsNullOrWhiteSpace(config.AccessToken) && string.IsNullOrWhiteSpace(config.AccessTokenPath)) + throw new Exception("Unable to add bot service without an access token or access token path!"); + + if(!string.IsNullOrWhiteSpace(config.AccessTokenPath)) { + var token = File.ReadAllText(config.AccessTokenPath); + config.AccessToken = token; + } + var hs = hsProvider.GetAuthenticatedWithToken(config.Homeserver, config.AccessToken).Result; return hs; diff --git a/Utilities/LibMatrix.Utilities.Bot/LibMatrixBotConfiguration.cs b/Utilities/LibMatrix.Utilities.Bot/LibMatrixBotConfiguration.cs index 245442f..728b169 100644 --- a/Utilities/LibMatrix.Utilities.Bot/LibMatrixBotConfiguration.cs +++ b/Utilities/LibMatrix.Utilities.Bot/LibMatrixBotConfiguration.cs @@ -6,7 +6,8 @@ namespace LibMatrix.Utilities.Bot; public class LibMatrixBotConfiguration { public LibMatrixBotConfiguration(IConfiguration config) => config.GetRequiredSection("LibMatrixBot").Bind(this); public string Homeserver { get; set; } - public string AccessToken { get; set; } + public string? AccessToken { get; set; } + public string? AccessTokenPath { get; set; } public List Prefixes { get; set; } public bool MentionPrefix { get; set; } public string? LogRoom { get; set; } -- cgit 1.4.1