Skip to main content

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                // hypixel whispers
104                if let Some(m) =
105                    regex!(r"^From (?:\[[^\]]+\] )(\w{1,16}): (.+)$").captures(&message)
106                {
107                    return (Some(m[1].to_string()), m[2].to_string());
108                }
109
110                (None, message)
111            }
112            ChatPacket::Player(p) => (
113                // If it's a player chat packet, then the sender and content
114                // are already split for us.
115                Some(p.chat_type.name.to_string()),
116                p.body.content.clone(),
117            ),
118            ChatPacket::Disguised(p) => (
119                // disguised chat packets are basically the same as player chat packets but without
120                // the chat signing things
121                Some(p.chat_type.name.to_string()),
122                p.message.to_string(),
123            ),
124        }
125    }
126
127    /// Get the username of the sender of the message.
128    ///
129    /// If it's not a player-sent chat message or the sender couldn't be
130    /// determined, this will be None.
131    ///
132    /// See [`Self::split_sender_and_content`] for more details about how this
133    /// works.
134    pub fn sender(&self) -> Option<String> {
135        self.split_sender_and_content().0
136    }
137
138    /// Get the UUID of the sender of the message.
139    ///
140    /// If it's not a player-sent chat message, this will be None (this is
141    /// sometimes the case when a server uses a plugin to modify chat
142    /// messages).
143    pub fn sender_uuid(&self) -> Option<Uuid> {
144        match self {
145            ChatPacket::System(_) => None,
146            ChatPacket::Player(m) => Some(m.sender),
147            ChatPacket::Disguised(_) => None,
148        }
149    }
150
151    /// Get the content part of the message as a string.
152    ///
153    /// This does not preserve formatting codes. If it's not a player-sent chat
154    /// message or the sender couldn't be determined, this will contain the
155    /// entire message.
156    pub fn content(&self) -> String {
157        self.split_sender_and_content().1
158    }
159
160    /// Create a new `ChatPacket` from a string. This is meant to be used as a
161    /// convenience function for testing.
162    pub fn new(message: &str) -> Self {
163        ChatPacket::System(Arc::new(ClientboundSystemChat {
164            content: FormattedText::from(message),
165            overlay: false,
166        }))
167    }
168
169    /// Whether this message is an incoming whisper message (i.e. someone else
170    /// messaged the bot with /msg).
171    ///
172    /// This is not guaranteed to work correctly on custom servers.
173    pub fn is_whisper(&self) -> bool {
174        match self {
175            ChatPacket::System(p) => {
176                let message = p.content.to_string();
177                if p.overlay {
178                    return false;
179                }
180                if regex!(r"^(-> me|\w{1,16} whispers: )").is_match(&message) {
181                    return true;
182                }
183                // hypixel
184                if regex!(r"^From (?:\[[^\]]+\] )?\w{1,16}: ").is_match(&message) {
185                    return true;
186                }
187
188                false
189            }
190            _ => match self.message() {
191                FormattedText::Text(_) => false,
192                FormattedText::Translatable(t) => t.key == "commands.message.display.incoming",
193            },
194        }
195    }
196}
197
198/// A client received a chat message packet.
199#[derive(Clone, Debug, Message)]
200pub struct ChatReceivedEvent {
201    pub entity: Entity,
202    pub packet: ChatPacket,
203}
204
205/// Send a chat message (or command, if it starts with a slash) to the server.
206#[derive(Message)]
207pub struct SendChatEvent {
208    pub entity: Entity,
209    pub content: String,
210}
211
212pub fn handle_send_chat_event(
213    mut events: MessageReader<SendChatEvent>,
214    mut send_chat_kind_events: MessageWriter<SendChatKindEvent>,
215) {
216    for event in events.read() {
217        if event.content.starts_with('/') {
218            send_chat_kind_events.write(SendChatKindEvent {
219                entity: event.entity,
220                content: event.content[1..].to_string(),
221                kind: ChatKind::Command,
222            });
223        } else {
224            send_chat_kind_events.write(SendChatKindEvent {
225                entity: event.entity,
226                content: event.content.clone(),
227                kind: ChatKind::Message,
228            });
229        }
230    }
231}
232
233/// A kind of chat packet, either a chat message or a command.
234pub enum ChatKind {
235    Message,
236    Command,
237}
238
239// TODO
240// MessageSigner, ChatMessageContent, LastSeenMessages
241// fn sign_message() -> MessageSignature {
242//     MessageSignature::default()
243// }