Commit 937021094a

937021094a16ff2d309f8789a3c137bd9320e3d2

parent: 8f219a7c04

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-06-23 17:49 UTC
committer: <strk@kbt.io>

feat: filter exported keys by room

Layout: unified · split

src/commands/keys.rs +27 −8
@@ -4,6 +4,7 @@ use clap::{
44 App as Argparse, AppSettings as ArgParseSettings, Arg, ArgMatches,
55 SubCommand,
66};
7use matrix_sdk::ruma::{OwnedRoomId, RoomId};
78
89use weechat::{
910 buffer::Buffer,
@@ -32,7 +33,7 @@ impl KeysCommand {
3233 let settings = CommandSettings::new("keys")
3334 .description(Self::DESCRIPTION)
3435 .add_argument("import <file> <passphrase>")
35 .add_argument("export <file> <passphrase>")
36 .add_argument("export [--room-id <room-id>] <file> <passphrase>")
3637 .arguments_description(
3738 "file: Path to a file that is or will contain the E2EE keys export",
3839 )
@@ -72,9 +73,14 @@ impl KeysCommand {
7273 Weechat::spawn(import()).detach();
7374 }
7475
75 fn export(server: MatrixServer, file: PathBuf, passphrase: String) {
76 fn export(
77 server: MatrixServer,
78 file: PathBuf,
79 passphrase: String,
80 room_id: Option<OwnedRoomId>,
81 ) {
7682 let export = || async move {
77 server.export_keys(file, passphrase).await;
83 server.export_keys(file, passphrase, room_id).await;
7884 };
7985 Weechat::spawn(export()).detach();
8086 }
@@ -89,10 +95,13 @@ impl KeysCommand {
8995 Self::import(server, file, passphrase);
9096 }
9197 ("export", args) => {
92 let (file, passphrase) = Self::upcast_args(
93 args.expect("No args were provided to the subcommand"),
94 );
95 Self::export(server, file, passphrase);
98 let args =
99 args.expect("No args were provided to the subcommand");
100 let (file, passphrase) = Self::upcast_args(args);
101 let room_id = args
102 .value_of("room-id")
103 .map(|room_id| RoomId::parse(room_id).unwrap());
104 Self::export(server, file, passphrase, room_id);
96105 }
97106 _ => unreachable!(),
98107 }
@@ -108,8 +117,18 @@ impl KeysCommand {
108117 .arg(Arg::with_name("file").required(true))
109118 .arg(Arg::with_name("passphrase").required(true)),
110119 SubCommand::with_name("export")
111 // TODO add the ability to export keys only for a given room.
112120 .about("Export your E2EE keys to the given file.")
121 .arg(
122 Arg::with_name("room-id")
123 .long("room-id")
124 .takes_value(true)
125 .value_name("room-id")
126 .validator(|room_id| {
127 RoomId::parse(room_id).map(|_| ()).map_err(|_| {
128 "The given room ID isn't valid".to_owned()
129 })
130 }),
131 )
113132 .arg(Arg::with_name("file").required(true))
114133 .arg(Arg::with_name("passphrase").required(true)),
115134 ]
src/server.rs +11 −2
@@ -1087,13 +1087,22 @@ impl InnerServer {
10871087 };
10881088 }
10891089
1090 pub async fn export_keys(&self, file: PathBuf, passphrase: String) {
1090 pub async fn export_keys(
1091 &self,
1092 file: PathBuf,
1093 passphrase: String,
1094 room_id: Option<OwnedRoomId>,
1095 ) {
10911096 let client = self.get_client().unwrap();
10921097
10931098 let export = async move {
10941099 client
10951100 .encryption()
1096 .export_room_keys(file, &passphrase, |_| true)
1101 .export_room_keys(file, &passphrase, |session| {
1102 room_id.as_ref().map_or(true, |room_id| {
1103 session.room_id().as_str() == room_id.as_str()
1104 })
1105 })
10971106 .await
10981107 };
10991108