pre-ai
This commit is contained in:
parent
be13205ffd
commit
367c19386c
17 changed files with 561 additions and 166 deletions
210
implementations/Nostro-Account.cpp
Normal file
210
implementations/Nostro-Account.cpp
Normal file
|
|
@ -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<std::streamoff>(sizeof(std::int64_t)), std::ios::end);
|
||||
std::int64_t lastTime;
|
||||
file.read(reinterpret_cast<char*>(&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<char*>(&transactions[i].amount), sizeof(transactions[i].amount));
|
||||
file.write(reinterpret_cast<char*>(&transactions[i].currency), sizeof(Currency));
|
||||
std::int64_t time = std::chrono::duration_cast<std::chrono::microseconds>(transactions[i].timePoint.time_since_epoch()).count();
|
||||
file.write(reinterpret_cast<char*>(&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<char*>(&tr.amount), sizeof(tr.amount));
|
||||
file.read(reinterpret_cast<char*>(&tr.currency), sizeof(tr.currency));
|
||||
std::int64_t time;
|
||||
file.read(reinterpret_cast<char*>(&time), sizeof(time));
|
||||
tr.timePoint = std::chrono::system_clock::time_point(std::chrono::duration_cast<std::chrono::system_clock::duration>(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<char*>(&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<char*>(&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<char*>(&tr.amount), sizeof(tr.amount));
|
||||
file.read(reinterpret_cast<char*>(&tr.currency), sizeof(tr.Currency));
|
||||
file.read(reinterpret_cast<char*>(&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<char*>(&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<char*>(&tr.amount), sizeof(tr.amount));
|
||||
file.read(reinterpret_cast<char*>(&tr.currency), sizeof(tr.Currency));
|
||||
file.read(reinterpret_cast<char*>(&tr.time), sizeof(tr.time));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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> 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<unsigned char>(c) < 0x20) {
|
||||
out += std::format("\\u{:04x}", static_cast<unsigned char>(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<std::string>();
|
||||
}
|
||||
|
||||
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<std::string>();
|
||||
}
|
||||
|
||||
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<std::string>());
|
||||
if(timePoint <= last) {
|
||||
route.clear();
|
||||
break;
|
||||
}
|
||||
|
||||
const json& counterpartyJ = paymentJ.at("counterparty_alias");
|
||||
const Currency currency = GetCurrency(paymentJ.at("amount").at("currency").get<std::string>());
|
||||
|
||||
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<std::string>(), 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
|
||||
|
|
|
|||
|
|
@ -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<EnabledBanks>([&jsonConnectors]<typename Bank>() {
|
||||
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<EnabledBanks>([&jsonConnectors]<typename Bank>() {
|
||||
Bank::connectors = std::move(jsonConnectors.at(Bank::connectorId).template get<std::vector<Bank>>());
|
||||
|
|
@ -90,36 +42,4 @@ void Connector::LoadConnectors() {
|
|||
bank.RefreshAccounts();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
std::string Connector::ListConnectors() {
|
||||
std::string out;
|
||||
|
||||
tuple_for<EnabledBanks>([&out]<typename Bank>() {
|
||||
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<EnabledBanks>([&out]<typename Bank>() {
|
||||
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;
|
||||
}
|
||||
81
implementations/Nostro-File.cpp
Normal file
81
implementations/Nostro-File.cpp
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
module;
|
||||
|
||||
#include <cerrno>
|
||||
#include <sys/stat.h>
|
||||
|
||||
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<char*>(&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<char*>(&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<char*>(&size), sizeof(size));
|
||||
file.seekg(size, std::ios::cur);
|
||||
return size;
|
||||
}
|
||||
13
implementations/Nostro-Transaction.cpp
Normal file
13
implementations/Nostro-Transaction.cpp
Normal file
|
|
@ -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) {
|
||||
|
||||
}
|
||||
|
|
@ -45,62 +45,81 @@ int main(int argc, char *argv[]) {
|
|||
std::println("unknown command");
|
||||
}
|
||||
} else {
|
||||
std::println("{}", Connector::ListConnectors());
|
||||
std::string out;
|
||||
|
||||
tuple_for<EnabledBanks>([&out]<typename Bank>() {
|
||||
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<EnabledBanks>([&out]<typename Bank>() {
|
||||
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<EnabledBanks>([argc, argv, accountName, ¬Found]<typename Bank>() {
|
||||
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<EnabledBanks>([argc, argv, accountName, ¬Found]<typename Bank>() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue