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;
19mod page_up; 19mod page_up;
20mod part; 20mod part;
21mod redact; 21mod redact;
22mod topic;
22mod upload; 23mod upload;
23mod verification; 24mod verification;
24 25
@@ -34,6 +35,7 @@ use names::NamesCommand;
34use page_up::PageUpCommand; 35use page_up::PageUpCommand;
35use part::PartCommand; 36use part::PartCommand;
36use redact::RedactCommand; 37use redact::RedactCommand;
38use topic::TopicCommand;
37use upload::UploadCommand; 39use upload::UploadCommand;
38 40
39pub struct Commands { 41pub 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 @@
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}