transmission/libtransmission/blocklist.h

121 lines
3.1 KiB
C
Raw Normal View History

// This file Copyright © Mnemosyne LLC.
2022-08-08 18:05:39 +00:00
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#pragma once
#ifndef __TRANSMISSION__
#error only libtransmission should #include this header.
#endif
2023-11-03 17:03:26 +00:00
#include <algorithm> // std::any_of
#include <cstddef> // size_t
2023-10-31 23:20:01 +00:00
#include <numeric>
#include <optional>
#include <string>
#include <string_view>
#include <utility> // for std::pair
#include <vector>
2023-10-31 23:20:01 +00:00
#include "libtransmission/tr-macros.h" // for TR_CONSTEXPR20
#include "libtransmission/net.h" // for tr_address
#include "libtransmission/observable.h"
namespace libtransmission
{
2023-10-31 23:20:01 +00:00
class Blocklists
{
public:
2023-10-31 23:20:01 +00:00
Blocklists() = default;
2023-10-31 23:20:01 +00:00
[[nodiscard]] bool contains(tr_address const& addr) const noexcept
{
2023-10-31 23:20:01 +00:00
return std::any_of(
std::begin(blocklists_),
std::end(blocklists_),
[&addr](auto const& blocklist) { return blocklist.enabled() && blocklist.contains(addr); });
}
2023-10-31 23:20:01 +00:00
[[nodiscard]] TR_CONSTEXPR20 auto num_lists() const noexcept
{
2023-10-31 23:20:01 +00:00
return std::size(blocklists_);
}
2023-10-31 23:20:01 +00:00
[[nodiscard]] TR_CONSTEXPR20 auto num_rules() const noexcept
{
2023-10-31 23:20:01 +00:00
return std::accumulate(
std::begin(blocklists_),
std::end(blocklists_),
size_t{},
[](int sum, auto& cur) { return sum + std::size(cur); });
}
2023-10-31 23:20:01 +00:00
void load(std::string_view folder, bool is_enabled);
void set_enabled(bool is_enabled);
size_t update_primary_blocklist(std::string_view external_file, bool is_enabled);
2023-10-31 23:20:01 +00:00
template<typename Observer>
[[nodiscard]] auto observe_changes(Observer observer)
{
2023-10-31 23:20:01 +00:00
return changed_.observe(std::move(observer));
}
private:
2023-10-31 23:20:01 +00:00
class Blocklist
{
public:
static std::optional<Blocklist> saveNew(std::string_view external_file, std::string_view bin_file, bool is_enabled);
2023-10-31 23:20:01 +00:00
Blocklist() = default;
2023-10-31 23:20:01 +00:00
Blocklist(std::string_view bin_file, bool is_enabled)
: bin_file_{ bin_file }
, is_enabled_{ is_enabled }
{
}
[[nodiscard]] bool contains(tr_address const& addr) const;
[[nodiscard]] auto size() const
{
ensureLoaded();
return std::size(rules_);
}
2023-10-31 23:20:01 +00:00
[[nodiscard]] constexpr bool enabled() const noexcept
{
return is_enabled_;
}
constexpr void setEnabled(bool is_enabled) noexcept
{
is_enabled_ = is_enabled;
}
[[nodiscard]] constexpr auto const& binFile() const noexcept
{
return bin_file_;
}
private:
void ensureLoaded() const;
mutable std::vector<std::pair<tr_address, tr_address>> rules_;
std::string bin_file_;
bool is_enabled_ = false;
};
std::vector<Blocklist> blocklists_;
std::string folder_;
libtransmission::SimpleObservable<> changed_;
[[nodiscard]] static std::vector<Blocklist> load_folder(std::string_view folder, bool is_enabled);
};
} // namespace libtransmission