Linux + iPhone Continuity / iMessage / SMS
fix: honor ios pre_existing flag on notifications to prevent spam
Zack Bartelde579ca03a79
diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.mdindex f3eada4..ddec409 100644--- a/docs/PROTOCOL.md+++ b/docs/PROTOCOL.md@@ -468,7 +468,8 @@ Titles, subtitles and bodies are present when notification content mirroring iswhich is the default -- the iPhone's own *Settings > Bluetooth > (i) > Show MessageNotifications* toggle is the consent gate. With it off, only `app_id` and `app_name` arepopulated. Messages notifications (`com.apple.MobileSMS`) are retained but never raise a-desktop popup, since MAP already delivers those with working read state.+desktop popup, since MAP already delivers those with working read state. Notifications+already on the phone when mirroring starts are listed too, but never raise a popup.Since 0.2.24 `bt_status` also carries `version`, the daemon's own `TETHER_VERSION`. Aclient that finds the field absent, or holding a version other than its own, is talking todiff --git a/src/core/include/tether/bluetooth/ancs/notifications.hpp b/src/core/include/tether/bluetooth/ancs/notifications.hppindex 7ad5d55..e69c66b 100644--- a/src/core/include/tether/bluetooth/ancs/notifications.hpp+++ b/src/core/include/tether/bluetooth/ancs/notifications.hpp@@ -24,6 +24,8 @@ namespace tether::bluetooth::ancs {bool silent = false;bool has_positive_action = false;bool has_negative_action = false;+ // Already on the phone when the subscription came up.+ bool pre_existing = false;uint64_t session = 0;bool operator==(const Notification&) const = default;@@ -44,9 +46,7 @@ namespace tether::bluetooth::ancs {// should see, so it is never suppressed by having seen the UID before.class NotificationRegistry {public:- // `initial` marks the first subscription of a session, where- // pre-existing notifications are the phone's backlog rather than news.- Decision classify(const SourceEvent& event, bool initial) const;+ Decision classify(const SourceEvent& event) const;void remember(const SourceEvent& event);void forget(uint32_t uid);diff --git a/src/core/src/bluetooth/ancs/client.cpp b/src/core/src/bluetooth/ancs/client.cppindex 8c79b80..1636a38 100644--- a/src/core/src/bluetooth/ancs/client.cpp+++ b/src/core/src/bluetooth/ancs/client.cpp@@ -40,7 +40,6 @@ namespace tether::bluetooth::ancs {bool subscribed = false;std::atomic<bool> ready{false};- bool initial_sync = true;bool content_enabled = false;std::string reason = "Waiting for the iPhone's notification service.";@@ -402,7 +401,7 @@ namespace tether::bluetooth::ancs {Decision decision;{std::lock_guard<std::mutex> lock(registry_mutex);- decision = registry.classify(event, initial_sync);+ decision = registry.classify(event);if (decision == Decision::Ignore)registry.remember(event);else if (decision == Decision::Withdraw)@@ -419,6 +418,9 @@ namespace tether::bluetooth::ancs {break;}+ if (event.pre_existing() && sequencer->queued() >= MAX_QUEUED_REQUESTS / 2)+ return;+std::vector<NotificationAttributeId> attributes{NotificationAttributeId::AppIdentifier,NotificationAttributeId::Date};if (content_enabled) {@@ -490,6 +492,7 @@ namespace tether::bluetooth::ancs {notification.silent = event.silent();notification.has_positive_action = event.has_positive_action();notification.has_negative_action = event.has_negative_action();+ notification.pre_existing = event.pre_existing();// ANCS dates are the phone's wall clock with no zone, some apps send none at all.const int64_t delivered =parse_map_timestamp(response.attribute(static_cast<uint8_t>(NotificationAttributeId::Date)));@@ -537,8 +540,7 @@ namespace tether::bluetooth::ancs {// The LE bearer drops and returns constantly, which shows up here as the// path alternating with "". That is one session ending, not a different// phone: wiping the registry for it would empty the notification list on- // every blip, and the phone's replay would then be suppressed as- // pre-existing, so nothing would ever come back.+ // every blip, and iOS does not always replay its backlog to refill it.const bool same_device =device_path.empty() || state_->last_known_device.empty() || device_path == state_->last_known_device;if (!device_path.empty())@@ -553,9 +555,6 @@ namespace tether::bluetooth::ancs {state_->next_verify = 0;state_->sequencer->reset();state_->in_progress.clear();- // A new LE session replays the phone's backlog, so the next batch of- // pre-existing notifications is a sync rather than news.- state_->initial_sync = true;{std::lock_guard<std::mutex> lock(state_->registry_mutex);if (same_device) {@@ -643,7 +642,6 @@ namespace tether::bluetooth::ancs {return;}s->subscribed = true;- s->initial_sync = true;uint64_t session = 0;{std::lock_guard<std::mutex> lock(s->registry_mutex);@@ -678,11 +676,6 @@ namespace tether::bluetooth::ancs {s->handle_source_event(event, now);}- // The phone's backlog arrives immediately after subscribing, so once a- // batch has been processed anything later is genuinely new.- if (!source.empty())- s->initial_sync = false;-s->sequencer->tick(now);// A successful StartNotify proves only that the subscription exists, notdiff --git a/src/core/src/bluetooth/ancs/notifications.cpp b/src/core/src/bluetooth/ancs/notifications.cppindex 07b50e2..1a67881 100644--- a/src/core/src/bluetooth/ancs/notifications.cpp+++ b/src/core/src/bluetooth/ancs/notifications.cpp@@ -43,13 +43,10 @@ namespace tether::bluetooth::ancs {}} // namespace- Decision NotificationRegistry::classify(const SourceEvent& event, bool initial) const {+ Decision NotificationRegistry::classify(const SourceEvent& event) const {if (event.event == EventId::Removed)return Decision::Withdraw;- if (initial && event.pre_existing())- return Decision::Ignore;-if (event.event == EventId::Modified)return Decision::Fetch;@@ -76,6 +73,18 @@ namespace tether::bluetooth::ancs {}void NotificationRegistry::store(const Notification& notification) {+ for (auto it = notifications_.begin(); it != notifications_.end();) {+ const Notification& stored = it->second;+ if (stored.session != session_ && stored.app_id == notification.app_id &&+ stored.title == notification.title && stored.subtitle == notification.subtitle &&+ stored.body == notification.body && stored.received == notification.received) {+ order_.erase(std::remove(order_.begin(), order_.end(), it->first), order_.end());+ it = notifications_.erase(it);+ } else {+ ++it;+ }+ }+if (!notifications_.count(notification.uid))order_.push_back(notification.uid);notifications_[notification.uid] = notification;@@ -128,7 +137,9 @@ namespace tether::bluetooth::ancs {return name;}- bool should_show_desktop_popup(const Notification& notification) { return notification.app_id != APP_ID_MESSAGES; }+ bool should_show_desktop_popup(const Notification& notification) {+ return !notification.pre_existing && notification.app_id != APP_ID_MESSAGES;+ }std::vector<std::string> icon_candidates(const Notification& notification) {std::vector<std::string> out;diff --git a/src/daemon/main.cpp b/src/daemon/main.cppindex a177058..6290720 100644--- a/src/daemon/main.cpp+++ b/src/daemon/main.cpp@@ -567,15 +567,18 @@ int main(int argc, char** argv) {[¬ifier](const tether::bluetooth::ancs::Notification& notification) {// Messages notifications are the only side channel that says// which conversation a MAP message belongs to.- if (notification.app_id == tether::bluetooth::ancs::APP_ID_MESSAGES) {+ if (!notification.pre_existing && notification.app_id == tether::bluetooth::ancs::APP_ID_MESSAGES) {tether::bluetooth::observe_message_notification(notification.title,notification.subtitle,notification.body,static_cast<int64_t>(std::time(nullptr)));}- const std::string otp = tether::otp_extract(notification.title + "\n" + notification.subtitle +- "\n" + notification.body);+ // A code in the backlog is stale.+ std::string otp;+ if (!notification.pre_existing)+ otp = tether::otp_extract(notification.title + "\n" + notification.subtitle + "\n" ++ notification.body);tether::otp_publish(otp);nlohmann::json event = tether::bluetooth::ancs::to_json(notification);diff --git a/test/test_ancs_notifications.cpp b/test/test_ancs_notifications.cppindex 71f5161..f21f558 100644--- a/test/test_ancs_notifications.cpp+++ b/test/test_ancs_notifications.cpp@@ -105,15 +105,15 @@ TEST(AncsRegistry, ANewSessionStopsRecycledUidsReadingAsReplays) {SourceEvent first;first.uid = 9;- EXPECT_EQ(registry.classify(first, false), Decision::Fetch);+ EXPECT_EQ(registry.classify(first), Decision::Fetch);registry.remember(first);- EXPECT_EQ(registry.classify(first, false), Decision::Ignore) << "same session, same uid: a replay";+ EXPECT_EQ(registry.classify(first), Decision::Ignore) << "same session, same uid: a replay";registry.begin_session();SourceEvent recycled;recycled.uid = 9; // a different notification, same counter value- EXPECT_EQ(registry.classify(recycled, false), Decision::Fetch)+ EXPECT_EQ(registry.classify(recycled), Decision::Fetch)<< "a recycled uid from a new session was dropped as a replay";}@@ -132,6 +132,36 @@ TEST(AncsRegistry, ANewSessionKeepsWhatIsOnScreen) {EXPECT_NE(registry.session(), before);}+// A reconnect replays the backlog under fresh uids. Without replacing the stale+// copy, every reconnect would list each notification again.+TEST(AncsRegistry, AReplayUnderANewSessionReplacesTheStaleCopy) {+ NotificationRegistry registry;+ Notification raid = make(9, "com.nianticlabs.pokemongo");+ raid.title = "Raid Invitation";+ raid.received = 100;+ registry.store(raid);+ Notification card = make(3, "com.apple.Passbook");+ card.title = "Apple Card";+ card.received = 100;+ registry.store(card);++ registry.begin_session();++ Notification replay = raid;+ replay.uid = 29;+ registry.store(replay);++ EXPECT_EQ(registry.size(), 2u) << "the replay was listed twice";+ EXPECT_EQ(registry.find(9), nullptr) << "the copy kept must carry the live uid";+ EXPECT_NE(registry.find(29), nullptr);+ EXPECT_NE(registry.find(3), nullptr) << "a different notification from the old session was dropped";++ Notification twin = replay;+ twin.uid = 30;+ registry.store(twin);+ EXPECT_EQ(registry.size(), 3u) << "identical notifications within one session are both real";+}+TEST(AncsRegistry, RecentIsOrderedByDeliveryTime) {NotificationRegistry registry;// Stored in the order iOS replayed them, which bears no relation to when thediff --git a/test/test_ancs_sequencer.cpp b/test/test_ancs_sequencer.cppindex 2520c5c..e04b0a8 100644--- a/test/test_ancs_sequencer.cpp+++ b/test/test_ancs_sequencer.cpp@@ -259,24 +259,26 @@ TEST(AncsSequencer, BoundsTheQueue) {// --- Delivery rules -------------------------------------------------------// iOS replays its whole backlog whenever the subscription comes up. Delivering-// it would turn every reconnect into a burst of desktop popups.-TEST(AncsNotifications, SkipsPreExistingOnFirstSubscribe) {+// iOS replays its backlog whenever the subscription comes up, and not in one+// batch. It belongs in the list, but popping it up would turn every reconnect+// into a burst of desktop popups.+TEST(AncsNotifications, ListsPreExistingWithoutAPopup) {NotificationRegistry registry;- auto pre_existing = event_for(1, EventId::Added, FlagPreExisting);+ EXPECT_EQ(registry.classify(event_for(1, EventId::Added, FlagPreExisting)), Decision::Fetch);- EXPECT_EQ(registry.classify(pre_existing, true), Decision::Ignore);- EXPECT_EQ(registry.classify(pre_existing, false), Decision::Fetch)- << "after the initial sync a pre-existing flag is not a reason to hide it";+ Notification backlog;+ backlog.app_id = "com.example.chat";+ backlog.pre_existing = true;+ EXPECT_FALSE(should_show_desktop_popup(backlog));}TEST(AncsNotifications, DedupesRepeatedAddedEvents) {NotificationRegistry registry;auto added = event_for(7);- EXPECT_EQ(registry.classify(added, false), Decision::Fetch);+ EXPECT_EQ(registry.classify(added), Decision::Fetch);registry.remember(added);- EXPECT_EQ(registry.classify(added, false), Decision::Ignore) << "a replay of the same UID is not news";+ EXPECT_EQ(registry.classify(added), Decision::Ignore) << "a replay of the same UID is not news";}// A modification is a genuine update — a call that became a missed call, a@@ -286,7 +288,7 @@ TEST(AncsNotifications, NeverSuppressesAModification) {auto added = event_for(7);registry.remember(added);- EXPECT_EQ(registry.classify(event_for(7, EventId::Modified), false), Decision::Fetch);+ EXPECT_EQ(registry.classify(event_for(7, EventId::Modified)), Decision::Fetch);}TEST(AncsNotifications, WithdrawsRemovedNotifications) {@@ -294,10 +296,10 @@ TEST(AncsNotifications, WithdrawsRemovedNotifications) {auto added = event_for(7);registry.remember(added);- EXPECT_EQ(registry.classify(event_for(7, EventId::Removed), false), Decision::Withdraw);+ EXPECT_EQ(registry.classify(event_for(7, EventId::Removed)), Decision::Withdraw);registry.forget(7);- EXPECT_EQ(registry.classify(added, false), Decision::Fetch) << "the UID is free to be seen again";+ EXPECT_EQ(registry.classify(added), Decision::Fetch) << "the UID is free to be seen again";}// MAP already delivers these with read state that stays in sync, so an ANCS@@ -341,5 +343,5 @@ TEST(AncsNotifications, ClearingForgetsEverything) {registry.clear();EXPECT_EQ(registry.size(), 0u);- EXPECT_EQ(registry.classify(event_for(1), false), Decision::Fetch);+ EXPECT_EQ(registry.classify(event_for(1)), Decision::Fetch);}