Skip to content

Commit

Permalink
Start of refactoring to allow auto importing of subs
Browse files Browse the repository at this point in the history
* Added config settings for backbone file, import defaults INI file, and also
  the setting to control auto-adding of subs
* Added UI for these in wwivconfig
* update wwivutil subs import to use the data from networks.json
  • Loading branch information
wwiv committed Dec 20, 2022
1 parent 06a33c8 commit 0559073
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 35 deletions.
3 changes: 2 additions & 1 deletion core/command_line.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ class CommandLineCommand : public Command {
[[nodiscard]] CommandLineValue arg(const std::string& name) const;
[[nodiscard]] bool contains_arg(const std::string& name) const noexcept;
[[nodiscard]] std::string sarg(const std::string& name) const { return arg(name).as_string(); }
[[nodiscard]] int iarg(const std::string& name) const { return arg(name).as_int(); }
template<typename T = int>
[[nodiscard]] T iarg(const std::string& name) const { return static_cast<T>(arg(name).as_int()); }
[[nodiscard]] bool barg(const std::string& name) const { return arg(name).as_bool(); }
[[nodiscard]] bool help_requested() const { return barg("help"); }
[[nodiscard]] const CommandLineCommand* command() const { return command_; }
Expand Down
16 changes: 16 additions & 0 deletions sdk/net/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ struct fido_node_config_t {
// ****
// ***************************************************************************************

/**
* 5.8+ specific per-network settings.
*
* These are common across all network types.
*/
struct common_network_config_t {
// Should unrecognized network subs be automatically added
bool auto_add{false};
// Configuration INI file for automatically adding subs
std::string auto_add_ini;
};

/**
* Fido specific per-network settings.
*/
Expand All @@ -211,6 +223,8 @@ struct fido_network_config_t {
std::string fido_address;
// Your FTN nodelist base name (i.e. NODELiST)
std::string nodelist_base;
// Your FTN network backbone.na file (i.e. FSXNET.NA)
std::string backbone_filename;
// Fidonet mailer type {FLO, Attach}.
// Only FLO is even close to being supported.
fido_mailer_t mailer_type;
Expand Down Expand Up @@ -286,6 +300,8 @@ class Network {
std::filesystem::path dir;
/* system number */
uint16_t sysnum{0};
/** Additional Common network settings */
common_network_config_t settings;

// Used by FTN type nodes.
fido_network_config_t fido{};
Expand Down
13 changes: 10 additions & 3 deletions sdk/net/networks_cereal.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ template <class Archive>
void load(Archive & ar, fido_network_config_t& n) {
SERIALIZE(n, fido_address);
SERIALIZE(n, nodelist_base);
SERIALIZE(n, backbone_filename);
SERIALIZE(n, mailer_type);
SERIALIZE(n, transport);
SERIALIZE(n, inbound_dir);
Expand All @@ -163,6 +164,7 @@ template <class Archive>
void save(Archive & ar, const fido_network_config_t& n) {
SERIALIZE(n, fido_address);
SERIALIZE(n, nodelist_base);
SERIALIZE(n, backbone_filename);
SERIALIZE(n, mailer_type);
SERIALIZE(n, transport);
SERIALIZE(n, inbound_dir);
Expand All @@ -181,22 +183,27 @@ void save(Archive & ar, const fido_network_config_t& n) {
SERIALIZE(n, allow_any_pipe_codes);
}

template <class Archive> void serialize(Archive& ar, common_network_config_t& n) {
SERIALIZE(n, auto_add);
SERIALIZE(n, auto_add_ini);
}

// This has to be in the cereal or default to match Network which
// is in the default namespace.
template <class Archive>
void serialize(Archive & ar, Network& n) {
template <class Archive> void serialize(Archive& ar, Network& n) {
SERIALIZE(n, type);
SERIALIZE(n, name);
SERIALIZE(n, dir);
SERIALIZE(n, sysnum);
SERIALIZE(n, settings);
// Serialize the Fido config
if (n.type == network_type_t::ftn) {
SERIALIZE(n, fido);
}
SERIALIZE(n, uuid);
}

} // namespace cereal
} // namespace cereal



Expand Down
13 changes: 11 additions & 2 deletions wwivconfig/networks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ static bool del_net(const Config& config, Networks& networks, int nn) {
// Base item of an editable value, this class does not use templates.
class FidoNetworkConfigSubDialog : public SubDialog<Network> {
public:
FidoNetworkConfigSubDialog(const Config& config, std::filesystem::path bbsdir,
FidoNetworkConfigSubDialog(const Config& config, std::filesystem::path netdir,
Network& d)
: SubDialog(config, d), netdir_(std::move(bbsdir)) {}
: SubDialog(config, d), netdir_(std::move(netdir)) {}
~FidoNetworkConfigSubDialog() override = default;

void RunSubDialog(CursesWindow* window) override {
Expand All @@ -165,6 +165,10 @@ class FidoNetworkConfigSubDialog : public SubDialog<Network> {
new StringEditItem<std::string&>(MAX_STRING_LEN, n->nodelist_base, EditLineMode::ALL),
"Base filename for the nodelist without path or extension. (e.g. FSXNET)", 1, y);
++y;
items.add(new Label("Backbone File:"),
new StringEditItem<std::string&>(MAX_STRING_LEN, n->backbone_filename, EditLineMode::ALL),
"Filename of ECHOLIST in BACKBONE.NA format (e.g. FSXNET.NA)", 1, y);
++y;
items.add(new Label("Inbound Dir:"),
new StringFilePathItem(MAX_STRING_LEN, netdir_, n->inbound_dir), 1, y);
++y;
Expand Down Expand Up @@ -518,6 +522,11 @@ static void edit_net(const Config& config, Networks& networks, int nn) {
items.add(new Label("Directory:"), new FileSystemFilePathItem(60, config.root_directory(), n.dir),
1, y);
y++;
items.add(new Label("Auto Add Subs:"), new BooleanEditItem(&n.settings.auto_add), 1, y);
y++;
items.add(new Label("Auto Add INI:"), new StringFilePathItem(60, n.dir, n.settings.auto_add_ini),
1, y);
y++;

const auto net_dir = File::absolute(config.root_directory(), n.dir);
if (n.type == network_type_t::ftn) {
Expand Down
88 changes: 59 additions & 29 deletions wwivutil/subs/import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,40 +63,77 @@ std::vector<backbone_t> ParseBackboneFile(const std::filesystem::path& file) {
}

std::string SubsImportCommand::GetUsage() const {
std::ostringstream ss;
ss << "Usage: import --format=[backbone] --defaults=<filename.ini> <subs filename>"
<< std::endl;
return ss.str();
return R"(
Imports subs using the backbone file and values specified in the ini file
specified in wwivconfig for the network or override using --defaults.
Usage: import --net=NN [--defaults=import.ini] [backbone.na]
Example: wwivutil subs import --net=1
Sample import.ini:
[backbone]
post_acs = user.sl >= 20
read_acs = user.sl >= 10
maxmsgs = 5000
net_num = 2
uplink = 21:2/100
)";
}

bool SubsImportCommand::AddSubCommands() {
add_argument({"format", 'f', "must be 'backbone'", "backbone"});
add_argument({"defaults", 'd', "Specify an ini file with defaults to use when creating new subs.", ""});
add_argument({"defaults", 'd', "Override the ini file with defaults to use when creating new subs.", ""});
add_argument({"net", "Network number to use (i.e. 0).", ""});
return true;
}

int SubsImportCommand::Execute() {
const auto format = sarg("format");
if (format != "backbone") {
std::cout << "Format must be backbone. " << std::endl;
return 1;
}

if (remaining().empty()) {
std::cout << GetUsage() << std::endl;
return 1;
}

const auto defaults_fn = sarg("defaults");
auto defaults_fn = sarg("defaults");
int16_t net_num = 0;
if (!arg("net").as_string().empty()) {
// Use the network number on the commandline as an override.
net_num = iarg<int16_t>("net");
}
const auto& net = config()->networks().at(net_num);

if (defaults_fn.empty()) {
// If --defaults is empty, use the one from networks.json
defaults_fn = net.settings.auto_add_ini;
}
if (defaults_fn.empty() || !core::File::Exists(defaults_fn)) {
std::cout << "Defaults INI file: " << defaults_fn << " does not exist " << std::endl;
std::cout << GetUsage() << std::endl;
return 1;
}

const auto& backbone_filename = remaining().front();
std::string backbone_filename;
if (net.type == network_type_t::ftn) {
backbone_filename = net.fido.backbone_filename;
}
if (!remaining().empty()) {
// Override the backbone filename
backbone_filename = remaining().front();
}

sdk::Subs subs(config()->config()->datadir(), config()->networks().networks(),
config()->config()->max_backups());
if (!subs.Load()) {
std::cout << "Unable to load subs.json" << std::endl;
return 1;
}

//
// From here down we assume backbone filename, net, etc are all set
//

IniFile ini(defaults_fn, {format});
IniFile ini(defaults_fn, {"backbone"});
if (!ini.IsOpen()) {
std::cout << "Unable to open INI file: " << defaults_fn << std::endl;
return 1;
Expand All @@ -108,27 +145,20 @@ int SubsImportCommand::Execute() {
prototype.maxmsgs = ini.value<uint16_t>("maxmsgs", 5000);
prototype.storage_type = ini.value<uint8_t>("storage_type", 2);

sdk::subboard_network_data_t net{};
sdk::subboard_network_data_t netdata{};
const sdk::fido::FidoAddress uplink(ini.value<std::string>("uplink"));
auto net_num = ini.value<int16_t>("net_num", 0);
net.net_num = net_num;
net.host = FTN_FAKE_OUTBOUND_NODE;
net.ftn_uplinks.emplace(sdk::fido::FidoAddress(ini.value<std::string>("uplink")));
prototype.nets.emplace_back(net);
netdata.net_num = net_num;

netdata.host = FTN_FAKE_OUTBOUND_NODE;
netdata.ftn_uplinks.emplace(sdk::fido::FidoAddress(ini.value<std::string>("uplink")));
prototype.nets.emplace_back(netdata);

auto backbone_echos = ParseBackboneFile(backbone_filename);
if (backbone_echos.empty()) {
std::cout << "Nothing to do, no subs parsed out of " << backbone_filename << std::endl;
return 0;
}

sdk::Subs subs(config()->config()->datadir(),
config()->networks().networks(),
config()->config()->max_backups());
if (!subs.Load()) {
std::cout << "Unable to load subs.json" << std::endl;
return 1;
}

// Build list of existing echos for this network.
std::unordered_set<std::string> existing_echos;
Expand All @@ -153,7 +183,7 @@ int SubsImportCommand::Execute() {
subs.add(new_sub);

// Create nECHOTAG_NAME.net file.
const auto& subnet = config()->networks().at(net.net_num);
const auto& subnet = config()->networks().at(netdata.net_num);
auto subscriberfile = FilePath(subnet.dir, fmt::format("n{}.net", b.tag));
wwiv::sdk::WriteFidoSubcriberFile(subscriberfile, {uplink});
}
Expand Down

0 comments on commit 0559073

Please sign in to comment.