Commit f247d1a0eb

f247d1a0eb059fcd66ca2d7d923fab0765889c2e

parent: 50334964bd

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-06-30 14:46 UTC
committer: <strk@kbt.io>

feat: hook matrix buffers into /join

Allow /join <room-id-or-alias> to work directly from Matrix buffers while leaving non-Matrix buffers for other protocols.

Also let /matrix join infer the server from the current Matrix buffer, or from the single configured server when there is only one.

Layout: unified · split

src/commands/join.rs added +92
@@ -0,0 +1,92 @@
1use std::borrow::Cow;
2
3use weechat::{
4 buffer::Buffer,
5 hooks::{CommandRun, CommandRunCallback},
6 Prefix, ReturnCode, Weechat,
7};
8
9use crate::{Servers, PLUGIN_NAME};
10
11pub struct JoinCommand {
12 servers: Servers,
13}
14
15impl JoinCommand {
16 pub fn create(servers: &Servers) -> Result<CommandRun, ()> {
17 CommandRun::new(
18 "/join *",
19 JoinCommand {
20 servers: servers.clone(),
21 },
22 )
23 }
24
25 pub fn join_room(
26 servers: &Servers,
27 buffer: &Buffer,
28 room_id_or_alias: String,
29 allow_single_server_fallback: bool,
30 ) -> bool {
31 let server = servers.find_server(buffer).or_else(|| {
32 if allow_single_server_fallback {
33 let servers = servers.borrow();
34
35 if servers.len() == 1 {
36 return servers.values().next().cloned();
37 }
38 }
39
40 None
41 });
42
43 if let Some(server) = server {
44 Weechat::spawn(async move {
45 server.join_room(room_id_or_alias).await;
46 })
47 .detach();
48
49 true
50 } else {
51 false
52 }
53 }
54}
55
56impl CommandRunCallback for JoinCommand {
57 fn callback(
58 &mut self,
59 _: &Weechat,
60 buffer: &Buffer,
61 command: Cow<str>,
62 ) -> ReturnCode {
63 let Some(room_id_or_alias) = command.strip_prefix("/join ") else {
64 return ReturnCode::Ok;
65 };
66
67 let room_id_or_alias = room_id_or_alias.trim();
68
69 if room_id_or_alias.is_empty() {
70 return ReturnCode::Ok;
71 }
72
73 if Self::join_room(
74 &self.servers,
75 buffer,
76 room_id_or_alias.to_owned(),
77 false,
78 ) {
79 ReturnCode::OkEat
80 } else {
81 ReturnCode::Ok
82 }
83 }
84}
85
86pub fn print_no_join_server_error() {
87 Weechat::print(&format!(
88 "{}{}: Run /matrix join from a Matrix buffer or configure exactly one server.",
89 Weechat::prefix(Prefix::Error),
90 PLUGIN_NAME
91 ));
92}
src/commands/matrix.rs +20 −28
@@ -10,7 +10,11 @@ use weechat::{
10 Args, Prefix, Weechat, 10 Args, Prefix, Weechat,
11}; 11};
12 12
13use super::{parse_and_run, verification::VerificationCommand}; 13use super::{
14 join::{print_no_join_server_error, JoinCommand},
15 parse_and_run,
16 verification::VerificationCommand,
17};
14use crate::{ 18use crate::{
15 commands::{DevicesCommand, KeysCommand, MediaCommand}, 19 commands::{DevicesCommand, KeysCommand, MediaCommand},
16 config::ConfigHandle, 20 config::ConfigHandle,
@@ -32,7 +36,7 @@ impl MatrixCommand {
32 .add_argument("server add <server-name> <hostname>[:<port>]") 36 .add_argument("server add <server-name> <hostname>[:<port>]")
33 .add_argument("server delete|list|listfull <server-name>") 37 .add_argument("server delete|list|listfull <server-name>")
34 .add_argument("connect <server-name>") 38 .add_argument("connect <server-name>")
35 .add_argument("join <server-name> <room-id-or-alias>") 39 .add_argument("join <room-id-or-alias>")
36 .add_argument("devices delete|list|set-name") 40 .add_argument("devices delete|list|set-name")
37 .add_argument("keys import|export <file> <passphrase>") 41 .add_argument("keys import|export <file> <passphrase>")
38 .add_argument("media download <mxc-uri> [file]") 42 .add_argument("media download <mxc-uri> [file]")
@@ -44,7 +48,7 @@ impl MatrixCommand {
44 connect: Connect to Matrix servers. 48 connect: Connect to Matrix servers.
45 disconnect: Disconnect from one or all Matrix servers. 49 disconnect: Disconnect from one or all Matrix servers.
46 reconnect: Reconnect to server(s). 50 reconnect: Reconnect to server(s).
47 join: Join a Matrix room by ID or alias (e.g. #room:matrix.org). 51 join: Join a Matrix room by ID or alias.
48 devices: {} 52 devices: {}
49 keys: {} 53 keys: {}
50 media: {} 54 media: {}
@@ -67,7 +71,6 @@ Use /matrix [command] help to find out more.\n",
67 .add_completion("connect %(matrix_servers)") 71 .add_completion("connect %(matrix_servers)")
68 .add_completion("disconnect %(matrix_servers)") 72 .add_completion("disconnect %(matrix_servers)")
69 .add_completion("reconnect %(matrix_servers)") 73 .add_completion("reconnect %(matrix_servers)")
70 .add_completion("join %(matrix_servers)")
71 .add_completion( 74 .add_completion(
72 "help server|connect|disconnect|reconnect|join|keys|devices|media", 75 "help server|connect|disconnect|reconnect|join|keys|devices|media",
73 ); 76 );
@@ -226,23 +229,19 @@ Use /matrix [command] help to find out more.\n",
226 } 229 }
227 } 230 }
228 231
229 fn join_command(&self, args: &ArgMatches) { 232 fn join_command(&self, buffer: &Buffer, 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 233 let room_id_or_alias = args
234 .value_of("room") 234 .value_of("room")
235 .expect("Room not set but was required") 235 .expect("Room not set but was required")
236 .to_string(); 236 .to_owned();
237 237
238 if let Some(s) = self.servers.get(server_name) { 238 if !JoinCommand::join_room(
239 let server = s.clone(); 239 &self.servers,
240 Weechat::spawn(async move { 240 buffer,
241 server.join_room(room_id_or_alias).await; 241 room_id_or_alias,
242 }) 242 true,
243 .detach(); 243 ) {
244 } else { 244 print_no_join_server_error();
245 self.server_not_found(server_name)
246 } 245 }
247 } 246 }
248 247
@@ -250,7 +249,7 @@ Use /matrix [command] help to find out more.\n",
250 match args.subcommand() { 249 match args.subcommand() {
251 ("connect", Some(subargs)) => self.connect_command(subargs), 250 ("connect", Some(subargs)) => self.connect_command(subargs),
252 ("disconnect", Some(subargs)) => self.disconnect_command(subargs), 251 ("disconnect", Some(subargs)) => self.disconnect_command(subargs),
253 ("join", Some(subargs)) => self.join_command(subargs), 252 ("join", Some(subargs)) => self.join_command(buffer, subargs),
254 ("server", Some(subargs)) => self.server_command(subargs), 253 ("server", Some(subargs)) => self.server_command(subargs),
255 ("devices", Some(subargs)) => { 254 ("devices", Some(subargs)) => {
256 DevicesCommand::run(buffer, &self.servers, subargs) 255 DevicesCommand::run(buffer, &self.servers, subargs)
@@ -363,18 +362,11 @@ impl CommandCallback for MatrixCommand {
363 ) 362 )
364 .subcommand( 363 .subcommand(
365 SubCommand::with_name("join") 364 SubCommand::with_name("join")
366 .about("Join a Matrix room by ID or alias.") 365 .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( 366 .arg(
374 Arg::with_name("room") 367 Arg::with_name("room")
375 .value_name("room-id-or-alias") 368 .value_name("room-id-or-alias")
376 .required(true) 369 .required(true),
377 .index(2),
378 ), 370 ),
379 ); 371 );
380 372
src/commands/mod.rs +4
@@ -9,6 +9,7 @@ use crate::{config::ConfigHandle, Servers};
9 9
10mod buffer_clear; 10mod buffer_clear;
11mod devices; 11mod devices;
12mod join;
12mod keys; 13mod keys;
13mod matrix; 14mod matrix;
14mod me; 15mod me;
@@ -19,6 +20,7 @@ mod verification;
19 20
20use buffer_clear::BufferClearCommand; 21use buffer_clear::BufferClearCommand;
21use devices::DevicesCommand; 22use devices::DevicesCommand;
23use join::JoinCommand;
22use keys::KeysCommand; 24use keys::KeysCommand;
23use matrix::MatrixCommand; 25use matrix::MatrixCommand;
24use me::MeCommand; 26use me::MeCommand;
@@ -33,6 +35,7 @@ pub struct Commands {
33 _page_up: CommandRun, 35 _page_up: CommandRun,
34 _verification: Command, 36 _verification: Command,
35 _buffer_clear: CommandRun, 37 _buffer_clear: CommandRun,
38 _join: CommandRun,
36 _me: CommandRun, 39 _me: CommandRun,
37 _upload: CommandRun, 40 _upload: CommandRun,
38} 41}
@@ -49,6 +52,7 @@ impl Commands {
49 _page_up: PageUpCommand::create(servers)?, 52 _page_up: PageUpCommand::create(servers)?,
50 _verification: VerificationCommand::create(servers)?, 53 _verification: VerificationCommand::create(servers)?,
51 _buffer_clear: BufferClearCommand::create(servers)?, 54 _buffer_clear: BufferClearCommand::create(servers)?,
55 _join: JoinCommand::create(servers)?,
52 _me: MeCommand::create(servers)?, 56 _me: MeCommand::create(servers)?,
53 _upload: UploadCommand::create(servers)?, 57 _upload: UploadCommand::create(servers)?,
54 }) 58 })