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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! PDO (Peer Data Operation) support for requesting message content from the primary device.
//!
//! When message decryption fails (e.g., due to session mismatch), instead of only sending
//! a retry receipt to the sender, we can also request the message content from our own
//! primary phone device. This is useful because:
//!
//! 1. The primary phone has already decrypted the message successfully
//! 2. It can share the decrypted content with linked devices via PDO
//! 3. This bypasses session issues entirely since we're asking our own trusted device
//!
//! The flow is:
//! 1. Decryption fails for a message
//! 2. We send a PeerDataOperationRequestMessage with type PLACEHOLDER_MESSAGE_RESEND
//! 3. The phone responds with PeerDataOperationRequestResponseMessage containing the decoded message
//! 4. We emit the message as if we had decrypted it ourselves
use crate::cache::Cache;
use crate::client::Client;
use crate::types::message::MessageInfo;
use log::{debug, info, warn};
use prost::Message;
use std::sync::Arc;
use std::time::Duration;
use wacore::types::message::{EditAttribute, MessageSource, MsgMetaInfo};
use wacore_binary::jid::{Jid, JidExt};
use waproto::whatsapp as wa;
/// Cache entry for pending PDO requests.
/// Contains the original message info needed to properly dispatch the response.
#[derive(Clone, Debug)]
pub struct PendingPdoRequest {
pub message_info: MessageInfo,
pub requested_at: wacore::time::Instant,
}
/// Creates a new PDO request cache.
/// The cache has a TTL of 30 seconds (phone should respond quickly) and limited capacity.
pub fn new_pdo_cache() -> Cache<String, PendingPdoRequest> {
Cache::builder()
.time_to_live(Duration::from_secs(30))
.max_capacity(500)
.build()
}
impl Client {
/// Sends a PDO (Peer Data Operation) request to our own primary phone to get the
/// decrypted content of a message that we failed to decrypt.
///
/// This is called when decryption fails and we want to ask our phone for the message.
/// The phone will respond with a PeerDataOperationRequestResponseMessage containing
/// the full WebMessageInfo which we can then dispatch as a normal message event.
///
/// # Arguments
/// * `info` - The MessageInfo for the message that failed to decrypt
///
/// # Returns
/// * `Ok(())` if the request was sent successfully
/// * `Err` if we couldn't send the request (e.g., not logged in)
pub async fn send_pdo_placeholder_resend_request(
self: &Arc<Self>,
info: &MessageInfo,
) -> Result<(), anyhow::Error> {
let device_snapshot = self.persistence_manager.get_device_snapshot().await;
// We need to send PDO to our PRIMARY PHONE (device 0), not to ourselves (linked device).
// The primary phone has already decrypted the message and can share the content with us.
let own_pn = device_snapshot
.pn
.clone()
.ok_or_else(|| anyhow::Error::from(crate::client::ClientError::NotLoggedIn))?;
// Create JID for device 0 (primary phone)
let primary_phone_jid = own_pn.with_device(0);
// Resolve JIDs to LID for the MessageKey and cache key, matching WhatsApp Web's behavior.
// This ensures the cache key matches the JID that the phone will respond with (usually LID).
let remote_jid = self.resolve_encryption_jid(&info.source.chat).await;
let participant = if info.source.is_group {
Some(self.resolve_encryption_jid(&info.source.sender).await)
} else {
None
};
// Check-and-insert to avoid duplicate PDO requests for the same message.
let cache_key = format!("{}:{}", remote_jid, info.id);
if self.pdo_pending_requests.get(&cache_key).await.is_some() {
debug!(
"PDO request already pending for message {} from {} (resolved: {})",
info.id, info.source.sender, remote_jid
);
return Ok(());
}
let pending = PendingPdoRequest {
message_info: info.clone(),
requested_at: wacore::time::Instant::now(),
};
self.pdo_pending_requests
.insert(cache_key.clone(), pending)
.await;
// Build the message key for the placeholder resend request
let message_key = wa::MessageKey {
remote_jid: Some(remote_jid.to_string()),
from_me: Some(info.source.is_from_me),
id: Some(info.id.clone()),
participant: participant.map(|p| p.to_string()),
};
// Build the PDO request message
let pdo_request = wa::message::PeerDataOperationRequestMessage {
peer_data_operation_request_type: Some(
wa::message::PeerDataOperationRequestType::PlaceholderMessageResend as i32,
),
placeholder_message_resend_request: vec![
wa::message::peer_data_operation_request_message::PlaceholderMessageResendRequest {
message_key: Some(message_key),
},
],
..Default::default()
};
// Wrap it in a protocol message
let protocol_message = wa::message::ProtocolMessage {
r#type: Some(
wa::message::protocol_message::Type::PeerDataOperationRequestMessage as i32,
),
peer_data_operation_request_message: Some(pdo_request),
..Default::default()
};
let msg = wa::Message {
protocol_message: Some(Box::new(protocol_message)),
..Default::default()
};
info!(
"Sending PDO placeholder resend request for message {} from {} in {} to primary phone {}",
info.id, info.source.sender, info.source.chat, primary_phone_jid
);
// Ensure E2E session exists before sending (matches WhatsApp Web behavior)
self.ensure_e2e_sessions(vec![primary_phone_jid.clone()])
.await?;
// Send the message to our primary phone (device 0)
match self.send_peer_message(primary_phone_jid, &msg).await {
Ok(_) => {
debug!("PDO request sent successfully for message {}", info.id);
Ok(())
}
Err(e) => {
// Remove from pending cache on failure
self.pdo_pending_requests.remove(&cache_key).await;
warn!(
"Failed to send PDO request for message {}: {:?}",
info.id, e
);
Err(e)
}
}
}
/// Request on-demand message history from the primary phone via PDO.
pub async fn fetch_message_history(
self: &Arc<Self>,
chat_jid: &Jid,
oldest_msg_id: &str,
oldest_msg_from_me: bool,
oldest_msg_timestamp_ms: i64,
count: i32,
) -> Result<String, anyhow::Error> {
let device_snapshot = self.persistence_manager.get_device_snapshot().await;
let own_pn = device_snapshot
.pn
.clone()
.ok_or_else(|| anyhow::Error::from(crate::client::ClientError::NotLoggedIn))?;
let primary_phone_jid = own_pn.with_device(0);
let pdo_request = wa::message::PeerDataOperationRequestMessage {
peer_data_operation_request_type: Some(
wa::message::PeerDataOperationRequestType::HistorySyncOnDemand as i32,
),
history_sync_on_demand_request: Some(
wa::message::peer_data_operation_request_message::HistorySyncOnDemandRequest {
chat_jid: Some(chat_jid.to_string()),
oldest_msg_id: Some(oldest_msg_id.to_string()),
oldest_msg_from_me: Some(oldest_msg_from_me),
oldest_msg_timestamp_ms: Some(oldest_msg_timestamp_ms),
on_demand_msg_count: Some(count),
..Default::default()
},
),
..Default::default()
};
let protocol_message = wa::message::ProtocolMessage {
r#type: Some(
wa::message::protocol_message::Type::PeerDataOperationRequestMessage as i32,
),
peer_data_operation_request_message: Some(pdo_request),
..Default::default()
};
let msg = wa::Message {
protocol_message: Some(Box::new(protocol_message)),
..Default::default()
};
info!(
"Sending PDO history sync on-demand request for chat {} (count={}) to primary phone {}",
chat_jid, count, primary_phone_jid
);
self.ensure_e2e_sessions(vec![primary_phone_jid.clone()])
.await?;
self.send_peer_message(primary_phone_jid, &msg).await
}
/// Sends a peer message (message to our own devices).
/// This is used for PDO requests and similar device-to-device communication.
async fn send_peer_message(
self: &Arc<Self>,
to: Jid,
msg: &wa::Message,
) -> Result<String, anyhow::Error> {
let msg_id = self.generate_message_id().await;
// Send with peer category and high priority
self.send_message_impl(
to,
msg,
Some(msg_id.clone()),
true, // is_peer_message
false, // is_retry
None,
vec![], // No extra stanza nodes for peer messages
)
.await?;
Ok(msg_id)
}
/// Handles a PDO response message from our primary phone.
/// This is called when we receive a PeerDataOperationRequestResponseMessage.
///
/// # Arguments
/// * `response` - The PDO response message
/// * `info` - The MessageInfo for the PDO response message itself
pub async fn handle_pdo_response(
self: &Arc<Self>,
response: &wa::message::PeerDataOperationRequestResponseMessage,
_pdo_msg_info: &MessageInfo,
) {
debug!(
"Received PDO response with {} results",
response.peer_data_operation_result.len()
);
for result in &response.peer_data_operation_result {
if let Some(placeholder_response) = &result.placeholder_message_resend_response {
self.handle_placeholder_resend_response(placeholder_response)
.await;
}
}
}
/// Handles a single placeholder message resend response from PDO.
async fn handle_placeholder_resend_response(
self: &Arc<Self>,
response: &wa::message::peer_data_operation_request_response_message::peer_data_operation_result::PlaceholderMessageResendResponse,
) {
let Some(web_message_info_bytes) = &response.web_message_info_bytes else {
warn!("PDO placeholder response missing webMessageInfoBytes");
return;
};
// Decode the WebMessageInfo
let web_msg_info = match wa::WebMessageInfo::decode(web_message_info_bytes.as_slice()) {
Ok(info) => info,
Err(e) => {
warn!("Failed to decode WebMessageInfo from PDO response: {:?}", e);
return;
}
};
// Extract message key to find the original pending request
let key = &web_msg_info.key;
let remote_jid = key.remote_jid.as_deref().unwrap_or("");
let msg_id = key.id.as_deref().unwrap_or("");
let cache_key = format!("{}:{}", remote_jid, msg_id);
// Remove from pending requests
let pending = self.pdo_pending_requests.remove(&cache_key).await;
let elapsed = pending
.as_ref()
.map(|p| p.requested_at.elapsed().as_millis())
.unwrap_or(0);
info!(
"Received PDO placeholder response for message {} (took {}ms)",
msg_id, elapsed
);
// Build MessageInfo from the WebMessageInfo or use the pending request's info
let message_info = if let Some(pending) = pending {
pending.message_info
} else {
// Reconstruct MessageInfo from WebMessageInfo if we don't have it cached
match self.message_info_from_web_message_info(&web_msg_info).await {
Ok(info) => info,
Err(e) => {
warn!(
"Failed to reconstruct MessageInfo from PDO response: {:?}",
e
);
return;
}
}
};
// Extract the actual message content
let Some(message) = web_msg_info.message else {
warn!("PDO response WebMessageInfo missing message content");
return;
};
// Dispatch the message as a normal message event
info!(
"Dispatching PDO-recovered message {} from {} via phone",
message_info.id, message_info.source.sender
);
self.core
.event_bus
.dispatch(&wacore::types::events::Event::Message(
Box::new(message),
message_info,
));
}
/// Reconstructs a MessageInfo from a WebMessageInfo.
/// This is used when we receive a PDO response but don't have the original pending request cached.
async fn message_info_from_web_message_info(
&self,
web_msg: &wa::WebMessageInfo,
) -> Result<MessageInfo, anyhow::Error> {
let key = &web_msg.key;
let remote_jid: Jid = key
.remote_jid
.as_ref()
.ok_or_else(|| anyhow::anyhow!("MessageKey missing remoteJid"))?
.parse()?;
let is_group = remote_jid.is_group();
let is_from_me = key.from_me.unwrap_or(false);
let sender = if is_group {
key.participant
.as_ref()
.map(|p: &String| p.parse())
.transpose()?
.unwrap_or_else(|| remote_jid.clone())
} else if is_from_me {
self.persistence_manager
.get_device_snapshot()
.await
.pn
.clone()
.unwrap_or_else(|| remote_jid.clone())
} else {
remote_jid.clone()
};
let timestamp = web_msg
.message_timestamp
.map(|ts| {
chrono::DateTime::from_timestamp(ts as i64, 0).unwrap_or_else(chrono::Utc::now)
})
.unwrap_or_else(chrono::Utc::now);
Ok(MessageInfo {
id: key.id.clone().unwrap_or_default(),
server_id: 0,
r#type: String::new(),
source: MessageSource {
chat: remote_jid,
sender,
sender_alt: None,
recipient_alt: None,
is_from_me,
is_group,
addressing_mode: None,
broadcast_list_owner: None,
recipient: None,
},
timestamp,
push_name: web_msg.push_name.clone().unwrap_or_default(),
category: String::new(),
multicast: false,
media_type: String::new(),
edit: EditAttribute::default(),
bot_info: None,
meta_info: MsgMetaInfo::default(),
verified_name: None,
device_sent_meta: None,
})
}
/// Spawns a PDO request for a message that failed to decrypt.
/// This is called alongside the retry receipt to increase chances of recovery.
///
/// When `immediate` is true, the PDO request is sent without delay.
/// This is used when we've exhausted retry attempts and need immediate PDO recovery.
pub(crate) fn spawn_pdo_request_with_options(
self: &Arc<Self>,
info: &MessageInfo,
immediate: bool,
) {
// Don't send PDO for our own messages or status broadcasts
if info.source.is_from_me {
return;
}
if info.source.chat.server == wacore_binary::jid::BROADCAST_SERVER {
return;
}
let client_clone = Arc::clone(self);
let info_clone = info.clone();
self.runtime
.spawn(Box::pin(async move {
if !immediate {
// Add a small delay to allow the retry receipt to be processed first
// This avoids overwhelming the phone with simultaneous requests
client_clone.runtime.sleep(Duration::from_millis(500)).await;
}
if let Err(e) = client_clone
.send_pdo_placeholder_resend_request(&info_clone)
.await
{
warn!(
"Failed to send PDO request for message {} from {}: {:?}",
info_clone.id, info_clone.source.sender, e
);
}
}))
.detach();
}
/// Spawns a PDO request for a message that failed to decrypt.
/// This is called alongside the retry receipt to increase chances of recovery.
pub(crate) fn spawn_pdo_request(self: &Arc<Self>, info: &MessageInfo) {
self.spawn_pdo_request_with_options(info, false);
}
}
#[cfg(test)]
mod tests {
use wacore_binary::jid::{DEFAULT_USER_SERVER, Jid, JidExt};
#[test]
fn test_pdo_primary_phone_jid_is_device_0() {
// PDO sends to device 0 (primary phone)
let own_pn = Jid::pn("559999999999");
let primary_phone_jid = own_pn.with_device(0);
assert_eq!(primary_phone_jid.device, 0);
assert!(!primary_phone_jid.is_ad()); // Device 0 is NOT an additional device
}
#[test]
fn test_pdo_primary_phone_jid_preserves_user() {
let own_pn = Jid::pn("559999999999");
let primary_phone_jid = own_pn.with_device(0);
assert_eq!(primary_phone_jid.user, "559999999999");
assert_eq!(primary_phone_jid.server, DEFAULT_USER_SERVER);
}
#[test]
fn test_pdo_primary_phone_jid_from_linked_device() {
// Even if we're device 33, PDO should send to device 0
let own_pn = Jid::pn_device("559999999999", 33);
let primary_phone_jid = own_pn.with_device(0);
assert_eq!(primary_phone_jid.user, "559999999999");
assert_eq!(primary_phone_jid.device, 0);
}
}