Commit 80ccd863a0

80ccd863a02df7bcb2b812be3c1391401ec709e5

parent: 8b9feba502

Unsigned

Strk <strk@strk.net> · 2026-07-27 11:48 UTC
committer: <strk@kbt.io>

feat: add /nick command to change display name

Closes #235

Assisted-By: big-pickle [via opencode 1.18.4]

Layout: unified · split

src/commands/mod.rs +4
@@ -18,6 +18,7 @@ mod me;
18mod media; 18mod media;
19mod moderation; 19mod moderation;
20mod names; 20mod names;
21mod nick;
21mod page_up; 22mod page_up;
22mod part; 23mod part;
23mod redact; 24mod redact;
@@ -36,6 +37,7 @@ use me::MeCommand;
36use media::MediaCommand; 37use media::MediaCommand;
37use moderation::ModerationCommand; 38use moderation::ModerationCommand;
38use names::NamesCommand; 39use names::NamesCommand;
40use nick::NickCommand;
39use page_up::PageUpCommand; 41use page_up::PageUpCommand;
40use part::PartCommand; 42use part::PartCommand;
41use redact::RedactCommand; 43use redact::RedactCommand;
@@ -61,6 +63,7 @@ pub struct Commands {
61 _part: CommandRun, 63 _part: CommandRun,
62 _names: CommandRun, 64 _names: CommandRun,
63 _unban: Command, 65 _unban: Command,
66 _nick: CommandRun,
64} 67}
65 68
66impl Commands { 69impl Commands {
@@ -87,6 +90,7 @@ impl Commands {
87 _part: PartCommand::create(servers)?, 90 _part: PartCommand::create(servers)?,
88 _names: NamesCommand::create(servers)?, 91 _names: NamesCommand::create(servers)?,
89 _unban: ModerationCommand::unban(servers)?, 92 _unban: ModerationCommand::unban(servers)?,
93 _nick: NickCommand::create(servers)?,
90 }) 94 })
91 } 95 }
92} 96}
src/commands/nick.rs added +80
@@ -0,0 +1,80 @@
1use std::borrow::Cow;
2
3use weechat::{
4 buffer::Buffer,
5 hooks::{CommandRun, CommandRunCallback},
6 ReturnCode, Weechat,
7};
8
9use crate::{Servers, PLUGIN_NAME};
10
11pub struct NickCommand {
12 servers: Servers,
13}
14
15impl NickCommand {
16 pub fn create(servers: &Servers) -> Result<CommandRun, ()> {
17 CommandRun::new(
18 "/nick",
19 NickCommand {
20 servers: servers.clone(),
21 },
22 )
23 }
24}
25
26impl CommandRunCallback for NickCommand {
27 fn callback(
28 &mut self,
29 _: &Weechat,
30 buffer: &Buffer,
31 cmd: Cow<str>,
32 ) -> ReturnCode {
33 let new_nick = cmd.strip_prefix("/nick ").map(|s| s.trim());
34
35 if let Some(server) = self.servers.find_server(&buffer) {
36 match new_nick {
37 Some(name) if !name.is_empty() => {
38 let name = name.to_owned();
39 Weechat::spawn(async move {
40 server.set_display_name(Some(&name)).await;
41 })
42 .detach();
43 }
44 None => {
45 // /nick with no arguments: show current display name
46 Weechat::spawn(async move {
47 match server.get_display_name().await {
48 Some(name) => {
49 Weechat::print(&format!(
50 "{}: Current display name: {}",
51 PLUGIN_NAME, name
52 ));
53 }
54 None => {
55 Weechat::print(&format!(
56 "{}: No display name set.",
57 PLUGIN_NAME
58 ));
59 }
60 }
61 })
62 .detach();
63 }
64 _ => {
65 Weechat::print(&format!(
66 "{}Usage: /nick <new-display-name>",
67 Weechat::prefix(weechat::Prefix::Error),
68 ));
69 }
70 }
71 } else {
72 Weechat::print(&format!(
73 "{}The /nick command must be run in a Matrix buffer.",
74 Weechat::prefix(weechat::Prefix::Error),
75 ));
76 }
77
78 ReturnCode::Ok
79 }
80}
src/server.rs +17
@@ -726,6 +726,23 @@ impl InnerServer {
726 self.settings.borrow().password.clone() 726 self.settings.borrow().password.clone()
727 } 727 }
728 728
729 /// Set the display name for this account on the homeserver.
730 pub async fn set_display_name(&self, name: Option<&str>) {
731 let Some(client) = self.get_client() else {
732 self.print_error("Not connected to a server");
733 return;
734 };
735 if let Err(e) = client.account().set_display_name(name).await {
736 self.print_error(&format!("Failed to set display name: {}", e));
737 }
738 }
739
740 /// Get the current display name for this account from the homeserver.
741 pub async fn get_display_name(&self) -> Option<String> {
742 let client = self.get_client()?;
743 client.account().get_display_name().await.ok().flatten()
744 }
745
729 pub async fn restore_room_by_id(&self, room_id: OwnedRoomId) { 746 pub async fn restore_room_by_id(&self, room_id: OwnedRoomId) {
730 if self.rooms.borrow().contains_key(&room_id) { 747 if self.rooms.borrow().contains_key(&room_id) {
731 return; 748 return;