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::{
1010 Args, Prefix, Weechat,
1111};
1212
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};
1418use crate::{
1519 commands::{DevicesCommand, KeysCommand, MediaCommand},
1620 config::ConfigHandle,
@@ -32,7 +36,7 @@ impl MatrixCommand {
3236 .add_argument("server add <server-name> <hostname>[:<port>]")
3337 .add_argument("server delete|list|listfull <server-name>")
3438 .add_argument("connect <server-name>")
35 .add_argument("join <server-name> <room-id-or-alias>")
39 .add_argument("join <room-id-or-alias>")
3640 .add_argument("devices delete|list|set-name")
3741 .add_argument("keys import|export <file> <passphrase>")
3842 .add_argument("media download <mxc-uri> [file]")
@@ -44,7 +48,7 @@ impl MatrixCommand {
4448 connect: Connect to Matrix servers.
4549 disconnect: Disconnect from one or all Matrix servers.
4650 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.
4852 devices: {}
4953 keys: {}
5054 media: {}
@@ -67,7 +71,6 @@ Use /matrix [command] help to find out more.\n",
6771 .add_completion("connect %(matrix_servers)")
6872 .add_completion("disconnect %(matrix_servers)")
6973 .add_completion("reconnect %(matrix_servers)")
70 .add_completion("join %(matrix_servers)")
7174 .add_completion(
7275 "help server|connect|disconnect|reconnect|join|keys|devices|media",
7376 );
@@ -226,23 +229,19 @@ Use /matrix [command] help to find out more.\n",
226229 }
227230 }
228231
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");
232 fn join_command(&self, buffer: &Buffer, args: &ArgMatches) {
233233 let room_id_or_alias = args
234234 .value_of("room")
235235 .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)
236 .to_owned();
237
238 if !JoinCommand::join_room(
239 &self.servers,
240 buffer,
241 room_id_or_alias,
242 true,
243 ) {
244 print_no_join_server_error();
246245 }
247246 }
248247
@@ -250,7 +249,7 @@ Use /matrix [command] help to find out more.\n",
250249 match args.subcommand() {
251250 ("connect", Some(subargs)) => self.connect_command(subargs),
252251 ("disconnect", Some(subargs)) => self.disconnect_command(subargs),
253 ("join", Some(subargs)) => self.join_command(subargs),
252 ("join", Some(subargs)) => self.join_command(buffer, subargs),
254253 ("server", Some(subargs)) => self.server_command(subargs),
255254 ("devices", Some(subargs)) => {
256255 DevicesCommand::run(buffer, &self.servers, subargs)
@@ -363,18 +362,11 @@ impl CommandCallback for MatrixCommand {
363362 )
364363 .subcommand(
365364 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 )
365 .about("Join a Matrix room by ID or alias")
373366 .arg(
374367 Arg::with_name("room")
375368 .value_name("room-id-or-alias")
376 .required(true)
377 .index(2),
369 .required(true),
378370 ),
379371 );
380372
src/commands/mod.rs +4
@@ -9,6 +9,7 @@ use crate::{config::ConfigHandle, Servers};
99
1010mod buffer_clear;
1111mod devices;
12mod join;
1213mod keys;
1314mod matrix;
1415mod me;
@@ -19,6 +20,7 @@ mod verification;
1920
2021use buffer_clear::BufferClearCommand;
2122use devices::DevicesCommand;
23use join::JoinCommand;
2224use keys::KeysCommand;
2325use matrix::MatrixCommand;
2426use me::MeCommand;
@@ -33,6 +35,7 @@ pub struct Commands {
3335 _page_up: CommandRun,
3436 _verification: Command,
3537 _buffer_clear: CommandRun,
38 _join: CommandRun,
3639 _me: CommandRun,
3740 _upload: CommandRun,
3841}
@@ -49,6 +52,7 @@ impl Commands {
4952 _page_up: PageUpCommand::create(servers)?,
5053 _verification: VerificationCommand::create(servers)?,
5154 _buffer_clear: BufferClearCommand::create(servers)?,
55 _join: JoinCommand::create(servers)?,
5256 _me: MeCommand::create(servers)?,
5357 _upload: UploadCommand::create(servers)?,
5458 })