Skip to content

Linux + iPhone Continuity / iMessage / SMS

fix: tel matching to contact name, display only

Zack Bartel575aee138e11
diff --git a/src/core/include/tether/bluetooth/contacts.hpp b/src/core/include/tether/bluetooth/contacts.hpp
index f0f3fe1..aa50018 100644
--- a/src/core/include/tether/bluetooth/contacts.hpp
+++ b/src/core/include/tether/bluetooth/contacts.hpp
@@ -28,6 +28,8 @@ namespace tether::bluetooth {
private:
std::vector<VCard> contacts_;
std::map<std::string, std::string> by_key_;
+ // Last ten digits of each contact number, for the national-vs-international fallback in name_for.
+ std::map<std::string, std::string> by_tel_suffix_;
};
// Cached at ~/.local/share/tether/contacts.json so names survive a restart
diff --git a/src/core/src/bluetooth/contacts.cpp b/src/core/src/bluetooth/contacts.cpp
index 6a599af..25c0954 100644
--- a/src/core/src/bluetooth/contacts.cpp
+++ b/src/core/src/bluetooth/contacts.cpp
@@ -22,19 +22,39 @@ namespace tether::bluetooth {
return {};
}
+ // The nationally significant part of a number
+ constexpr size_t TEL_SUFFIX_DIGITS = 10;
+
+ std::string tel_suffix(const std::string& normalized) {
+ std::string digits;
+ for (char c : normalized) {
+ if (std::isdigit(static_cast<unsigned char>(c)))
+ digits += c;
+ }
+ return digits.size() < TEL_SUFFIX_DIGITS ? std::string{} : digits.substr(digits.size() - TEL_SUFFIX_DIGITS);
+ }
+
} // namespace
void ContactStore::set(std::vector<VCard> contacts) {
contacts_ = std::move(contacts);
by_key_.clear();
+ by_tel_suffix_.clear();
for (const auto& card : contacts_) {
if (card.name.empty())
continue;
for (const auto& tel : card.tels) {
std::string normalized = normalize_phone(tel);
- if (!normalized.empty())
- by_key_.emplace("tel:" + normalized, card.name);
+ if (normalized.empty())
+ continue;
+ by_key_.emplace("tel:" + normalized, card.name);
+
+ if (std::string suffix = tel_suffix(normalized); !suffix.empty()) {
+ auto [at, fresh] = by_tel_suffix_.emplace(suffix, card.name);
+ if (!fresh && at->second != card.name)
+ at->second.clear();
+ }
}
for (const auto& email : card.emails) {
std::string normalized = normalize_email(email);
@@ -45,8 +65,16 @@ namespace tether::bluetooth {
}
std::string ContactStore::name_for(const std::string& thread_key) const {
- auto it = by_key_.find(thread_key);
- return it == by_key_.end() ? std::string{} : it->second;
+ if (auto it = by_key_.find(thread_key); it != by_key_.end())
+ return it->second;
+
+ if (thread_key.rfind("tel:", 0) == 0) {
+ if (std::string suffix = tel_suffix(thread_key.substr(4)); !suffix.empty()) {
+ if (auto it = by_tel_suffix_.find(suffix); it != by_tel_suffix_.end())
+ return it->second;
+ }
+ }
+ return {};
}
std::vector<std::string> ContactStore::addresses_for_name(const std::string& name) const {
diff --git a/test/test_vcard.cpp b/test/test_vcard.cpp
index 8170f5e..d1e0059 100644
--- a/test/test_vcard.cpp
+++ b/test/test_vcard.cpp
@@ -114,14 +114,42 @@ TEST(ContactStore, ResolvesNamesByTelAndEmail) {
EXPECT_EQ(store.name_for("tel:+15559999999"), "");
}
-// Tether never invents a country code, so the two forms stay distinct keys. A
-// contact stored one way must not silently label the other.
-TEST(ContactStore, DoesNotEquateNationalAndE164Forms) {
+// Most contacts are saved without a country code while the phone keys threads
+// in E.164, so a strict match leaves the majority of conversations labelled with
+// a bare number. A display name is not routing -- the thread key is untouched
+// and a reply still goes to the address on the thread -- so the two forms are
+// allowed to meet here, and only here.
+TEST(ContactStore, MatchesNationalAndE164FormsForDisplay) {
ContactStore store;
- store.set(parse_vcards("BEGIN:VCARD\nFN:Ada\nTEL:+15551234567\nEND:VCARD\n"));
+ store.set(parse_vcards("BEGIN:VCARD\nFN:Ada\nTEL:+15551234567\nEND:VCARD\n"
+ "BEGIN:VCARD\nFN:Grace\nTEL:5039998888\nEND:VCARD\n"));
EXPECT_EQ(store.name_for("tel:+15551234567"), "Ada");
- EXPECT_EQ(store.name_for("tel:5551234567"), "");
+ EXPECT_EQ(store.name_for("tel:5551234567"), "Ada") << "a contact saved in E.164 must label a national thread";
+ EXPECT_EQ(store.name_for("tel:+15039998888"), "Grace") << "and the reverse, which is the common case";
+
+ EXPECT_EQ(store.name_for("tel:+15550000000"), "") << "an unknown number stays unlabelled";
+}
+
+// Two people cannot be told apart by the fallback alone, and labelling a thread
+// with the wrong person's name is worse than labelling it with a number.
+TEST(ContactStore, RefusesToGuessWhenTwoContactsShareASuffix) {
+ ContactStore store;
+ store.set(parse_vcards("BEGIN:VCARD\nFN:Ada\nTEL:+15551234567\nEND:VCARD\n"
+ "BEGIN:VCARD\nFN:Grace\nTEL:+445551234567\nEND:VCARD\n"));
+
+ EXPECT_EQ(store.name_for("tel:+15551234567"), "Ada") << "an exact key still wins outright";
+ EXPECT_EQ(store.name_for("tel:5551234567"), "") << "an ambiguous suffix resolves to no name";
+}
+
+// Shortcodes and extensions are too short to carry a country code, so they match
+// exactly or not at all.
+TEST(ContactStore, DoesNotSuffixMatchShortNumbers) {
+ ContactStore store;
+ store.set(parse_vcards("BEGIN:VCARD\nFN:Alerts\nTEL:4125\nEND:VCARD\n"));
+
+ EXPECT_EQ(store.name_for("tel:4125"), "Alerts");
+ EXPECT_EQ(store.name_for("tel:+15554125"), "");
}
TEST(ContactStore, SurvivesJsonRoundTrip) {