Commit 65cc837eb9
Unsigned
Layout: unified · split
src/commands/mod.rs +4
| @@ -19,6 +19,7 @@ mod names; | |||
| 19 | mod page_up; | 19 | mod page_up; |
| 20 | mod part; | 20 | mod part; |
| 21 | mod redact; | 21 | mod redact; |
| 22 | mod topic; | ||
| 22 | mod upload; | 23 | mod upload; |
| 23 | mod verification; | 24 | mod verification; |
| 24 | 25 | ||
| @@ -34,6 +35,7 @@ use names::NamesCommand; | |||
| 34 | use page_up::PageUpCommand; | 35 | use page_up::PageUpCommand; |
| 35 | use part::PartCommand; | 36 | use part::PartCommand; |
| 36 | use redact::RedactCommand; | 37 | use redact::RedactCommand; |
| 38 | use topic::TopicCommand; | ||
| 37 | use upload::UploadCommand; | 39 | use upload::UploadCommand; |
| 38 | 40 | ||
| 39 | pub struct Commands { | 41 | pub struct Commands { |
| @@ -43,6 +45,7 @@ pub struct Commands { | |||
| 43 | _invite: Command, | 45 | _invite: Command, |
| 44 | _page_up: CommandRun, | 46 | _page_up: CommandRun, |
| 45 | _redact: Command, | 47 | _redact: Command, |
| 48 | _topic: Command, | ||
| 46 | _verification: Command, | 49 | _verification: Command, |
| 47 | _buffer_clear: CommandRun, | 50 | _buffer_clear: CommandRun, |
| 48 | _join: CommandRun, | 51 | _join: CommandRun, |
| @@ -64,6 +67,7 @@ impl Commands { | |||
| 64 | _keys: KeysCommand::create(servers)?, | 67 | _keys: KeysCommand::create(servers)?, |
| 65 | _page_up: PageUpCommand::create(servers)?, | 68 | _page_up: PageUpCommand::create(servers)?, |
| 66 | _redact: RedactCommand::create(servers)?, | 69 | _redact: RedactCommand::create(servers)?, |
| 70 | _topic: TopicCommand::create(servers)?, | ||
| 67 | _verification: VerificationCommand::create(servers)?, | 71 | _verification: VerificationCommand::create(servers)?, |
| 68 | _buffer_clear: BufferClearCommand::create(servers)?, | 72 | _buffer_clear: BufferClearCommand::create(servers)?, |
| 69 | _join: JoinCommand::create(servers)?, | 73 | _join: JoinCommand::create(servers)?, |
src/commands/topic.rs added +74
| @@ -0,0 +1,74 @@ | |||
| 1 | use clap::{App as Argparse, AppSettings as ArgParseSettings, Arg}; | ||
| 2 | use weechat::{ | ||
| 3 | buffer::Buffer, | ||
| 4 | hooks::{Command, CommandCallback, CommandSettings}, | ||
| 5 | Args, Prefix, Weechat, | ||
| 6 | }; | ||
| 7 | |||
| 8 | use super::parse_and_run; | ||
| 9 | use crate::Servers; | ||
| 10 | |||
| 11 | pub struct TopicCommand { | ||
| 12 | servers: Servers, | ||
| 13 | } | ||
| 14 | |||
| 15 | impl TopicCommand { | ||
| 16 | pub fn create(servers: &Servers) -> Result<Command, ()> { | ||
| 17 | let settings = CommandSettings::new("topic") | ||
| 18 | .description("Set or clear the current Matrix room topic.") | ||
| 19 | .add_argument("[topic]") | ||
| 20 | .arguments_description( | ||
| 21 | "topic: The new topic for the current room. Omit it to clear the topic.", | ||
| 22 | ); | ||
| 23 | |||
| 24 | Command::new( | ||
| 25 | settings, | ||
| 26 | TopicCommand { | ||
| 27 | servers: servers.clone(), | ||
| 28 | }, | ||
| 29 | ) | ||
| 30 | } | ||
| 31 | |||
| 32 | fn set_topic(&self, buffer: &Buffer, topic: String) { | ||
| 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.set_room_topic(&topic)) { | ||
| 37 | Ok(_) => (), | ||
| 38 | Err(error) => Weechat::print(&format!( | ||
| 39 | "{}Failed to set room topic: {}", | ||
| 40 | Weechat::prefix(Prefix::Error), | ||
| 41 | error | ||
| 42 | )), | ||
| 43 | } | ||
| 44 | } else { | ||
| 45 | Weechat::print( | ||
| 46 | "The /topic command needs to be run in a Matrix room buffer.", | ||
| 47 | ); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | impl CommandCallback for TopicCommand { | ||
| 53 | fn callback(&mut self, _: &Weechat, buffer: &Buffer, arguments: Args) { | ||
| 54 | let parser = Argparse::new("topic") | ||
| 55 | .about("Set or clear the current Matrix room topic.") | ||
| 56 | .settings(&[ | ||
| 57 | ArgParseSettings::DisableHelpFlags, | ||
| 58 | ArgParseSettings::DisableVersion, | ||
| 59 | ]) | ||
| 60 | .arg( | ||
| 61 | Arg::with_name("topic") | ||
| 62 | .multiple(true) | ||
| 63 | .allow_hyphen_values(true), | ||
| 64 | ); | ||
| 65 | |||
| 66 | parse_and_run(parser, arguments, |matches| { | ||
| 67 | let topic = matches | ||
| 68 | .values_of("topic") | ||
| 69 | .map(|t| t.collect::<Vec<_>>().join(" ")); | ||
| 70 | |||
| 71 | self.set_topic(buffer, topic.unwrap_or_default()); | ||
| 72 | }); | ||
| 73 | } | ||
| 74 | } | ||