Skip to content

Linux + iPhone Continuity / iMessage / SMS

fix: the phone spells numbers differently in the same thread (+1 vs not)

Zack Bartel3b907f3a4932
diff --git a/src/core/include/tether/bluetooth/bmessage.hpp b/src/core/include/tether/bluetooth/bmessage.hpp
index 9c92b05..dfcb9b1 100644
--- a/src/core/include/tether/bluetooth/bmessage.hpp
+++ b/src/core/include/tether/bluetooth/bmessage.hpp
@@ -55,6 +55,14 @@ namespace tether::bluetooth {
// The stable key for one conversation.
std::string thread_key_for(const VCardParty& party);
+ // The nationally significant digits of a number, used to recognize two
+ // spellings of one number. Empty when there are fewer than 10 digits.
+ std::string tel_suffix(const std::string& raw);
+
+ // Groups the thread keys that name one peer. MAP spells the same number
+ // several ways (+15555550123, 15555550123, 5555550123)
+ std::string thread_bucket(const std::string& thread_key);
+
// Parses MAP's basic-ISO timestamp (optionally with a trailing offset) into a Unix epoch.
// Returns 0 when unparseable.
int64_t parse_map_timestamp(const std::string& text);
diff --git a/src/core/include/tether/bluetooth/messages.hpp b/src/core/include/tether/bluetooth/messages.hpp
index 8a542fd..8d66b8d 100644
--- a/src/core/include/tether/bluetooth/messages.hpp
+++ b/src/core/include/tether/bluetooth/messages.hpp
@@ -79,10 +79,14 @@ namespace tether::bluetooth {
// now that the phone has listed its own copy of it.
void drop_local_echo(const Message& arrived);
+ // The key every message of `thread_key`'s peer is filed under
+ const std::string& canonical_key(const std::string& thread_key);
+
size_t max_messages_;
std::map<std::string, Message> by_handle_;
// Insertion order, used to evict the oldest when the cap is reached.
std::vector<std::string> order_;
+ std::map<std::string, std::string> representative_;
};
nlohmann::json to_json(const Message& message);
diff --git a/src/core/src/bluetooth/bmessage.cpp b/src/core/src/bluetooth/bmessage.cpp
index cf2932f..3f86d7a 100644
--- a/src/core/src/bluetooth/bmessage.cpp
+++ b/src/core/src/bluetooth/bmessage.cpp
@@ -248,6 +248,33 @@ namespace tether::bluetooth {
return {};
}
+ namespace {
+ constexpr size_t TEL_SUFFIX_DIGITS = 10;
+ } // namespace
+
+ std::string tel_suffix(const std::string& raw) {
+ std::string digits;
+ for (char c : raw) {
+ 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);
+ }
+
+ std::string thread_bucket(const std::string& thread_key) {
+ if (thread_key.rfind("tel:", 0) != 0)
+ return thread_key;
+ const std::string number = thread_key.substr(4);
+ std::string suffix = tel_suffix(number);
+ if (suffix.empty()) {
+ for (char c : number) {
+ if (std::isdigit(static_cast<unsigned char>(c)))
+ suffix += c;
+ }
+ }
+ return suffix.empty() ? thread_key : "tel:" + suffix;
+ }
+
namespace {
// Anything below 0x20, plus DEL. CR and LF are the injection vector, but
diff --git a/src/core/src/bluetooth/contacts.cpp b/src/core/src/bluetooth/contacts.cpp
index 25c0954..0d0204a 100644
--- a/src/core/src/bluetooth/contacts.cpp
+++ b/src/core/src/bluetooth/contacts.cpp
@@ -22,18 +22,6 @@ 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) {
diff --git a/src/core/src/bluetooth/messages.cpp b/src/core/src/bluetooth/messages.cpp
index 05d2ad1..79c54d2 100644
--- a/src/core/src/bluetooth/messages.cpp
+++ b/src/core/src/bluetooth/messages.cpp
@@ -36,10 +36,42 @@ namespace tether::bluetooth {
return out;
}
+ // Which of two spellings of one address to file the conversation under.
+ bool better_key(const std::string& candidate, const std::string& current) {
+ const std::string a = candidate.substr(candidate.find(':') + 1);
+ const std::string b = current.substr(current.find(':') + 1);
+ const bool a_plus = !a.empty() && a.front() == '+';
+ const bool b_plus = !b.empty() && b.front() == '+';
+ if (a_plus != b_plus)
+ return a_plus;
+ if (a.size() != b.size())
+ return a.size() > b.size();
+ return a < b;
+ }
+
} // namespace
MessageStore::MessageStore(size_t max_messages) : max_messages_(max_messages) {}
+ const std::string& MessageStore::canonical_key(const std::string& thread_key) {
+ const std::string bucket = thread_bucket(thread_key);
+ auto [at, fresh] = representative_.emplace(bucket, thread_key);
+ if (fresh || at->second == thread_key)
+ return at->second;
+ if (!better_key(thread_key, at->second))
+ return at->second;
+
+ // A better spelling arrived; move the conversation to it so every
+ // consumer sees one key.
+ const std::string previous = at->second;
+ at->second = thread_key;
+ for (auto& [handle, stored] : by_handle_) {
+ if (stored.thread_key == previous)
+ stored.thread_key = thread_key;
+ }
+ return at->second;
+ }
+
bool MessageStore::add(const Message& message) {
if (message.handle.empty())
return false;
@@ -50,11 +82,15 @@ namespace tether::bluetooth {
return false;
}
- if (message.outgoing && !is_local_handle(message.handle))
- drop_local_echo(message);
+ Message stored = message;
+ if (!stored.thread_key.empty())
+ stored.thread_key = canonical_key(stored.thread_key);
+
+ if (stored.outgoing && !is_local_handle(stored.handle))
+ drop_local_echo(stored);
- by_handle_.emplace(message.handle, message);
- order_.push_back(message.handle);
+ order_.push_back(stored.handle);
+ by_handle_.emplace(stored.handle, std::move(stored));
trim();
return true;
}
@@ -143,9 +179,11 @@ namespace tether::bluetooth {
}
std::vector<Message> MessageStore::messages(const std::string& thread_key, size_t limit) const {
+ // Matched on the bucket, not the string
+ const std::string bucket = thread_bucket(thread_key);
std::vector<Message> result;
for (const auto& [handle, message] : by_handle_) {
- if (message.thread_key == thread_key)
+ if (thread_bucket(message.thread_key) == bucket)
result.push_back(message);
}
diff --git a/test/test_bmessage.cpp b/test/test_bmessage.cpp
index 44a71d6..ebfea42 100644
--- a/test/test_bmessage.cpp
+++ b/test/test_bmessage.cpp
@@ -526,6 +526,62 @@ TEST(MessageStore, EchoIsCollapsedOnReplayToo) {
EXPECT_EQ(store.threads().size(), 1u);
}
+// iOS names the same peer "+14255550123" in the sent folder and "14255550123"
+// in the inbox listing, which used to file one person under two conversations.
+TEST(MessageStore, OneNumberSpelledTwoWaysIsOneThread) {
+ for (bool plus_first : {true, false}) {
+ MessageStore store;
+ const std::string plus = "tel:+14255550123";
+ const std::string bare = "tel:14255550123";
+ store.add(make("a", plus_first ? plus : bare, 100));
+ store.add(make("b", plus_first ? bare : plus, 200));
+
+ auto threads = store.threads();
+ ASSERT_EQ(threads.size(), 1u);
+ EXPECT_EQ(threads[0].key, plus) << "the reply address must keep its E.164 form";
+ EXPECT_EQ(threads[0].count, 2);
+
+ // Either spelling still resolves the conversation, so a client holding
+ // the older key is not left with an empty thread.
+ EXPECT_EQ(store.messages(plus).size(), 2u);
+ EXPECT_EQ(store.messages(bare).size(), 2u);
+ }
+}
+
+// A number with its country code and the same number without one are the same
+// peer; keying them apart splits the conversation the moment the phone changes
+// how it reports the address.
+TEST(MessageStore, NationalAndInternationalFormsShareAThread) {
+ MessageStore store;
+ store.add(make("a", "tel:4255550123", 100));
+ store.add(make("b", "tel:+14255550123", 200));
+
+ auto threads = store.threads();
+ ASSERT_EQ(threads.size(), 1u);
+ EXPECT_EQ(threads[0].key, "tel:+14255550123");
+}
+
+// The local echo is keyed on whatever spelling the UI held, the phone's copy on
+// the sent folder's. Different spellings must not keep them apart, or the reply
+// shows twice.
+TEST(MessageStore, EchoCollapsesAcrossAddressSpellings) {
+ MessageStore store;
+ ASSERT_TRUE(store.add(local_send("tel:14255550123", "on my way", 1000)));
+ ASSERT_TRUE(store.add(phone_copy("m9", "tel:+14255550123", "on my way", 1005)));
+
+ EXPECT_EQ(store.size(), 1u);
+ ASSERT_EQ(store.threads().size(), 1u);
+ EXPECT_EQ(store.messages("tel:+14255550123").size(), 1u);
+}
+
+// Short numbers have no country code to discount, so every digit still counts.
+TEST(MessageStore, ShortNumbersStaySeparate) {
+ MessageStore store;
+ store.add(make("a", "tel:1111", 100));
+ store.add(make("b", "tel:2222", 200));
+ EXPECT_EQ(store.threads().size(), 2u);
+}
+
// Captured from the phone's own sent folder: iOS writes the originator's number
// scheme-tagged. The tag is transport framing, not part of the address, and
// leaving it on would key the conversation under an address nobody has.