Commit 1272f4576c
Unsigned
Layout: unified · split
src/commands/mod.rs +8
| @@ -15,6 +15,7 @@ mod keys; | ||
| 15 | 15 | mod matrix; |
| 16 | 16 | mod me; |
| 17 | 17 | mod media; |
| 18 | mod moderation; | |
| 18 | 19 | mod names; |
| 19 | 20 | mod page_up; |
| 20 | 21 | mod part; |
| @@ -31,6 +32,7 @@ use keys::KeysCommand; | ||
| 31 | 32 | use matrix::MatrixCommand; |
| 32 | 33 | use me::MeCommand; |
| 33 | 34 | use media::MediaCommand; |
| 35 | use moderation::ModerationCommand; | |
| 34 | 36 | use names::NamesCommand; |
| 35 | 37 | use page_up::PageUpCommand; |
| 36 | 38 | use part::PartCommand; |
| @@ -43,6 +45,8 @@ pub struct Commands { | ||
| 43 | 45 | _keys: Command, |
| 44 | 46 | _devices: Command, |
| 45 | 47 | _invite: Command, |
| 48 | _ban: Command, | |
| 49 | _kick: Command, | |
| 46 | 50 | _page_up: CommandRun, |
| 47 | 51 | _redact: Command, |
| 48 | 52 | _topic: Command, |
| @@ -53,6 +57,7 @@ pub struct Commands { | ||
| 53 | 57 | _upload: CommandRun, |
| 54 | 58 | _part: CommandRun, |
| 55 | 59 | _names: CommandRun, |
| 60 | _unban: Command, | |
| 56 | 61 | } |
| 57 | 62 | |
| 58 | 63 | impl Commands { |
| @@ -64,6 +69,8 @@ impl Commands { | ||
| 64 | 69 | _matrix: MatrixCommand::create(servers, config)?, |
| 65 | 70 | _devices: DevicesCommand::create(servers)?, |
| 66 | 71 | _invite: InviteCommand::create(servers)?, |
| 72 | _ban: ModerationCommand::ban(servers)?, | |
| 73 | _kick: ModerationCommand::kick(servers)?, | |
| 67 | 74 | _keys: KeysCommand::create(servers)?, |
| 68 | 75 | _page_up: PageUpCommand::create(servers)?, |
| 69 | 76 | _redact: RedactCommand::create(servers)?, |
| @@ -75,6 +82,7 @@ impl Commands { | ||
| 75 | 82 | _upload: UploadCommand::create(servers)?, |
| 76 | 83 | _part: PartCommand::create(servers)?, |
| 77 | 84 | _names: NamesCommand::create(servers)?, |
| 85 | _unban: ModerationCommand::unban(servers)?, | |
| 78 | 86 | }) |
| 79 | 87 | } |
| 80 | 88 | } |
src/commands/moderation.rs added +173
| @@ -0,0 +1,173 @@ | ||
| 1 | use clap::{App as Argparse, AppSettings as ArgParseSettings, Arg, ArgMatches}; | |
| 2 | use matrix_sdk::ruma::{OwnedUserId, UserId}; | |
| 3 | use weechat::{ | |
| 4 | buffer::Buffer, | |
| 5 | hooks::{Command, CommandCallback, CommandSettings}, | |
| 6 | Args, Prefix, Weechat, | |
| 7 | }; | |
| 8 | ||
| 9 | use super::parse_and_run; | |
| 10 | use crate::{Servers, PLUGIN_NAME}; | |
| 11 | ||
| 12 | #[derive(Clone, Copy)] | |
| 13 | enum ModerationAction { | |
| 14 | Ban, | |
| 15 | Kick, | |
| 16 | Unban, | |
| 17 | } | |
| 18 | ||
| 19 | impl 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 | ||
| 49 | pub struct ModerationCommand { | |
| 50 | servers: Servers, | |
| 51 | action: ModerationAction, | |
| 52 | } | |
| 53 | ||
| 54 | impl 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 | ||
| 141 | impl 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 | } | |