1use super::*;
7
8const CGI: u16 = 594;
9const PATH: &str = "/cgi-bin/micromsg-bin/revokemsg";
10
11#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)]
12#[serde(rename_all = "camelCase", deny_unknown_fields)]
13pub struct MessageRecall {
14 pub peer_username: String,
15 pub server_id: String,
16 pub local_id: u32,
17 pub create_time: u32,
18 pub client_message_id: String,
19}
20
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct MessageRecallReply {
23 pub service_code: i32,
24 pub accepted: bool,
25}
26
27impl MessageRecall {
28 pub fn for_source(
31 peer_username: String,
32 server_id: String,
33 local_id: u32,
34 create_time: u32,
35 ) -> Self {
36 let client_message_id = format!("{peer_username}_{create_time}_{local_id}");
37 Self {
38 peer_username,
39 server_id,
40 local_id,
41 create_time,
42 client_message_id,
43 }
44 }
45 pub fn validate(&self) -> Result<()> {
46 let identity = |value: &str, max: usize| {
47 !value.is_empty() && value.len() <= max && !value.chars().any(char::is_control)
48 };
49 ensure!(
50 identity(&self.peer_username, 512) && identity(&self.client_message_id, 1024),
51 "Invalid recall identity"
52 );
53 ensure!(
54 self.local_id != 0 && self.create_time != 0,
55 "Invalid recall source"
56 );
57 ensure!(
58 self.server_id
59 .parse::<u64>()
60 .is_ok_and(|n| n != 0 && n.to_string() == self.server_id),
61 "Invalid recall server identity"
62 );
63 Ok(())
64 }
65}
66
67fn request(state: &AccountState, recall: &MessageRecall) -> Result<Vec<u8>> {
68 state.validate()?;
69 recall.validate()?;
70 ensure!(
71 !state.own_username.is_empty()
72 && state.own_username.len() <= 256
73 && !state.own_username.chars().any(char::is_control),
74 "Invalid recall sender"
75 );
76 p::message(&[
77 p::bytes(1, &wire::base_request(&state.device, state.uin, 0)?),
78 p::bytes(2, recall.client_message_id.as_bytes()),
79 p::uint(3, u64::from(recall.local_id)),
80 p::uint(4, u64::from(recall.create_time)),
81 p::uint(5, 0),
82 p::bytes(6, state.own_username.as_bytes()),
83 p::bytes(7, recall.peer_username.as_bytes()),
84 p::uint(8, 0),
85 p::uint(9, recall.server_id.parse()?),
86 ])
87}
88
89fn response(bytes: &[u8]) -> Result<MessageRecallReply> {
90 let fields = p::decode(bytes)?;
91 let base = p::decode(p::data(&fields, 1)?)?;
92 let raw = p::scalar(&base, 1)?;
93 let code = raw as u32 as i32;
94 ensure!(
95 raw <= u32::MAX as u64 || raw == code as i64 as u64,
96 "Invalid recall response status"
97 );
98 p::optional_data(&fields, 2)?;
101 p::optional_data(&fields, 3)?;
102 Ok(MessageRecallReply {
103 service_code: code,
104 accepted: code == 0,
105 })
106}
107
108impl NativeClient {
109 #[doc(hidden)]
111 pub(crate) fn recall_message_snapshot(
112 &self,
113 state: &mut AccountState,
114 recall: &MessageRecall,
115 ) -> Result<MessageRecallReply> {
116 let body = request(state, recall)?;
117 response(&self.authenticated_rpc(state, CGI, PATH, &body)?)
118 }
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124 fn state() -> AccountState {
125 AccountState {
126 schema: 1,
127 device: DeviceProfile {
128 device_id: b"0123456789abcdef".to_vec(),
129 device_type: "UnifiedPCLinux x86_64".into(),
130 client_version: -227271398,
131 },
132 uin: 42,
133 own_username: "synthetic_user".into(),
134 cookie: vec![],
135 session_key: vec![1; 16],
136 request_key: vec![2; 24],
137 response_key: vec![3; 24],
138 auto_auth_key: vec![4; 16],
139 key_info: vec![],
140 database_password: vec![],
141 auth_snapshot: Default::default(),
142 long_host: "long.weixin.qq.com".into(),
143 short_host: "short.weixin.qq.com".into(),
144 }
145 }
146 fn recall() -> MessageRecall {
147 MessageRecall::for_source(
148 "synthetic_peer".into(),
149 u64::MAX.to_string(),
150 u32::MAX,
151 1_800_000_000,
152 )
153 }
154 #[test]
155 fn native_client_identity_uses_exact_peer_time_and_unsigned_local_id() {
156 assert_eq!(
157 MessageRecall::for_source(
158 "synthetic_room@chatroom".into(),
159 "20".into(),
160 u32::MAX,
161 u32::MAX
162 )
163 .client_message_id,
164 "synthetic_room@chatroom_4294967295_4294967295"
165 );
166 assert_eq!(
167 MessageRecall::for_source("peer_with_underscore".into(), "20".into(), 1, 100)
168 .client_message_id,
169 "peer_with_underscore_100_1"
170 );
171 }
172 #[test]
173 fn native_recall_uses_source_fields_and_full_server_identity() {
174 assert_eq!(CGI, 594);
175 assert_eq!(PATH, "/cgi-bin/micromsg-bin/revokemsg");
176 let input = recall();
177 let body = request(&state(), &input).unwrap();
178 assert_eq!(body, request(&state(), &input).unwrap());
179 let f = p::decode(&body).unwrap();
180 assert_eq!(
181 f.iter().map(|f| f.number).collect::<Vec<_>>(),
182 [1, 2, 3, 4, 5, 6, 7, 8, 9]
183 );
184 assert_eq!(
185 p::scalar(&p::decode(p::data(&f, 1).unwrap()).unwrap(), 2).unwrap(),
186 42
187 );
188 assert_eq!(p::data(&f, 2).unwrap(), input.client_message_id.as_bytes());
189 assert_eq!(p::scalar(&f, 3).unwrap(), u64::from(u32::MAX));
190 assert_eq!(p::scalar(&f, 4).unwrap(), 1_800_000_000);
191 assert_eq!(p::scalar(&f, 5).unwrap(), 0);
192 assert_eq!(p::data(&f, 6).unwrap(), b"synthetic_user");
193 assert_eq!(p::data(&f, 7).unwrap(), b"synthetic_peer");
194 assert_eq!(p::scalar(&f, 8).unwrap(), 0);
195 assert_eq!(p::scalar(&f, 9).unwrap(), u64::MAX);
196 }
197 #[test]
198 fn invalid_source_or_lossy_id_is_rejected_before_transport() {
199 for sid in ["", "0", "01", "-1", "+1", " 1", "18446744073709551616"] {
200 let mut r = recall();
201 r.server_id = sid.into();
202 assert!(request(&state(), &r).is_err());
203 }
204 let mut r = recall();
205 r.local_id = 0;
206 assert!(request(&state(), &r).is_err());
207 r = recall();
208 r.create_time = 0;
209 assert!(request(&state(), &r).is_err());
210 for peer in ["".into(), "peer\nother".into(), "x".repeat(513)] {
211 r = recall();
212 r.peer_username = peer;
213 assert!(request(&state(), &r).is_err());
214 }
215 }
216 #[test]
217 fn only_unique_initialized_base_response_confirms_acceptance() {
218 assert!(response(&p::bytes(1, &p::uint(1, 0))).unwrap().accepted);
219 let rejected = response(&p::bytes(1, &p::int32(1, -13))).unwrap();
220 assert_eq!(rejected.service_code, -13);
221 assert!(!rejected.accepted);
222 for bytes in [
223 vec![],
224 p::bytes(1, &[]),
225 p::uint(1, 0),
226 p::bytes(1, &p::uint(1, 1u64 << 32)),
227 [p::bytes(1, &p::uint(1, 0)), p::bytes(1, &p::uint(1, 0))].concat(),
228 [p::bytes(1, &p::uint(1, 0)), p::uint(2, 1)].concat(),
229 ] {
230 assert!(response(&bytes).is_err());
231 }
232 }
233}