Commit 65cc837eb9

65cc837eb9a7e8301ce99cb60b255c747cdb8faa

parent: 0a3b576412

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-06-23 16:18 UTC
committer: <strk@kbt.io>

feat: add matrix topic command

Layout: unified · split

src/commands/mod.rs +4
@@ -19,6 +19,7 @@ mod names;
1919mod page_up;
2020mod part;
2121mod redact;
22mod topic;
2223mod upload;
2324mod verification;
2425
@@ -34,6 +35,7 @@ use names::NamesCommand;
3435use page_up::PageUpCommand;
3536use part::PartCommand;
3637use redact::RedactCommand;
38use topic::TopicCommand;
3739use upload::UploadCommand;
3840
3941pub struct Commands {
@@ -43,6 +45,7 @@ pub struct Commands {
4345 _invite: Command,
4446 _page_up: CommandRun,
4547 _redact: Command,
48 _topic: Command,
4649 _verification: Command,
4750 _buffer_clear: CommandRun,
4851 _join: CommandRun,
@@ -64,6 +67,7 @@ impl Commands {
6467 _keys: KeysCommand::create(servers)?,
6568 _page_up: PageUpCommand::create(servers)?,
6669 _redact: RedactCommand::create(servers)?,
70 _topic: TopicCommand::create(servers)?,
6771 _verification: VerificationCommand::create(servers)?,
6872 _buffer_clear: BufferClearCommand::create(servers)?,
6973 _join: JoinCommand::create(servers)?,
src/commands/topic.rs added +74
@@ -0,0 +1,74 @@
1use clap::{App as Argparse, AppSettings as ArgParseSettings, Arg};
2use weechat::{
3 buffer::Buffer,
4 hooks::{Command, CommandCallback, CommandSettings},
5 Args, Prefix, Weechat,
6};
7
8use super::parse_and_run;
9use crate::Servers;
10
11pub struct TopicCommand {
12 servers: Servers,
13}
14
15impl 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
52impl 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}