imsd, engine: a received text records the subscriber it arrived for

The Messaging interface gave a received text with its sender and the
service centre but not the subscriber it was for. A consumer completing a
national-form sender with a country code had to take the code from the
daemon's status at import time, which is the wrong moment: the daemon
keeps a text on disk until an app deletes it, so the app that imports it
after a SIM change knows only the new SIM's code, and a national number
under the old SIM's plan is keyed, and replied to, under the new one —
another subscriber. Only the daemon knows at receipt which subscriber a
text arrived for. The stored record and the Received and List
dictionaries carry it
as `to`: the own number the daemon holds at the moment of receipt — the
first tel: P-Associated-URI of this run's latest answer to a fresh
registration, a resume or a reconnect that carried one (a periodic
refresh's answer is not read for it) and, until the first such, the
state file's once a resume attempt has taken it up. The key is absent
while the daemon holds none.
It is additive: an app that does not know it ignores it, and a record
without it reads as before. The value comes from a header, so the
emission makes it valid UTF-8, as it does a failed submission's reason.

Tested: tests/Engine, the record round trip with `to` and a record
without it.
This commit is contained in:
Jorijn van der Graaf 2026-11-02 20:54:15 +01:00
commit 04c615fc15
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
5 changed files with 24 additions and 12 deletions

View file

@ -833,6 +833,7 @@ struct Event {
std::string text; // Fatal, Idle
imsd::engine::SmsReceived sms; // SmsReceived
std::int64_t at = 0; // SmsReceived: when it arrived (epoch seconds)
std::string to; // SmsReceived: the subscriber it arrived for (the own number the daemon held then)
};
GDBusConnection* DbusConn = nullptr;
@ -1078,7 +1079,7 @@ std::vector<imsd::engine::StoredSms> LoadStoredSms(bool log = true) {
std::size_t StoredSmsCount() { return LoadStoredSms(false).size(); }
// a received short message as the Messaging interface's a{sv} (§8.4)
GVariant* SmsVariant(const imsd::engine::SmsReceived& m, std::int64_t receivedAt) {
GVariant* SmsVariant(const imsd::engine::SmsReceived& m, std::int64_t receivedAt, const std::string& to) {
GVariantBuilder b;
g_variant_builder_init(&b, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&b, "{sv}", "from", g_variant_new_string(m.from.c_str()));
@ -1086,6 +1087,8 @@ GVariant* SmsVariant(const imsd::engine::SmsReceived& m, std::int64_t receivedAt
g_variant_builder_add(&b, "{sv}", "timestamp", g_variant_new_string(m.scts.c_str()));
g_variant_builder_add(&b, "{sv}", "received", g_variant_new_int64(receivedAt));
g_variant_builder_add(&b, "{sv}", "sc", g_variant_new_string(m.sc.c_str()));
// from a header: made valid UTF-8 like a failed submission's reason
if (!to.empty()) g_variant_builder_add(&b, "{sv}", "to", g_variant_new_take_string(g_utf8_make_valid(to.c_str(), -1)));
g_variant_builder_add(&b, "{sv}", "class", g_variant_new_int32(m.msgClass.value_or(-1)));
g_variant_builder_add(&b, "{sv}", "pid", g_variant_new_byte(m.pid));
g_variant_builder_add(&b, "{sv}", "dcs", g_variant_new_byte(m.dcs));
@ -1140,7 +1143,7 @@ gboolean OnIdleEvent(gpointer data) {
break;
case Event::Type::SmsReceived:
for (const std::string& to : Subscribers)
EmitSignalTo(to, "Received", g_variant_new("(s@a{sv})", ev->uni.c_str(), SmsVariant(ev->sms, ev->at)));
EmitSignalTo(to, "Received", g_variant_new("(s@a{sv})", ev->uni.c_str(), SmsVariant(ev->sms, ev->at, ev->to)));
break;
case Event::Type::SmsState: {
std::set<std::string> to = Subscribers;
@ -2899,11 +2902,12 @@ private:
ev->type = Event::Type::SmsReceived;
ev->at = NowEpoch();
ev->sms = m;
ev->to = ppi_; // the subscriber it arrived for: the own number held now, not a later SIM's
std::string why;
StoreResult stored = StoreResult::Failed;
for (int tries = 0; tries < 1000; tries++) {
ev->uni = imsd::engine::ReceivedSmsId(smsRun_, ev->at, ++smsInSeq_);
stored = StoreReceivedSms({ev->uni, ev->at, m}, why);
stored = StoreReceivedSms({ev->uni, ev->at, m, ppi_}, why);
if (stored != StoreResult::Exists) break;
Log(std::format("sms store: {} exists already; taking the next id", ev->uni));
}
@ -3308,7 +3312,7 @@ void HandleMessagingMethod(GDBusConnection* conn, const gchar*, const gchar*, co
if (m == "List") {
GVariantBuilder b;
g_variant_builder_init(&b, G_VARIANT_TYPE("a(sa{sv})"));
for (const auto& r : LoadStoredSms()) g_variant_builder_add(&b, "(s@a{sv})", r.id.c_str(), SmsVariant(r.sms, r.receivedAt));
for (const auto& r : LoadStoredSms()) g_variant_builder_add(&b, "(s@a{sv})", r.id.c_str(), SmsVariant(r.sms, r.receivedAt, r.to));
g_dbus_method_invocation_return_value(inv, g_variant_new("(a(sa{sv}))", &b));
return;
}