aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs
blob: fedc81df894660f64ca95edd43cd1489fe18b0f8 (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
use azalea_buf::McBuf;
use azalea_chat::{
    translatable_component::{StringOrComponent, TranslatableComponent},
    Component,
};
use azalea_core::BitSet;
use azalea_crypto::{MessageSignature, SignedMessageHeader};
use azalea_protocol_macros::ClientboundGamePacket;
use uuid::Uuid;

#[derive(Clone, Debug, McBuf, ClientboundGamePacket)]
pub struct ClientboundPlayerChatPacket {
    pub message: PlayerChatMessage,
    pub chat_type: ChatTypeBound,
}

#[derive(Copy, Clone, Debug, McBuf, PartialEq, Eq)]
pub enum ChatType {
    Chat = 0,
    SayCommand = 1,
    MsgCommandIncoming = 2,
    MsgCommandOutgoing = 3,
    TeamMsgCommandIncoming = 4,
    TeamMsgCommandOutgoing = 5,
    EmoteCommand = 6,
}

#[derive(Clone, Debug, McBuf)]
pub struct ChatTypeBound {
    pub chat_type: ChatType,
    pub name: Component,
    pub target_name: Option<Component>,
}

#[derive(Clone, Debug, McBuf)]
pub struct PlayerChatMessage {
    pub signed_header: SignedMessageHeader,
    pub header_signature: MessageSignature,
    pub signed_body: SignedMessageBody,
    pub unsigned_content: Option<Component>,
    pub filter_mask: FilterMask,
}

#[derive(Clone, Debug, McBuf)]
pub struct SignedMessageBody {
    pub content: ChatMessageContent,
    pub timestamp: u64,
    pub salt: u64,
    pub last_seen: Vec<LastSeenMessagesEntry>,
}

impl PlayerChatMessage {
    /// Returns the content of the message. If you want to get the Component
    /// for the whole message including the sender part, use
    /// [`ClientboundPlayerChatPacket::message`].
    pub fn content(&self, only_secure_chat: bool) -> Component {
        if only_secure_chat {
            return self
                .signed_body
                .content
                .decorated
                .clone()
                .unwrap_or_else(|| Component::from(self.signed_body.content.plain.clone()));
        }
        self.unsigned_content
            .clone()
            .unwrap_or_else(|| self.content(true))
    }
}

impl ClientboundPlayerChatPacket {
    /// Get the full message, including the sender part.
    pub fn message(&self, only_secure_chat: bool) -> Component {
        let sender = self.chat_type.name.clone();
        let content = self.message.content(only_secure_chat);
        let target = self.chat_type.target_name.clone();

        let translation_key = self.chat_type.chat_type.chat_translation_key();

        let mut args = vec![
            StringOrComponent::Component(sender),
            StringOrComponent::Component(content),
        ];
        if let Some(target) = target {
            args.push(StringOrComponent::Component(target));
        }

        let component = TranslatableComponent::new(translation_key.to_string(), args);

        Component::Translatable(component)
    }
}

impl ChatType {
    pub fn chat_translation_key(&self) -> &'static str {
        match self {
            ChatType::Chat => "chat.type.text",
            ChatType::SayCommand => "chat.type.announcement",
            ChatType::MsgCommandIncoming => "commands.message.display.incoming",
            ChatType::MsgCommandOutgoing => "commands.message.display.outgoing",
            ChatType::TeamMsgCommandIncoming => "chat.type.team.text",
            ChatType::TeamMsgCommandOutgoing => "chat.type.team.sent",
            ChatType::EmoteCommand => "chat.type.emote",
        }
    }

    pub fn narrator_translation_key(&self) -> &'static str {
        match self {
            ChatType::Chat => "chat.type.text.narrate",
            ChatType::SayCommand => "chat.type.text.narrate",
            ChatType::MsgCommandIncoming => "chat.type.text.narrate",
            ChatType::MsgCommandOutgoing => "chat.type.text.narrate",
            ChatType::TeamMsgCommandIncoming => "chat.type.text.narrate",
            ChatType::TeamMsgCommandOutgoing => "chat.type.text.narrate",
            ChatType::EmoteCommand => "chat.type.emote",
        }
    }
}

#[derive(Clone, Debug, McBuf)]
pub struct LastSeenMessagesEntry {
    pub profile_id: Uuid,
    pub last_signature: MessageSignature,
}

#[derive(Clone, Debug, McBuf, Default)]
pub struct LastSeenMessagesUpdate {
    pub last_seen: Vec<LastSeenMessagesEntry>,
    pub last_received: Option<LastSeenMessagesEntry>,
}

#[derive(Clone, Debug, McBuf)]
pub struct ChatMessageContent {
    pub plain: String,
    /// Only sent if the decorated message is different than the plain.
    pub decorated: Option<Component>,
}

#[derive(Clone, Debug, McBuf)]
pub enum FilterMask {
    PassThrough,
    FullyFiltered,
    PartiallyFiltered(BitSet),
}

#[cfg(test)]
mod tests {
    use super::*;
    use azalea_buf::McBufReadable;
    use std::io::Cursor;

    #[test]
    fn test_chat_type() {
        let chat_type_enum = ChatType::read_from(&mut Cursor::new(&[0x06])).unwrap();
        assert_eq!(chat_type_enum, ChatType::EmoteCommand);
        assert_eq!(
            ChatType::read_from(&mut Cursor::new(&[0x07])).unwrap(),
            ChatType::Chat
        );
    }
}