Commit 42153b6c4f

42153b6c4fabbdda2088c482bd013938b500e205

parent: 8c3d805f25

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-14 13:29 UTC
committer: <strk@kbt.io>

fix: require explicit topic clearing

Layout: unified · split

src/commands/topic.rs +102 −20
@@ -8,6 +8,13 @@ use weechat::{
8use super::parse_and_run; 8use super::parse_and_run;
9use crate::Servers; 9use crate::Servers;
10 10
11#[derive(Debug, Eq, PartialEq)]
12enum TopicAction {
13 Show,
14 Clear,
15 Set(String),
16}
17
11pub struct TopicCommand { 18pub struct TopicCommand {
12 servers: Servers, 19 servers: Servers,
13} 20}
@@ -15,10 +22,11 @@ pub struct TopicCommand {
15impl TopicCommand { 22impl TopicCommand {
16 pub fn create(servers: &Servers) -> Result<Command, ()> { 23 pub fn create(servers: &Servers) -> Result<Command, ()> {
17 let settings = CommandSettings::new("topic") 24 let settings = CommandSettings::new("topic")
18 .description("Set or clear the current Matrix room topic.") 25 .description("Show, set, or clear the current Matrix room topic.")
19 .add_argument("[topic]") 26 .add_argument("[--clear] [topic]")
20 .arguments_description( 27 .arguments_description(
21 "topic: The new topic for the current room. Omit it to clear the topic.", 28 "topic: The new topic for the current room. Omit it to show the current topic.\n\
29 --clear: Clear the topic.",
22 ); 30 );
23 31
24 Command::new( 32 Command::new(
@@ -29,6 +37,54 @@ impl TopicCommand {
29 ) 37 )
30 } 38 }
31 39
40 fn parser() -> Argparse<'static, 'static> {
41 Argparse::new("topic")
42 .about("Show, set, or clear the current Matrix room topic.")
43 .settings(&[
44 ArgParseSettings::DisableHelpFlags,
45 ArgParseSettings::DisableVersion,
46 ])
47 .arg(
48 Arg::with_name("clear")
49 .long("clear")
50 .help("Clear the current room topic.")
51 .conflicts_with("topic"),
52 )
53 .arg(
54 Arg::with_name("topic")
55 .multiple(true)
56 .allow_hyphen_values(true),
57 )
58 }
59
60 fn action(matches: &clap::ArgMatches) -> TopicAction {
61 if matches.is_present("clear") {
62 TopicAction::Clear
63 } else {
64 matches
65 .values_of("topic")
66 .map(|t| TopicAction::Set(t.collect::<Vec<_>>().join(" ")))
67 .unwrap_or(TopicAction::Show)
68 }
69 }
70
71 fn show_topic(&self, buffer: &Buffer) {
72 if let Some(room) = self.servers.find_room(buffer) {
73 match room.room().topic() {
74 Some(topic) if !topic.is_empty() => {
75 buffer.print(&format!("Topic: {}", topic));
76 }
77 _ => {
78 buffer.print("No topic is set.");
79 }
80 }
81 } else {
82 Weechat::print(
83 "The /topic command needs to be run in a Matrix room buffer.",
84 );
85 }
86 }
87
32 fn set_topic(&self, buffer: &Buffer, topic: String) { 88 fn set_topic(&self, buffer: &Buffer, topic: String) {
33 if let Some(room) = self.servers.find_room(buffer) { 89 if let Some(room) = self.servers.find_room(buffer) {
34 let room = room.room().clone(); 90 let room = room.room().clone();
@@ -53,24 +109,50 @@ impl TopicCommand {
53 109
54impl CommandCallback for TopicCommand { 110impl CommandCallback for TopicCommand {
55 fn callback(&mut self, _: &Weechat, buffer: &Buffer, arguments: Args) { 111 fn callback(&mut self, _: &Weechat, buffer: &Buffer, arguments: Args) {
56 let parser = Argparse::new("topic") 112 parse_and_run(Self::parser(), arguments, |matches| match Self::action(
57 .about("Set or clear the current Matrix room topic.") 113 matches,
58 .settings(&[ 114 ) {
59 ArgParseSettings::DisableHelpFlags, 115 TopicAction::Show => self.show_topic(buffer),
60 ArgParseSettings::DisableVersion, 116 TopicAction::Clear => self.set_topic(buffer, String::new()),
61 ]) 117 TopicAction::Set(topic) => self.set_topic(buffer, topic),
62 .arg( 118 });
63 Arg::with_name("topic") 119 }
64 .multiple(true) 120}
65 .allow_hyphen_values(true),
66 );
67 121
68 parse_and_run(parser, arguments, |matches| { 122#[cfg(test)]
69 let topic = matches 123mod tests {
70 .values_of("topic") 124 use super::{TopicAction, TopicCommand};
71 .map(|t| t.collect::<Vec<_>>().join(" "));
72 125
73 self.set_topic(buffer, topic.unwrap_or_default()); 126 fn parse(args: &[&str]) -> TopicAction {
74 }); 127 let matches = TopicCommand::parser()
128 .get_matches_from_safe(args)
129 .expect("topic arguments should parse");
130
131 TopicCommand::action(&matches)
132 }
133
134 #[test]
135 fn omitted_topic_shows_current_topic() {
136 assert_eq!(TopicAction::Show, parse(&["topic"]));
137 }
138
139 #[test]
140 fn clear_flag_explicitly_clears_topic() {
141 assert_eq!(TopicAction::Clear, parse(&["topic", "--clear"]));
142 }
143
144 #[test]
145 fn clear_flag_rejects_topic_text() {
146 assert!(TopicCommand::parser()
147 .get_matches_from_safe(&["topic", "--clear", "not empty"])
148 .is_err());
149 }
150
151 #[test]
152 fn non_empty_topic_selects_set_action() {
153 assert_eq!(
154 TopicAction::Set("new room topic".to_owned()),
155 parse(&["topic", "new", "room", "topic"])
156 );
75 } 157 }
76} 158}