From 367c19386c13e2f65cf0b8703d0aaa6a1bfa5a42 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Sat, 31 Oct 2026 20:50:57 +0100 Subject: [PATCH] pre-ai --- .gitignore | 2 + README.md | 1 + implementations/Nostro-Account.cpp | 210 +++++++++++++++++++++++++ implementations/Nostro-Bunq.cpp | 105 +++++++++---- implementations/Nostro-Connector.cpp | 92 +---------- implementations/Nostro-File.cpp | 81 ++++++++++ implementations/Nostro-Transaction.cpp | 13 ++ implementations/main.cpp | 95 ++++++----- interfaces/Nostro-Account.cppm | 14 +- interfaces/Nostro-Bunq.cppm | 4 +- interfaces/Nostro-Connector.cppm | 3 - interfaces/Nostro-Currency.cppm | 2 +- interfaces/Nostro-File.cppm | 19 +++ interfaces/Nostro-Transaction.cppm | 10 +- interfaces/Nostro-Utils.cppm | 69 ++++++++ interfaces/Nostro.cppm | 2 + project.cpp | 5 + 17 files changed, 561 insertions(+), 166 deletions(-) create mode 100644 .gitignore create mode 100644 implementations/Nostro-Account.cpp create mode 100644 implementations/Nostro-File.cpp create mode 100644 implementations/Nostro-Transaction.cpp create mode 100644 interfaces/Nostro-File.cppm create mode 100644 interfaces/Nostro-Utils.cppm diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1cb8379 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin/ +build/ \ No newline at end of file diff --git a/README.md b/README.md index 26388ac..410155e 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ nostro connectors # shows currently added connectors and their status nostro accounts # shows all accounts of all connectors nostro connectors add {type} {...} nostro connectors add bunq {api key} {allowed ip, optional} +nostro send {account name} {recipient iban} {recipient name} {amount} {description} # amount is in the smallest unit of your currency, so for EUR 100 = €1.00 ``` ## Supported banks diff --git a/implementations/Nostro-Account.cpp b/implementations/Nostro-Account.cpp new file mode 100644 index 0000000..ce2eb96 --- /dev/null +++ b/implementations/Nostro-Account.cpp @@ -0,0 +1,210 @@ +//SPDX-License-Identifier: LGPL-3.0-only +//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +module Nostro:Account_impl; +import :Account; +import :File; +import std; + +using namespace Nostro; +namespace fs = std::filesystem; + +std::chrono::system_clock::time_point Account::GetLastTransactionTime(std::string_view connector) const { + const fs::path path = cachePath / "transactions" / connector / (name + ".bin"); + if(!fs::exists(path) || fs::file_size(path) == 0) { + return std::chrono::system_clock::time_point::min(); + } + + std::ifstream file(path, std::ios::binary); + if(!file) { + throw std::runtime_error(std::format("cannot open {}", path.string())); + } + + file.seekg(-static_cast(sizeof(std::int64_t)), std::ios::end); + std::int64_t lastTime; + file.read(reinterpret_cast(&lastTime), sizeof(lastTime)); + return std::chrono::system_clock::time_point(std::chrono::microseconds(lastTime)); +} + +void Account::SaveTransactions(std::string_view connector) { + const fs::path path = cachePath / "transactions" / connector / (name + ".bin"); + CreatePrivateFile(path); + std::ofstream file(path, std::ios::in | std::ios::out | std::ios::binary); + if(!file) { + throw std::runtime_error(std::format("cannot open {}", path.string())); + } + + const std::chrono::system_clock::time_point last = GetLastTransactionTime(connector); + + std::uint32_t pendingIndex = transactions.size(); + for(std::uint32_t i = 0; i < transactions.size(); i++) { + if(transactions[i].timePoint > last) { + pendingIndex = i; + break; + } + } + + file.seekp(0, std::ios::end); + for(std::uint32_t i = pendingIndex; i < transactions.size(); i++) { + WriteString(file, transactions[i].name); + WriteString(file, transactions[i].bic); + WriteString(file, transactions[i].iban); + WriteString(file, transactions[i].description); + file.write(reinterpret_cast(&transactions[i].amount), sizeof(transactions[i].amount)); + file.write(reinterpret_cast(&transactions[i].currency), sizeof(Currency)); + std::int64_t time = std::chrono::duration_cast(transactions[i].timePoint.time_since_epoch()).count(); + file.write(reinterpret_cast(&time), sizeof(time)); + } +} + +Transaction& Account::LoadTransaction(std::ifstream& file) { + Transaction& tr = transactions.emplace_back(); + ReadString(file, tr.name); + ReadString(file, tr.bic); + ReadString(file, tr.iban); + ReadString(file, tr.description); + file.read(reinterpret_cast(&tr.amount), sizeof(tr.amount)); + file.read(reinterpret_cast(&tr.currency), sizeof(tr.currency)); + std::int64_t time; + file.read(reinterpret_cast(&time), sizeof(time)); + tr.timePoint = std::chrono::system_clock::time_point(std::chrono::duration_cast(std::chrono::microseconds(time))); + return tr; +} + +void Account::LoadTransactions(std::string_view connector) { + const fs::path path = cachePath / "transactions" / connector / (name + ".bin"); + if(!fs::exists(path)) { + return; + } + + std::fstream file(path, std::ios::in | std::ios::binary); + if(!file) { + throw std::runtime_error(std::format("cannot open {}", path.string())); + } + + while(file.peek() != std::fstream::traits_type::eof()) { + LoadTransaction(); + } +} + +std::ifstream Account::LoadUntilFrom(std::string_view connector, std::chrono::system_clock::time_point from) { + const fs::path path = cachePath / "transactions" / connector / (name + ".bin"); + if(!fs::exists(path)) { + return; + } + + std::ifstream file(path, std::ios::in | std::ios::binary); + if(!file) { + throw std::runtime_error(std::format("cannot open {}", path.string())); + } + + while(file.peek() != std::fstream::traits_type::eof()) { + std::uint16_t seeked = SeekString(); + seeked += SeekString(); + seeked += SeekString(); + seeked += SeekString(); + in.seekg(sizeof(tr.amount)+sizeof(tr.Currency), std::ios::cur); + std::int64_t timeI; + file.read(reinterpret_cast(&timeI), sizeof(tr.timeI)); + std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::from_time_t(timeI); + if(timePoint > from) { + in.seekg(-seeked-sizeof(tr.amount)-sizeof(tr.Currency)-sizeof(tr.timeI), std::ios::cur); + LoadTransaction(); + break; + } + } + + return file; +} + +void Account::LoadTransactions(std::string_view connector, std::chrono::system_clock::time_point from, std::chrono::system_clock::time_point to) { + std::ifstream file = LoadUntilFrom(connector, from); + while(file.peek() != std::fstream::traits_type::eof()) { + Transaction& tr = LoadTransaction(); + if(tr.timePoint > to) { + transactions.pop_back(); + break; + } + } +} + +void Account::LoadTransactions(std::string_view connector, std::chrono::system_clock::time_point from) { + std::ifstream file = LoadUntilFrom(connector, from); + while(file.peek() != std::fstream::traits_type::eof()) { + LoadTransaction(); + } +} + + +void Account::LoadTransactionsByProperty(std::string_view connector, std::string_view property) { + const fs::path path = cachePath / "transactions" / connector / (name + ".bin"); + if(!fs::exists(path)) { + return; + } + + std::ifstream file(path, std::ios::in | std::ios::binary); + if(!file) { + throw std::runtime_error(std::format("cannot open {}", path.string())); + } + + while(file.peek() != std::fstream::traits_type::eof()) { + std::uint16_t seeked = SeekString(); + seeked += SeekString(); + seeked += SeekString(); + std::uint16_t size; + file.read(reinterpret_cast(&size), sizeof(size)); + if(description.size() == size) { + std::string readDescription; + readDescription.resize(size); + file.read(readDescription.data(), size); + if(readDescription == description) { + in.seekg(-seeked-readDescription.size()-sizeof(size), std::ios::cur); + Transaction& tr = transactions.emplace_back(); + ReadString(file, tr.name); + ReadString(file, tr.bic); + ReadString(file, tr.iban); + SeekString(); + file.read(reinterpret_cast(&tr.amount), sizeof(tr.amount)); + file.read(reinterpret_cast(&tr.currency), sizeof(tr.Currency)); + file.read(reinterpret_cast(&tr.time), sizeof(tr.time)); + } + } + } +} + +void Account::LoadTransactionsByIban(std::string_view connector, std::string_view iban) { + transactions.clear(); + + const fs::path path = cachePath / "transactions" / connector / (name + ".bin"); + if(!fs::exists(path)) { + return; + } + + std::ifstream file(path, std::ios::in | std::ios::binary); + if(!file) { + throw std::runtime_error(std::format("cannot open {}", path.string())); + } + + while(file.peek() != std::fstream::traits_type::eof()) { + std::uint16_t seeked = SeekString(); + seeked += SeekString(); + std::uint16_t size; + file.read(reinterpret_cast(&size), sizeof(size)); + if(iban.size() == size) { + std::string readIban; + readIban.resize(size); + file.read(readIban.data(), size); + if(readIban == iban) { + in.seekg(-seeked-readIban.size()-sizeof(size), std::ios::cur); + Transaction& tr = transactions.emplace_back(); + ReadString(file, tr.name); + ReadString(file, tr.bic); + SeekString(); + ReadString(file, tr.description); + file.read(reinterpret_cast(&tr.amount), sizeof(tr.amount)); + file.read(reinterpret_cast(&tr.currency), sizeof(tr.Currency)); + file.read(reinterpret_cast(&tr.time), sizeof(tr.time)); + } + } + } +} \ No newline at end of file diff --git a/implementations/Nostro-Bunq.cpp b/implementations/Nostro-Bunq.cpp index ea4b739..068569e 100644 --- a/implementations/Nostro-Bunq.cpp +++ b/implementations/Nostro-Bunq.cpp @@ -14,7 +14,9 @@ module Nostro:Bunq_impl; import std; import :Bunq; +import :Connector; import :Crypto; +import :Utils; import Crafter.Network; using namespace Nostro; @@ -24,27 +26,6 @@ using namespace Crafter; std::vector BunqConnector::connectors; -std::string EscB(std::string_view s) { - std::string out; - out.reserve(s.size() + 8); - for (const char c : s) { - switch (c) { - case '"': out += "\\\""; break; - case '\\': out += "\\\\"; break; - case '\n': out += "\\n"; break; - case '\r': out += "\\r"; break; - case '\t': out += "\\t"; break; - default: - if (static_cast(c) < 0x20) { - out += std::format("\\u{:04x}", static_cast(c)); - } else { - out += c; - } - } - } - return out; -} - json InResponse(const json& doc, std::string_view key) { for (const json& item : doc.at("Response")) { if (item.is_object()) { @@ -64,13 +45,21 @@ json GetUser(std::string_view body) { } } +std::string StringOrEmpty(const json& object, std::string_view key) { + const json::const_iterator value = object.find(key); + if(value == object.end() || !value->is_string()) { + return ""; + } + return value->get(); +} + BunqAccount::BunqAccount(std::string&& name, std::string&& bic, std::string&& iban, Currency currency, std::int64_t balance, std::uint32_t id, BunqConnector* connector) : Account(std::move(name), std::move(bic), std::move(iban), currency, balance), id(id), connector(connector) { } - -void BunqAccount::SendPayment(std::string body) const { - std::string route = std::format("v1/user/{}/monetary-account/{}/payment", connector->id, id); +void BunqAccount::Send(std::string_view iban, std::string_view name, std::int64_t amount, Currency currency, std::string_view description) const { + const std::string body = std::format("{{\"amount\": {{\"value\":\"{}\", \"currency\":\"{}\"}},\"counterparty_alias\":{{\"type\":\"IBAN\",\"value\":\"{}\", \"name\":\"{}\"}}, \"description\":\"{}\"}}", amount, GetCurrencyCode(currency), iban, name, description); + const std::string route = std::format("/v1/user/{}/monetary-account/{}/payment", connector->id, id); HTTPResponse response = connector->client.Send(CreateRequestHTTP("POST", route, "public-api.sandbox.bunq.com", {{"User-Agent", "Nostro Crafter.Network"}, {"Content-Type", "application/json"}, {"Accept", "application/json"}, {"X-Bunq-Client-Authentication", connector->sessionToken}, {"X-Bunq-Client-Signature", EscB(SignBody(connector->privateKey, body))}}, body @@ -81,18 +70,70 @@ void BunqAccount::SendPayment(std::string body) const { } } -void BunqAccount::Send(std::string_view iban, std::string_view name, std::int64_t amount, Currency currency) const { - std::string body = std::format("{{\"amount\": {{\"value\":{}, \"currency\":\"{}\"}},\"counterparty_alias\":{{\"type\":\"IBAN\",\"value\":{}, \"name\":{}}}}}", amount, GetCurrencyCode(currency), iban, name); - SendPayment(body); -} +void BunqConnector::GetNewTransactions() { + for(BunqAccount& account : accounts) { + account.transactions.clear(); -void BunqAccount::Send(std::string_view iban, std::string_view name, std::int64_t amount, Currency currency, std::string_view description) const { - std::string body = std::format("{{\"amount\": {{\"value\":{}, \"currency\":\"{}\"}},\"counterparty_alias\":{{\"type\":\"IBAN\",\"value\":{}, \"name\":{}}}, \"description\":\"{}\"}}", amount, GetCurrencyCode(currency), iban, name, description); - SendPayment(body); + const std::chrono::system_clock::time_point last = account.GetLastTransactionTime(connectorId); + + std::string route = std::format("/v1/user/{}/monetary-account/{}/payment?count=200", id, account.id); + while(!route.empty()) { + HTTPResponse response = client.Send(CreateRequestHTTP("GET", route, "public-api.sandbox.bunq.com", {{"User-Agent", "Nostro Crafter.Network"}, {"Content-Type", "application/json"}, {"Accept", "application/json"}, {"X-Bunq-Client-Authentication", sessionToken}})); + + if(response.status == "429") { + std::this_thread::sleep_for(std::chrono::seconds(2)); + continue; + } + + if(response.status != "200") { + throw std::runtime_error(std::format("{} non 200 status code: {}", route, response.body)); + } + + const json doc = json::parse(response.body); + + const json& olderUrl = doc.at("Pagination").at("older_url"); + if( olderUrl.is_null()) { + route.clear(); + } else { + route = olderUrl.get(); + } + + for(const json& item : doc.at("Response")) { + const json::const_iterator paymentIt = item.find("Payment"); + if(paymentIt == item.end()) { + continue; + } + + const json& paymentJ = *paymentIt; + const std::chrono::system_clock::time_point timePoint = ParseTimestamp(paymentJ.at("created").get()); + if(timePoint <= last) { + route.clear(); + break; + } + + const json& counterpartyJ = paymentJ.at("counterparty_alias"); + const Currency currency = GetCurrency(paymentJ.at("amount").at("currency").get()); + + account.transactions.emplace_back( + StringOrEmpty(counterpartyJ, "display_name"), + StringOrEmpty(counterpartyJ, "swift_bic"), + StringOrEmpty(counterpartyJ, "iban"), + StringOrEmpty(paymentJ, "description"), + currency, + ParseAmount(paymentJ.at("amount").at("value").get(), currency), + timePoint + ); + } + } + + std::ranges::reverse(account.transactions); + + account.SaveTransactions(); + } } void BunqConnector::RefreshSession() { - std::string body = std::format("{{\"secret\": \"{}\"}}", apiKey); + const std::string body = std::format("{{\"secret\": \"{}\"}}", apiKey); HTTPResponse response = client.Send(CreateRequestHTTP("POST", "/v1/session-server", "public-api.sandbox.bunq.com", {{"User-Agent", "Nostro Crafter.Network"}, {"Content-Type", "application/json"}, {"Accept", "application/json"}, {"X-Bunq-Client-Authentication", installationToken}, {"X-Bunq-Client-Signature", EscB(SignBody(privateKey, body))}}, body diff --git a/implementations/Nostro-Connector.cpp b/implementations/Nostro-Connector.cpp index 5c400c3..8e59e9f 100644 --- a/implementations/Nostro-Connector.cpp +++ b/implementations/Nostro-Connector.cpp @@ -14,75 +14,27 @@ module Nostro:Connector_impl; import std; import :Connector; import :Banks; +import :File; using namespace Nostro; using namespace nlohmann; namespace fs = std::filesystem; -fs::path Connector::GetConnectorPath() { - const char* const xdg = std::getenv("XDG_CONFIG_HOME"); - if(xdg) { - return fs::path(fs::path(std::move(xdg)) / "nostro/connectors.json"); - } - - const char* const home = std::getenv("HOME"); - if(home) { - return fs::path(fs::path(std::move(home)) / ".config/nostro/connectors.json"); - } - - throw std::runtime_error("XDG_CONFIG_HOME and HOME not set"); -} void Connector::SaveConnectors() { - const fs::path path = GetConnectorPath(); - const fs::path dir(path.parent_path()); - - fs::create_directories(dir.parent_path()); - - if(mkdir(dir.c_str(), S_IRWXU) == -1) { - if(errno == EEXIST) { - fs::permissions(dir, fs::perms::owner_all); - } else { - throw std::system_error(errno, std::generic_category(), std::format("cannot create {}", dir.string())); - } - - } - - const std::int32_t fd = ::open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR); - - if(fd == -1) { - throw std::system_error(errno, std::generic_category(), std::format("cannot create {}", path.string())); - } - + const fs::path path = configPath/"connectors.json"; + CreatePrivateFile(path); json jsonConnectors = json::object(); tuple_for([&jsonConnectors]() { jsonConnectors[Bank::connectorId] = Bank::connectors; }); - const std::string text = jsonConnectors.dump(4); - const char* const textPtr = text.c_str(); - - for(ssize_t i = 0; i < text.size();) { - const ssize_t written = write(fd, textPtr + i, text.size() - i); - - if(written == -1) { - if(errno == EINTR) { - continue; - } - - close(fd); - throw std::system_error(errno, std::generic_category(), std::format("cannot write {}", path.string())); - } - - i += written; - } - - close(fd); + std::ofstream out(path, std::ios::binary); + out << jsonConnectors.dump(4); } void Connector::LoadConnectors() { - const fs::path path = GetConnectorPath(); - std::ifstream f(path); + std::ifstream f(configPath/"connectors.json"); const json jsonConnectors = json::parse(f); tuple_for([&jsonConnectors]() { Bank::connectors = std::move(jsonConnectors.at(Bank::connectorId).template get>()); @@ -90,36 +42,4 @@ void Connector::LoadConnectors() { bank.RefreshAccounts(); } }); -} - - -std::string Connector::ListConnectors() { - std::string out; - - tuple_for([&out]() { - for(Bank& bank : Bank::connectors) { - out += std::format("{}\t{}\t{}\n", bank, Bank::connectorId, bank.TestConnector()); - } - }); - - out.pop_back(); - - return out; -} - -std::string Connector::ListBalance() { - std::string out; - - tuple_for([&out]() { - for(Bank& bank : Bank::connectors) { - out += std::format("{}\t{}\n", bank, Bank::connectorId); - for(typename Bank::Account& account : bank.accounts) { - out += std::format("\t{}\n", account); - } - } - }); - - out.pop_back(); - - return out; } \ No newline at end of file diff --git a/implementations/Nostro-File.cpp b/implementations/Nostro-File.cpp new file mode 100644 index 0000000..6c24b34 --- /dev/null +++ b/implementations/Nostro-File.cpp @@ -0,0 +1,81 @@ +//SPDX-License-Identifier: LGPL-3.0-only +//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +module; + +#include +#include + +module Nostro:File_impl; +import :File; +import std; + +using namespace Nostro; +namespace fs = std::filesystem; + +fs::path Nostro::GetConfigPath() { + const char* const xdg = std::getenv("XDG_CONFIG_HOME"); + if(xdg) { + return fs::path(fs::path(std::move(xdg)) / ".config/nostro"); + } + + const char* const home = std::getenv("HOME"); + if(home) { + return fs::path(fs::path(std::move(home)) / ".config/nostro"); + } + + throw std::runtime_error("XDG_CONFIG_HOME and HOME not set"); +} + +fs::path Nostro::GetCachePath() { + const char* const xdg = std::getenv("XDG_CACHE_HOME"); + if(xdg) { + return fs::path(fs::path(std::move(xdg)) / "nostro"); + } + + const char* const home = std::getenv("HOME"); + if(home) { + return fs::path(fs::path(std::move(home)) / ".cache/nostro"); + } + + throw std::runtime_error("XDG_CACHE_HOME and HOME not set"); +} + +void Nostro::CreatePrivateFile(const fs::path& path) { + const fs::path dir(path.parent_path()); + + fs::create_directories(dir.parent_path()); + + if(mkdir(dir.c_str(), S_IRWXU) == -1) { + if(errno != EEXIST) { + throw std::system_error(errno, std::generic_category(), std::format("cannot create {}", dir.string())); + } + + } + + if(mknod(path.c_str(), S_IFREG | S_IRUSR | S_IWUSR, 0) == -1) { + if(errno != EEXIST) { + throw std::system_error(errno, std::generic_category(), std::format("cannot create {}", path.string())); + } + } +} + +void Nostro::WriteString(std::ofstream& file, std::string_view str) { + std::uint16_t size = str.size(); + file.write(reinterpret_cast(&size), sizeof(size)); + file.write(str.data(), size); +} + +void Nostro::ReadString(std::ifstream& file, std::string& str) { + std::uint16_t size; + file.read(reinterpret_cast(&size), sizeof(size)); + str.resize(size); + file.read(str.data(), size); +} + +std::uint16_t Nostro::SeekString(std::ifstream& file) { + std::uint16_t size; + file.read(reinterpret_cast(&size), sizeof(size)); + file.seekg(size, std::ios::cur); + return size; +} \ No newline at end of file diff --git a/implementations/Nostro-Transaction.cpp b/implementations/Nostro-Transaction.cpp new file mode 100644 index 0000000..44f2f5c --- /dev/null +++ b/implementations/Nostro-Transaction.cpp @@ -0,0 +1,13 @@ +//SPDX-License-Identifier: LGPL-3.0-only +//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +module Nostro:Transaction_impl; +import :Transaction; +import std; + +using namespace Nostro; +namespace fs = std::filesystem; + +Transaction::Transaction(std::string&& name, std::string&& bic, std::string&& iban, std::string&& description, Currency currency, std::int64_t amount, std::chrono::system_clock::time_point timePoint): name(std::move(name)), bic(std::move(bic)), iban(std::move(iban)), description(std::move(description)), currency(currency), amount(amount), timePoint(timePoint) { + +} \ No newline at end of file diff --git a/implementations/main.cpp b/implementations/main.cpp index df9acb9..7c14077 100644 --- a/implementations/main.cpp +++ b/implementations/main.cpp @@ -45,62 +45,81 @@ int main(int argc, char *argv[]) { std::println("unknown command"); } } else { - std::println("{}", Connector::ListConnectors()); + std::string out; + + tuple_for([&out]() { + for(Bank& bank : Bank::connectors) { + out += std::format("{}\t{}\t{}\n", bank, Bank::connectorId, bank.TestConnector()); + } + }); + + if(out.size()) { + out.pop_back(); + } + + std::println("{}", out); } } else if(cmd == "accounts") { + std::string out; + + tuple_for([&out]() { + for(Bank& bank : Bank::connectors) { + out += std::format("{}\t{}\n", bank, Bank::connectorId); + for(typename Bank::Account& account : bank.accounts) { + out += std::format("\t{}\n", account); + } + } + }); + + if(out.size()) { + out.pop_back(); + } + + std::println("{}", out); + } else if(cmd == "send") { if(argc > 2) { - cmd = argv[2]; - if(cmd == "send") { - if(argc > 3) { - std::string_view accountName = argv[3]; - bool notFound = true; - tuple_for([argc, argv, accountName, ¬Found]() { - for(Bank& bank : Bank::connectors) { - for(typename Bank::Account& account : bank.accounts) { - if(account.name == accountName) { - notFound = false; - if(argc > 4) { - std::int64_t amount = std::stoll(argv[4]); - if(argc > 5) { - std::string_view iban = argv[5]; - if(argc > 6) { - std::string_view name = argv[6]; - if(argc > 7) { - account.Send(iban, name, amount, account.currency, argv[7]); - } else { - account.Send(iban, name, amount, account.currency); - } - } else { - std::println("reciepant name not provided"); - } + std::string_view accountName = argv[2]; + bool notFound = true; + tuple_for([argc, argv, accountName, ¬Found]() { + for(Bank& bank : Bank::connectors) { + for(typename Bank::Account& account : bank.accounts) { + if(account.name == accountName) { + notFound = false; + if(argc > 3) { + std::string_view iban = argv[3]; + if(argc > 4) { + std::string_view name = argv[4]; + if(argc > 5) { + std::int64_t amount = std::stoll(argv[5]); + if(argc > 6) { + account.Send(iban, name, amount, account.currency, argv[6]); } else { - std::println("iban not provided"); + account.Send(iban, name, amount, account.currency, ""); } } else { std::println("amount not provided"); } - return; + } else { + std::println("recipient name not provided"); } + } else { + std::println("iban not provided"); } + return; } - }); - if(!notFound) { - std::println("account not found"); - return -1; } - } else { - std::println("account name not provided"); - return -1; } - } else { - std::println("unknown command"); + }); + if(notFound) { + std::println("account not found"); return -1; } } else { - std::println("{}", Connector::ListBalance()); + std::println("account name not provided"); + return -1; } } else { std::println("unknown command"); return -1; } -} +} \ No newline at end of file diff --git a/interfaces/Nostro-Account.cppm b/interfaces/Nostro-Account.cppm index 31303b8..508cc5b 100644 --- a/interfaces/Nostro-Account.cppm +++ b/interfaces/Nostro-Account.cppm @@ -14,8 +14,20 @@ namespace Nostro { std::string iban; Currency currency; std::int64_t balance; - std::vector transactions; + + std::chrono::system_clock::time_point GetLastTransactionTime(std::string_view connector) const; + void SaveTransactions(std::string_view connector); + void LoadTransactions(std::string_view connector); + void LoadTransactions(std::string_view connector, std::chrono::system_clock::time_point from); + void LoadTransactions(std::string_view connector, std::chrono::system_clock::time_point from, std::chrono::system_clock::time_point to); + void LoadTransactionsByDescription(std::string_view connector, std::string_view description); + void LoadTransactionsByIban(std::string_view connector, std::string_view iban); + void LoadTransactions(std::string_view connector, std::int64_t amount); + + private: + std::ifstream LoadUntilFrom(std::string_view connector, std::chrono::system_clock::time_point from); + Transaction& LoadTransaction(std::ifstream& file); }; } diff --git a/interfaces/Nostro-Bunq.cppm b/interfaces/Nostro-Bunq.cppm index a17b301..4aab8bf 100644 --- a/interfaces/Nostro-Bunq.cppm +++ b/interfaces/Nostro-Bunq.cppm @@ -27,10 +27,7 @@ namespace Nostro { BunqConnector* connector; BunqAccount(std::string&& name, std::string&& bic, std::string&& iban, Currency currency, std::int64_t balance, std::uint32_t id, BunqConnector* connector); - void Send(std::string_view iban, std::string_view name, std::int64_t amount, Currency currency) const; void Send(std::string_view iban, std::string_view name, std::int64_t amount, Currency currency, std::string_view description) const; - private: - void SendPayment(std::string iban) const; }; export struct BunqConnector { @@ -44,6 +41,7 @@ namespace Nostro { void RefreshSession(); void RefreshAccounts(); + void GetNewTransactions(); bool TestConnector(); static constexpr std::string connectorId = "bunq"; diff --git a/interfaces/Nostro-Connector.cppm b/interfaces/Nostro-Connector.cppm index 5a6be30..1ed0f1a 100644 --- a/interfaces/Nostro-Connector.cppm +++ b/interfaces/Nostro-Connector.cppm @@ -9,9 +9,6 @@ namespace Nostro { export struct Connector { static void SaveConnectors(); static void LoadConnectors(); - static std::filesystem::path GetConnectorPath(); - static std::string ListConnectors(); - static std::string ListBalance(); }; } diff --git a/interfaces/Nostro-Currency.cppm b/interfaces/Nostro-Currency.cppm index 420fccc..ed7e1a4 100644 --- a/interfaces/Nostro-Currency.cppm +++ b/interfaces/Nostro-Currency.cppm @@ -6,7 +6,7 @@ export module Nostro:Currency; import std; export namespace Nostro { - enum class Currency { + enum class Currency : std::uint16_t { Euro }; diff --git a/interfaces/Nostro-File.cppm b/interfaces/Nostro-File.cppm new file mode 100644 index 0000000..bda9349 --- /dev/null +++ b/interfaces/Nostro-File.cppm @@ -0,0 +1,19 @@ +//SPDX-License-Identifier: LGPL-3.0-only +//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +export module Nostro:File; + +import std; + +namespace fs = std::filesystem; + +export namespace Nostro { + fs::path GetConfigPath(); + fs::path GetCachePath(); + fs::path configPath = GetConfigPath(); + fs::path cachePath = GetCachePath(); + void CreatePrivateFile(const fs::path& path); + void WriteString(std::ofstream& file, std::string_view str); + void ReadString(std::ifstream& file, std::string& str); + std::uint16_t SeekString(std::ifstream& file); +} \ No newline at end of file diff --git a/interfaces/Nostro-Transaction.cppm b/interfaces/Nostro-Transaction.cppm index 6c39223..014a3bc 100644 --- a/interfaces/Nostro-Transaction.cppm +++ b/interfaces/Nostro-Transaction.cppm @@ -8,10 +8,16 @@ import :Currency; namespace Nostro { export struct Transaction { - std::string_view bic; - std::string_view iban; + std::string name; + std::string bic; + std::string iban; + std::string description; Currency currency; std::int64_t amount; + std::chrono::system_clock::time_point timePoint; + + Transaction(); + Transaction(std::string&& name, std::string&& bic, std::string&& iban, std::string&& description, Currency currency, std::int64_t amount, std::chrono::system_clock::time_point timePoint); }; } diff --git a/interfaces/Nostro-Utils.cppm b/interfaces/Nostro-Utils.cppm new file mode 100644 index 0000000..e8ddab5 --- /dev/null +++ b/interfaces/Nostro-Utils.cppm @@ -0,0 +1,69 @@ +//SPDX-License-Identifier: LGPL-3.0-only +//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +export module Nostro:Utils; + +import std; + +namespace fs = std::filesystem; + +export namespace Nostro { + constexpr std::string EscB(std::string_view s) { + std::string out; + out.reserve(s.size() + 8); + for (const char c : s) { + switch (c) { + case '"': out += "\\\""; break; + case '\\': out += "\\\\"; break; + case '\n': out += "\\n"; break; + case '\r': out += "\\r"; break; + case '\t': out += "\\t"; break; + default: + if (static_cast(c) < 0x20) { + out += std::format("\\u{:04x}", static_cast(c)); + } else { + out += c; + } + } + } + return out; + } + + constexpr std::chrono::system_clock::time_point ParseTimestamp(std::string_view text) { + const auto field = [text](std::size_t offset, std::size_t length) { + std::uint32_t value = 0; + const char* const first = text.data() + offset; + if(offset + length > text.size()) { + throw std::runtime_error(std::format("malformed timestamp '{}'", text)); + } + const std::from_chars_result result = std::from_chars(first, first + length, value); + if(result.ec != std::errc() || result.ptr != first + length) { + throw std::runtime_error(std::format("malformed timestamp '{}'", text)); + } + return value; + }; + + if(text.size() < 19 || text[4] != '-' || text[7] != '-' || text[10] != ' ' || text[13] != ':' || text[16] != ':') { + throw std::runtime_error(std::format("malformed timestamp '{}'", text)); + } + + std::uint32_t micros = 0; + if(text.size() > 19) { + const std::size_t digits = text.size() - 20; + if(text[19] != '.' || digits == 0 || digits > 6) { + throw std::runtime_error(std::format("malformed timestamp '{}'", text)); + } + micros = field(20, digits); + for(std::size_t i = digits; i < 6; i++) { + micros *= 10; + } + } + + const std::chrono::year_month_day date(std::chrono::year(static_cast(field(0, 4))), std::chrono::month(field(5, 2)), std::chrono::day(field(8, 2))); + if(!date.ok()) { + throw std::runtime_error(std::format("malformed timestamp '{}'", text)); + } + + return std::chrono::sys_days(date) + std::chrono::hours(field(11, 2)) + std::chrono::minutes(field(14, 2)) + std::chrono::seconds(field(17, 2)) + std::chrono::microseconds(micros); + } +} \ No newline at end of file diff --git a/interfaces/Nostro.cppm b/interfaces/Nostro.cppm index 1319d71..b1335b8 100644 --- a/interfaces/Nostro.cppm +++ b/interfaces/Nostro.cppm @@ -8,6 +8,8 @@ export import :Transaction; export import :Connector; export import :Banks; export import :Currency; +export import :File; +export import :Utils; #ifdef NOSTRO_BUNQ_ENABLED export import :Bunq; diff --git a/project.cpp b/project.cpp index c10d2ee..1f08892 100644 --- a/project.cpp +++ b/project.cpp @@ -71,9 +71,14 @@ extern "C" Configuration CrafterBuildProject(std::span a "interfaces/Nostro-Bunq", "interfaces/Nostro-Crypto", "interfaces/Nostro-Currency", + "interfaces/Nostro-File", + "interfaces/Nostro-Utils", }; std::vector libImplementations = { + "implementations/Nostro-Account", + "implementations/Nostro-File", + "implementations/Nostro-Transaction", "implementations/Nostro-Connector" };