// SPDX-License-Identifier: LGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® module; #ifdef NOSTRO_DBUS_ENABLED #include #endif export module Nostro:Client; #ifdef NOSTRO_DBUS_ENABLED import std; import :Account; import :Bunq; import :Bus; import :Transaction; namespace Nostro { // An error nostrod answered with, by its D-Bus name, for the errors a client tells apart: Bus::errorBank, // Bus::errorOAuthCannotPay, Bus::errorCancelled, org.freedesktop.DBus.Error.AccessDenied and the like. Input // nostrod refused before doing anything comes as std::invalid_argument instead. export struct DaemonError : std::runtime_error { std::string name; DaemonError(std::string name, const std::string& message); }; // nostrod is not running, and the system bus cannot start it: not installed, or not set up. export struct DaemonMissing : std::runtime_error { using std::runtime_error::runtime_error; }; // A call that changes something got no answer in time, or bunq answered it with an error that leaves open whether // it was carried out (a 5xx, BunqError::maybeCarriedOut): what was asked may have happened. Check before asking again. export struct OutcomeUnknown : std::runtime_error { using std::runtime_error::runtime_error; }; // What an asynchronous call answers: its result, or the exception the blocking call would have thrown, and the // library's warnings that came with it. An error answer carries no notes. export template struct Answer { std::variant result; std::vector notes; // The result, or the exception rethrown. T& Get() { if(const std::exception_ptr* error = std::get_if(&result)) { std::rethrow_exception(*error); } return std::get(result); } bool Failed() const { return std::holds_alternative(result); } // The exception, or null when the call worked. std::exception_ptr Error() const { const std::exception_ptr* error = std::get_if(&result); return error ? *error : nullptr; } }; // nostrod over the system bus: each method of its interface (Bus::methods), with nostro's own types. `account` is // an account's name or IBAN. A call may wait for a password dialog, which nostrod has polkit show, and for the // bank. The library's warnings during the calls come out of TakeNotes(). // // Each method also has an asynchronous form, for a program with an event loop that must not wait: Attach() the // client to an sd-event loop, and the method returns at once with a Pending, and calls `then` from that loop once // nostrod answered, with an Answer. Notes are collected per user by nostrod's worker, so an answer's notes can be // another call's of the same user, the CLI's included. export class Client { public: // An asynchronous call whose answer is awaited. Destroying it, or Cancel(), drops the answer: `then` is not // called. It does not undo the call, which nostrod and the bank carry on with. class Pending { public: Pending() = default; Pending(Pending&& other) noexcept; Pending& operator=(Pending&& other) noexcept; Pending(const Pending&) = delete; Pending& operator=(const Pending&) = delete; ~Pending(); void Cancel(); // Whether `then` is still to be called. bool Active() const; private: friend class Client; explicit Pending(sd_bus_slot* slot); sd_bus_slot* slot = nullptr; }; template using Then = std::function)>; // Connects to the system bus; D-Bus starts nostrod with the first call. A call waits up to `timeout` for its // answer: long enough for the user to type a password, and for the bank's rate limits. Throws when there is // no system bus. static Client System(std::chrono::seconds timeout = std::chrono::minutes(10)); // Hands the connection to `event`, whose loop then sends the asynchronous calls and delivers their answers. // Throws when the client is attached already. void Attach(sd_event* event); // Whether the connection to the bus is still open; once it is not, make a new client. bool Connected() const; std::vector Connectors(); [[nodiscard]] Pending Connectors(Then> then); // The key says itself which environment it is of, by its prefix: leave `environment` empty. Naming one, // "production" or "sandbox", only has a key of the other refused (std::invalid_argument), before anything is sent. AddedConnector AddBunq(std::string_view apiKey, std::string_view allowedIps, std::string_view environment); [[nodiscard]] Pending AddBunq(std::string_view apiKey, std::string_view allowedIps, std::string_view environment, Then then); AddedConnector AddBunqOAuth(std::string_view environment, std::string_view clientId, std::string_view clientSecret, std::string_view redirectUri, std::string_view code); [[nodiscard]] Pending AddBunqOAuth(std::string_view environment, std::string_view clientId, std::string_view clientSecret, std::string_view redirectUri, std::string_view code, Then then); // By name, or by its number in Connectors(); returns the name. std::string RemoveConnector(std::string_view connector); [[nodiscard]] Pending RemoveConnector(std::string_view connector, Then then); std::vector Accounts(); [[nodiscard]] Pending Accounts(Then> then); // Fetches what is new from the bank first, then answers from the stored history; with `query.stored` from the // stored history alone, at once and without the bank. std::vector Transactions(std::string_view account, const TransactionQuery& query); [[nodiscard]] Pending Transactions(std::string_view account, const TransactionQuery& query, Then> then); Created Send(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description); [[nodiscard]] Pending Send(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description, Then then); Created Schedule(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description, const BunqSchedule& schedule); [[nodiscard]] Pending Schedule(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description, const BunqSchedule& schedule, Then then); Created Draft(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description); [[nodiscard]] Pending Draft(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description, Then then); BunqPaymentRequest Request(std::string_view account, std::string_view from, std::string_view name, std::int64_t amount, std::string_view description); [[nodiscard]] Pending Request(std::string_view account, std::string_view from, std::string_view name, std::int64_t amount, std::string_view description, Then then); std::vector Requests(std::string_view account); [[nodiscard]] Pending Requests(std::string_view account, Then> then); void RevokeRequest(std::string_view account, std::uint64_t id); [[nodiscard]] Pending RevokeRequest(std::string_view account, std::uint64_t id, Then then); std::vector IncomingRequests(std::string_view account); [[nodiscard]] Pending IncomingRequests(std::string_view account, Then> then); void RespondToRequest(std::string_view account, std::uint64_t id, bool accept); [[nodiscard]] Pending RespondToRequest(std::string_view account, std::uint64_t id, bool accept, Then then); std::vector ScheduledPayments(std::string_view account); [[nodiscard]] Pending ScheduledPayments(std::string_view account, Then> then); void CancelScheduledPayment(std::string_view account, std::uint64_t id); [[nodiscard]] Pending CancelScheduledPayment(std::string_view account, std::uint64_t id, Then then); std::vector DraftPayments(std::string_view account); [[nodiscard]] Pending DraftPayments(std::string_view account, Then> then); void RespondToDraftPayment(std::string_view account, std::uint64_t id, bool accept); [[nodiscard]] Pending RespondToDraftPayment(std::string_view account, std::uint64_t id, bool accept, Then then); std::vector Cards(); [[nodiscard]] Pending Cards(Then> then); // "ACTIVE", "DEACTIVATED", "LOST" or "STOLEN"; returns the card as bunq has it afterwards. BunqCard SetCardStatus(std::uint64_t card, std::string_view status); [[nodiscard]] Pending SetCardStatus(std::uint64_t card, std::string_view status, Then then); // The account apps' checkouts (Nostro.Checkout) pay from: none until one is chosen, or once it is gone. std::optional DefaultCheckoutAccount(); [[nodiscard]] Pending DefaultCheckoutAccount(Then> then); // Chooses it, and returns it. An account reached through OAuth cannot pay a shop, and is refused. Account SetDefaultCheckoutAccount(std::string_view account); [[nodiscard]] Pending SetDefaultCheckoutAccount(std::string_view account, Then then); // The library's warnings from the blocking calls so far, taken out. std::vector TakeNotes(); private: explicit Client(Bus::BusPtr bus); Bus::BusPtr bus; std::vector notes; }; } #endif