aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src/plugins/chat/handler.rs
blob: a289eb14d8788e7bb8ce83214cabf3f30caa064a (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
use std::time::{SystemTime, UNIX_EPOCH};

use azalea_crypto::SignChatMessageOptions;
use azalea_protocol::packets::{
    Packet,
    game::{ServerboundChat, ServerboundChatCommand, s_chat::LastSeenMessagesUpdate},
};
use bevy_ecs::prelude::*;

use super::ChatKind;
use crate::{Account, chat_signing::ChatSigningSession, packet::game::SendPacketEvent};

/// Send a chat packet to the server of a specific kind (chat message or
/// command). Usually you just want [`SendChatEvent`] instead.
///
/// Usually setting the kind to `Message` will make it send a chat message even
/// if it starts with a slash, but some server implementations will always do a
/// command if it starts with a slash.
///
/// If you're wondering why this isn't two separate events, it's so ordering is
/// preserved if multiple chat messages and commands are sent at the same time.
///
/// [`SendChatEvent`]: super::SendChatEvent
#[derive(Event)]
pub struct SendChatKindEvent {
    pub entity: Entity,
    pub content: String,
    pub kind: ChatKind,
}

pub fn handle_send_chat_kind_event(
    mut events: EventReader<SendChatKindEvent>,
    mut commands: Commands,
    mut query: Query<(&Account, &mut ChatSigningSession)>,
) {
    for event in events.read() {
        let content = event
            .content
            .chars()
            .filter(|c| !matches!(c, '\x00'..='\x1F' | '\x7F' | '§'))
            .take(256)
            .collect::<String>();

        let timestamp = SystemTime::now();

        let packet = match event.kind {
            ChatKind::Message => {
                let salt = azalea_crypto::make_salt();

                let signature = if let Ok((account, mut chat_session)) = query.get_mut(event.entity)
                {
                    Some(create_signature(
                        account,
                        &mut chat_session,
                        salt,
                        timestamp,
                        &content,
                    ))
                } else {
                    None
                };

                ServerboundChat {
                    message: content,
                    timestamp: timestamp
                        .duration_since(UNIX_EPOCH)
                        .expect("Time shouldn't be before epoch")
                        .as_millis()
                        .try_into()
                        .expect("Instant should fit into a u64"),
                    salt,
                    signature,
                    // TODO: implement last_seen_messages
                    last_seen_messages: LastSeenMessagesUpdate::default(),
                }
            }
            .into_variant(),
            ChatKind::Command => {
                // TODO: commands that require chat signing
                ServerboundChatCommand { command: content }.into_variant()
            }
        };

        commands.trigger(SendPacketEvent::new(event.entity, packet));
    }
}

pub fn create_signature(
    account: &Account,
    chat_session: &mut ChatSigningSession,
    salt: u64,
    timestamp: SystemTime,
    message: &str,
) -> azalea_crypto::MessageSignature {
    let certs = account.certs.lock();
    let certs = certs.as_ref().expect("certs shouldn't be set back to None");

    let signature = azalea_crypto::sign_chat_message(&SignChatMessageOptions {
        account_uuid: account.uuid.expect("account must have a uuid"),
        chat_session_uuid: chat_session.session_id,
        message_index: chat_session.messages_sent,
        salt,
        timestamp,
        message: message.to_owned(),
        private_key: certs.private_key.clone(),
    });

    chat_session.messages_sent += 1;

    signature
}