Commit 2767831328
Unsigned
Layout: unified · split
src/commands/mod.rs +4
| @@ -16,6 +16,7 @@ mod me; | ||
| 16 | 16 | mod media; |
| 17 | 17 | mod page_up; |
| 18 | 18 | mod upload; |
| 19 | mod part; | |
| 19 | 20 | mod verification; |
| 20 | 21 | |
| 21 | 22 | use buffer_clear::BufferClearCommand; |
| @@ -27,6 +28,7 @@ use me::MeCommand; | ||
| 27 | 28 | use media::MediaCommand; |
| 28 | 29 | use page_up::PageUpCommand; |
| 29 | 30 | use upload::UploadCommand; |
| 31 | use part::PartCommand; | |
| 30 | 32 | |
| 31 | 33 | pub struct Commands { |
| 32 | 34 | _matrix: Command, |
| @@ -38,6 +40,7 @@ pub struct Commands { | ||
| 38 | 40 | _join: CommandRun, |
| 39 | 41 | _me: CommandRun, |
| 40 | 42 | _upload: CommandRun, |
| 43 | _part: CommandRun, | |
| 41 | 44 | } |
| 42 | 45 | |
| 43 | 46 | impl Commands { |
| @@ -55,6 +58,7 @@ impl Commands { | ||
| 55 | 58 | _join: JoinCommand::create(servers)?, |
| 56 | 59 | _me: MeCommand::create(servers)?, |
| 57 | 60 | _upload: UploadCommand::create(servers)?, |
| 61 | _part: PartCommand::create(servers)?, | |
| 58 | 62 | }) |
| 59 | 63 | } |
| 60 | 64 | } |
src/commands/part.rs added +50
| @@ -0,0 +1,50 @@ | ||
| 1 | use std::borrow::Cow; | |
| 2 | ||
| 3 | use weechat::{ | |
| 4 | buffer::Buffer, | |
| 5 | hooks::{CommandRun, CommandRunCallback}, | |
| 6 | ReturnCode, Weechat, | |
| 7 | }; | |
| 8 | ||
| 9 | use crate::Servers; | |
| 10 | ||
| 11 | pub struct PartCommand { | |
| 12 | servers: Servers, | |
| 13 | } | |
| 14 | ||
| 15 | impl PartCommand { | |
| 16 | pub fn create(servers: &Servers) -> Result<CommandRun, ()> { | |
| 17 | CommandRun::new( | |
| 18 | "/part", | |
| 19 | PartCommand { | |
| 20 | servers: servers.clone(), | |
| 21 | }, | |
| 22 | ) | |
| 23 | } | |
| 24 | } | |
| 25 | ||
| 26 | impl CommandRunCallback for PartCommand { | |
| 27 | fn callback( | |
| 28 | &mut self, | |
| 29 | _: &Weechat, | |
| 30 | buffer: &Buffer, | |
| 31 | _: Cow<str>, | |
| 32 | ) -> ReturnCode { | |
| 33 | if let Some(room) = self.servers.find_room(buffer) { | |
| 34 | let room = room.room().clone(); | |
| 35 | ||
| 36 | match self.servers.runtime().block_on(room.leave()) { | |
| 37 | Ok(()) => ReturnCode::Ok, | |
| 38 | Err(error) => { | |
| 39 | Weechat::print(&format!("Failed to leave room: {}", error)); | |
| 40 | ReturnCode::Error | |
| 41 | } | |
| 42 | } | |
| 43 | } else { | |
| 44 | Weechat::print( | |
| 45 | "The /part command needs to be run in a Matrix room buffer.", | |
| 46 | ); | |
| 47 | ReturnCode::Error | |
| 48 | } | |
| 49 | } | |
| 50 | } | |