Commit 50334964bd
Unsigned
Layout: unified · split
src/commands/matrix.rs +41 −1
| @@ -32,6 +32,7 @@ impl MatrixCommand { | |||
| 32 | .add_argument("server add <server-name> <hostname>[:<port>]") | 32 | .add_argument("server add <server-name> <hostname>[:<port>]") |
| 33 | .add_argument("server delete|list|listfull <server-name>") | 33 | .add_argument("server delete|list|listfull <server-name>") |
| 34 | .add_argument("connect <server-name>") | 34 | .add_argument("connect <server-name>") |
| 35 | .add_argument("join <server-name> <room-id-or-alias>") | ||
| 35 | .add_argument("devices delete|list|set-name") | 36 | .add_argument("devices delete|list|set-name") |
| 36 | .add_argument("keys import|export <file> <passphrase>") | 37 | .add_argument("keys import|export <file> <passphrase>") |
| 37 | .add_argument("media download <mxc-uri> [file]") | 38 | .add_argument("media download <mxc-uri> [file]") |
| @@ -43,6 +44,7 @@ impl MatrixCommand { | |||
| 43 | connect: Connect to Matrix servers. | 44 | connect: Connect to Matrix servers. |
| 44 | disconnect: Disconnect from one or all Matrix servers. | 45 | disconnect: Disconnect from one or all Matrix servers. |
| 45 | reconnect: Reconnect to server(s). | 46 | reconnect: Reconnect to server(s). |
| 47 | join: Join a Matrix room by ID or alias (e.g. #room:matrix.org). | ||
| 46 | devices: {} | 48 | devices: {} |
| 47 | keys: {} | 49 | keys: {} |
| 48 | media: {} | 50 | media: {} |
| @@ -65,8 +67,9 @@ Use /matrix [command] help to find out more.\n", | |||
| 65 | .add_completion("connect %(matrix_servers)") | 67 | .add_completion("connect %(matrix_servers)") |
| 66 | .add_completion("disconnect %(matrix_servers)") | 68 | .add_completion("disconnect %(matrix_servers)") |
| 67 | .add_completion("reconnect %(matrix_servers)") | 69 | .add_completion("reconnect %(matrix_servers)") |
| 70 | .add_completion("join %(matrix_servers)") | ||
| 68 | .add_completion( | 71 | .add_completion( |
| 69 | "help server|connect|disconnect|reconnect|keys|devices|media", | 72 | "help server|connect|disconnect|reconnect|join|keys|devices|media", |
| 70 | ); | 73 | ); |
| 71 | 74 | ||
| 72 | Command::new( | 75 | Command::new( |
| @@ -223,10 +226,31 @@ Use /matrix [command] help to find out more.\n", | |||
| 223 | } | 226 | } |
| 224 | } | 227 | } |
| 225 | 228 | ||
| 229 | fn join_command(&self, args: &ArgMatches) { | ||
| 230 | let server_name = args | ||
| 231 | .value_of("server") | ||
| 232 | .expect("Server name not set but was required"); | ||
| 233 | let room_id_or_alias = args | ||
| 234 | .value_of("room") | ||
| 235 | .expect("Room not set but was required") | ||
| 236 | .to_string(); | ||
| 237 | |||
| 238 | if let Some(s) = self.servers.get(server_name) { | ||
| 239 | let server = s.clone(); | ||
| 240 | Weechat::spawn(async move { | ||
| 241 | server.join_room(room_id_or_alias).await; | ||
| 242 | }) | ||
| 243 | .detach(); | ||
| 244 | } else { | ||
| 245 | self.server_not_found(server_name) | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 226 | fn run(&self, buffer: &Buffer, args: &ArgMatches) { | 249 | fn run(&self, buffer: &Buffer, args: &ArgMatches) { |
| 227 | match args.subcommand() { | 250 | match args.subcommand() { |
| 228 | ("connect", Some(subargs)) => self.connect_command(subargs), | 251 | ("connect", Some(subargs)) => self.connect_command(subargs), |
| 229 | ("disconnect", Some(subargs)) => self.disconnect_command(subargs), | 252 | ("disconnect", Some(subargs)) => self.disconnect_command(subargs), |
| 253 | ("join", Some(subargs)) => self.join_command(subargs), | ||
| 230 | ("server", Some(subargs)) => self.server_command(subargs), | 254 | ("server", Some(subargs)) => self.server_command(subargs), |
| 231 | ("devices", Some(subargs)) => { | 255 | ("devices", Some(subargs)) => { |
| 232 | DevicesCommand::run(buffer, &self.servers, subargs) | 256 | DevicesCommand::run(buffer, &self.servers, subargs) |
| @@ -336,6 +360,22 @@ impl CommandCallback for MatrixCommand { | |||
| 336 | .value_name("server-name") | 360 | .value_name("server-name") |
| 337 | .required(true), | 361 | .required(true), |
| 338 | ), | 362 | ), |
| 363 | ) | ||
| 364 | .subcommand( | ||
| 365 | SubCommand::with_name("join") | ||
| 366 | .about("Join a Matrix room by ID or alias.") | ||
| 367 | .arg( | ||
| 368 | Arg::with_name("server") | ||
| 369 | .value_name("server-name") | ||
| 370 | .required(true) | ||
| 371 | .index(1), | ||
| 372 | ) | ||
| 373 | .arg( | ||
| 374 | Arg::with_name("room") | ||
| 375 | .value_name("room-id-or-alias") | ||
| 376 | .required(true) | ||
| 377 | .index(2), | ||
| 378 | ), | ||
| 339 | ); | 379 | ); |
| 340 | 380 | ||
| 341 | parse_and_run(argparse, arguments, |args| self.run(buffer, args)); | 381 | parse_and_run(argparse, arguments, |args| self.run(buffer, args)); |
src/server.rs +67
| @@ -214,6 +214,73 @@ impl MatrixServer { | |||
| 214 | Rc::downgrade(&self.inner) | 214 | Rc::downgrade(&self.inner) |
| 215 | } | 215 | } |
| 216 | 216 | ||
| 217 | /// Join a Matrix room by ID or alias | ||
| 218 | pub async fn join_room(&self, room_id_or_alias: String) { | ||
| 219 | let connection = if let Some(c) = self.connection() { | ||
| 220 | c | ||
| 221 | } else { | ||
| 222 | self.print_error("Not connected. Please connect first."); | ||
| 223 | return; | ||
| 224 | }; | ||
| 225 | |||
| 226 | self.print_network(&format!("Joining room {}...", room_id_or_alias)); | ||
| 227 | |||
| 228 | // Determine if it's a room ID or alias | ||
| 229 | let result = if room_id_or_alias.starts_with('!') { | ||
| 230 | // It's a room ID | ||
| 231 | use matrix_sdk::ruma::OwnedRoomId; | ||
| 232 | match room_id_or_alias.parse::<OwnedRoomId>() { | ||
| 233 | Ok(room_id) => { | ||
| 234 | let client = connection.client().clone(); | ||
| 235 | connection.spawn(async move { | ||
| 236 | client.join_room_by_id(&room_id).await | ||
| 237 | }).await | ||
| 238 | } | ||
| 239 | Err(e) => { | ||
| 240 | self.print_error(&format!("Invalid room ID: {:?}", e)); | ||
| 241 | return; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | } else { | ||
| 245 | // It's a room alias (starts with # or we add it) | ||
| 246 | use matrix_sdk::ruma::OwnedRoomOrAliasId; | ||
| 247 | let alias = if room_id_or_alias.starts_with('#') { | ||
| 248 | room_id_or_alias.clone() | ||
| 249 | } else { | ||
| 250 | format!("#{}", room_id_or_alias) | ||
| 251 | }; | ||
| 252 | |||
| 253 | match alias.parse::<OwnedRoomOrAliasId>() { | ||
| 254 | Ok(room_or_alias) => { | ||
| 255 | let client = connection.client().clone(); | ||
| 256 | connection | ||
| 257 | .spawn(async move { | ||
| 258 | client | ||
| 259 | .join_room_by_id_or_alias(&room_or_alias, &[]) | ||
| 260 | .await | ||
| 261 | }) | ||
| 262 | .await | ||
| 263 | } | ||
| 264 | Err(e) => { | ||
| 265 | self.print_error(&format!("Invalid room alias: {:?}", e)); | ||
| 266 | return; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | }; | ||
| 270 | |||
| 271 | match result { | ||
| 272 | Ok(room) => { | ||
| 273 | self.print_network(&format!( | ||
| 274 | "Successfully joined room {}", | ||
| 275 | room.room_id() | ||
| 276 | )); | ||
| 277 | } | ||
| 278 | Err(e) => { | ||
| 279 | self.print_error(&format!("Failed to join room: {:?}", e)); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 217 | pub fn connect(&self) -> Result<(), ServerError> { | 284 | pub fn connect(&self) -> Result<(), ServerError> { |
| 218 | if self.connected() { | 285 | if self.connected() { |
| 219 | self.print_error(&format!( | 286 | self.print_error(&format!( |