imsd-messages/interfaces/ImsdMessages.Bus-Variant.cppm

130 lines
5.9 KiB
Text
Raw Permalink Normal View History

// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
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).
2026-10-17 23:45:29 +02:00
// lint-disable-file no-char-pointer
/*
ImsdMessages.Bus:Variant — imsd's dictionaries and errors as the core's
types. The keys and types are those imsd writes (AGENTS.md §8.4: the
Received dictionary of its SmsVariant, GetStatus's): a key that is
missing or of another type reads as its default.
*/
module;
#include <gio/gio.h>
export module ImsdMessages.Bus:Variant;
import std;
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).
2026-10-17 23:45:29 +02:00
import ImsdMessages;
namespace ImsdMessages::Bus {
std::string LookupString(GVariant* dict, const char* key) {
GVariant* v = g_variant_lookup_value(dict, key, G_VARIANT_TYPE_STRING);
if (!v) return "";
std::string s = g_variant_get_string(v, nullptr);
g_variant_unref(v);
return s;
}
template <class T>
T LookupScalar(GVariant* dict, const char* key, const GVariantType* type, T (*get)(GVariant*), T fallback) {
GVariant* v = g_variant_lookup_value(dict, key, type);
if (!v) return fallback;
const T x = get(v);
g_variant_unref(v);
return x;
}
std::vector<std::uint8_t> LookupBytes(GVariant* dict, const char* key) {
GVariant* v = g_variant_lookup_value(dict, key, G_VARIANT_TYPE_BYTESTRING);
if (!v) return {};
gsize n = 0;
const guint8* p = static_cast<const guint8*>(g_variant_get_fixed_array(v, &n, 1));
std::vector<std::uint8_t> out(p, p + n);
g_variant_unref(v);
return out;
}
std::optional<std::uint32_t> LookupOptionalUint32(GVariant* dict, const char* key) {
GVariant* v = g_variant_lookup_value(dict, key, G_VARIANT_TYPE_UINT32);
if (!v) return std::nullopt;
const std::uint32_t x = g_variant_get_uint32(v);
g_variant_unref(v);
return x;
}
}
export namespace ImsdMessages::Bus {
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).
2026-10-17 23:45:29 +02:00
// The GLib the bus layer runs on, for --version.
inline const std::string glibVersion = std::format("{}.{}.{}", glib_major_version, glib_minor_version, glib_micro_version);
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).
2026-10-17 23:45:29 +02:00
// The number of a tel: URI (the status's `ppi`, a Received's `to`);
// "" for none or another kind of URI.
std::string TelNumber(std::string_view uri) { return uri.starts_with("tel:") ? std::string(uri.substr(4)) : ""; }
// The a{sv} of a Received signal or a List entry.
ReceivedMessage ReceivedFromVariant(std::string_view id, GVariant* dict) {
ReceivedMessage m;
m.imsdId = std::string(id);
m.from = LookupString(dict, "from");
m.text = LookupString(dict, "text");
m.timestamp = LookupString(dict, "timestamp");
m.received = LookupScalar<gint64>(dict, "received", G_VARIANT_TYPE_INT64, g_variant_get_int64, 0);
m.sc = LookupString(dict, "sc");
m.to = TelNumber(LookupString(dict, "to"));
m.simOperator = LookupString(dict, "operator");
m.smsClass = LookupScalar<gint32>(dict, "class", G_VARIANT_TYPE_INT32, g_variant_get_int32, -1);
m.pid = LookupScalar<guint8>(dict, "pid", G_VARIANT_TYPE_BYTE, g_variant_get_byte, 0);
m.dcs = LookupScalar<guint8>(dict, "dcs", G_VARIANT_TYPE_BYTE, g_variant_get_byte, 0);
m.segments = LookupScalar<gint32>(dict, "segments", G_VARIANT_TYPE_INT32, g_variant_get_int32, 1);
m.messageWaiting = LookupScalar<gboolean>(dict, "message-waiting", G_VARIANT_TYPE_BOOLEAN, g_variant_get_boolean, FALSE) == TRUE;
m.portDst = LookupOptionalUint32(dict, "port-dst");
m.portSrc = LookupOptionalUint32(dict, "port-src");
m.data = LookupBytes(dict, "data");
m.pdu = LookupBytes(dict, "pdu");
return m;
}
// The a{sv} of GetStatus.
Status StatusFromVariant(GVariant* dict) {
Status s;
s.enabled = LookupScalar<gboolean>(dict, "enabled", G_VARIANT_TYPE_BOOLEAN, g_variant_get_boolean, FALSE) == TRUE;
s.smsc = LookupString(dict, "smsc");
s.ownNumber = TelNumber(LookupString(dict, "ppi")); // the frozen interface's `ppi`, in this answer too
s.simOperator = LookupString(dict, "operator");
s.registered = LookupScalar<gboolean>(dict, "registered", G_VARIANT_TYPE_BOOLEAN, g_variant_get_boolean, FALSE) == TRUE;
s.subscribers = LookupScalar<guint32>(dict, "subscribers", G_VARIANT_TYPE_UINT32, g_variant_get_uint32, 0);
return s;
}
// A GError as the core's BusError; the remote error's name is read off
// GDBus's mapping and stripped from the text. The caller frees err.
BusError ErrorFrom(GError* err) {
BusError e;
if (!err) return e;
if (err->domain == G_DBUS_ERROR) {
switch (err->code) {
case G_DBUS_ERROR_ACCESS_DENIED: e.kind = BusError::Kind::AccessDenied; break;
case G_DBUS_ERROR_NOT_SUPPORTED: e.kind = BusError::Kind::NotSupported; break;
case G_DBUS_ERROR_FILE_NOT_FOUND: e.kind = BusError::Kind::NotFound; break;
case G_DBUS_ERROR_INVALID_ARGS: e.kind = BusError::Kind::InvalidArgs; break;
case G_DBUS_ERROR_SERVICE_UNKNOWN:
case G_DBUS_ERROR_NAME_HAS_NO_OWNER: e.kind = BusError::Kind::NoOwner; break;
case G_DBUS_ERROR_NO_REPLY:
case G_DBUS_ERROR_TIMEOUT:
case G_DBUS_ERROR_TIMED_OUT: e.kind = BusError::Kind::Timeout; break;
default: e.kind = BusError::Kind::Other; break;
}
} else if (g_error_matches(err, G_IO_ERROR, G_IO_ERROR_TIMED_OUT)) {
e.kind = BusError::Kind::Timeout;
}
g_dbus_error_strip_remote_error(err);
e.message = err->message ? err->message : "";
return e;
}
// Send's options dictionary (floating).
GVariant* SendOptions(bool statusReport) {
GVariantBuilder b;
g_variant_builder_init(&b, G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(&b, "{sv}", "status-report", g_variant_new_boolean(statusReport ? TRUE : FALSE));
return g_variant_builder_end(&b);
}
}