Commit 472581ce58

472581ce58937975da8e721eabb7acf61811452d

parent: 9377b55ad6

Unsigned

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

feat: add matrix invite command

Layout: unified · split

src/commands/invite.rs added +91
@@ -0,0 +1,91 @@
1use clap::{App as Argparse, AppSettings as ArgParseSettings, Arg};
2use matrix_sdk::ruma::{OwnedUserId, UserId};
3use weechat::{
4 buffer::Buffer,
5 hooks::{Command, CommandCallback, CommandSettings},
6 Args, Prefix, Weechat,
7};
8
9use super::parse_and_run;
10use crate::Servers;
11
12pub struct InviteCommand {
13 servers: Servers,
14}
15
16impl InviteCommand {
17 pub fn create(servers: &Servers) -> Result<Command, ()> {
18 let settings = CommandSettings::new("invite")
19 .description("Invite a Matrix user to the current room.")
20 .add_argument("<user-id>")
21 .arguments_description(
22 "user-id: The Matrix user ID to invite to the current room.",
23 )
24 .add_completion("%(matrix-users)");
25
26 Command::new(
27 settings,
28 InviteCommand {
29 servers: servers.clone(),
30 },
31 )
32 }
33
34 fn invite(&self, buffer: &Buffer, user_id: OwnedUserId) {
35 if let Some(room) = self.servers.find_room(buffer) {
36 let room = room.room().clone();
37 let invited_user = user_id.clone();
38
39 Weechat::spawn(async move {
40 match room.invite_user_by_id(&user_id).await {
41 Ok(()) => Weechat::print(&format!(
42 "{}Invited {} to the room.",
43 Weechat::prefix(Prefix::Network),
44 invited_user
45 )),
46 Err(error) => Weechat::print(&format!(
47 "{}Failed to invite {}: {}",
48 Weechat::prefix(Prefix::Error),
49 invited_user,
50 error
51 )),
52 }
53 })
54 .detach();
55 } else {
56 Weechat::print(
57 "The /invite command needs to be run in a Matrix room buffer.",
58 );
59 }
60 }
61}
62
63impl CommandCallback for InviteCommand {
64 fn callback(&mut self, _: &Weechat, buffer: &Buffer, arguments: Args) {
65 let parser = Argparse::new("invite")
66 .about("Invite a Matrix user to the current room.")
67 .settings(&[
68 ArgParseSettings::DisableHelpFlags,
69 ArgParseSettings::DisableVersion,
70 ])
71 .arg(Arg::with_name("user-id").required(true).validator(|u| {
72 UserId::parse(u)
73 .map_err(|_| {
74 "The given user isn't a valid user ID".to_owned()
75 })
76 .map(|_| ())
77 }));
78
79 parse_and_run(parser, arguments, |matches| {
80 let user_id = matches
81 .value_of("user-id")
82 .map(|u| {
83 UserId::parse(u)
84 .expect("Argument was already validated as a user ID")
85 })
86 .expect("User ID not set but was required");
87
88 self.invite(buffer, user_id);
89 });
90 }
91}
src/commands/mod.rs +4
@@ -9,6 +9,7 @@ use crate::{config::ConfigHandle, Servers};
99
1010mod buffer_clear;
1111mod devices;
12mod invite;
1213mod join;
1314mod keys;
1415mod matrix;
@@ -23,6 +24,7 @@ mod verification;
2324
2425use buffer_clear::BufferClearCommand;
2526use devices::DevicesCommand;
27use invite::InviteCommand;
2628use join::JoinCommand;
2729use keys::KeysCommand;
2830use matrix::MatrixCommand;
@@ -38,6 +40,7 @@ pub struct Commands {
3840 _matrix: Command,
3941 _keys: Command,
4042 _devices: Command,
43 _invite: Command,
4144 _page_up: CommandRun,
4245 _redact: Command,
4346 _verification: Command,
@@ -57,6 +60,7 @@ impl Commands {
5760 Ok(Commands {
5861 _matrix: MatrixCommand::create(servers, config)?,
5962 _devices: DevicesCommand::create(servers)?,
63 _invite: InviteCommand::create(servers)?,
6064 _keys: KeysCommand::create(servers)?,
6165 _page_up: PageUpCommand::create(servers)?,
6266 _redact: RedactCommand::create(servers)?,