Bus: the GDBus client, imsd's dictionaries, the notifier and the owned name

The GLib side of the app. Variant reads imsd's Received dictionary by
key and type as its SmsVariant writes it (`to`, the number the text
arrived for, a tel: URI read like the status's `ppi`; `operator`, the
SIM it arrived under), the GetStatus one with the subscriber's own
number and the SIM's operator in it (`ppi`, a tel: URI as the frozen
interface gives it; `operator`), and maps a GError to the core's error
kinds off GDBus's own mapping. Client is the Sync logic's transport:
asynchronous calls with their continuations as user data — every reply
guarded where it is delivered (OnCallDone), as are the signal handlers
and the name watch's callbacks: a throw is a fault the events hear, not
the end of the process — the calls behind imsd's polkit gate allow
interaction: Subscribe, List and Delete wait two minutes; Send has no
bound of the client's, since imsd sends the text once its prompt lets
the call through, however late, and a Send given up on would be failed
by Sync and be sent all the same; GetStatus ten seconds — (one call: the
Messaging status carries the own number, imsd reading both from one
snapshot; AGENTS.md §1 names both interfaces), the Messaging and
RegistrationChanged signals, and a watch on net.catcrafts.IMS1 whose
appeared callback is where Sync subscribes. Notifier shows one desktop
notification per conversation and replaces it as more texts arrive: one
Notify at a time per conversation, since the id to replace comes with
the answer; its default action asks the app for that conversation; the
body is escaped when the server renders markup (GetCapabilities, asked
once; Plasma and GNOME do), so a text's '<' or '&' shows as typed.
Instance owns net.catcrafts.ImsdMessages and serves
org.freedesktop.Application there, the Desktop Entry Specification's
interface of a running application: Activate, Open and ActivateAction,
each with its platform data — sent empty and not read: nothing in the
app can use an activation token yet — and one action, quit, any other
refused (the object registered before the name is asked for, so a launch
that forwards the moment the name is granted finds it; Own tells a name
another instance holds from a request that failed or was refused, and
keeps the error's words); it forwards a call to the owner. project.cpp
computes the gio flags once for the bus library, the executable and the
Decode test.

Tested: tests/Decode (every key of imsd's Received dictionary, one with
ten of them, GetStatus, the own number, the SIM's operator, the error
kinds, the markup escape, the parameters of the instance's three calls).
This commit is contained in:
Jorijn van der Graaf 2026-10-17 23:45:29 +02:00
commit dcf1aecafd
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
10 changed files with 930 additions and 31 deletions

159
tests/Decode/main.cpp Normal file
View file

@ -0,0 +1,159 @@
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// lint-disable-file no-char-pointer
// The bus layer's decoding: a Received dictionary with every key imsd's
// SmsVariant writes, one with ten of them and a duplicate key of another
// type, a GetStatus dictionary, and GErrors as the core's BusError kinds.
#include <gio/gio.h>
import std;
import ImsdMessages;
import ImsdMessages.Bus;
using namespace ImsdMessages;
namespace {
std::int32_t failures = 0;
void Check(bool ok, std::string_view what) {
std::println("{} {}", ok ? "PASS" : "FAIL", what);
if (!ok) ++failures;
}
GVariant* FullDict() {
const std::vector<std::uint8_t> data = { 1, 2, 3 };
const std::vector<std::uint8_t> pdu = { 4, 11, 145 };
GVariantBuilder b;
g_variant_builder_init(&b, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&b, "{sv}", "from", g_variant_new_string("+31621308050"));
g_variant_builder_add(&b, "{sv}", "text", g_variant_new_string("Tëst"));
g_variant_builder_add(&b, "{sv}", "timestamp", g_variant_new_string("2026-10-17T00:41:40+02:00"));
g_variant_builder_add(&b, "{sv}", "received", g_variant_new_int64(1792192661));
g_variant_builder_add(&b, "{sv}", "sc", g_variant_new_string("+31624000000"));
g_variant_builder_add(&b, "{sv}", "to", g_variant_new_string("tel:+31612345678"));
g_variant_builder_add(&b, "{sv}", "operator", g_variant_new_string("20408"));
g_variant_builder_add(&b, "{sv}", "class", g_variant_new_int32(0));
g_variant_builder_add(&b, "{sv}", "pid", g_variant_new_byte(0x41));
g_variant_builder_add(&b, "{sv}", "dcs", g_variant_new_byte(0x04));
g_variant_builder_add(&b, "{sv}", "segments", g_variant_new_int32(2));
g_variant_builder_add(&b, "{sv}", "message-waiting", g_variant_new_boolean(TRUE));
g_variant_builder_add(&b, "{sv}", "port-dst", g_variant_new_uint32(2948));
g_variant_builder_add(&b, "{sv}", "port-src", g_variant_new_uint32(9200));
g_variant_builder_add(&b, "{sv}", "data", g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, data.data(), data.size(), 1));
g_variant_builder_add(&b, "{sv}", "pdu", g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, pdu.data(), pdu.size(), 1));
return g_variant_ref_sink(g_variant_builder_end(&b));
}
GVariant* PlainDict() {
GVariantBuilder b;
g_variant_builder_init(&b, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&b, "{sv}", "from", g_variant_new_string("KPN"));
g_variant_builder_add(&b, "{sv}", "text", g_variant_new_string("Welkom"));
g_variant_builder_add(&b, "{sv}", "timestamp", g_variant_new_string("2026-10-17T00:48:32+02:00"));
g_variant_builder_add(&b, "{sv}", "received", g_variant_new_int64(1792192662));
g_variant_builder_add(&b, "{sv}", "sc", g_variant_new_string("+31624000000"));
g_variant_builder_add(&b, "{sv}", "class", g_variant_new_int32(-1));
g_variant_builder_add(&b, "{sv}", "pid", g_variant_new_byte(0));
g_variant_builder_add(&b, "{sv}", "dcs", g_variant_new_byte(0));
g_variant_builder_add(&b, "{sv}", "segments", g_variant_new_int32(1));
g_variant_builder_add(&b, "{sv}", "message-waiting", g_variant_new_boolean(FALSE));
g_variant_builder_add(&b, "{sv}", "received", g_variant_new_string("not an int64, and after the one that is"));
return g_variant_ref_sink(g_variant_builder_end(&b));
}
}
int main() {
GVariant* full = FullDict();
const ReceivedMessage m = Bus::ReceivedFromVariant("ims-sms-in-1792192661-1", full);
g_variant_unref(full);
Check(m.imsdId == "ims-sms-in-1792192661-1", "the id");
Check(m.from == "+31621308050" && m.text == "Tëst" && m.timestamp == "2026-10-17T00:41:40+02:00" && m.sc == "+31624000000", "the strings");
Check(m.to == "+31612345678", "the number the text arrived for, out of its tel: URI");
Check(m.simOperator == "20408", "the operator of the SIM it arrived under");
Check(m.received == 1792192661, "received x");
Check(m.smsClass == 0 && m.pid == 0x41 && m.dcs == 0x04 && m.segments == 2 && m.messageWaiting, "class, pid, dcs, segments, message-waiting");
Check(m.portDst == 2948u && m.portSrc == 9200u, "the ports");
Check(m.data == std::vector<std::uint8_t>{ 1, 2, 3 } && m.pdu == std::vector<std::uint8_t>{ 4, 11, 145 }, "data and pdu");
GVariant* plain = PlainDict();
const ReceivedMessage p = Bus::ReceivedFromVariant("ims-sms-in-1792192662-2", plain);
g_variant_unref(plain);
Check(p.from == "KPN" && p.text == "Welkom" && p.smsClass == -1 && p.segments == 1 && !p.messageWaiting, "a plain text");
Check(!p.portDst && !p.portSrc && p.data.empty() && p.pdu.empty() && p.to.empty() && p.simOperator.empty(), "absent keys read as absent");
Check(p.received == 1792192662, "a later duplicate key of another type does not disturb the typed lookup");
GVariantBuilder sb;
g_variant_builder_init(&sb, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&sb, "{sv}", "enabled", g_variant_new_boolean(TRUE));
g_variant_builder_add(&sb, "{sv}", "smsc", g_variant_new_string("+31653131313"));
g_variant_builder_add(&sb, "{sv}", "ppi", g_variant_new_string("tel:+31612345678"));
g_variant_builder_add(&sb, "{sv}", "operator", g_variant_new_string("20408"));
g_variant_builder_add(&sb, "{sv}", "registered", g_variant_new_boolean(TRUE));
g_variant_builder_add(&sb, "{sv}", "subscribers", g_variant_new_uint32(1));
GVariant* sd = g_variant_ref_sink(g_variant_builder_end(&sb));
const Status s = Bus::StatusFromVariant(sd);
g_variant_unref(sd);
Check(s.enabled && s.registered && s.smsc == "+31653131313" && s.ownNumber == "+31612345678" && s.simOperator == "20408" && s.subscribers == 1, "GetStatus, the own number and the SIM's operator in it");
for (const auto& [ppi, number, what] : std::array<std::tuple<const char*, const char*, const char*>, 3>{ { { "tel:+31612345678", "+31612345678", "the own number is the ppi's tel: URI" }, { "sip:alice@example.org", "", "another kind of URI is no number" }, { nullptr, "", "no ppi, no number" } } }) {
GVariantBuilder pb;
g_variant_builder_init(&pb, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&pb, "{sv}", "registered", g_variant_new_boolean(TRUE));
if (ppi) g_variant_builder_add(&pb, "{sv}", "ppi", g_variant_new_string(ppi));
GVariant* pd = g_variant_ref_sink(g_variant_builder_end(&pb));
Check(Bus::StatusFromVariant(pd).ownNumber == number, what);
Check(Bus::StatusFromVariant(pd).simOperator.empty(), "and no operator from an answer without one");
g_variant_unref(pd);
}
GError* denied = g_dbus_error_new_for_dbus_error("org.freedesktop.DBus.Error.AccessDenied", ":1.5 is not authorized for net.catcrafts.IMS1.Messaging");
const BusError e1 = Bus::ErrorFrom(denied);
g_error_free(denied);
Check(e1.kind == BusError::Kind::AccessDenied && e1.message == ":1.5 is not authorized for net.catcrafts.IMS1.Messaging", "AccessDenied, the remote name stripped from the text");
GError* missing = g_dbus_error_new_for_dbus_error("org.freedesktop.DBus.Error.FileNotFound", "no such message");
Check(Bus::ErrorFrom(missing).kind == BusError::Kind::NotFound, "FileNotFound");
g_error_free(missing);
GError* off = g_dbus_error_new_for_dbus_error("org.freedesktop.DBus.Error.NotSupported", "SMS over IP is off");
Check(Bus::ErrorFrom(off).kind == BusError::Kind::NotSupported, "NotSupported");
g_error_free(off);
GError* badId = g_dbus_error_new_for_dbus_error("org.freedesktop.DBus.Error.InvalidArgs", "not an id");
Check(Bus::ErrorFrom(badId).kind == BusError::Kind::InvalidArgs, "InvalidArgs");
g_error_free(badId);
GError* nobody = g_error_new_literal(G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN, "The name net.catcrafts.IMS1 was not provided by any .service files");
Check(Bus::ErrorFrom(nobody).kind == BusError::Kind::NoOwner, "ServiceUnknown is NoOwner");
g_error_free(nobody);
GError* late = g_error_new_literal(G_IO_ERROR, G_IO_ERROR_TIMED_OUT, "Timeout was reached");
Check(Bus::ErrorFrom(late).kind == BusError::Kind::Timeout, "a GIO timeout is Timeout");
g_error_free(late);
GError* other = g_dbus_error_new_for_dbus_error("org.freedesktop.DBus.Error.Failed", "boom");
Check(Bus::ErrorFrom(other).kind == BusError::Kind::Other && Bus::ErrorFrom(other).message == "boom", "anything else is Other with its text");
g_error_free(other);
Check(Bus::ErrorFrom(nullptr).kind == BusError::Kind::Other, "no error is a harmless Other");
// org.freedesktop.Application's three calls, as a launch forwards them and the instance reads them.
GVariant* activate = g_variant_ref_sink(Bus::ActivateParameters());
Check(g_variant_is_of_type(activate, G_VARIANT_TYPE("(a{sv})")), "Activate's parameters: the platform data");
g_variant_unref(activate);
const std::vector<std::string> links = { "sms:+31612345678", "sms:1234" };
GVariant* open = g_variant_ref_sink(Bus::OpenParameters(links));
Check(g_variant_is_of_type(open, G_VARIANT_TYPE("(asa{sv})")) && Bus::OpenUris(open) == links, "Open's parameters: the URIs, in order, and the platform data");
g_variant_unref(open);
GVariant* none = g_variant_ref_sink(Bus::OpenParameters({}));
Check(Bus::OpenUris(none).empty(), "an Open without a URI reads as none");
g_variant_unref(none);
GVariant* quit = g_variant_ref_sink(Bus::ActionParameters(Bus::quitAction));
Check(g_variant_is_of_type(quit, G_VARIANT_TYPE("(sava{sv})")) && Bus::ActionName(quit) == "quit", "ActivateAction's parameters: the action's name, no parameter, the platform data");
g_variant_unref(quit);
GVariant* options = g_variant_ref_sink(Bus::SendOptions(true));
gboolean report = FALSE;
Check(g_variant_lookup(options, "status-report", "b", &report) && report == TRUE, "Send's options carry status-report");
g_variant_unref(options);
Check(Bus::Preview("\nYour code: 1234") == "Your code: 1234" && Bus::Preview("\r\n\nhello\nworld") == "hello…" && Bus::Preview(" \t\nYour code: 1234") == "Your code: 1234" && Bus::Preview(" hello") == "hello", "a notification's preview is the first line with text on it: leading line breaks and blanks are skipped");
Check(Bus::MarkupEscaped("in <5 min & \"more\"") == "in &lt;5 min &amp; &quot;more&quot;", "a notification body is escaped for a server that renders markup");
return failures == 0 ? 0 : 1;
}