aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src/plugins/chat/mod.rs
blob: bd90a8d6791ce419c0399d4fa639088fac59302f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//! Implementations of chat-related features.

pub mod handler;

use std::sync::Arc;

use azalea_chat::FormattedText;
use azalea_protocol::packets::game::{
    c_disguised_chat::ClientboundDisguisedChat, c_player_chat::ClientboundPlayerChat,
    c_system_chat::ClientboundSystemChat,
};
use bevy_app::{App, Plugin, Update};
use bevy_ecs::prelude::*;
use handler::{SendChatKindEvent, handle_send_chat_kind_event};
use uuid::Uuid;

use crate::client::Client;

pub struct ChatPlugin;
impl Plugin for ChatPlugin {
    fn build(&self, app: &mut App) {
        app.add_message::<SendChatEvent>()
            .add_message::<SendChatKindEvent>()
            .add_message::<ChatReceivedEvent>()
            .add_systems(
                Update,
                (handle_send_chat_event, handle_send_chat_kind_event).chain(),
            );
    }
}

/// A chat packet, either a system message or a chat message.
#[derive(Clone, Debug, PartialEq)]
pub enum ChatPacket {
    System(Arc<ClientboundSystemChat>),
    Player(Arc<ClientboundPlayerChat>),
    Disguised(Arc<ClientboundDisguisedChat>),
}

macro_rules! regex {
    ($re:literal $(,)?) => {{
        static RE: std::sync::LazyLock<regex::Regex> =
            std::sync::LazyLock::new(|| regex::Regex::new($re).unwrap());
        &RE
    }};
}

impl ChatPacket {
    /// Get the message shown in chat for this packet.
    ///
    /// See [`Self::split_sender_and_content`] for more details about how this
    /// works.
    pub fn message(&self) -> FormattedText {
        match self {
            ChatPacket::System(p) => p.content.clone(),
            ChatPacket::Player(p) => p.message(),
            ChatPacket::Disguised(p) => p.message(),
        }
    }

    /// A convenience function to determine the username of the sender and
    /// the content of a chat message.
    ///
    /// This does not preserve formatting codes.
    ///
    /// This function uses a few checks to attempt to split the chat message,
    /// and is intended to work on most servers. It won't work for every server
    /// though, so in certain cases you may have to reimplement this yourself.
    ///
    /// If it's not a player-sent chat message or the sender couldn't be
    /// determined, the username part will be None.
    ///
    /// Also see [`Self::sender`] and [`Self::content`] if you only need one of
    /// the parts.
    pub fn split_sender_and_content(&self) -> (Option<String>, String) {
        match self {
            ChatPacket::System(p) => {
                let message = p.content.to_string();
                // overlay messages aren't in chat
                if p.overlay {
                    return (None, message);
                }

                // it's a system message, so we'll have to match the content with regex

                // username surrounded by angle brackets (vanilla-like chat), and allow username
                // prefixes like [Owner]
                if let Some(m) = regex!(r"^<(?:\[[^\]]+?\] )?(\w{1,16})> (.+)$").captures(&message)
                {
                    return (Some(m[1].to_string()), m[2].to_string());
                }
                // username surrounded by square brackets (essentials whispers, vanilla-like
                // /say), and allow username prefixes
                if let Some(m) =
                    regex!(r"^\[(?:\[[^\]]+?\] )?(\w{1,16})(?: -> me)?\] (.+)$").captures(&message)
                {
                    return (Some(m[1].to_string()), m[2].to_string());
                }
                // username without angle brackets (2b2t whispers, vanilla-like whispers)
                if let Some(m) =
                    regex!(r"^(\w{1,16}) whispers(?: to you)?: (.+)$").captures(&message)
                {
                    return (Some(m[1].to_string()), m[2].to_string());
                }

                (None, message)
            }
            ChatPacket::Player(p) => (
                // If it's a player chat packet, then the sender and content
                // are already split for us.
                Some(p.chat_type.name.to_string()),
                p.body.content.clone(),
            ),
            ChatPacket::Disguised(p) => (
                // disguised chat packets are basically the same as player chat packets but without
                // the chat signing things
                Some(p.chat_type.name.to_string()),
                p.message.to_string(),
            ),
        }
    }

    /// Get the username of the sender of the message.
    ///
    /// If it's not a player-sent chat message or the sender couldn't be
    /// determined, this will be None.
    ///
    /// See [`Self::split_sender_and_content`] for more details about how this
    /// works.
    pub fn sender(&self) -> Option<String> {
        self.split_sender_and_content().0
    }

    /// Get the UUID of the sender of the message.
    ///
    /// If it's not a player-sent chat message, this will be None (this is
    /// sometimes the case when a server uses a plugin to modify chat
    /// messages).
    pub fn sender_uuid(&self) -> Option<Uuid> {
        match self {
            ChatPacket::System(_) => None,
            ChatPacket::Player(m) => Some(m.sender),
            ChatPacket::Disguised(_) => None,
        }
    }

    /// Get the content part of the message as a string.
    ///
    /// This does not preserve formatting codes. If it's not a player-sent chat
    /// message or the sender couldn't be determined, this will contain the
    /// entire message.
    pub fn content(&self) -> String {
        self.split_sender_and_content().1
    }

    /// Create a new `ChatPacket` from a string. This is meant to be used as a
    /// convenience function for testing.
    pub fn new(message: &str) -> Self {
        ChatPacket::System(Arc::new(ClientboundSystemChat {
            content: FormattedText::from(message),
            overlay: false,
        }))
    }

    /// Whether this message is an incoming whisper message (i.e. someone else
    /// messaged the bot with /msg).
    ///
    /// This is not guaranteed to work correctly on custom servers.
    pub fn is_whisper(&self) -> bool {
        match self {
            ChatPacket::System(p) => {
                let message = p.content.to_string();
                if p.overlay {
                    return false;
                }
                if regex!("^(-> me|[a-zA-Z_0-9]{1,16} whispers: )").is_match(&message) {
                    return true;
                }

                false
            }
            _ => match self.message() {
                FormattedText::Text(_) => false,
                FormattedText::Translatable(t) => t.key == "commands.message.display.incoming",
            },
        }
    }
}

impl Client {
    /// Send a chat message to the server.
    ///
    /// This only sends the chat packet and not the command packet, which means
    /// on some servers you can use this to send chat messages that start
    /// with a `/`. The [`Client::chat`] function handles checking whether
    /// the message is a command and using the proper packet for you, so you
    /// should use that instead.
    pub fn write_chat_packet(&self, message: &str) {
        self.ecs.lock().write_message(SendChatKindEvent {
            entity: self.entity,
            content: message.to_owned(),
            kind: ChatKind::Message,
        });
    }

    /// Send a command packet to the server. The `command` argument should not
    /// include the slash at the front.
    ///
    /// You can also just use [`Client::chat`] and start your message with a `/`
    /// to send a command.
    pub fn write_command_packet(&self, command: &str) {
        self.ecs.lock().write_message(SendChatKindEvent {
            entity: self.entity,
            content: command.to_owned(),
            kind: ChatKind::Command,
        });
    }

    /// Send a message in chat.
    ///
    /// ```rust,no_run
    /// # use azalea_client::Client;
    /// # async fn example(bot: Client) -> anyhow::Result<()> {
    /// bot.chat("Hello, world!");
    /// # Ok(())
    /// # }
    /// ```
    pub fn chat(&self, content: impl Into<String>) {
        self.ecs.lock().write_message(SendChatEvent {
            entity: self.entity,
            content: content.into(),
        });
    }
}

/// A client received a chat message packet.
#[derive(Clone, Debug, Message)]
pub struct ChatReceivedEvent {
    pub entity: Entity,
    pub packet: ChatPacket,
}

/// Send a chat message (or command, if it starts with a slash) to the server.
#[derive(Message)]
pub struct SendChatEvent {
    pub entity: Entity,
    pub content: String,
}

pub fn handle_send_chat_event(
    mut events: MessageReader<SendChatEvent>,
    mut send_chat_kind_events: MessageWriter<SendChatKindEvent>,
) {
    for event in events.read() {
        if event.content.starts_with('/') {
            send_chat_kind_events.write(SendChatKindEvent {
                entity: event.entity,
                content: event.content[1..].to_string(),
                kind: ChatKind::Command,
            });
        } else {
            send_chat_kind_events.write(SendChatKindEvent {
                entity: event.entity,
                content: event.content.clone(),
                kind: ChatKind::Message,
            });
        }
    }
}

/// A kind of chat packet, either a chat message or a command.
pub enum ChatKind {
    Message,
    Command,
}

// TODO
// MessageSigner, ChatMessageContent, LastSeenMessages
// fn sign_message() -> MessageSignature {
//     MessageSignature::default()
// }