Commit 937021094a
Unsigned
Layout: unified · split
src/commands/keys.rs +27 −8
| @@ -4,6 +4,7 @@ use clap::{ | ||
| 4 | 4 | App as Argparse, AppSettings as ArgParseSettings, Arg, ArgMatches, |
| 5 | 5 | SubCommand, |
| 6 | 6 | }; |
| 7 | use matrix_sdk::ruma::{OwnedRoomId, RoomId}; | |
| 7 | 8 | |
| 8 | 9 | use weechat::{ |
| 9 | 10 | buffer::Buffer, |
| @@ -32,7 +33,7 @@ impl KeysCommand { | ||
| 32 | 33 | let settings = CommandSettings::new("keys") |
| 33 | 34 | .description(Self::DESCRIPTION) |
| 34 | 35 | .add_argument("import <file> <passphrase>") |
| 35 | .add_argument("export <file> <passphrase>") | |
| 36 | .add_argument("export [--room-id <room-id>] <file> <passphrase>") | |
| 36 | 37 | .arguments_description( |
| 37 | 38 | "file: Path to a file that is or will contain the E2EE keys export", |
| 38 | 39 | ) |
| @@ -72,9 +73,14 @@ impl KeysCommand { | ||
| 72 | 73 | Weechat::spawn(import()).detach(); |
| 73 | 74 | } |
| 74 | 75 | |
| 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 | ) { | |
| 76 | 82 | let export = || async move { |
| 77 | server.export_keys(file, passphrase).await; | |
| 83 | server.export_keys(file, passphrase, room_id).await; | |
| 78 | 84 | }; |
| 79 | 85 | Weechat::spawn(export()).detach(); |
| 80 | 86 | } |
| @@ -89,10 +95,13 @@ impl KeysCommand { | ||
| 89 | 95 | Self::import(server, file, passphrase); |
| 90 | 96 | } |
| 91 | 97 | ("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); | |
| 96 | 105 | } |
| 97 | 106 | _ => unreachable!(), |
| 98 | 107 | } |
| @@ -108,8 +117,18 @@ impl KeysCommand { | ||
| 108 | 117 | .arg(Arg::with_name("file").required(true)) |
| 109 | 118 | .arg(Arg::with_name("passphrase").required(true)), |
| 110 | 119 | SubCommand::with_name("export") |
| 111 | // TODO add the ability to export keys only for a given room. | |
| 112 | 120 | .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 | ) | |
| 113 | 132 | .arg(Arg::with_name("file").required(true)) |
| 114 | 133 | .arg(Arg::with_name("passphrase").required(true)), |
| 115 | 134 | ] |
src/server.rs +11 −2
| @@ -1087,13 +1087,22 @@ impl InnerServer { | ||
| 1087 | 1087 | }; |
| 1088 | 1088 | } |
| 1089 | 1089 | |
| 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 | ) { | |
| 1091 | 1096 | let client = self.get_client().unwrap(); |
| 1092 | 1097 | |
| 1093 | 1098 | let export = async move { |
| 1094 | 1099 | client |
| 1095 | 1100 | .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 | }) | |
| 1097 | 1106 | .await |
| 1098 | 1107 | }; |
| 1099 | 1108 | |