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
//! Blocking feature for managing blocked contacts.
//!
//! This module provides high-level APIs for blocking and unblocking contacts.
//! Protocol-level types are defined in `wacore::iq::blocklist`.
use crate::client::Client;
use crate::request::IqError;
use log::debug;
use thiserror::Error;
pub use wacore::iq::blocklist::BlocklistEntry;
use wacore::iq::blocklist::{GetBlocklistSpec, UpdateBlocklistSpec};
use wacore_binary::Jid;
/// Error returned by blocklist operations.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum BlockingError {
/// The IQ to the server failed (transport, timeout, server rejection).
#[error("{0}")]
Iq(#[from] IqError),
/// The target JID is not a user JID, or has no resolvable LID↔PN mapping
/// (modern WA requires both sides for a block).
#[error("invalid blocklist target: {0}")]
InvalidJid(String),
/// Catch-all for internal failures (e.g. LID/PN store lookup).
#[error("{0}")]
Internal(#[from] anyhow::Error),
}
/// Feature handle for blocklist operations.
pub struct Blocking<'a> {
client: &'a Client,
}
impl<'a> Blocking<'a> {
pub(crate) fn new(client: &'a Client) -> Self {
Self { client }
}
/// Resolve `bare` (LID or PN) into the `(lid, pn)` pair the server expects
/// on blocklist stanzas.
async fn resolve_lid_pn(&self, bare: Jid) -> Result<(Jid, Jid), BlockingError> {
if !(bare.is_lid() || bare.is_pn()) {
return Err(BlockingError::InvalidJid(
"jid is neither PN nor LID".into(),
));
}
let entry = self.client.get_lid_pn_entry(&bare).await?.ok_or_else(|| {
BlockingError::InvalidJid("no LID↔PN mapping for provided jid".into())
})?;
Ok(if bare.is_lid() {
(bare, Jid::pn(&*entry.phone_number))
} else {
(Jid::lid(&*entry.lid), bare)
})
}
/// Block a contact. Accepts either LID or PN; the wire stanza always
/// carries both (`jid=LID, pn_jid=PN`) — modern WA rejects PN-only blocks.
pub async fn block(&self, jid: &Jid) -> Result<(), BlockingError> {
debug!(target: "Blocking", "Blocking contact");
let (lid_jid, pn_jid) = self.resolve_lid_pn(jid.to_non_ad()).await?;
self.client
.execute(UpdateBlocklistSpec::block_with_pn(&lid_jid, &pn_jid))
.await?;
debug!(target: "Blocking", "Successfully blocked contact");
Ok(())
}
/// Unblock a contact. Stanza only needs the LID, but PN input is accepted
/// and resolved through the mapping.
pub async fn unblock(&self, jid: &Jid) -> Result<(), BlockingError> {
debug!(target: "Blocking", "Unblocking contact");
// The unblock stanza only needs the LID, so a LID input must not require a
// PN↔LID mapping (resolve_lid_pn hard-fails when none exists).
let bare = jid.to_non_ad();
let lid_jid = if bare.is_lid() {
bare
} else {
self.resolve_lid_pn(bare).await?.0
};
self.client
.execute(UpdateBlocklistSpec::unblock(&lid_jid))
.await?;
debug!(target: "Blocking", "Successfully unblocked contact");
Ok(())
}
/// Get the full blocklist.
pub async fn get_blocklist(&self) -> Result<Vec<BlocklistEntry>, BlockingError> {
debug!(target: "Blocking", "Fetching blocklist...");
let entries = self.client.execute(GetBlocklistSpec).await?;
debug!(target: "Blocking", "Fetched {} blocked contacts", entries.len());
Ok(entries)
}
/// Check if a contact is blocked.
///
/// Compares only the user part of the JID, ignoring device ID, since blocking
/// applies to the entire user account, not individual devices.
pub async fn is_blocked(&self, jid: &Jid) -> Result<bool, BlockingError> {
let blocklist = self.get_blocklist().await?;
let bare = jid.to_non_ad();
// Blocks are stored keyed by LID (block() always resolves the input to a LID), so a
// PN-input query must resolve to its LID before comparing or it never matches a
// LID-keyed entry. Match against the raw user plus the resolved LID and PN. Propagate a
// backend failure (swallowing it would fall back to the raw user and re-introduce the
// false negative); a genuine absence (Ok(None), incl. a non-LID/PN input) falls back.
let mapping = self.client.get_lid_pn_entry(&bare).await?;
let mut users: Vec<&str> = vec![bare.user.as_str()];
if let Some(entry) = mapping.as_ref() {
users.push(&*entry.lid);
users.push(&*entry.phone_number);
}
Ok(blocklist_contains(&blocklist, &users))
}
}
/// Whether any blocklist entry's user part matches one of `candidate_users`.
///
/// Blocks are stored keyed by LID, so the caller resolves the queried JID to its
/// LID/PN pair and passes all of them (raw, LID, PN) to catch a LID-keyed entry from
/// a PN-input query (and the reverse).
fn blocklist_contains(blocklist: &[BlocklistEntry], candidate_users: &[&str]) -> bool {
blocklist
.iter()
.any(|e| candidate_users.contains(&e.jid.user.as_str()))
}
impl Client {
/// Access blocking operations.
pub fn blocking(&self) -> Blocking<'_> {
Blocking::new(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn lid_entry(user: &str) -> BlocklistEntry {
BlocklistEntry {
jid: Jid::lid(user.to_string()),
timestamp: None,
}
}
#[test]
fn pn_query_matches_lid_keyed_block_only_when_resolved() {
// A block stored under the LID (modern WA) must be found once the PN query is
// resolved to that LID. Without resolution the LID-keyed block is missed (the bug).
let blocklist = vec![lid_entry("100000012345678")];
assert!(
blocklist_contains(&blocklist, &["559980000001", "100000012345678"]),
"resolved PN->LID candidate matches the LID-keyed block"
);
assert!(
!blocklist_contains(&blocklist, &["559980000001"]),
"raw PN alone misses the LID-keyed block (the false negative)"
);
assert!(
blocklist_contains(&blocklist, &["100000012345678"]),
"a LID query matches directly"
);
assert!(
!blocklist_contains(&blocklist, &["559981111111", "100000099999999"]),
"an unrelated contact is not blocked"
);
}
}