Commit e24322a553

e24322a55367a01668700b8fc04e0cfcec7b7183

parent: 71960d5ded

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-09-03 07:48 UTC
committer: <strk@kbt.io>

fix: keep /say in Matrix rooms

Layout: unified · split

src/commands/mod.rs +4
@@ -28,6 +28,7 @@ mod page_up;
2828mod part;
2929mod redact;
3030mod reply;
31mod say;
3132mod topic;
3233mod upload;
3334mod verification;
@@ -56,6 +57,7 @@ use page_up::PageUpCommand;
5657use part::PartCommand;
5758use redact::RedactCommand;
5859use reply::ReplyCommand;
60use say::SayCommand;
5961use topic::TopicCommand;
6062use upload::UploadCommand;
6163use verify::VerifyCommand;
@@ -74,6 +76,7 @@ pub struct Commands {
7476 _page_up: CommandRun,
7577 _redact: Command,
7678 _reply: Command,
79 _say: CommandRun,
7780 _topic: Command,
7881 _verification: Command,
7982 _buffer_clear: CommandRun,
@@ -108,6 +111,7 @@ impl Commands {
108111 _page_up: PageUpCommand::create(servers)?,
109112 _redact: RedactCommand::create(servers)?,
110113 _reply: ReplyCommand::create(servers)?,
114 _say: SayCommand::create(servers)?,
111115 _topic: TopicCommand::create(servers)?,
112116 _verification: VerificationCommand::create(servers)?,
113117 _buffer_clear: BufferClearCommand::create(servers)?,
src/commands/say.rs added +68
@@ -0,0 +1,68 @@
1use std::borrow::Cow;
2
3use weechat::{
4 buffer::Buffer,
5 hooks::{CommandRun, CommandRunCallback},
6 ReturnCode, Weechat,
7};
8
9use crate::Servers;
10
11pub struct SayCommand {
12 servers: Servers,
13}
14
15fn say_body(command: &str) -> Option<&str> {
16 command.strip_prefix("/say ").map(str::trim_start)
17}
18
19impl SayCommand {
20 pub fn create(servers: &Servers) -> Result<CommandRun, ()> {
21 CommandRun::new(
22 "/say *",
23 SayCommand {
24 servers: servers.clone(),
25 },
26 )
27 }
28}
29
30impl CommandRunCallback for SayCommand {
31 fn callback(
32 &mut self,
33 _: &Weechat,
34 buffer: &Buffer,
35 command: Cow<str>,
36 ) -> ReturnCode {
37 let Some(room) = self.servers.find_room(buffer) else {
38 return ReturnCode::Ok;
39 };
40
41 let Some(body) = say_body(&command) else {
42 return ReturnCode::Ok;
43 };
44
45 let content = room.text_message_content(buffer, body.to_owned());
46 Weechat::spawn(async move { room.send_message(content).await })
47 .detach();
48
49 ReturnCode::OkEat
50 }
51}
52
53#[cfg(test)]
54mod tests {
55 use super::say_body;
56
57 #[test]
58 fn say_body_preserves_message_text() {
59 assert_eq!(say_body("/say hello Matrix"), Some("hello Matrix"));
60 assert_eq!(say_body("/say leading spaces"), Some("leading spaces"));
61 }
62
63 #[test]
64 fn say_body_excludes_other_commands() {
65 assert_eq!(say_body("/msg hello"), None);
66 assert_eq!(say_body("hello"), None);
67 }
68}
src/room/mod.rs +18 −20
@@ -808,42 +808,40 @@ impl BufferInputCallbackAsync for MatrixRoom {
808808 return;
809809 }
810810
811 let thread_root = buffer
812 .upgrade()
813 .ok()
814 .and_then(|buffer| thread_root_from_buffer(&buffer));
811 if let Ok(buffer) = buffer.upgrade() {
812 let content = self.text_message_content(&buffer, input);
813 self.send_message(content).await;
814 }
815 }
816}
817
818impl MatrixRoom {
819 pub(crate) fn text_message_content(
820 &self,
821 buffer: &Buffer,
822 input: String,
823 ) -> RoomMessageEventContent {
824 let thread_root = thread_root_from_buffer(buffer);
815825 let latest_thread_event = thread_root.as_ref().and_then(|root| {
816826 self.latest_thread_event_ids.borrow().get(root).cloned()
817827 });
818 let content = make_text_message_content(
828
829 make_text_message_content(
819830 input,
820831 self.config.borrow().input().markdown_input(),
821832 thread_root,
822833 latest_thread_event,
823 );
824
825 self.send_message(content).await;
834 )
826835 }
827}
828836
829impl MatrixRoom {
830837 pub(crate) fn mention_message_content(
831838 &self,
832839 buffer: &Buffer,
833840 input: String,
834841 mentioned_user_ids: Vec<OwnedUserId>,
835842 ) -> RoomMessageEventContent {
836 let thread_root = thread_root_from_buffer(buffer);
837 let latest_thread_event = thread_root.as_ref().and_then(|root| {
838 self.latest_thread_event_ids.borrow().get(root).cloned()
839 });
840843 with_mentions(
841 make_text_message_content(
842 input,
843 self.config.borrow().input().markdown_input(),
844 thread_root,
845 latest_thread_event,
846 ),
844 self.text_message_content(buffer, input),
847845 mentioned_user_ids,
848846 )
849847 }