azalea_client/plugins/chat/mod.rs
1//! Implementations of chat-related features.
2
3pub mod handler;
4
5use std::sync::Arc;
6
7use azalea_chat::FormattedText;
8use azalea_protocol::packets::game::{
9 c_disguised_chat::ClientboundDisguisedChat, c_player_chat::ClientboundPlayerChat,
10 c_system_chat::ClientboundSystemChat,
11};
12use bevy_app::{App, Plugin, Update};
13use bevy_ecs::prelude::*;
14use handler::{SendChatKindEvent, handle_send_chat_kind_event};
15use uuid::Uuid;
16
17pub struct ChatPlugin;
18impl Plugin for ChatPlugin {
19 fn build(&self, app: &mut App) {
20 app.add_message::<SendChatEvent>()
21 .add_message::<SendChatKindEvent>()
22 .add_message::<ChatReceivedEvent>()
23 .add_systems(
24 Update,
25 (handle_send_chat_event, handle_send_chat_kind_event).chain(),
26 );
27 }
28}
29
30/// A chat packet, either a system message or a chat message.
31#[derive(Clone, Debug, PartialEq)]
32pub enum ChatPacket {
33 System(Arc<ClientboundSystemChat>),
34 Player(Arc<ClientboundPlayerChat>),
35 Disguised(Arc<ClientboundDisguisedChat>),
36}
37
38macro_rules! regex {
39 ($re:literal $(,)?) => {{
40 static RE: std::sync::LazyLock<regex::Regex> =
41 std::sync::LazyLock::new(|| regex::Regex::new($re).unwrap());
42 &RE
43 }};
44}
45
46impl ChatPacket {
47 /// Get the message shown in chat for this packet.
48 ///
49 /// See [`Self::split_sender_and_content`] for more details about how this
50 /// works.
51 pub fn message(&self) -> FormattedText {
52 match self {
53 ChatPacket::System(p) => p.content.clone(),
54 ChatPacket::Player(p) => p.message(),
55 ChatPacket::Disguised(p) => p.message(),
56 }
57 }
58
59 /// A convenience function to determine the username of the sender and
60 /// the content of a chat message.
61 ///
62 /// This does not preserve formatting codes.
63 ///
64 /// This function uses a few checks to attempt to split the chat message,
65 /// and is intended to work on most servers. It won't work for every server
66 /// though, so in certain cases you may have to reimplement this yourself.
67 ///
68 /// If it's not a player-sent chat message or the sender couldn't be
69 /// determined, the username part will be None.
70 ///
71 /// Also see [`Self::sender`] and [`Self::content`] if you only need one of
72 /// the parts.
73 pub fn split_sender_and_content(&self) -> (Option<String>, String) {
74 match self {
75 ChatPacket::System(p) => {
76 let message = p.content.to_string();
77 // overlay messages aren't in chat
78 if p.overlay {
79 return (None, message);
80 }
81
82 // it's a system message, so we'll have to match the content with regex
83
84 // username surrounded by angle brackets (vanilla-like chat), and allow username
85 // prefixes like [Owner]
86 if let Some(m) = regex!(r"^<(?:\[[^\]]+?\] )?(\w{1,16})> (.+)$").captures(&message)
87 {
88 return (Some(m[1].to_string()), m[2].to_string());
89 }
90 // username surrounded by square brackets (essentials whispers, vanilla-like
91 // /say), and allow username prefixes
92 if let Some(m) =
93 regex!(r"^\[(?:\[[^\]]+?\] )?(\w{1,16})(?: -> me)?\] (.+)$").captures(&message)
94 {
95 return (Some(m[1].to_string()), m[2].to_string());
96 }
97 // username without angle brackets (2b2t whispers, vanilla-like whispers)
98 if let Some(m) =
99 regex!(r"^(\w{1,16}) whispers(?: to you)?: (.+)$").captures(&message)
100 {
101 return (Some(m[1].to_string()), m[2].to_string());
102 }
103
104 (None, message)
105 }
106 ChatPacket::Player(p) => (
107 // If it's a player chat packet, then the sender and content
108 // are already split for us.
109 Some(p.chat_type.name.to_string()),
110 p.body.content.clone(),
111 ),
112 ChatPacket::Disguised(p) => (
113 // disguised chat packets are basically the same as player chat packets but without
114 // the chat signing things
115 Some(p.chat_type.name.to_string()),
116 p.message.to_string(),
117 ),
118 }
119 }
120
121 /// Get the username of the sender of the message.
122 ///
123 /// If it's not a player-sent chat message or the sender couldn't be
124 /// determined, this will be None.
125 ///
126 /// See [`Self::split_sender_and_content`] for more details about how this
127 /// works.
128 pub fn sender(&self) -> Option<String> {
129 self.split_sender_and_content().0
130 }
131
132 /// Get the UUID of the sender of the message.
133 ///
134 /// If it's not a player-sent chat message, this will be None (this is
135 /// sometimes the case when a server uses a plugin to modify chat
136 /// messages).
137 pub fn sender_uuid(&self) -> Option<Uuid> {
138 match self {
139 ChatPacket::System(_) => None,
140 ChatPacket::Player(m) => Some(m.sender),
141 ChatPacket::Disguised(_) => None,
142 }
143 }
144
145 /// Get the content part of the message as a string.
146 ///
147 /// This does not preserve formatting codes. If it's not a player-sent chat
148 /// message or the sender couldn't be determined, this will contain the
149 /// entire message.
150 pub fn content(&self) -> String {
151 self.split_sender_and_content().1
152 }
153
154 /// Create a new `ChatPacket` from a string. This is meant to be used as a
155 /// convenience function for testing.
156 pub fn new(message: &str) -> Self {
157 ChatPacket::System(Arc::new(ClientboundSystemChat {
158 content: FormattedText::from(message),
159 overlay: false,
160 }))
161 }
162
163 /// Whether this message is an incoming whisper message (i.e. someone else
164 /// messaged the bot with /msg).
165 ///
166 /// This is not guaranteed to work correctly on custom servers.
167 pub fn is_whisper(&self) -> bool {
168 match self {
169 ChatPacket::System(p) => {
170 let message = p.content.to_string();
171 if p.overlay {
172 return false;
173 }
174 if regex!("^(-> me|[a-zA-Z_0-9]{1,16} whispers: )").is_match(&message) {
175 return true;
176 }
177
178 false
179 }
180 _ => match self.message() {
181 FormattedText::Text(_) => false,
182 FormattedText::Translatable(t) => t.key == "commands.message.display.incoming",
183 },
184 }
185 }
186}
187
188/// A client received a chat message packet.
189#[derive(Clone, Debug, Message)]
190pub struct ChatReceivedEvent {
191 pub entity: Entity,
192 pub packet: ChatPacket,
193}
194
195/// Send a chat message (or command, if it starts with a slash) to the server.
196#[derive(Message)]
197pub struct SendChatEvent {
198 pub entity: Entity,
199 pub content: String,
200}
201
202pub fn handle_send_chat_event(
203 mut events: MessageReader<SendChatEvent>,
204 mut send_chat_kind_events: MessageWriter<SendChatKindEvent>,
205) {
206 for event in events.read() {
207 if event.content.starts_with('/') {
208 send_chat_kind_events.write(SendChatKindEvent {
209 entity: event.entity,
210 content: event.content[1..].to_string(),
211 kind: ChatKind::Command,
212 });
213 } else {
214 send_chat_kind_events.write(SendChatKindEvent {
215 entity: event.entity,
216 content: event.content.clone(),
217 kind: ChatKind::Message,
218 });
219 }
220 }
221}
222
223/// A kind of chat packet, either a chat message or a command.
224pub enum ChatKind {
225 Message,
226 Command,
227}
228
229// TODO
230// MessageSigner, ChatMessageContent, LastSeenMessages
231// fn sign_message() -> MessageSignature {
232// MessageSignature::default()
233// }