246 lines
10 KiB
C++
246 lines
10 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
module;
|
|
|
|
#include <systemd/sd-bus.h>
|
|
|
|
module Nostro:Client_impl;
|
|
import std;
|
|
import :Client;
|
|
import :Bus;
|
|
import :Account;
|
|
import :Bunq;
|
|
import :Currency;
|
|
import :Transaction;
|
|
|
|
using namespace Nostro;
|
|
using namespace Nostro::Bus;
|
|
|
|
DaemonError::DaemonError(std::string name, const std::string& message) : std::runtime_error(message), name(std::move(name)) {
|
|
}
|
|
|
|
Client Client::System(std::chrono::seconds timeout) {
|
|
sd_bus* bus = nullptr;
|
|
ThrowOnSdBus(sd_bus_open_system_with_description(&bus, "nostro"), "connect to the system bus");
|
|
BusPtr owned(bus);
|
|
ThrowOnSdBus(sd_bus_set_method_call_timeout(bus, static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>(timeout).count())), "connect to the system bus");
|
|
ThrowOnSdBus(sd_bus_set_allow_interactive_authorization(bus, 1), "connect to the system bus");
|
|
return Client(std::move(owned));
|
|
}
|
|
|
|
Client::Client(BusPtr bus) : bus(std::move(bus)) {
|
|
}
|
|
|
|
namespace {
|
|
// nostrod's error, as an exception: input it refused as std::invalid_argument, the rest as a DaemonError.
|
|
[[noreturn]] void Throw(const sd_bus_error& error, bool changes) {
|
|
const std::string name = ErrorName(error);
|
|
const std::string message = ErrorMessage(error);
|
|
if(name == SD_BUS_ERROR_SERVICE_UNKNOWN) {
|
|
throw std::runtime_error(std::format("nostrod is not running, and the system bus cannot start it: is it installed? ({})", message));
|
|
}
|
|
if(name == errorInvalidArgument) {
|
|
throw std::invalid_argument(message);
|
|
}
|
|
if(changes && (name == SD_BUS_ERROR_NO_REPLY || name == SD_BUS_ERROR_TIMEOUT)) {
|
|
throw std::runtime_error(std::format("nostrod did not answer in time ({}): what you asked for may have been carried out, check before trying again", message));
|
|
}
|
|
throw DaemonError(name, message);
|
|
}
|
|
|
|
// Calls `member` with what `append` appends, reads the answer with `read`, and keeps the notes that end it.
|
|
// `changes` says whether the call moves money or changes anything, for what a lost answer means.
|
|
template<typename AppendArguments, typename ReadAnswer>
|
|
auto Call(sd_bus* bus, std::vector<std::string>& notes, std::string_view member, bool changes, AppendArguments&& append, ReadAnswer&& read) {
|
|
sd_bus_message* raw = nullptr;
|
|
ThrowOnSdBus(sd_bus_message_new_method_call(bus, &raw, busName.data(), objectPath.data(), interfaceName.data(), std::string(member).c_str()), "call nostrod");
|
|
const MessagePtr call(raw);
|
|
if(IsSensitive(member)) {
|
|
ThrowOnSdBus(sd_bus_message_sensitive(raw), "call nostrod");
|
|
}
|
|
append(raw);
|
|
Error error;
|
|
sd_bus_message* answer = nullptr;
|
|
if(sd_bus_call(bus, raw, 0, &error.error, &answer) < 0) {
|
|
Throw(error.error, changes);
|
|
}
|
|
const MessagePtr reply(answer);
|
|
auto result = read(answer);
|
|
const std::vector<std::string> more = Read<std::vector<std::string>>(answer);
|
|
notes.insert(notes.end(), more.begin(), more.end());
|
|
return result;
|
|
}
|
|
|
|
void Nothing(sd_bus_message*) {
|
|
}
|
|
|
|
// The answer of a call that answers nothing but its notes.
|
|
std::monostate NoAnswer(sd_bus_message*) {
|
|
return std::monostate();
|
|
}
|
|
|
|
AddedConnector ReadAdded(sd_bus_message* reply) {
|
|
AddedConnector added;
|
|
added.name = Read<std::string>(reply);
|
|
added.environment = Read<std::string>(reply);
|
|
added.accounts = Read<std::uint32_t>(reply);
|
|
return added;
|
|
}
|
|
|
|
Created ReadCreated(sd_bus_message* reply) {
|
|
Created created;
|
|
created.id = Read<std::uint64_t>(reply);
|
|
created.currency = Read<Currency>(reply);
|
|
return created;
|
|
}
|
|
|
|
// The arguments a payment, a draft, a schedule and a request share.
|
|
auto PaymentArguments(std::string_view account, std::string_view counterparty, std::string_view name, std::int64_t amount, std::string_view description) {
|
|
return [=](sd_bus_message* call) {
|
|
Append(call, std::string(account));
|
|
Append(call, std::string(counterparty));
|
|
Append(call, std::string(name));
|
|
Append(call, amount);
|
|
Append(call, std::string(description));
|
|
};
|
|
}
|
|
|
|
auto AccountArgument(std::string_view account) {
|
|
return [account](sd_bus_message* call) {
|
|
Append(call, std::string(account));
|
|
};
|
|
}
|
|
|
|
auto AccountAndId(std::string_view account, std::uint64_t id) {
|
|
return [account, id](sd_bus_message* call) {
|
|
Append(call, std::string(account));
|
|
Append(call, id);
|
|
};
|
|
}
|
|
|
|
template<typename T>
|
|
T ReadAs(sd_bus_message* reply) {
|
|
return Read<T>(reply);
|
|
}
|
|
}
|
|
|
|
std::vector<ConnectorSummary> Client::Connectors() {
|
|
return Call(bus.get(), notes, "Connectors", false, Nothing, ReadAs<std::vector<ConnectorSummary>>);
|
|
}
|
|
|
|
AddedConnector Client::AddBunq(std::string_view apiKey, std::string_view allowedIps, std::string_view environment) {
|
|
return Call(bus.get(), notes, "AddBunq", true, [=](sd_bus_message* call) {
|
|
Append(call, std::string(apiKey));
|
|
Append(call, std::string(allowedIps));
|
|
Append(call, std::string(environment));
|
|
}, ReadAdded);
|
|
}
|
|
|
|
AddedConnector Client::AddBunqOAuth(std::string_view environment, std::string_view clientId, std::string_view clientSecret, std::string_view redirectUri, std::string_view code) {
|
|
return Call(bus.get(), notes, "AddBunqOAuth", true, [=](sd_bus_message* call) {
|
|
Append(call, std::string(environment));
|
|
Append(call, std::string(clientId));
|
|
Append(call, std::string(clientSecret));
|
|
Append(call, std::string(redirectUri));
|
|
Append(call, std::string(code));
|
|
}, ReadAdded);
|
|
}
|
|
|
|
std::string Client::RemoveConnector(std::string_view connector) {
|
|
return Call(bus.get(), notes, "RemoveConnector", true, AccountArgument(connector), ReadAs<std::string>);
|
|
}
|
|
|
|
std::vector<ConnectorAccounts> Client::Accounts() {
|
|
return Call(bus.get(), notes, "Accounts", false, Nothing, ReadAs<std::vector<ConnectorAccounts>>);
|
|
}
|
|
|
|
std::vector<Transaction> Client::Transactions(std::string_view account, const TransactionQuery& query) {
|
|
return Call(bus.get(), notes, "Transactions", false, [account, &query](sd_bus_message* call) {
|
|
Append(call, std::string(account));
|
|
AppendQuery(call, query);
|
|
}, ReadAs<std::vector<Transaction>>);
|
|
}
|
|
|
|
Created Client::Send(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description) {
|
|
return Call(bus.get(), notes, "Send", true, PaymentArguments(account, to, name, amount, description), ReadCreated);
|
|
}
|
|
|
|
Created Client::Schedule(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description, const BunqSchedule& schedule) {
|
|
const auto payment = PaymentArguments(account, to, name, amount, description);
|
|
return Call(bus.get(), notes, "Schedule", true, [&payment, &schedule](sd_bus_message* call) {
|
|
payment(call);
|
|
Append(call, schedule);
|
|
}, ReadCreated);
|
|
}
|
|
|
|
Created Client::Draft(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description) {
|
|
return Call(bus.get(), notes, "Draft", true, PaymentArguments(account, to, name, amount, description), ReadCreated);
|
|
}
|
|
|
|
BunqPaymentRequest Client::Request(std::string_view account, std::string_view from, std::string_view name, std::int64_t amount, std::string_view description) {
|
|
return Call(bus.get(), notes, "Request", true, PaymentArguments(account, from, name, amount, description), ReadAs<BunqPaymentRequest>);
|
|
}
|
|
|
|
std::vector<BunqPaymentRequest> Client::Requests(std::string_view account) {
|
|
return Call(bus.get(), notes, "Requests", false, AccountArgument(account), ReadAs<std::vector<BunqPaymentRequest>>);
|
|
}
|
|
|
|
void Client::RevokeRequest(std::string_view account, std::uint64_t id) {
|
|
Call(bus.get(), notes, "RevokeRequest", true, AccountAndId(account, id), NoAnswer);
|
|
}
|
|
|
|
std::vector<BunqIncomingRequest> Client::IncomingRequests(std::string_view account) {
|
|
return Call(bus.get(), notes, "IncomingRequests", false, AccountArgument(account), ReadAs<std::vector<BunqIncomingRequest>>);
|
|
}
|
|
|
|
void Client::RespondToRequest(std::string_view account, std::uint64_t id, bool accept) {
|
|
Call(bus.get(), notes, "RespondToRequest", true, [account, id, accept](sd_bus_message* call) {
|
|
Append(call, std::string(account));
|
|
Append(call, id);
|
|
Append(call, accept);
|
|
}, NoAnswer);
|
|
}
|
|
|
|
std::vector<BunqScheduledPayment> Client::ScheduledPayments(std::string_view account) {
|
|
return Call(bus.get(), notes, "ScheduledPayments", false, AccountArgument(account), ReadAs<std::vector<BunqScheduledPayment>>);
|
|
}
|
|
|
|
void Client::CancelScheduledPayment(std::string_view account, std::uint64_t id) {
|
|
Call(bus.get(), notes, "CancelScheduledPayment", true, AccountAndId(account, id), NoAnswer);
|
|
}
|
|
|
|
std::vector<BunqDraftPayment> Client::DraftPayments(std::string_view account) {
|
|
return Call(bus.get(), notes, "DraftPayments", false, AccountArgument(account), ReadAs<std::vector<BunqDraftPayment>>);
|
|
}
|
|
|
|
void Client::RespondToDraftPayment(std::string_view account, std::uint64_t id, bool accept) {
|
|
Call(bus.get(), notes, "RespondToDraftPayment", true, [account, id, accept](sd_bus_message* call) {
|
|
Append(call, std::string(account));
|
|
Append(call, id);
|
|
Append(call, accept);
|
|
}, NoAnswer);
|
|
}
|
|
|
|
std::vector<ConnectorCards> Client::Cards() {
|
|
return Call(bus.get(), notes, "Cards", false, Nothing, ReadAs<std::vector<ConnectorCards>>);
|
|
}
|
|
|
|
BunqCard Client::SetCardStatus(std::uint64_t card, std::string_view status) {
|
|
return Call(bus.get(), notes, "SetCardStatus", true, [card, status](sd_bus_message* call) {
|
|
Append(call, card);
|
|
Append(call, std::string(status));
|
|
}, ReadAs<BunqCard>);
|
|
}
|
|
|
|
std::optional<Account> Client::DefaultCheckoutAccount() {
|
|
return Call(bus.get(), notes, "DefaultCheckoutAccount", false, Nothing, ReadAs<std::optional<Account>>);
|
|
}
|
|
|
|
Account Client::SetDefaultCheckoutAccount(std::string_view account) {
|
|
return Call(bus.get(), notes, "SetDefaultCheckoutAccount", true, AccountArgument(account), ReadAs<Account>);
|
|
}
|
|
|
|
std::vector<std::string> Client::TakeNotes() {
|
|
return std::exchange(notes, {});
|
|
}
|