Commit 1272f4576c

1272f4576c5c69584a97501048e2d53f71ad8a56

parent: 041cb2af2d

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-06-24 06:54 UTC
committer: <strk@kbt.io>

feat: add room moderation commands

Layout: unified · split

src/commands/mod.rs +8
@@ -15,6 +15,7 @@ mod keys;
1515mod matrix;
1616mod me;
1717mod media;
18mod moderation;
1819mod names;
1920mod page_up;
2021mod part;
@@ -31,6 +32,7 @@ use keys::KeysCommand;
3132use matrix::MatrixCommand;
3233use me::MeCommand;
3334use media::MediaCommand;
35use moderation::ModerationCommand;
3436use names::NamesCommand;
3537use page_up::PageUpCommand;
3638use part::PartCommand;
@@ -43,6 +45,8 @@ pub struct Commands {
4345 _keys: Command,
4446 _devices: Command,
4547 _invite: Command,
48 _ban: Command,
49 _kick: Command,
4650 _page_up: CommandRun,
4751 _redact: Command,
4852 _topic: Command,
@@ -53,6 +57,7 @@ pub struct Commands {
5357 _upload: CommandRun,
5458 _part: CommandRun,
5559 _names: CommandRun,
60 _unban: Command,
5661}
5762
5863impl Commands {
@@ -64,6 +69,8 @@ impl Commands {
6469 _matrix: MatrixCommand::create(servers, config)?,
6570 _devices: DevicesCommand::create(servers)?,
6671 _invite: InviteCommand::create(servers)?,
72 _ban: ModerationCommand::ban(servers)?,
73 _kick: ModerationCommand::kick(servers)?,
6774 _keys: KeysCommand::create(servers)?,
6875 _page_up: PageUpCommand::create(servers)?,
6976 _redact: RedactCommand::create(servers)?,
@@ -75,6 +82,7 @@ impl Commands {
7582 _upload: UploadCommand::create(servers)?,
7683 _part: PartCommand::create(servers)?,
7784 _names: NamesCommand::create(servers)?,
85 _unban: ModerationCommand::unban(servers)?,
7886 })
7987 }
8088}
src/commands/moderation.rs added +173
@@ -0,0 +1,173 @@
1use clap::{App as Argparse, AppSettings as ArgParseSettings, Arg, ArgMatches};
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, PLUGIN_NAME};
11
12#[derive(Clone, Copy)]
13enum ModerationAction {
14 Ban,
15 Kick,
16 Unban,
17}
18
19impl ModerationAction {
20 fn command(&self) -> &'static str {
21 match self {
22 ModerationAction::Ban => "ban",
23 ModerationAction::Kick => "kick",
24 ModerationAction::Unban => "unban",
25 }
26 }
27
28 fn description(&self) -> &'static str {
29 match self {
30 ModerationAction::Ban => "Ban a Matrix user from the current room.",
31 ModerationAction::Kick => {
32 "Kick a Matrix user from the current room."
33 }
34 ModerationAction::Unban => {
35 "Unban a Matrix user from the current room."
36 }
37 }
38 }
39
40 fn verb(&self) -> &'static str {
41 match self {
42 ModerationAction::Ban => "ban",
43 ModerationAction::Kick => "kick",
44 ModerationAction::Unban => "unban",
45 }
46 }
47}
48
49pub struct ModerationCommand {
50 servers: Servers,
51 action: ModerationAction,
52}
53
54impl ModerationCommand {
55 fn create(
56 servers: &Servers,
57 action: ModerationAction,
58 ) -> Result<Command, ()> {
59 let settings = CommandSettings::new(action.command())
60 .description(action.description())
61 .add_argument("<user-id> [reason]")
62 .arguments_description(format!(
63 "user-id: The Matrix user ID to {}.\nreason: Optional reason.",
64 action.verb()
65 ))
66 .add_completion("%(matrix-users) %-");
67
68 Command::new(
69 settings,
70 ModerationCommand {
71 servers: servers.clone(),
72 action,
73 },
74 )
75 }
76
77 pub fn ban(servers: &Servers) -> Result<Command, ()> {
78 Self::create(servers, ModerationAction::Ban)
79 }
80
81 pub fn kick(servers: &Servers) -> Result<Command, ()> {
82 Self::create(servers, ModerationAction::Kick)
83 }
84
85 pub fn unban(servers: &Servers) -> Result<Command, ()> {
86 Self::create(servers, ModerationAction::Unban)
87 }
88
89 fn parse_reason(args: &ArgMatches) -> Option<String> {
90 args.values_of("reason")
91 .map(|values| values.collect::<Vec<_>>().join(" "))
92 .filter(|reason| !reason.is_empty())
93 }
94
95 fn run(
96 &self,
97 buffer: &Buffer,
98 user_id: OwnedUserId,
99 reason: Option<String>,
100 ) {
101 let Some(room) = self.servers.find_room(buffer) else {
102 Weechat::print(&format!(
103 "{}{}: /{} needs to be run in a Matrix room buffer.",
104 Weechat::prefix(Prefix::Error),
105 PLUGIN_NAME,
106 self.action.command()
107 ));
108 return;
109 };
110
111 let room = room.room().clone();
112 let action = self.action;
113 let error_user_id = user_id.clone();
114 let result = self.servers.runtime().block_on(async move {
115 match action {
116 ModerationAction::Ban => {
117 room.ban_user(&user_id, reason.as_deref()).await
118 }
119 ModerationAction::Kick => {
120 room.kick_user(&user_id, reason.as_deref()).await
121 }
122 ModerationAction::Unban => {
123 room.unban_user(&user_id, reason.as_deref()).await
124 }
125 }
126 });
127
128 if let Err(error) = result {
129 Weechat::print(&format!(
130 "{}{}: Failed to {} {}: {}",
131 Weechat::prefix(Prefix::Error),
132 PLUGIN_NAME,
133 self.action.verb(),
134 error_user_id,
135 error
136 ));
137 }
138 }
139}
140
141impl CommandCallback for ModerationCommand {
142 fn callback(&mut self, _: &Weechat, buffer: &Buffer, arguments: Args) {
143 let parser = Argparse::new(self.action.command())
144 .about(self.action.description())
145 .settings(&[
146 ArgParseSettings::DisableHelpFlags,
147 ArgParseSettings::DisableVersion,
148 ])
149 .arg(Arg::with_name("user-id").required(true).validator(|u| {
150 UserId::parse(u).map(|_| ()).map_err(|_| {
151 "The given user is not a valid user ID".to_owned()
152 })
153 }))
154 .arg(
155 Arg::with_name("reason")
156 .multiple(true)
157 .allow_hyphen_values(true),
158 );
159
160 parse_and_run(parser, arguments, |matches| {
161 let user_id = matches
162 .value_of("user-id")
163 .map(|u| {
164 UserId::parse(u)
165 .expect("Argument was already validated as a user ID")
166 })
167 .expect("User ID not set but was required");
168 let reason = Self::parse_reason(matches);
169
170 self.run(buffer, user_id, reason);
171 });
172 }
173}