Skip to content

Linux + iPhone Continuity / iMessage / SMS

fix: message threading gets confused on multiple ANCS reconnects

Zack Bartela795209987d6
diff --git a/src/core/include/tether/bluetooth/map_session.hpp b/src/core/include/tether/bluetooth/map_session.hpp
index 84c2fe6..66d9913 100644
--- a/src/core/include/tether/bluetooth/map_session.hpp
+++ b/src/core/include/tether/bluetooth/map_session.hpp
@@ -11,11 +11,16 @@
namespace tether::bluetooth {
// One entry from MessageAccess1.ListMessages.
+ //
+ // Both ends are recorded: a message the phone sent names the user as its
+ // sender, so only the recipient identifies the conversation it belongs to.
struct MapListing {
std::string handle;
std::string subject;
std::string sender_address;
std::string sender_name;
+ std::string recipient_address;
+ std::string recipient_name;
std::string timestamp;
std::string type;
std::string folder;
diff --git a/src/core/include/tether/bluetooth/messages.hpp b/src/core/include/tether/bluetooth/messages.hpp
index 46fa556..8a542fd 100644
--- a/src/core/include/tether/bluetooth/messages.hpp
+++ b/src/core/include/tether/bluetooth/messages.hpp
@@ -10,6 +10,19 @@
namespace tether::bluetooth {
+ // Handles minted locally for a message sent from here, before the phone has
+ // listed its own copy. The prefix is what tells the two apart.
+ inline constexpr const char* LOCAL_HANDLE_PREFIX = "local-";
+
+ inline bool is_local_handle(const std::string& handle) { return handle.rfind(LOCAL_HANDLE_PREFIX, 0) == 0; }
+
+ // How far apart the local record and the phone's copy of the same sent
+ // message may be before they stop being treated as the same message.
+ inline constexpr int64_t LOCAL_ECHO_WINDOW_SECONDS = 300;
+
+ // True for a MAP folder holding messages the phone sent.
+ bool is_outgoing_folder(const std::string& folder);
+
struct Message {
std::string handle;
std::string object_path;
@@ -62,6 +75,9 @@ namespace tether::bluetooth {
private:
void trim();
+ // Removes the placeholder written when the user sent `arrived` from here,
+ // now that the phone has listed its own copy of it.
+ void drop_local_echo(const Message& arrived);
size_t max_messages_;
std::map<std::string, Message> by_handle_;
diff --git a/src/core/src/bluetooth/connection.cpp b/src/core/src/bluetooth/connection.cpp
index 6d60e28..2eb74bb 100644
--- a/src/core/src/bluetooth/connection.cpp
+++ b/src/core/src/bluetooth/connection.cpp
@@ -35,8 +35,12 @@ namespace tether::bluetooth {
std::atomic<bool> g_group_replies_enabled{false};
// MAP folders are navigated from the session root one level at a time.
- constexpr const char* MAP_INBOX = "telecom/msg/inbox";
+ // The session parks on the message root and lists its children, so both
+ // halves of a conversation can be pulled without walking between them.
+ constexpr const char* MAP_MSG_ROOT = "telecom/msg";
constexpr const char* MAP_OUTBOX = "telecom/msg/outbox";
+ constexpr const char* MAP_FOLDER_INBOX = "inbox";
+ constexpr const char* MAP_FOLDER_SENT = "sent";
} // namespace
MessageStore& message_store() { return g_messages; }
@@ -104,7 +108,8 @@ namespace tether::bluetooth {
{
std::lock_guard<std::mutex> lock(g_messages_mutex);
- sent.handle = "local-" + std::to_string(std::time(nullptr)) + "-" + std::to_string(g_messages.size());
+ sent.handle =
+ LOCAL_HANDLE_PREFIX + std::to_string(std::time(nullptr)) + "-" + std::to_string(g_messages.size());
g_messages.add(sent);
g_journal.append(sent);
}
@@ -448,6 +453,12 @@ namespace tether::bluetooth {
int map_failures = 0;
int64_t next_message_poll = 0;
std::string open_map_path;
+ // Whether the phone answers a listing for a child of the message root.
+ // Cleared once, permanently for the session, if it turns out not to.
+ bool map_lists_subfolders = true;
+ // Not every phone serves the sent folder; losing it must not cost us the
+ // inbox as well.
+ bool map_lists_sent = true;
// The first listing on a new MAP session returns the phone's existing
// inbox, which on a first run is every message it holds.
bool map_session_backfill = true;
@@ -549,12 +560,14 @@ namespace tether::bluetooth {
g_map_session = session;
}
// A freshly opened session starts at the root, and SetFolder walks the
- // MAP hierarchy from there — "inbox" on its own is not a valid step.
+ // MAP hierarchy from there — "msg" on its own is not a valid step.
std::string err;
- if (!session->set_folder(MAP_INBOX, err))
- debug::log(WARN, "bluetooth: SetFolder({}) failed: {}", MAP_INBOX, err);
+ if (!session->set_folder(MAP_MSG_ROOT, err))
+ debug::log(WARN, "bluetooth: SetFolder({}) failed: {}", MAP_MSG_ROOT, err);
next_message_poll = 0;
map_session_backfill = true;
+ map_lists_subfolders = true;
+ map_lists_sent = true;
}
if (now < next_message_poll)
@@ -570,8 +583,25 @@ namespace tether::bluetooth {
return;
std::string err;
- // Empty means the current folder, which SetFolder already selected.
- auto listings = session->list_messages("", MESSAGE_LIST_MAX, err);
+ // Empty means the current folder; a name means that child of it.
+ auto listings = session->list_messages(map_lists_subfolders ? MAP_FOLDER_INBOX : "", MESSAGE_LIST_MAX, err);
+
+ // Some stacks only answer for the folder they are standing in. Walking
+ // into the inbox costs the sent folder but keeps messages working, so it
+ // is worth one attempt before declaring the session dead.
+ if (!err.empty() && map_lists_subfolders) {
+ debug::log(
+ WARN, "bluetooth: listing '{}' failed ({}); falling back to the inbox alone", MAP_FOLDER_INBOX, err);
+ map_lists_subfolders = false;
+ map_lists_sent = false;
+ std::string nav_err;
+ // Relative: the session is parked on the message root.
+ if (!session->set_folder(MAP_FOLDER_INBOX, nav_err))
+ debug::log(WARN, "bluetooth: SetFolder({}) failed: {}", MAP_FOLDER_INBOX, nav_err);
+ err.clear();
+ listings = session->list_messages("", MESSAGE_LIST_MAX, err);
+ }
+
if (!err.empty()) {
debug::log(WARN, "bluetooth: ListMessages failed: {}", err);
// ponytail: a fail count, not an obexd ObjectManager watcher. Costs up
@@ -589,6 +619,21 @@ namespace tether::bluetooth {
}
map_failures = 0;
+ // Replies sent from the phone itself live in a separate folder and name
+ // the user as their sender, so without this a conversation only ever
+ // shows one side of itself.
+ if (map_lists_sent) {
+ std::string sent_err;
+ auto sent = session->list_messages(MAP_FOLDER_SENT, MESSAGE_LIST_MAX, sent_err);
+ if (sent_err.empty()) {
+ listings.insert(listings.end(), sent.begin(), sent.end());
+ } else {
+ debug::log(
+ WARN, "bluetooth: the sent folder is unavailable ({}); showing received messages only", sent_err);
+ map_lists_sent = false;
+ }
+ }
+
std::vector<Message> fresh;
{
std::lock_guard<std::mutex> lock(g_messages_mutex);
diff --git a/src/core/src/bluetooth/map_session.cpp b/src/core/src/bluetooth/map_session.cpp
index f2ac998..f2fb70c 100644
--- a/src/core/src/bluetooth/map_session.cpp
+++ b/src/core/src/bluetooth/map_session.cpp
@@ -127,6 +127,10 @@ namespace tether::bluetooth {
listing.sender_name = variant_string(value);
else if (name == "SenderAddress")
listing.sender_address = variant_string(value);
+ else if (name == "Recipient")
+ listing.recipient_name = variant_string(value);
+ else if (name == "RecipientAddress")
+ listing.recipient_address = variant_string(value);
else if (name == "Timestamp")
listing.timestamp = variant_string(value);
else if (name == "Type")
@@ -319,14 +323,24 @@ namespace tether::bluetooth {
}
Message message_from_listing(const MapListing& listing) {
+ // iOS sets Sent on messages in the sent folder, but not every phone
+ // does, so the folder is the second opinion.
+ const bool outgoing = listing.sent || is_outgoing_folder(listing.folder);
+
+ // A conversation is keyed on the other party. For a message the phone
+ // sent, that is the recipient — keying on the sender would file every
+ // outgoing message under the user's own number instead of the thread it
+ // belongs to, and would aim a reply there too.
+ const std::string& address = outgoing ? listing.recipient_address : listing.sender_address;
+
VCardParty peer;
- peer.name = listing.sender_name;
+ peer.name = outgoing ? listing.recipient_name : listing.sender_name;
// MAP reports the address in one field without saying which kind it is;
// an '@' is the only available discriminator.
- if (listing.sender_address.find('@') != std::string::npos)
- peer.email = listing.sender_address;
+ if (address.find('@') != std::string::npos)
+ peer.email = address;
else
- peer.tel = listing.sender_address;
+ peer.tel = address;
Message message;
message.handle = map_handle_from_path(listing.handle);
@@ -334,8 +348,10 @@ namespace tether::bluetooth {
message.body = listing.subject;
message.timestamp = parse_map_timestamp(listing.timestamp);
message.read = listing.read;
- message.outgoing = listing.sent;
+ message.outgoing = outgoing;
message.folder = listing.folder;
+ // Empty when the phone gave no usable address for the other end. The
+ // caller drops those rather than filing them under a guess.
message.thread_key = thread_key_for(peer);
message.peer_address = peer.tel.empty() ? normalize_email(peer.email) : normalize_phone(peer.tel);
message.peer_name = peer.name;
diff --git a/src/core/src/bluetooth/messages.cpp b/src/core/src/bluetooth/messages.cpp
index fb3d7d8..ad8eeca 100644
--- a/src/core/src/bluetooth/messages.cpp
+++ b/src/core/src/bluetooth/messages.cpp
@@ -1,14 +1,15 @@
#include "tether/bluetooth/messages.hpp"
#include <algorithm>
+#include <cstdlib>
namespace tether::bluetooth {
- namespace {
+ bool is_outgoing_folder(const std::string& folder) {
+ return folder.find("sent") != std::string::npos || folder.find("outbox") != std::string::npos;
+ }
- bool is_outgoing_folder(const std::string& folder) {
- return folder.find("sent") != std::string::npos || folder.find("outbox") != std::string::npos;
- }
+ namespace {
std::string display_address(const VCardParty& party) {
if (!party.tel.empty())
@@ -30,12 +31,33 @@ namespace tether::bluetooth {
return false;
}
+ if (message.outgoing && !is_local_handle(message.handle))
+ drop_local_echo(message);
+
by_handle_.emplace(message.handle, message);
order_.push_back(message.handle);
trim();
return true;
}
+ // Linear scan. Only a handful of local sends are ever waiting to be
+ // matched, and the prefix check rejects everything else before any string is
+ // compared. Index by thread if the store ever grows enough for this to show.
+ void MessageStore::drop_local_echo(const Message& arrived) {
+ for (auto it = by_handle_.begin(); it != by_handle_.end(); ++it) {
+ const Message& existing = it->second;
+ if (!existing.outgoing || !is_local_handle(existing.handle))
+ continue;
+ if (existing.thread_key != arrived.thread_key || existing.body != arrived.body)
+ continue;
+ if (std::llabs(existing.timestamp - arrived.timestamp) > LOCAL_ECHO_WINDOW_SECONDS)
+ continue;
+ order_.erase(std::remove(order_.begin(), order_.end(), existing.handle), order_.end());
+ by_handle_.erase(it);
+ return;
+ }
+ }
+
bool MessageStore::set_read(const std::string& handle, bool read) {
auto it = by_handle_.find(handle);
if (it == by_handle_.end() || it->second.read == read)