From d133a168c17d605236e967ff63728420ddb0ed98 Mon Sep 17 00:00:00 2001 From: Rory& Date: Wed, 31 Jan 2024 18:50:16 +0100 Subject: Repo split --- .../PluralContactBotPoC/Bot/AccountData/BotData.cs | 16 ---- .../Bot/AccountData/SystemData.cs | 15 --- .../Bot/Commands/CreateSystemCommand.cs | 57 ------------ .../PluralContactBotPoC/Bot/PluralContactBot.cs | 102 --------------------- .../Bot/PluralContactBotConfiguration.cs | 9 -- 5 files changed, 199 deletions(-) delete mode 100644 ExampleBots/PluralContactBotPoC/Bot/AccountData/BotData.cs delete mode 100644 ExampleBots/PluralContactBotPoC/Bot/AccountData/SystemData.cs delete mode 100644 ExampleBots/PluralContactBotPoC/Bot/Commands/CreateSystemCommand.cs delete mode 100644 ExampleBots/PluralContactBotPoC/Bot/PluralContactBot.cs delete mode 100644 ExampleBots/PluralContactBotPoC/Bot/PluralContactBotConfiguration.cs (limited to 'ExampleBots/PluralContactBotPoC/Bot') diff --git a/ExampleBots/PluralContactBotPoC/Bot/AccountData/BotData.cs b/ExampleBots/PluralContactBotPoC/Bot/AccountData/BotData.cs deleted file mode 100644 index 2adcbb3..0000000 --- a/ExampleBots/PluralContactBotPoC/Bot/AccountData/BotData.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.Text.Json.Serialization; -using LibMatrix.EventTypes; -using LibMatrix.Interfaces; - -namespace PluralContactBotPoC.Bot.AccountData; -[MatrixEvent(EventName = "gay.rory.plural_contact_bot.bot_config")] -public class BotData : EventContent { - [JsonPropertyName("control_room")] - public string ControlRoom { get; set; } = ""; - - [JsonPropertyName("log_room")] - public string? LogRoom { get; set; } = ""; - - [JsonPropertyName("policy_room")] - public string? PolicyRoom { get; set; } = ""; -} diff --git a/ExampleBots/PluralContactBotPoC/Bot/AccountData/SystemData.cs b/ExampleBots/PluralContactBotPoC/Bot/AccountData/SystemData.cs deleted file mode 100644 index ad8ab1d..0000000 --- a/ExampleBots/PluralContactBotPoC/Bot/AccountData/SystemData.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Text.Json.Serialization; -using LibMatrix.EventTypes; -using LibMatrix.Interfaces; - -namespace PluralContactBotPoC.Bot.StateEventTypes; - -[MatrixEvent(EventName = "gay.rory.plural_contact_bot.system_data")] -public class SystemData : EventContent { - [JsonPropertyName("control_room")] - public required string ControlRoom { get; set; } - [JsonPropertyName("system_members")] - public List Members { get; set; } = new(); - [JsonPropertyName("dm_space")] - public string? DmSpace { get; set; } -} diff --git a/ExampleBots/PluralContactBotPoC/Bot/Commands/CreateSystemCommand.cs b/ExampleBots/PluralContactBotPoC/Bot/Commands/CreateSystemCommand.cs deleted file mode 100644 index 0bb3265..0000000 --- a/ExampleBots/PluralContactBotPoC/Bot/Commands/CreateSystemCommand.cs +++ /dev/null @@ -1,57 +0,0 @@ -using LibMatrix; -using LibMatrix.EventTypes.Spec.State; -using LibMatrix.Helpers; -using LibMatrix.Services; -using LibMatrix.Utilities.Bot.Interfaces; -using PluralContactBotPoC.Bot.AccountData; -using PluralContactBotPoC.Bot.StateEventTypes; - -namespace PluralContactBotPoC.Bot.Commands; - -public class CreateSystemCommand(IServiceProvider services, HomeserverProviderService hsProvider, HomeserverResolverService hsResolver) : ICommand { - public string Name { get; } = "createsystem"; - public string Description { get; } = "Create a new system"; - - public async Task CanInvoke(CommandContext ctx) { - return true; - } - - public async Task Invoke(CommandContext ctx) { - if (ctx.Args.Length != 1) { - await ctx.Reply(MessageFormatter.FormatError("Only one argument is allowed: system name!")); - return; - } - - var sysName = ctx.Args[0]; - try { - try { - await ctx.Homeserver.GetAccountDataAsync("gay.rory.plural_contact_bot.system_data"); - await ctx.Reply(MessageFormatter.FormatError($"System {sysName} already exists!")); - } - catch (MatrixException e) { - if (e is { ErrorCode: "M_NOT_FOUND" }) { - var sysData = new SystemData() { - ControlRoom = ctx.Room.RoomId, - Members = new(), - }; - - var state = ctx.Room.GetMembersEnumerableAsync(); - await foreach (var member in state) { - sysData.Members.Add(member.StateKey); - } - - await ctx.Room.SendStateEventAsync("m.room.name", new RoomNameEventContent() { - Name = sysName + " control room" - }); - - return; - } - - throw; - } - } - catch (Exception e) { - await ctx.Reply(MessageFormatter.FormatException("Something went wrong!", e)); - } - } -} diff --git a/ExampleBots/PluralContactBotPoC/Bot/PluralContactBot.cs b/ExampleBots/PluralContactBotPoC/Bot/PluralContactBot.cs deleted file mode 100644 index 231af95..0000000 --- a/ExampleBots/PluralContactBotPoC/Bot/PluralContactBot.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System.Text; -using ArcaneLibs.Extensions; -using LibMatrix.EventTypes.Spec; -using LibMatrix.EventTypes.Spec.State; -using LibMatrix.Helpers; -using LibMatrix.Homeservers; -using LibMatrix.RoomTypes; -using LibMatrix.Services; -using LibMatrix.Utilities.Bot; -using LibMatrix.Utilities.Bot.Interfaces; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using PluralContactBotPoC.Bot.AccountData; -using PluralContactBotPoC.Bot.StateEventTypes; - -namespace PluralContactBotPoC.Bot; - -public class PluralContactBot(AuthenticatedHomeserverGeneric hs, ILogger logger, LibMatrixBotConfiguration botConfiguration, - PluralContactBotConfiguration configuration, - HomeserverResolverService hsResolver) : IHostedService { - private readonly IEnumerable _commands; - - private Task _listenerTask; - - private GenericRoom? _logRoom; - - /// Triggered when the application host is ready to start the service. - /// Indicates that the start process has been aborted. - 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; - - _logRoom = hs.GetRoom(botConfiguration.LogRoom); - - var syncHelper = new SyncHelper(hs); - - 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 {} by {} with reason: {}", args.Key, inviteEvent.Sender, (inviteEvent.TypedContent as RoomMemberEventContent).Reason); - - try { - var accountData = await hs.GetAccountDataAsync($"gay.rory.plural_contact_bot.system_data#{inviteEvent.StateKey}"); - if (accountData.Members.Contains(inviteEvent.Sender)) { - await (hs.GetRoom(args.Key)).JoinAsync(reason: "I was invited by a system member!"); - - await _logRoom.SendMessageEventAsync( - MessageFormatter.FormatSuccess( - $"I was invited by a system member ({MessageFormatter.HtmlFormatMention(inviteEvent.Sender)}) to {MessageFormatter.HtmlFormatMention(args.Key)}")); - - return; - } - } - catch (Exception e) { - await _logRoom.SendMessageEventAsync( - MessageFormatter.FormatException( - $"Exception handling event {inviteEvent.EventId} by {inviteEvent.Sender} in {MessageFormatter.HtmlFormatMention(inviteEvent.RoomId)}", e)); - } - - 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 }) { } - } - catch (Exception e) { - logger.LogError("{}", e.ToString()); - await _logRoom.SendMessageEventAsync( - MessageFormatter.FormatException($"Exception handling event {@event.EventId} by {@event.Sender} in {MessageFormatter.HtmlFormatMention(room.RoomId)}", e)); - await using var stream = new MemoryStream(Encoding.UTF8.GetBytes(e.ToString())); - await _logRoom.SendFileAsync("error.log.cs", stream); - } - }); - } - - /// Triggered when the application host is performing a graceful shutdown. - /// Indicates that the shutdown process should no longer be graceful. - public async Task StopAsync(CancellationToken cancellationToken) { - logger.LogInformation("Shutting down bot!"); - } -} diff --git a/ExampleBots/PluralContactBotPoC/Bot/PluralContactBotConfiguration.cs b/ExampleBots/PluralContactBotPoC/Bot/PluralContactBotConfiguration.cs deleted file mode 100644 index f5c21af..0000000 --- a/ExampleBots/PluralContactBotPoC/Bot/PluralContactBotConfiguration.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Microsoft.Extensions.Configuration; - -namespace PluralContactBotPoC.Bot; - -public class PluralContactBotConfiguration { - public PluralContactBotConfiguration(IConfiguration config) => config.GetRequiredSection("PluralContactBot").Bind(this); - - // public string -} -- cgit 1.4.1