Skip to main content

wechat_protocol/
status_notify.rs

1//! One-attempt native read-session notifications for the supported profile.
2//!
3//! This reports an entire conversation through a caller-frozen boundary. It is
4//! deliberately separate from an exact set of locally read message IDs. The
5//! caller must journal admission and an uncertain outcome before retrying could
6//! be considered; an I/O error is never permission to submit it again.
7use super::*;
8
9const CGI: u16 = 251;
10const PATH: &str = "/cgi-bin/micromsg-bin/statusnotify";
11
12#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
13#[serde(rename_all = "camelCase", deny_unknown_fields)]
14pub struct SessionReadNotification {
15    pub peer_username: String,
16    pub last_message_server_id: String,
17    pub last_message_create_time: u32,
18    /// A stable request identity retained with the durable frozen boundary.
19    pub client_message_id: String,
20}
21
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct SessionReadReply {
24    pub service_code: i32,
25    pub accepted: bool,
26}
27
28fn bounded_identity(value: &str, max: usize) -> bool {
29    !value.is_empty() && value.len() <= max && !value.chars().any(char::is_control)
30}
31
32fn request(state: &AccountState, notification: &SessionReadNotification) -> Result<Vec<u8>> {
33    state.validate()?;
34    ensure!(
35        bounded_identity(&state.own_username, 256)
36            && bounded_identity(&notification.peer_username, 512)
37            && bounded_identity(&notification.client_message_id, 256),
38        "Invalid read notification identity"
39    );
40    let id = notification.last_message_server_id.parse::<u64>();
41    ensure!(
42        id.is_ok_and(|id| id != 0 && id.to_string() == notification.last_message_server_id)
43            && notification.last_message_create_time != 0,
44        "Invalid read notification boundary"
45    );
46    let argument = serde_json::to_vec(&serde_json::json!({
47        "messageSvrId": notification.last_message_server_id,
48        "MsgCreateTime": notification.last_message_create_time.to_string(),
49    }))?;
50    p::message(&[
51        p::bytes(1, &wire::base_request(&state.device, state.uin, 0)?),
52        p::uint(2, 1),
53        p::bytes(3, state.own_username.as_bytes()),
54        p::bytes(4, notification.peer_username.as_bytes()),
55        p::bytes(5, notification.client_message_id.as_bytes()),
56        p::bytes(
57            8,
58            &p::message(&[p::bytes(1, b"lastMessage"), p::bytes(2, &argument)])?,
59        ),
60    ])
61}
62
63fn response(bytes: &[u8]) -> Result<SessionReadReply> {
64    let fields = p::decode(bytes)?;
65    let base = p::decode(p::data(&fields, 1)?)?;
66    let raw = p::scalar(&base, 1)?;
67    let code = raw as u32 as i32;
68    ensure!(
69        raw <= u32::MAX as u64 || raw == code as i64 as u64,
70        "Invalid read notification response status"
71    );
72    Ok(SessionReadReply {
73        service_code: code,
74        accepted: code == 0,
75    })
76}
77
78impl NativeClient {
79    /// Submit once, with no renewal, retry, or login fallback. A caller must
80    /// retain the boundary and request identity before invoking this method.
81    #[doc(hidden)]
82    pub(crate) fn notify_read_session_snapshot(
83        &self,
84        state: &mut AccountState,
85        notification: &SessionReadNotification,
86    ) -> Result<SessionReadReply> {
87        let body = request(state, notification)?;
88        response(&self.authenticated_rpc(state, CGI, PATH, &body)?)
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    fn state() -> AccountState {
97        AccountState {
98            schema: 1,
99            device: DeviceProfile {
100                device_id: b"0123456789abcdef".to_vec(),
101                device_type: "UnifiedPCLinux x86_64".into(),
102                client_version: -227271398,
103            },
104            uin: 42,
105            own_username: "synthetic_user".into(),
106            cookie: vec![],
107            session_key: vec![1; 16],
108            request_key: vec![2; 24],
109            response_key: vec![3; 24],
110            auto_auth_key: vec![4; 16],
111            key_info: vec![],
112            database_password: vec![],
113            auth_snapshot: Default::default(),
114            long_host: "long.weixin.qq.com".into(),
115            short_host: "short.weixin.qq.com".into(),
116        }
117    }
118
119    fn notification() -> SessionReadNotification {
120        SessionReadNotification {
121            peer_username: "synthetic_room@chatroom".into(),
122            last_message_server_id: u64::MAX.to_string(),
123            last_message_create_time: 1_800_000_000,
124            client_message_id: "read-fixture-stable-id".into(),
125        }
126    }
127
128    #[test]
129    fn read_session_uses_proven_fields_and_lossless_boundary_strings() {
130        assert_eq!(CGI, 251);
131        assert_eq!(PATH, "/cgi-bin/micromsg-bin/statusnotify");
132        let input = notification();
133        let body = request(&state(), &input).unwrap();
134        assert_eq!(body, request(&state(), &input).unwrap());
135        let outer = p::decode(&body).unwrap();
136        assert_eq!(
137            outer.iter().map(|field| field.number).collect::<Vec<_>>(),
138            [1, 2, 3, 4, 5, 8]
139        );
140        let base = p::decode(p::data(&outer, 1).unwrap()).unwrap();
141        assert_eq!(p::scalar(&base, 2).unwrap(), 42);
142        assert_eq!(p::scalar(&outer, 2).unwrap(), 1);
143        assert_eq!(p::data(&outer, 3).unwrap(), b"synthetic_user");
144        assert_eq!(p::data(&outer, 4).unwrap(), b"synthetic_room@chatroom");
145        assert_eq!(p::data(&outer, 5).unwrap(), b"read-fixture-stable-id");
146        let extra = p::decode(p::data(&outer, 8).unwrap()).unwrap();
147        assert_eq!(p::data(&extra, 1).unwrap(), b"lastMessage");
148        let argument: serde_json::Value =
149            serde_json::from_slice(p::data(&extra, 2).unwrap()).unwrap();
150        assert_eq!(argument["messageSvrId"], u64::MAX.to_string());
151        assert_eq!(argument["MsgCreateTime"], "1800000000");
152    }
153
154    #[test]
155    fn malformed_boundaries_and_identities_fail_before_transport() {
156        for id in ["", "0", "-1", "01", " 1", "+1", "18446744073709551616"] {
157            let mut input = notification();
158            input.last_message_server_id = id.into();
159            assert!(request(&state(), &input).is_err());
160        }
161        for value in [String::new(), "peer\nother".into(), "x".repeat(513)] {
162            let mut input = notification();
163            input.peer_username = value;
164            assert!(request(&state(), &input).is_err());
165        }
166        let mut input = notification();
167        input.last_message_create_time = 0;
168        assert!(request(&state(), &input).is_err());
169        let mut input = notification();
170        input.client_message_id.clear();
171        assert!(request(&state(), &input).is_err());
172        let mut invalid = state();
173        invalid.uin = 0;
174        assert!(request(&invalid, &notification()).is_err());
175    }
176
177    #[test]
178    fn reply_preserves_rejection_and_requires_unique_initialized_base_status() {
179        assert!(response(&p::bytes(1, &p::uint(1, 0))).unwrap().accepted);
180        let rejected = response(&p::bytes(1, &p::int32(1, -13))).unwrap();
181        assert_eq!(rejected.service_code, -13);
182        assert!(!rejected.accepted);
183        for invalid in [
184            vec![],
185            p::bytes(1, &[]),
186            p::uint(1, 0),
187            p::bytes(1, &p::uint(1, 1u64 << 32)),
188            [p::bytes(1, &p::uint(1, 0)), p::bytes(1, &p::uint(1, 0))].concat(),
189        ] {
190            assert!(response(&invalid).is_err());
191        }
192    }
193}