Linux + iPhone Continuity / iMessage / SMS
fix: iphone truncates the first 2 characters of an imessage only address?!
Zack Bartel3e91b04c42cc
diff --git a/src/core/include/tether/bluetooth/bmessage.hpp b/src/core/include/tether/bluetooth/bmessage.hppindex 7910e13..ce25aa8 100644--- a/src/core/include/tether/bluetooth/bmessage.hpp+++ b/src/core/include/tether/bluetooth/bmessage.hpp@@ -68,6 +68,16 @@ namespace tether::bluetooth {bool operator==(const Recipient&) const = default;};+ // Why a message to this recipient may not arrive, or an empty string when+ // there is no known problem. Advisory: the send still goes out.+ //+ // An iPhone truncates the EMAIL address in a pushed bMessage and delivers to+ // the result, which is not an address anyone owns: "[email protected]"+ // arrives as "[email protected]" and the message reaches nobody. Confirmed+ // against two independent senders emitting byte-identical bMessages, so this+ // is the phone's behaviour and no wording of the message avoids it.+ std::string delivery_warning(const Recipient& recipient);+// Returns false with err_out set when the address must not be used.bool validate_recipient(const Recipient& recipient, std::string& err_out);diff --git a/src/core/include/tether/bluetooth/messages.hpp b/src/core/include/tether/bluetooth/messages.hppindex f845689..8a542fd 100644--- a/src/core/include/tether/bluetooth/messages.hpp+++ b/src/core/include/tether/bluetooth/messages.hpp@@ -76,9 +76,8 @@ 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, and carries the- // placeholder's peer over when the phone's copy names a different one.- void adopt_local_echo(Message& arrived);+ // 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/bmessage.cpp b/src/core/src/bluetooth/bmessage.cppindex 3cdd221..b8e439d 100644--- a/src/core/src/bluetooth/bmessage.cpp+++ b/src/core/src/bluetooth/bmessage.cpp@@ -328,6 +328,13 @@ namespace tether::bluetooth {return true;}+ std::string delivery_warning(const Recipient& recipient) {+ if (recipient.kind == RecipientKind::Email)+ return "The iPhone corrupts Apple ID email addresses in Bluetooth messages, so this probably will not "+ "arrive. It reports the send as successful either way. Send from the iPhone to be sure.";+ return {};+ }+bool recipient_from_thread_key(const std::string& thread_key, Recipient& out, std::string& err_out) {if (thread_key.rfind("tel:", 0) == 0) {out.kind = RecipientKind::Tel;@@ -381,18 +388,20 @@ namespace tether::bluetooth {out += "TYPE:SMS_GSM\r\n";out += "FOLDER:telecom/msg/outbox\r\n";- // The originator vCard is empty and sits outside the envelope: the phone- // fills in its own identity.+ // vCard 2.1 with all five N components present, which is what MAP+ // implementations emit and the one iOS parses correctly.out += "BEGIN:VCARD\r\n";- out += "VERSION:3.0\r\n";- out += "N:\r\n";+ out += "VERSION:2.1\r\n";+ out += "N:;;;;\r\n";+ // The originator address is left empty for the phone to fill in, but the property itself is present.+ out += "TEL:\r\n";out += "END:VCARD\r\n";out += "BEGIN:BENV\r\n";for (const auto& recipient : recipients) {out += "BEGIN:VCARD\r\n";- out += "VERSION:3.0\r\n";- out += "N:\r\n";+ out += "VERSION:2.1\r\n";+ out += "N:;;;;\r\n";out += (recipient.kind == RecipientKind::Tel ? "TEL:" : "EMAIL:");out += recipient.address;out += "\r\n";@@ -401,8 +410,9 @@ namespace tether::bluetooth {out += "BEGIN:BBODY\r\n";out += "CHARSET:UTF-8\r\n";- // LENGTH counts the whole message block, delimiters included.- out += "LENGTH:" + std::to_string(message_block.size()) + "\r\n";+ // LENGTH counts the body alone, without the BEGIN:MSG/END:MSG delimiters+ // that surround it.+ out += "LENGTH:" + std::to_string(stuffed.size()) + "\r\n";out += message_block;out += "END:BBODY\r\n";out += "END:BENV\r\n";diff --git a/src/core/src/bluetooth/connection.cpp b/src/core/src/bluetooth/connection.cppindex 33c13b6..fac8f6e 100644--- a/src/core/src/bluetooth/connection.cpp+++ b/src/core/src/bluetooth/connection.cpp@@ -666,6 +666,15 @@ namespace tether::bluetooth {{std::lock_guard<std::mutex> lock(g_messages_mutex);for (const auto& listing : listings) {+ // raw, before anything here can correct or discard it+ if ((listing.sent || is_outgoing_folder(listing.folder)) && !g_messages.find(listing.handle))+ debug::log(DEBUG,+ "bluetooth: phone lists sent msg folder='{}' recipient=[{}] sender=[{}] handle={}",+ listing.folder,+ listing.recipient_address,+ listing.sender_address,+ listing.handle);+Message message = message_from_listing(listing);if (message.thread_key.empty())continue;diff --git a/src/core/src/bluetooth/map_session.cpp b/src/core/src/bluetooth/map_session.cppindex 5a7003d..772f199 100644--- a/src/core/src/bluetooth/map_session.cpp+++ b/src/core/src/bluetooth/map_session.cpp@@ -253,7 +253,8 @@ namespace tether::bluetooth {for (size_t at = 0; at < bmessage.size();) {const size_t end = bmessage.find('\r', at);const std::string line = bmessage.substr(at, end == std::string::npos ? end : end - at);- if (line.rfind("TEL:", 0) == 0 || line.rfind("EMAIL:", 0) == 0)+ const bool addressed = line.size() > 4 && (line.rfind("TEL:", 0) == 0 || line.rfind("EMAIL:", 0) == 0);+ if (addressed && line.find(':') + 1 < line.size())debug::log(DEBUG, "bluetooth: sending to {} ({})", line, folder);if (end == std::string::npos)break;diff --git a/src/core/src/bluetooth/messages.cpp b/src/core/src/bluetooth/messages.cppindex cd5b09b..ad8eeca 100644--- a/src/core/src/bluetooth/messages.cpp+++ b/src/core/src/bluetooth/messages.cpp@@ -31,13 +31,11 @@ namespace tether::bluetooth {return false;}- Message entry = message;- if (entry.outgoing && !is_local_handle(entry.handle))- adopt_local_echo(entry);+ if (message.outgoing && !is_local_handle(message.handle))+ drop_local_echo(message);- const std::string handle = entry.handle;- by_handle_.emplace(handle, std::move(entry));- order_.push_back(handle);+ by_handle_.emplace(message.handle, message);+ order_.push_back(message.handle);trim();return true;}@@ -45,34 +43,17 @@ namespace tether::bluetooth {// 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::adopt_local_echo(Message& arrived) {- auto erase_echo = [&](std::map<std::string, Message>::iterator it) {- order_.erase(std::remove(order_.begin(), order_.end(), it->second.handle), order_.end());- by_handle_.erase(it);- };- auto echoes = [&](const Message& existing) {- return existing.outgoing && is_local_handle(existing.handle) && existing.body == arrived.body &&- std::llabs(existing.timestamp - arrived.timestamp) <= LOCAL_ECHO_WINDOW_SECONDS;- };-- // An exact match settles it without any guessing about addresses.- for (auto it = by_handle_.begin(); it != by_handle_.end(); ++it) {- if (echoes(it->second) && it->second.thread_key == arrived.thread_key) {- erase_echo(it);- return;- }- }-+ void MessageStore::drop_local_echo(const Message& arrived) {for (auto it = by_handle_.begin(); it != by_handle_.end(); ++it) {- const std::string& sent_to = it->second.peer_address;- if (!echoes(it->second) || arrived.peer_address.empty() || arrived.peer_address == sent_to)+ const Message& existing = it->second;+ if (!existing.outgoing || !is_local_handle(existing.handle))continue;- if (sent_to.size() <= arrived.peer_address.size() || !sent_to.ends_with(arrived.peer_address))+ if (existing.thread_key != arrived.thread_key || existing.body != arrived.body)continue;- arrived.thread_key = it->second.thread_key;- arrived.peer_address = it->second.peer_address;- arrived.peer_name = it->second.peer_name;- erase_echo(it);+ 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;}}diff --git a/src/core/src/net.cpp b/src/core/src/net.cppindex 3c946d3..ca9f9cc 100644--- a/src/core/src/net.cpp+++ b/src/core/src/net.cpp@@ -365,6 +365,12 @@ namespace tether {for (auto& entry : threads) {if (!entry.value("group", false)) {entry["repliable"] = true;+ bluetooth::Recipient recipient;+ std::string parse_err;+ if (bluetooth::recipient_from_thread_key(entry.value("thread", ""), recipient, parse_err)) {+ if (std::string why = bluetooth::delivery_warning(recipient); !why.empty())+ entry["reply_warning"] = why;+ }continue;}std::string reason;@@ -771,12 +777,25 @@ namespace tether {std::thread([]() {nlohmann::json event;event["command"] = "bt_solicit_result";++ // Telling someone to switch on what is already on and+ // already working reads as the app not knowing its own+ // state. Nothing needs soliciting in that case.+ if (build_bt_connection_status().value("ancs_ready", false)) {+ event["success"] = true;+ event["message"] = "Notification mirroring is already active; nothing to do.";+ broadcast_local_event(event.dump());+ return;+ }+std::string err;event["success"] = bluetooth::g_bluez && bluetooth::solicit_ancs(*bluetooth::g_bluez, err);event["message"] =event["success"]- ? "Open Settings > Bluetooth > (i) on the iPhone and enable Show Message "- "Notifications and Sync Contacts. They can take a few minutes to appear."+ ? "Asked the iPhone to re-offer notification access. Watch the connection "+ "status. If it does not go active within a minute or two, toggle Show "+ "Message Notifications off and on in Settings > Bluetooth > (i) -- it can "+ "need re-granting even when it already looks enabled.": (err.empty() ? "Bluetooth is unavailable." : err);broadcast_local_event(event.dump());}).detach();diff --git a/src/gtk/messages_view.cpp b/src/gtk/messages_view.cppindex 37445cd..46c32ea 100644--- a/src/gtk/messages_view.cpp+++ b/src/gtk/messages_view.cpp@@ -29,6 +29,7 @@ namespace tether::ui {bool visible = false;bool map_open = false;bool sending = false;+ GtkWidget* composer_notice = nullptr;// Rebuilding the thread list destroys and recreates the selected row.// The handler is blocked across that so the churn is not mistaken for// the user opening a conversation.@@ -46,6 +47,7 @@ namespace tether::ui {// why not when it cannot.bool selected_repliable = false;std::string selected_block_reason;+ std::string selected_warning;};MessagesState g_messages;@@ -108,6 +110,8 @@ namespace tether::ui {// The daemon owns the decision about whether a group can be replied// to; the UI must not re-derive it from the key shape.g_object_set_data(G_OBJECT(row), "repliable", GINT_TO_POINTER(thread.value("repliable", true) ? 1 : 0));+ g_object_set_data_full(+ G_OBJECT(row), "reply_warning", g_strdup(thread.value("reply_warning", "").c_str()), g_free);g_object_set_data_full(G_OBJECT(row), "reply_reason", g_strdup(thread.value("reply_reason", "").c_str()), g_free);g_object_set_data(G_OBJECT(row), "group", GINT_TO_POINTER(thread.value("group", false) ? 1 : 0));@@ -169,12 +173,20 @@ namespace tether::ui {GtkStyleContext* bubble_style = gtk_widget_get_style_context(bubble);gtk_style_context_add_class(bubble_style, "tether-bubble");gtk_style_context_add_class(bubble_style, outgoing ? "tether-bubble-out" : "tether-bubble-in");+ // The phone reports every send as delivered, including the ones it addressed to nobody.+ const bool unconfirmed = outgoing && !g_messages.selected_warning.empty();+ if (unconfirmed)+ gtk_style_context_add_class(bubble_style, "tether-bubble-unconfirmed");gtk_box_pack_start(GTK_BOX(box), bubble, FALSE, FALSE, 0);- if (!stamp.empty()) {- GtkWidget* time_label = gtk_label_new(stamp.c_str());+ const std::string meta =+ unconfirmed ? (stamp.empty() ? std::string("not confirmed") : stamp + " \u00b7 not confirmed") : stamp;+ if (!meta.empty()) {+ GtkWidget* time_label = gtk_label_new(meta.c_str());gtk_label_set_xalign(GTK_LABEL(time_label), outgoing ? 1.0 : 0.0);gtk_style_context_add_class(gtk_widget_get_style_context(time_label), "muted");+ if (unconfirmed)+ gtk_widget_set_tooltip_text(time_label, g_messages.selected_warning.c_str());gtk_box_pack_start(GTK_BOX(box), time_label, FALSE, FALSE, 0);}@@ -361,6 +373,20 @@ namespace tether::ui {else if (!can_send)reason = "Replying to this conversation is not available.";gtk_widget_set_tooltip_text(g_messages.send_button, reason);++ if (g_messages.composer_notice) {+ // Why the box is shut, or -- when it is open but the phone is+ // known to mangle this conversation's address -- why a send that+ // reports success may still not arrive.+ const char* notice = nullptr;+ if (reason && !composer_live && !g_messages.sending)+ notice = reason;+ else if (composer_live && !g_messages.selected_warning.empty())+ notice = g_messages.selected_warning.c_str();+ if (notice)+ set_text(g_messages.composer_notice, notice);+ gtk_widget_set_visible(g_messages.composer_notice, notice != nullptr);+ }}std::string composer_text() {@@ -453,6 +479,7 @@ namespace tether::ui {g_messages.selected_name.clear();g_messages.selected_repliable = false;g_messages.selected_block_reason.clear();+ g_messages.selected_warning.clear();update_composer_sensitivity();gtk_stack_set_visible_child_name(GTK_STACK(g_messages.placeholder_stack), "placeholder");}@@ -463,10 +490,12 @@ namespace tether::ui {const char* thread = (const char*)g_object_get_data(G_OBJECT(row), "thread");const char* name = (const char*)g_object_get_data(G_OBJECT(row), "name");const char* block_reason = (const char*)g_object_get_data(G_OBJECT(row), "reply_reason");+ const char* warning = (const char*)g_object_get_data(G_OBJECT(row), "reply_warning");g_messages.selected_thread = thread ? thread : "";g_messages.selected_name = name ? name : "";g_messages.selected_repliable = g_object_get_data(G_OBJECT(row), "repliable") != nullptr;g_messages.selected_block_reason = block_reason ? block_reason : "";+ g_messages.selected_warning = warning ? warning : "";// Which conversation is open is otherwise only legible from the// selection highlight in the list beside it.set_markup(g_messages.conversation_header,@@ -650,6 +679,18 @@ namespace tether::ui {// Enabled only once MAP is up and a conversation is open.update_composer_sensitivity();+ // A dead composer with the explanation hidden in the send button's tooltip+ // reads as the app being broken. The button is insensitive, so the tooltip+ // never appears at all.+ g_messages.composer_notice = gtk_label_new(nullptr);+ gtk_label_set_xalign(GTK_LABEL(g_messages.composer_notice), 0.0);+ gtk_label_set_line_wrap(GTK_LABEL(g_messages.composer_notice), TRUE);+ gtk_widget_set_no_show_all(g_messages.composer_notice, TRUE);+ gtk_style_context_add_class(gtk_widget_get_style_context(g_messages.composer_notice), "muted");+ gtk_widget_set_margin_start(g_messages.composer_notice, 8);+ gtk_widget_set_margin_end(g_messages.composer_notice, 8);+ gtk_box_pack_start(GTK_BOX(conversation_box), g_messages.composer_notice, FALSE, FALSE, 0);+gtk_box_pack_start(GTK_BOX(conversation_box), composer_box, FALSE, FALSE, 0);gtk_stack_add_named(GTK_STACK(g_messages.placeholder_stack), conversation_box, "conversation");diff --git a/src/gtk/ui_util.cpp b/src/gtk/ui_util.cppindex 54f080e..8d01ac3 100644--- a/src/gtk/ui_util.cpp+++ b/src/gtk/ui_util.cpp@@ -26,6 +26,11 @@ namespace tether::ui {color: @theme_selected_fg_color;}+.tether-bubble-unconfirmed {+ background-image: none;+ border: 1px dashed alpha(@theme_selected_fg_color, 0.55);+}+.tether-badge {background-color: @theme_selected_bg_color;color: @theme_selected_fg_color;diff --git a/test/test_bmessage.cpp b/test/test_bmessage.cppindex a2b14c1..c5b6024 100644--- a/test/test_bmessage.cpp+++ b/test/test_bmessage.cpp@@ -486,28 +486,6 @@ TEST(MessageStore, PhoneCopySupersedesTheLocalEcho) {EXPECT_EQ(messages[0].handle, "m9") << "the phone's copy is the one with a usable handle";}-// iOS has been observed recording an Apple-ID recipient with characters missing-// from the front of the address it was sent. Filing the phone's copy under that-// address opens a second conversation nobody can reply to, so the local echo ---// which knows what was actually addressed -- carries the peer over.-TEST(MessageStore, PhoneCopyWithATruncatedAddressJoinsTheRealConversation) {- MessageStore store;- Message echo = local_send("email:[email protected]", "test3", 1000);- echo.peer_address = "[email protected]";- Message copy = phone_copy("m9", "email:[email protected]", "test3", 1005);- copy.peer_address = "[email protected]";-- ASSERT_TRUE(store.add(echo));- ASSERT_TRUE(store.add(copy));-- EXPECT_EQ(store.size(), 1u);- EXPECT_EQ(store.threads().size(), 1u) << "the mangled address opened a second conversation";- auto messages = store.messages("email:[email protected]");- ASSERT_EQ(messages.size(), 1u);- EXPECT_EQ(messages[0].handle, "m9");- EXPECT_EQ(messages[0].peer_address, "[email protected]");-}-TEST(MessageStore, UnrelatedOutgoingMessagesAreNotCollapsed) {MessageStore store;store.add(local_send("tel:+1111", "on my way", 1000));diff --git a/test/test_bmessage_build.cpp b/test/test_bmessage_build.cppindex 719e58c..43f88fe 100644--- a/test/test_bmessage_build.cpp+++ b/test/test_bmessage_build.cpp@@ -44,7 +44,22 @@ TEST(BMessageBuild, AlwaysUsesSmsGsmEvenForAppleIdRecipients) {EXPECT_NE(out.find("TYPE:SMS_GSM"), std::string::npos);EXPECT_NE(out.find("EMAIL:[email protected]"), std::string::npos);- EXPECT_EQ(out.find("TEL:"), std::string::npos);+ // The originator carries an empty TEL; no TEL may carry an address.+ EXPECT_EQ(out.find("TEL:+"), std::string::npos);+ EXPECT_EQ(out.find("TEL:someone"), std::string::npos);+}++// iOS was observed recording a recipient with characters missing from the front+// of the address, against an envelope that differed from a known-good sender's+// only in these details. Pinned so the shape cannot drift back.+TEST(BMessageBuild, UsesTheVcardShapeIosParsesCorrectly) {+ const std::string out = build_bmessage({email("[email protected]")}, "hi");++ EXPECT_EQ(out.find("VERSION:3.0"), std::string::npos) << "vCard 3.0 is not the shape that parsed correctly";+ EXPECT_NE(out.find("VERSION:2.1"), std::string::npos);+ // All five N components present; a bare "N:" preceded the mangled address.+ EXPECT_NE(out.find("N:;;;;"), std::string::npos);+ EXPECT_EQ(out.find("N:\r\n"), std::string::npos);}// The address the phone acts on has to survive a round trip through the same@@ -58,19 +73,32 @@ TEST(BMessageBuild, EmailRecipientSurvivesAParseOfWhatWasBuilt) {EXPECT_EQ(parsed.recipients.front().email, address) << "the recipient lost characters between build and parse";}-TEST(BMessageBuild, LengthCountsTheWholeMessageBlock) {+// An iPhone truncates the EMAIL address in a pushed bMessage and delivers to the+// result, so the message reaches nobody -- and reports the transfer complete+// either way. Verified against two independent senders emitting byte-identical+// bMessages. The send is allowed, so the warning is the only thing standing+// between the user and a message they believe arrived.+TEST(BMessageBuild, AppleIdRecipientsCarryADeliveryWarning) {+ const std::string why = delivery_warning(email("[email protected]"));+ ASSERT_FALSE(why.empty()) << "an Apple ID recipient was reported as reliable";+ EXPECT_NE(why.find("not arrive"), std::string::npos) << "the warning does not say what goes wrong";++ EXPECT_TRUE(delivery_warning(tel("+15551234567")).empty()) << "phone recipients are unaffected";+}++TEST(BMessageBuild, LengthCountsTheBodyWithoutItsDelimiters) {const std::string out = build_bmessage({tel("+15551234567")}, "hello");const size_t length_at = out.find("LENGTH:");ASSERT_NE(length_at, std::string::npos);const size_t declared = std::stoul(out.substr(length_at + 7));- const size_t begin = out.find("BEGIN:MSG");- const size_t end = out.find("END:MSG");+ const std::string open = "BEGIN:MSG\r\n";+ const size_t begin = out.find(open);+ const size_t end = out.find("\r\nEND:MSG");ASSERT_NE(begin, std::string::npos);ASSERT_NE(end, std::string::npos);- const size_t actual = (end + std::string("END:MSG\r\n").size()) - begin;- EXPECT_EQ(declared, actual);+ EXPECT_EQ(declared, end - (begin + open.size()));}// --- The trust boundary --------------------------------------------------