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
use azalea_chat::FormattedText;
use azalea_protocol::packets::game::{
clientbound_player_chat_packet::ClientboundPlayerChatPacket,
clientbound_system_chat_packet::ClientboundSystemChatPacket,
serverbound_chat_command_packet::ServerboundChatCommandPacket,
serverbound_chat_packet::{LastSeenMessagesUpdate, ServerboundChatPacket},
};
use std::{
sync::Arc,
time::{SystemTime, UNIX_EPOCH},
};
use uuid::Uuid;
use crate::client::Client;
#[derive(Debug, Clone, PartialEq)]
pub enum ChatPacket {
System(Arc<ClientboundSystemChatPacket>),
Player(Arc<ClientboundPlayerChatPacket>),
}
macro_rules! regex {
($re:literal $(,)?) => {{
static RE: once_cell::sync::OnceCell<regex::Regex> = once_cell::sync::OnceCell::new();
RE.get_or_init(|| regex::Regex::new($re).unwrap())
}};
}
impl ChatPacket {
pub fn message(&self) -> FormattedText {
match self {
ChatPacket::System(p) => p.content.clone(),
ChatPacket::Player(p) => p.message(),
}
}
pub fn split_sender_and_content(&self) -> (Option<String>, String) {
match self {
ChatPacket::Player(p) => (
Some(p.chat_type.name.to_string()),
p.body.content.clone(),
),
ChatPacket::System(p) => {
let message = p.content.to_string();
if p.overlay {
return (None, message);
}
if let Some(m) = regex!("^<([a-zA-Z_0-9]{1,16})> (.+)$").captures(&message) {
return (Some(m[1].to_string()), m[2].to_string());
}
(None, message)
}
}
}
pub fn username(&self) -> Option<String> {
self.split_sender_and_content().0
}
pub fn uuid(&self) -> Option<Uuid> {
match self {
ChatPacket::System(_) => None,
ChatPacket::Player(m) => Some(m.sender),
}
}
pub fn content(&self) -> String {
self.split_sender_and_content().1
}
pub fn new(message: &str) -> Self {
ChatPacket::System(Arc::new(ClientboundSystemChatPacket {
content: FormattedText::from(message),
overlay: false,
}))
}
}
impl Client {
pub fn send_chat_packet(&self, message: &str) {
let packet = ServerboundChatPacket {
message: message.to_string(),
timestamp: SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time shouldn't be before epoch")
.as_millis()
.try_into()
.expect("Instant should fit into a u64"),
salt: azalea_crypto::make_salt(),
signature: None,
last_seen_messages: LastSeenMessagesUpdate::default(),
}
.get();
self.write_packet(packet);
}
pub fn send_command_packet(&self, command: &str) {
let packet = ServerboundChatCommandPacket {
command: command.to_string(),
timestamp: SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time shouldn't be before epoch")
.as_millis()
.try_into()
.expect("Instant should fit into a u64"),
salt: azalea_crypto::make_salt(),
argument_signatures: vec![],
last_seen_messages: LastSeenMessagesUpdate::default(),
}
.get();
self.write_packet(packet);
}
pub fn chat(&self, message: &str) {
if let Some(command) = message.strip_prefix('/') {
self.send_command_packet(command);
} else {
self.send_chat_packet(message);
}
}
}