Linux + iPhone Continuity / iMessage / SMS
fix: pure email only icloud deliveries fail
Zack Bartel1fe5d7e63304
diff --git a/src/core/include/tether/bluetooth/bmessage.hpp b/src/core/include/tether/bluetooth/bmessage.hppindex 5b3a7a7..9c92b05 100644--- a/src/core/include/tether/bluetooth/bmessage.hpp+++ b/src/core/include/tether/bluetooth/bmessage.hpp@@ -68,16 +68,6 @@ 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);@@ -103,9 +93,8 @@ namespace tether::bluetooth {// Builds a MAP 1.4 bMessage for PushMessage. Recipients must already be// validated//- // TYPE is always SMS_GSM, including for AppleID recipients: iOS decides- // between SMS and iMessage itself, and nothing here can force or observe- // that choice.+ // TYPE is SMS_GSM, including for AppleID recipients: iOS decides between SMS+ // and iMessage itself, and nothing here can force or observe that choice.std::string build_bmessage(const std::vector<Recipient>& recipients, const std::string& body);} // namespace tether::bluetoothdiff --git a/src/core/src/bluetooth/bmessage.cpp b/src/core/src/bluetooth/bmessage.cppindex 6d6d6f2..637d052 100644--- a/src/core/src/bluetooth/bmessage.cpp+++ b/src/core/src/bluetooth/bmessage.cpp@@ -4,6 +4,7 @@#include <algorithm>#include <cctype>#include <cstdio>+#include <cstdlib>#include <ctime>namespace tether::bluetooth {@@ -50,6 +51,13 @@ namespace tether::bluetooth {using tether::bluetooth::vcard_name_from_n;using tether::bluetooth::vcard_split_property;+ // iOS tags address values with a one-character scheme prefix and a colon+ // ("TEL:7:+15035551234"). Neither a phone number nor an email address can+ // contain a colon, so a colon in second position is always such a tag.+ std::string strip_scheme_tag(const std::string& value) {+ return value.size() > 2 && value[1] == ':' ? value.substr(2) : value;+ }+// Reads one BEGIN:VCARD..END:VCARD block starting at `index`, which must// point at the BEGIN line. Leaves `index` on the END line.VCardParty read_vcard(const std::vector<std::string>& lines, size_t& index) {@@ -66,9 +74,9 @@ namespace tether::bluetooth {continue;if (name == "TEL" && party.tel.empty())- party.tel = value;+ party.tel = strip_scheme_tag(value);else if (name == "EMAIL" && party.email.empty())- party.email = value;+ party.email = strip_scheme_tag(value);else if (name == "FN")party.name = value;else if (name == "N")@@ -268,6 +276,11 @@ namespace tether::bluetooth {// silently losing its leading space.bool line_needs_stuffing(const std::string& line) { return is_stuffable_token(line); }+ const char* env_or(const char* name, const char* fallback) {+ const char* value = std::getenv(name);+ return value && *value ? value : fallback;+ }+} // namespacebool validate_recipient(const Recipient& recipient, std::string& err_out) {@@ -328,13 +341,6 @@ 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;@@ -400,7 +406,8 @@ namespace tether::bluetooth {out += "BEGIN:BMSG\r\n";out += "VERSION:1.0\r\n";out += "STATUS:UNREAD\r\n";- // Always SMS_GSM, even for an Apple-ID recipient; iOS picks the transport.+ // Always SMS_GSM, even for an Apple-ID recipient; iOS picks the transport,+ // and its own inbound Apple-ID messages arrive typed SMS_GSM too.out += "TYPE:SMS_GSM\r\n";out += "FOLDER:telecom/msg/outbox\r\n";@@ -413,12 +420,26 @@ namespace tether::bluetooth {out += "TEL:\r\n";out += "END:VCARD\r\n";+ // iOS discards the first two characters of an EMAIL property value, so a+ // bare address arrives two characters short and reaches nobody. Its own+ // vCards tag address values with a two-character scheme prefix+ // ("TEL:7:+15035551234"); emitting one gives the parser something to+ // consume other than the address. TEL values are not truncated and take+ // no tag.+ //+ // The tag was read off the phone's own vCards rather than any spec, so+ // TETHER_BMSG_ADDR_PREFIX overrides it if a future iOS expects another.+ const char* email_tag = env_or("TETHER_BMSG_ADDR_PREFIX", "7:");+out += "BEGIN:BENV\r\n";for (const auto& recipient : recipients) {out += "BEGIN:VCARD\r\n";out += "VERSION:2.1\r\n";out += "N:;;;;\r\n";- out += (recipient.kind == RecipientKind::Tel ? "TEL:" : "EMAIL:");+ const bool is_email = recipient.kind == RecipientKind::Email;+ out += is_email ? "EMAIL:" : "TEL:";+ if (is_email)+ out += email_tag;out += recipient.address;out += "\r\n";out += "END:VCARD\r\n";@@ -426,9 +447,9 @@ namespace tether::bluetooth {out += "BEGIN:BBODY\r\n";out += "CHARSET:UTF-8\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";+ // MAP 3.1.3: LENGTH is the byte count of the whole BEGIN:MSG..END:MSG+ // block, delimiters and their CRLFs included, not of the payload alone.+ out += "LENGTH:" + std::to_string(message_block.size()) + "\r\n";out += message_block;out += "END:BBODY\r\n";out += "END:BENV\r\n";diff --git a/src/core/src/net.cpp b/src/core/src/net.cppindex 2da7465..fac9da5 100644--- a/src/core/src/net.cpp+++ b/src/core/src/net.cpp@@ -410,12 +410,6 @@ 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;diff --git a/src/gtk/messages_view.cpp b/src/gtk/messages_view.cppindex 0273137..f9da57e 100644--- a/src/gtk/messages_view.cpp+++ b/src/gtk/messages_view.cpp@@ -51,7 +51,6 @@ namespace tether::ui {// why not when it cannot.bool selected_repliable = false;std::string selected_block_reason;- std::string selected_warning;// Addressing a message to someone with no conversation yet.bool composing = false;@@ -126,8 +125,6 @@ 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));@@ -189,20 +186,12 @@ 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);- 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());+ if (!stamp.empty()) {+ GtkWidget* time_label = gtk_label_new(stamp.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);}@@ -399,14 +388,10 @@ namespace tether::ui {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.+ // Why the box is shut.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);@@ -506,7 +491,6 @@ 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");}@@ -517,12 +501,10 @@ 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,@@ -628,7 +610,6 @@ namespace tether::ui {g_messages.selected_name.clear();g_messages.selected_repliable = false;g_messages.selected_block_reason.clear();- g_messages.selected_warning.clear();bluetooth::Recipient recipient;std::string err;@@ -637,9 +618,6 @@ namespace tether::ui {} else if (bluetooth::recipient_from_input(text, recipient, err)) {g_messages.selected_thread = bluetooth::thread_key_for(recipient);g_messages.selected_repliable = !g_messages.selected_thread.empty();- // Worth knowing before the message goes out, not only after the- // phone reports a send that reached nobody.- g_messages.selected_warning = bluetooth::delivery_warning(recipient);if (auto known = g_messages.compose_names.find(g_messages.selected_thread);known != g_messages.compose_names.end())g_messages.selected_name = known->second;diff --git a/src/gtk/ui_util.cpp b/src/gtk/ui_util.cppindex 350b4c8..368b15e 100644--- a/src/gtk/ui_util.cpp+++ b/src/gtk/ui_util.cpp@@ -39,11 +39,6 @@ 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-route-bar {border-top: 1px solid alpha(@theme_fg_color, 0.12);}diff --git a/test/test_bmessage.cpp b/test/test_bmessage.cppindex c5b6024..7c5e3bc 100644--- a/test/test_bmessage.cpp+++ b/test/test_bmessage.cpp@@ -511,3 +511,35 @@ TEST(MessageStore, EchoIsCollapsedOnReplayToo) {EXPECT_EQ(store.size(), 1u);EXPECT_EQ(store.threads().size(), 1u);}++// 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.+TEST(BMessageParse, StripsTheSchemeTagIosPutsOnAddresses) {+ const BMessage m = parse_bmessage("BEGIN:BMSG\r\n"+ "VERSION:1.0\r\n"+ "STATUS:READ\r\n"+ "TYPE:SMS_GSM\r\n"+ "FOLDER:telecom/msg/sent\r\n"+ "BEGIN:VCARD\r\nVERSION:2.1\r\nTEL:7:+15033201884\r\nEND:VCARD\r\n"+ "BEGIN:BENV\r\n"+ "BEGIN:VCARD\r\nVERSION:2.1\r\nEMAIL:7:[email protected]\r\nEND:VCARD\r\n"+ "BEGIN:BBODY\r\nCHARSET:UTF-8\r\nLENGTH:26\r\n"+ "BEGIN:MSG\r\ntest\r\nEND:MSG\r\n"+ "END:BBODY\r\nEND:BENV\r\nEND:BMSG\r\n");++ ASSERT_TRUE(m.valid);+ EXPECT_EQ(m.originator.tel, "+15033201884");+ ASSERT_EQ(m.recipients.size(), 1u);+ EXPECT_EQ(m.recipients.front().email, "[email protected]");+ EXPECT_EQ(thread_key_for(m.recipients.front()), "email:[email protected]");+}++// An untagged address is the common case and must survive untouched.+TEST(BMessageParse, LeavesAnUntaggedAddressAlone) {+ const BMessage m = parse_bmessage("BEGIN:BMSG\r\nVERSION:1.0\r\nTYPE:SMS_GSM\r\n"+ "BEGIN:VCARD\r\nVERSION:2.1\r\nEMAIL:[email protected]\r\nEND:VCARD\r\n"+ "END:BMSG\r\n");+ ASSERT_TRUE(m.valid);+ EXPECT_EQ(m.originator.email, "[email protected]");+}diff --git a/test/test_bmessage_build.cpp b/test/test_bmessage_build.cppindex 1a6904f..8889085 100644--- a/test/test_bmessage_build.cpp+++ b/test/test_bmessage_build.cpp@@ -43,7 +43,7 @@ TEST(BMessageBuild, AlwaysUsesSmsGsmEvenForAppleIdRecipients) {const std::string out = build_bmessage({email("[email protected]")}, "hi");EXPECT_NE(out.find("TYPE:SMS_GSM"), std::string::npos);- EXPECT_NE(out.find("EMAIL:[email protected]"), std::string::npos);+ EXPECT_NE(out.find("EMAIL:7:[email protected]"), 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);@@ -52,6 +52,22 @@ TEST(BMessageBuild, AlwaysUsesSmsGsmEvenForAppleIdRecipients) {// 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.+// iOS discards the first two characters of an EMAIL value. A two-character+// scheme tag, the same shape iOS writes its own addresses with, is eaten in the+// address's place; without it "cece.blair@..." is recorded as "ce.blair@..." and+// the message reaches nobody. Verified against the phone's own sent folder.+TEST(BMessageBuild, AppleIdAddressesCarryASchemeTagSoIosCannotEatThem) {+ const std::string out = build_bmessage({email("[email protected]")}, "hi");++ EXPECT_NE(out.find("EMAIL:7:[email protected]"), std::string::npos);+ EXPECT_EQ(out.find("EMAIL:cece.blair"), std::string::npos) << "an untagged address loses two characters";++ // A phone number is not truncated and must stay untagged.+ const std::string tel_out = build_bmessage({tel("+15551234567")}, "hi");+ EXPECT_NE(tel_out.find("TEL:+15551234567"), std::string::npos);+ EXPECT_EQ(tel_out.find("TEL:7:"), std::string::npos);+}+TEST(BMessageBuild, UsesTheVcardShapeIosParsesCorrectly) {const std::string out = build_bmessage({email("[email protected]")}, "hi");@@ -73,32 +89,22 @@ TEST(BMessageBuild, EmailRecipientSurvivesAParseOfWhatWasBuilt) {EXPECT_EQ(parsed.recipients.front().email, address) << "the recipient lost characters between build and parse";}-// 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) {+// MAP 3.1.3 counts the whole BEGIN:MSG..END:MSG block, delimiters and their+// CRLFs included -- the "body + 22" every other MAP implementation encodes.+TEST(BMessageBuild, LengthCountsTheWholeMessageBlock) {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 std::string open = "BEGIN:MSG\r\n";- const size_t begin = out.find(open);- const size_t end = out.find("\r\nEND:MSG");+ const std::string close = "END:MSG\r\n";+ const size_t begin = out.find("BEGIN:MSG\r\n");+ const size_t end = out.find(close, begin);ASSERT_NE(begin, std::string::npos);ASSERT_NE(end, std::string::npos);- EXPECT_EQ(declared, end - (begin + open.size()));+ EXPECT_EQ(declared, end + close.size() - begin);+ EXPECT_EQ(declared, std::string("hello").size() + 22);}// --- The trust boundary --------------------------------------------------