about summary refs log tree commit diff
diff options
context:
space:
mode:
m---------LibMatrix0
-rw-r--r--MatrixUtils.RoomUpgradeCLI/Commands/DevCommands/DevDeleteRoomCommand.cs33
-rw-r--r--MatrixUtils.RoomUpgradeCLI/Commands/DevCommands/DevGetRoomDirStateCommand.cs31
-rw-r--r--MatrixUtils.RoomUpgradeCLI/Commands/ExecuteCommand.cs30
-rw-r--r--MatrixUtils.RoomUpgradeCLI/Commands/NewFileCommand.cs13
-rw-r--r--MatrixUtils.RoomUpgradeCLI/Commands/NewFromRoomDirCommand.cs115
-rw-r--r--MatrixUtils.RoomUpgradeCLI/Extensions/RoomBuilderExtensions.cs3
-rw-r--r--MatrixUtils.RoomUpgradeCLI/MatrixUtils.RoomUpgradeCLI.csproj4
-rw-r--r--MatrixUtils.RoomUpgradeCLI/Program.cs4
-rwxr-xr-xMatrixUtils.RoomUpgradeCLI/mass-upgrade.sh9
10 files changed, 238 insertions, 4 deletions
diff --git a/LibMatrix b/LibMatrix
-Subproject 65b5491a4729b223bf4df86ba81c1e598294851
+Subproject c660b3e620de241c1158d08edaf0a9902836497
diff --git a/MatrixUtils.RoomUpgradeCLI/Commands/DevCommands/DevDeleteRoomCommand.cs b/MatrixUtils.RoomUpgradeCLI/Commands/DevCommands/DevDeleteRoomCommand.cs
new file mode 100644

index 0000000..10d667f --- /dev/null +++ b/MatrixUtils.RoomUpgradeCLI/Commands/DevCommands/DevDeleteRoomCommand.cs
@@ -0,0 +1,33 @@ +using LibMatrix.Homeservers; + +namespace MatrixUtils.RoomUpgradeCLI.Commands; + +public class DevDeleteRoomCommand(ILogger<DevDeleteRoomCommand> logger, IHost host, RuntimeContext ctx, AuthenticatedHomeserverGeneric hs) : IHostedService { + public async Task StartAsync(CancellationToken cancellationToken) { + var synapse = hs as AuthenticatedHomeserverSynapse; + if (ctx.Args.Length == 2) { + var room = synapse.GetRoom(ctx.Args[1]); + await synapse.Admin.DeleteRoom(room.RoomId, new() { Purge = true }); + } + else { + string line; + do { + line = Console.ReadLine(); + if (string.IsNullOrWhiteSpace(line)) continue; + var room = synapse.GetRoom(line); + await synapse.Admin.DeleteRoom(room.RoomId, new() { Purge = true }); + } while (line is not null); + } + + await host.StopAsync(cancellationToken); + } + + public async Task StopAsync(CancellationToken cancellationToken) { } + + private async Task PrintHelp() { + Console.WriteLine("Usage: execute [filename]"); + Console.WriteLine("Options:"); + Console.WriteLine(" --help Show this help message"); + await host.StopAsync(); + } +} \ No newline at end of file diff --git a/MatrixUtils.RoomUpgradeCLI/Commands/DevCommands/DevGetRoomDirStateCommand.cs b/MatrixUtils.RoomUpgradeCLI/Commands/DevCommands/DevGetRoomDirStateCommand.cs new file mode 100644
index 0000000..7ff7b6a --- /dev/null +++ b/MatrixUtils.RoomUpgradeCLI/Commands/DevCommands/DevGetRoomDirStateCommand.cs
@@ -0,0 +1,31 @@ +using System.Web; +using LibMatrix.Homeservers; + +namespace MatrixUtils.RoomUpgradeCLI.Commands; + +public class DevGetRoomDirStateCommand(ILogger<DevGetRoomDirStateCommand> logger, IHost host, RuntimeContext ctx, AuthenticatedHomeserverGeneric hs) : IHostedService { + public async Task StartAsync(CancellationToken cancellationToken) { + var synapse = hs as AuthenticatedHomeserverSynapse; + if (ctx.Args.Length == 2) { + var res = await hs.ClientHttpClient.GetAsync(" /_matrix/client/v3/directory/list/room/" + HttpUtility.UrlEncode(ctx.Args[1])); + if (res.IsSuccessStatusCode) { + var data = await res.Content.ReadAsStringAsync(); + Console.WriteLine("Room Directory State for " + ctx.Args[1] + ":"); + Console.WriteLine(data); + } else { + Console.WriteLine("Failed to get room directory state for " + ctx.Args[1] + ": " + res.ReasonPhrase); + } + } + + await host.StopAsync(cancellationToken); + } + + public async Task StopAsync(CancellationToken cancellationToken) { } + + private async Task PrintHelp() { + Console.WriteLine("Usage: execute [filename]"); + Console.WriteLine("Options:"); + Console.WriteLine(" --help Show this help message"); + await host.StopAsync(); + } +} \ No newline at end of file diff --git a/MatrixUtils.RoomUpgradeCLI/Commands/ExecuteCommand.cs b/MatrixUtils.RoomUpgradeCLI/Commands/ExecuteCommand.cs
index 5815a35..41c8cca 100644 --- a/MatrixUtils.RoomUpgradeCLI/Commands/ExecuteCommand.cs +++ b/MatrixUtils.RoomUpgradeCLI/Commands/ExecuteCommand.cs
@@ -1,6 +1,5 @@ using System.Text.Json; using System.Text.Json.Nodes; -using ArcaneLibs.Extensions; using LibMatrix.Helpers; using LibMatrix.Homeservers; @@ -18,14 +17,39 @@ public class ExecuteCommand(ILogger<ExecuteCommand> logger, IHost host, RuntimeC await PrintHelp(); } + if (Directory.Exists(filename)) { + await ExecuteDirectory(filename); + } + else if (File.Exists(filename)) { + await ExecuteFile(filename); + } + else { + Console.WriteLine($"File or directory {filename} does not exist."); + await PrintHelp(); + } + + await host.StopAsync(cancellationToken); + } + + public async Task ExecuteFile(string filename) { var rbj = await JsonSerializer.DeserializeAsync<JsonObject>(File.OpenRead(filename)); var rb = rbj.ContainsKey(nameof(RoomUpgradeBuilder.OldRoomId)) ? rbj.Deserialize<RoomUpgradeBuilder>() : rbj.Deserialize<RoomBuilder>(); Console.WriteLine($"Executing room builder file of type {rb.GetType().Name}..."); await rb!.Create(hs); - - await host.StopAsync(cancellationToken); + } + + public async Task ExecuteDirectory(string dirName) { + if (!Directory.Exists(dirName)) { + Console.WriteLine($"Directory {dirName} does not exist."); + return; + } + var files = Directory.GetFiles(dirName, "*.json"); + foreach (var file in files) { + Console.WriteLine($"Executing file: {file}"); + await ExecuteFile(file); + } } public async Task StopAsync(CancellationToken cancellationToken) { } diff --git a/MatrixUtils.RoomUpgradeCLI/Commands/NewFileCommand.cs b/MatrixUtils.RoomUpgradeCLI/Commands/NewFileCommand.cs
index 0959857..08daf71 100644 --- a/MatrixUtils.RoomUpgradeCLI/Commands/NewFileCommand.cs +++ b/MatrixUtils.RoomUpgradeCLI/Commands/NewFileCommand.cs
@@ -19,6 +19,19 @@ public class NewFileCommand(ILogger<NewFileCommand> logger, IHost host, RuntimeC await PrintHelp(); } await rb.ApplyRoomUpgradeCLIArgs(hs, ctx.Args[2..], isNewState: true); + // check for room membership! + if (rb is RoomUpgradeBuilder rub) { + try { + var room = hs.GetRoom(rub.OldRoomId); + var membership = await room.GetStateAsync<RoomMemberEventContent>(RoomMemberEventContent.EventId, hs.UserId); + } + catch (Exception e) { + Console.WriteLine("Error checking room membership: " + e.Message); + Console.WriteLine("Please ensure you are a member of the room you are trying to upgrade. -- ABORTING --"); + await host.StopAsync(); + return; + } + } await File.WriteAllTextAsync(filename, rb.ToJson(), cancellationToken); await host.StopAsync(cancellationToken); diff --git a/MatrixUtils.RoomUpgradeCLI/Commands/NewFromRoomDirCommand.cs b/MatrixUtils.RoomUpgradeCLI/Commands/NewFromRoomDirCommand.cs new file mode 100644
index 0000000..40ab791 --- /dev/null +++ b/MatrixUtils.RoomUpgradeCLI/Commands/NewFromRoomDirCommand.cs
@@ -0,0 +1,115 @@ +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec.State.RoomInfo; +using LibMatrix.Helpers; +using LibMatrix.Homeservers; +using MatrixUtils.RoomUpgradeCLI.Extensions; + +namespace MatrixUtils.RoomUpgradeCLI.Commands; + +public class NewFromRoomDirCommand(ILogger<NewFromRoomDirCommand> logger, IHost host, RuntimeContext ctx, AuthenticatedHomeserverGeneric hs) : IHostedService { + public async Task StartAsync(CancellationToken cancellationToken) { + if (ctx.Args.Length <= 1) { + await PrintHelp(); + return; + } + + var dirName = ctx.Args[1]; + if (dirName.StartsWith("--")) { + Console.WriteLine("Directory name cannot start with --, please provide a valid directory name."); + await PrintHelp(); + } + + if (Directory.Exists(dirName)) + Directory.Delete(dirName, true); + Directory.CreateDirectory(dirName); + List<Task> tasks = []; + await foreach (var rooms in hs.EnumeratePublicRoomsAsync().WithCancellation(cancellationToken)) { + // foreach (var room in rooms.Chunk) { } + tasks.AddRange(rooms.Chunk.Select(x=> ProcessRoom(dirName, x))); + } + await Task.WhenAll(tasks); + + // var rb = ctx.Args.Contains("--upgrade") ? new RoomUpgradeBuilder() : new RoomBuilder(); + // + // // check for room membership! + // if (rb is RoomUpgradeBuilder rub) { + + // } + await host.StopAsync(cancellationToken); + } + + private async Task ProcessRoom(string dirName, PublicRoomDirectoryResult.PublicRoomListItem roomListItem) { + Console.WriteLine(roomListItem.Name ?? roomListItem.RoomId); + var room = hs.GetRoom(roomListItem.RoomId); + var rb = new RoomUpgradeBuilder() { + OldRoomId = roomListItem.RoomId + }; + + await rb.ApplyRoomUpgradeCLIArgs(hs, ctx.Args[2..], isNewState: true); + try { + var membership = await room.GetStateAsync<RoomMemberEventContent>(RoomMemberEventContent.EventId, hs.UserId); + } + catch (Exception e) { + Console.WriteLine("Error checking room membership: " + e.Message); + Console.WriteLine("Please ensure you are a member of the room you are trying to upgrade. -- ABORTING --"); + await host.StopAsync(); + return; + } + + await rb.ImportAsync(hs.GetRoom(roomListItem.RoomId)); + + var validFileNameChars = (roomListItem.Name ?? roomListItem.CanonicalAlias ?? roomListItem.RoomId) + // .Replace('&', '_') + // .Replace(':', '_') + // .Replace('\'', '_') + // .Replace(' ', '_') + .ToList(); + validFileNameChars.RemoveAll(Path.GetInvalidFileNameChars().Contains); + var filename = string.Join("", validFileNameChars); + while (File.Exists(filename)) + filename += "_"; + + await File.WriteAllTextAsync(dirName + "/" + filename + ".json", rb.ToJson()); + } + + public async Task StopAsync(CancellationToken cancellationToken) { } + + private async Task PrintHelp() { + Console.WriteLine("Usage: new [filename] [options]"); + Console.WriteLine("Options:"); + Console.WriteLine(" --help Show this help message"); + Console.WriteLine(" --version <version> Set the room version (e.g. 9, 10, 11, 12)"); + Console.WriteLine("-- New room options --"); + Console.WriteLine(" --alias <alias> Set the room alias (local part)"); + Console.WriteLine(" --avatar-url <url> Set the room avatar URL"); + Console.WriteLine(" --copy-avatar <roomId> Copy the avatar from an existing room"); + Console.WriteLine(" --copy-powerlevels <roomId> Copy power levels from an existing room"); + Console.WriteLine(" --invite-admin <userId> Invite a user as an admin (userId must start with '@')"); + Console.WriteLine(" --invite <userId> Invite a user (userId must start with '@')"); + Console.WriteLine(" --name <name> Set the room name (can be multiple words)"); + Console.WriteLine(" --topic <topic> Set the room topic (can be multiple words)"); + Console.WriteLine(" --federate <true|false> Set whether the room is federatable"); + Console.WriteLine(" --public Set the room join rule to public"); + Console.WriteLine(" --invite-only Set the room join rule to invite-only"); + Console.WriteLine(" --knock Set the room join rule to knock"); + Console.WriteLine(" --restricted Set the room join rule to restricted"); + Console.WriteLine(" --knock_restricted Set the room join rule to knock_restricted"); + Console.WriteLine(" --private Set the room join rule to private"); + Console.WriteLine(" --join-rule <rule> Set the room join rule (public, invite, knock, restricted, knock_restricted, private)"); + Console.WriteLine(" --history-visibility <visibility> Set the room history visibility (shared, invited, joined, world_readable)"); + Console.WriteLine(" --type <type> Set the room type (e.g. m.space, m.room, support.feline.policy.list.msc.v1 etc.)"); + // upgrade opts + Console.WriteLine("-- Upgrade options --"); + Console.WriteLine( + " --upgrade <roomId> Create a room upgrade file instead of a new room file - WARNING: incompatible with non-upgrade options"); + Console.WriteLine(" --invite-members Invite members during room upgrade"); + Console.WriteLine(" --invite-powerlevel-users Invite users with power levels during room upgrade"); + Console.WriteLine(" --migrate-bans Migrate bans during room upgrade"); + Console.WriteLine(" --migrate-empty-state-events Migrate empty state events during room upgrade"); + Console.WriteLine(" --upgrade-unstable-values Upgrade unstable values during room upgrade"); + Console.WriteLine(" --msc4321-policy-list-upgrade <move|transition> Upgrade MSC4321 policy list"); + Console.WriteLine( + "WARNING: The --upgrade option is incompatible with options listed under \"New room\", please use the equivalent options in the `modify` command instead."); + await host.StopAsync(); + } +} \ No newline at end of file diff --git a/MatrixUtils.RoomUpgradeCLI/Extensions/RoomBuilderExtensions.cs b/MatrixUtils.RoomUpgradeCLI/Extensions/RoomBuilderExtensions.cs
index 0faf2b9..000dd6b 100644 --- a/MatrixUtils.RoomUpgradeCLI/Extensions/RoomBuilderExtensions.cs +++ b/MatrixUtils.RoomUpgradeCLI/Extensions/RoomBuilderExtensions.cs
@@ -7,7 +7,7 @@ namespace MatrixUtils.RoomUpgradeCLI.Extensions; public static class RoomBuilderExtensions { public static async Task ApplyRoomUpgradeCLIArgs(this RoomBuilder rb, AuthenticatedHomeserverGeneric hs, string[] args, bool isNewState = false) { for (int i = 0; i < args.Length; i++) { - Console.WriteLine($"Parsing arg {i}: {args[i]}"); + // Console.WriteLine($"Parsing arg {i}: {args[i]}"); switch (args[i]) { case "--alias": rb.AliasLocalPart = args[++i]; @@ -128,6 +128,7 @@ public static class RoomBuilderExtensions { upgradeBuilder.UpgradeOptions.InviteMembers = true; break; case "--invite-powerlevel-users": + case "--invite-power-level-users": if (rb is not RoomUpgradeBuilder upgradeBuilderInvite) { throw new InvalidOperationException("Invite powerlevel users can only be used with room upgrades"); } diff --git a/MatrixUtils.RoomUpgradeCLI/MatrixUtils.RoomUpgradeCLI.csproj b/MatrixUtils.RoomUpgradeCLI/MatrixUtils.RoomUpgradeCLI.csproj
index bf76cfe..4d0299a 100644 --- a/MatrixUtils.RoomUpgradeCLI/MatrixUtils.RoomUpgradeCLI.csproj +++ b/MatrixUtils.RoomUpgradeCLI/MatrixUtils.RoomUpgradeCLI.csproj
@@ -15,4 +15,8 @@ <ProjectReference Include="..\LibMatrix\LibMatrix\LibMatrix.csproj" /> <ProjectReference Include="..\LibMatrix\Utilities\LibMatrix.Utilities.Bot\LibMatrix.Utilities.Bot.csproj" /> </ItemGroup> + + <ItemGroup> + <Folder Include="tmp\" /> + </ItemGroup> </Project> diff --git a/MatrixUtils.RoomUpgradeCLI/Program.cs b/MatrixUtils.RoomUpgradeCLI/Program.cs
index c4157e9..a93a329 100644 --- a/MatrixUtils.RoomUpgradeCLI/Program.cs +++ b/MatrixUtils.RoomUpgradeCLI/Program.cs
@@ -23,9 +23,13 @@ foreach (var group in args.Split(";")) { }); if (argGroup[0] == "new") builder.Services.AddHostedService<NewFileCommand>(); + else if (argGroup[0] == "new-from-room-dir") builder.Services.AddHostedService<NewFromRoomDirCommand>(); else if (argGroup[0] == "modify") builder.Services.AddHostedService<ModifyCommand>(); else if (argGroup[0] == "import-upgrade-state") builder.Services.AddHostedService<ImportUpgradeStateCommand>(); else if (argGroup[0] == "execute") builder.Services.AddHostedService<ExecuteCommand>(); + // dev cmds + else if (argGroup[0] == "dev-delete-room") builder.Services.AddHostedService<DevDeleteRoomCommand>(); + else if (argGroup[0] == "dev-get-room-dir-state") builder.Services.AddHostedService<DevGetRoomDirStateCommand>(); else { Console.WriteLine("Unknown command. Use 'new', 'modify', 'import-upgrade-state' or 'execute'."); return; diff --git a/MatrixUtils.RoomUpgradeCLI/mass-upgrade.sh b/MatrixUtils.RoomUpgradeCLI/mass-upgrade.sh new file mode 100755
index 0000000..f21ea3c --- /dev/null +++ b/MatrixUtils.RoomUpgradeCLI/mass-upgrade.sh
@@ -0,0 +1,9 @@ +#! /usr/bin/env sh +dotnet build -c Release +cat lst | while read id +do + DOTNET_ENVIRONMENT=Local dotnet bin/Release/net9.0/MatrixUtils.RoomUpgradeCLI.dll new tmp/$id.json --upgrade $id --upgrade-unstable-values --force-upgrade --invite-powerlevel-users \; \ + import-upgrade-state tmp/$id.json \; \ + modify tmp/$id.json --version 12 & +done +wait \ No newline at end of file