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
//! Message information provided by Unofficial Extras.

use crate::{strip_account_prefix, util::str_from_cstr_len};
use chrono::{DateTime, FixedOffset};
use std::os::raw::c_char;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "strum")]
use strum::{Display, EnumCount, EnumIter, IntoStaticStr, VariantNames};

/// A chat message.
///
/// Strings are available for the duration of the call.
/// If you need it for longer than that, consider converting it to [`ChatMessageInfoOwned`].
///
/// ```no_run
/// # use arcdps::extras::{ChatMessageInfo, ChatMessageInfoOwned};
/// # let message: ChatMessageInfo = todo!();
/// let owned = message.to_owned();
/// let owned: ChatMessageInfoOwned = message.into();
/// ```
#[derive(Debug, Clone)]
pub struct ChatMessageInfo<'a> {
    /// A unique identifier for the channel this chat message was sent over.
    ///
    /// Can be used to for example differentiate between squad messages sent to different squads.
    pub channel_id: u32,

    /// Whether the message is sent in a party or a squad.
    ///
    /// Note that messages sent to the party chat while in a squad will have the type [`ChannelType::Squad`].
    pub channel_type: ChannelType,

    /// The subgroup the message was sent to, or `0` if it was sent to the entire squad.
    pub subgroup: u8,

    /// Whether the message is a broadcast.
    pub is_broadcast: bool,

    /// Timestamp when the message was received.
    ///
    /// This is the "absolute ordering" for chat messages,
    /// however the time can potentially differ several seconds between the client and server because of latency and clock skew.
    pub timestamp: DateTime<FixedOffset>,

    /// Account name of the player that sent the message.
    pub account_name: &'a str,

    /// Character name of the player that sent the message.
    pub character_name: &'a str,

    /// Content of the message.
    pub text: &'a str,
}

impl ChatMessageInfo<'_> {
    /// Converts the [`ChatMessageInfo`] to the owned version [`ChatMessageInfoOwned`].
    pub fn to_owned(self) -> ChatMessageInfoOwned {
        self.into()
    }
}

impl<'a> From<&'a RawChatMessageInfo> for ChatMessageInfo<'a> {
    fn from(raw: &RawChatMessageInfo) -> Self {
        let timestamp = unsafe { str_from_cstr_len(raw.timestamp, raw.timestamp_length) };
        let timestamp =
            DateTime::parse_from_rfc3339(timestamp).expect("failed to parse message timestamp");

        let account_name = unsafe { str_from_cstr_len(raw.account_name, raw.account_name_length) };
        let character_name =
            unsafe { str_from_cstr_len(raw.character_name, raw.character_name_length) };
        let text = unsafe { str_from_cstr_len(raw.text, raw.text_length) };

        let is_broadcast = (raw.is_broadcast & 0x01) != 0;

        Self {
            channel_id: raw.channel_id,
            channel_type: raw.channel_type,
            subgroup: raw.subgroup,
            is_broadcast,
            timestamp,
            account_name: strip_account_prefix(account_name),
            character_name,
            text,
        }
    }
}

/// [`ChatMessageInfo`] with owned [`String`] fields.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ChatMessageInfoOwned {
    /// A unique identifier for the channel this chat message was sent over.
    ///
    /// Can be used to, for example, differentiate between squad messages sent to different squads.
    pub channel_id: u32,

    /// Whether the message is sent in a party or a squad.
    ///
    /// Note that messages sent to the party chat while in a squad will have the type [`ChannelType::Squad`].
    pub channel_type: ChannelType,

    /// The subgroup the message was sent to, or `0` if it was sent to the entire squad.
    pub subgroup: u8,

    /// Whether the message is a broadcast.
    pub is_broadcast: bool,

    /// Timestamp when the message was received.
    ///
    /// This is the "absolute ordering" for chat messages,
    /// however the time can potentially differ several seconds between the client and server because of latency and clock skew.
    pub timestamp: DateTime<FixedOffset>,

    /// Account name of the player that sent the message.
    pub account_name: String,

    /// Character name of the player that sent the message.
    pub character_name: String,

    /// Content of the message.
    pub text: String,
}

impl From<ChatMessageInfo<'_>> for ChatMessageInfoOwned {
    fn from(chat: ChatMessageInfo<'_>) -> Self {
        Self {
            channel_id: chat.channel_id,
            channel_type: chat.channel_type,
            subgroup: chat.subgroup,
            is_broadcast: chat.is_broadcast,
            timestamp: chat.timestamp,
            account_name: chat.account_name.to_string(),
            character_name: chat.character_name.to_string(),
            text: chat.text.to_string(),
        }
    }
}

/// Raw chat message information.
#[derive(Debug, Clone)]
#[repr(C)]
pub struct RawChatMessageInfo {
    /// A unique identifier for the channel this chat message was sent over.
    ///
    /// Can be used to, for example, differentiate between squad messages sent to different squads.
    pub channel_id: u32,

    /// Whether the message is sent in a party or a squad.
    ///
    /// Note that messages sent to the party chat while in a squad will have the type [`ChannelType::Squad`].
    pub channel_type: ChannelType,

    /// The subgroup the message was sent to, or `0` if it was sent to the entire squad.
    pub subgroup: u8,

    /// The lowest bit of this field will be set to `1` if the message is a broadcast, and `0` if it is not a broadcast.
    /// The upper bits of this field may be used in a later version and **must not** be interpreted.
    pub is_broadcast: u8,

    /// Unused padding.
    pub _unused1: u8,

    /// Null terminated iso8601 formatted string denoting when this message was
    /// received by the server, e.g. `"2022-07-09T11:45:24.888Z"`.
    /// This is the "absolute ordering" for chat messages,
    /// however the time can potentially differ several seconds between the client and server because of latency and clock skew.
    ///
    /// The string is only valid for the duration of the call.
    pub timestamp: *const c_char,
    pub timestamp_length: u64,

    /// Null terminated account name of the player that sent the message, including leading ':'.
    ///
    /// The string is only valid for the duration of the call.
    pub account_name: *const c_char,
    pub account_name_length: u64,

    /// Null terminated character name of the player that sent the message.
    ///
    /// The string is only valid for the duration of the call.
    pub character_name: *const c_char,
    pub character_name_length: u64,

    /// Null terminated string containing the content of the message that was sent.
    ///
    /// The string is only valid for the duration of the call.
    pub text: *const c_char,
    pub text_length: u64,
}

/// Type of message channel.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "strum",
    derive(Display, EnumCount, EnumIter, IntoStaticStr, VariantNames)
)]
#[repr(u8)]
pub enum ChannelType {
    Party = 0,
    Squad = 1,
    Reserved = 2,
    Invalid = 3,
}