Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.
nntpchan/contrib/backends/nntpchan-daemon/daemon/main.cpp

165 lines
4.5 KiB
C++
Raw Normal View History

2016-10-15 18:12:01 +05:00
#include "ini.hpp"
2017-10-11 18:48:27 +05:00
#include <nntpchan/crypto.hpp>
#include <nntpchan/event.hpp>
#include <nntpchan/exec_frontend.hpp>
2017-10-17 19:29:56 +05:00
#include <nntpchan/nntp_server.hpp>
2017-10-11 18:48:27 +05:00
#include <nntpchan/staticfile_frontend.hpp>
2017-10-17 19:29:56 +05:00
#include <nntpchan/storage.hpp>
2016-10-15 18:12:01 +05:00
#include <string>
2017-10-17 19:29:56 +05:00
#include <vector>
2016-10-15 18:12:01 +05:00
2018-05-03 20:47:20 +05:00
2018-05-06 19:21:58 +05:00
int main(int argc, char *argv[], char * argenv[])
2017-10-17 19:29:56 +05:00
{
if (argc != 2)
{
2016-10-15 18:12:01 +05:00
std::cerr << "usage: " << argv[0] << " config.ini" << std::endl;
return 1;
}
nntpchan::Crypto crypto;
2017-05-03 20:37:09 +05:00
2018-05-04 17:38:34 +05:00
std::unique_ptr<nntpchan::ev::Loop> loop(nntpchan::NewMainLoop());
2017-05-03 18:44:42 +05:00
2018-05-04 17:38:34 +05:00
std::unique_ptr<nntpchan::NNTPServer> nntp = std::make_unique<nntpchan::NNTPServer>(loop.get());
2017-05-03 18:44:42 +05:00
2016-10-15 18:12:01 +05:00
std::string fname(argv[1]);
std::ifstream i(fname);
2017-10-17 19:29:56 +05:00
if (i.is_open())
{
2016-10-15 18:12:01 +05:00
INI::Parser conf(i);
2017-05-03 18:44:42 +05:00
2017-05-03 22:33:04 +05:00
std::vector<std::string> requiredSections = {"nntp", "articles"};
2017-05-03 18:44:42 +05:00
2017-10-17 19:29:56 +05:00
auto &level = conf.top();
2017-05-03 18:44:42 +05:00
2017-10-17 19:29:56 +05:00
for (const auto &section : requiredSections)
{
if (level.sections.find(section) == level.sections.end())
{
2016-10-15 18:12:01 +05:00
std::cerr << "config file " << fname << " does not have required section: ";
std::cerr << section << std::endl;
return 1;
}
}
2017-10-17 19:29:56 +05:00
auto &storeconf = level.sections["articles"].values;
2016-10-15 18:12:01 +05:00
2017-10-17 19:29:56 +05:00
if (storeconf.find("store_path") == storeconf.end())
{
2017-05-03 22:33:04 +05:00
std::cerr << "storage section does not have 'store_path' value" << std::endl;
2016-10-15 18:12:01 +05:00
return 1;
}
2018-05-03 20:47:20 +05:00
nntp->SetStoragePath(storeconf["store_path"]);
2017-05-03 18:44:42 +05:00
2017-10-17 19:29:56 +05:00
auto &nntpconf = level.sections["nntp"].values;
2016-10-15 18:12:01 +05:00
2017-10-17 19:29:56 +05:00
if (nntpconf.find("bind") == nntpconf.end())
{
2016-10-15 18:12:01 +05:00
std::cerr << "nntp section does not have 'bind' value" << std::endl;
return 1;
}
2017-10-17 19:29:56 +05:00
if (nntpconf.find("instance_name") == nntpconf.end())
{
2017-05-03 22:33:04 +05:00
std::cerr << "nntp section lacks 'instance_name' value" << std::endl;
return 1;
}
2018-05-03 20:47:20 +05:00
nntp->SetInstanceName(nntpconf["instance_name"]);
2017-05-03 22:33:04 +05:00
2017-10-17 19:29:56 +05:00
if (nntpconf.find("authdb") != nntpconf.end())
{
2018-05-03 20:47:20 +05:00
nntp->SetLoginDB(nntpconf["authdb"]);
2016-10-15 22:53:35 +05:00
}
2017-10-17 19:29:56 +05:00
if (level.sections.find("frontend") != level.sections.end())
{
2016-10-18 16:03:51 +05:00
// frontend enabled
2017-10-17 19:29:56 +05:00
auto &frontconf = level.sections["frontend"].values;
if (frontconf.find("type") == frontconf.end())
{
2016-10-18 16:03:51 +05:00
std::cerr << "frontend section provided but 'type' value not provided" << std::endl;
return 1;
}
2018-05-03 20:47:20 +05:00
auto &ftype = frontconf["type"];
if (ftype == "exec")
{
if (frontconf.find("exec") == frontconf.end())
{
2016-10-18 16:03:51 +05:00
std::cerr << "exec frontend specified but no 'exec' value provided" << std::endl;
return 1;
}
2018-05-06 19:21:58 +05:00
nntp->SetFrontend(new nntpchan::ExecFrontend(frontconf["exec"], argenv));
}
else if (ftype == "staticfile")
{
2017-10-17 19:29:56 +05:00
auto required = {"template_dir", "out_dir", "template_dialect", "max_pages"};
for (const auto &opt : required)
{
2017-10-17 19:29:56 +05:00
if (frontconf.find(opt) == frontconf.end())
{
std::cerr << "staticfile frontend specified but no '" << opt << "' value provided" << std::endl;
return 1;
}
}
auto maxPages = std::stoi(frontconf["max_pages"]);
2017-10-17 19:29:56 +05:00
if (maxPages <= 0)
{
std::cerr << "max_pages invalid value '" << frontconf["max_pages"] << "'" << std::endl;
return 1;
}
2018-05-04 17:38:34 +05:00
auto & dialect = frontconf["template_dialect"];
auto templateEngine = nntpchan::CreateTemplateEngine(dialect);
if(templateEngine == nullptr)
{
std::cerr << "invalid template dialect '" << dialect << "'" << std::endl;
return 1;
}
nntp->SetFrontend(new nntpchan::StaticFileFrontend(templateEngine, frontconf["template_dir"], frontconf["out_dir"], maxPages));
}
else
{
2016-10-18 16:03:51 +05:00
std::cerr << "unknown frontend type '" << ftype << "'" << std::endl;
2017-05-03 22:33:04 +05:00
return 1;
2016-10-18 16:03:51 +05:00
}
}
2018-05-04 17:38:34 +05:00
else
{
std::cerr << "no frontend configured, running without generating markup" << std::endl;
}
2017-05-03 18:44:42 +05:00
2017-10-17 19:29:56 +05:00
auto &a = nntpconf["bind"];
2016-10-18 16:03:51 +05:00
2017-10-17 19:29:56 +05:00
try
{
2018-05-03 20:47:20 +05:00
if(nntp->Bind(a))
{
std::cerr << "nntpd for " << nntp->InstanceName() << " bound to " << a << std::endl;
}
else
{
2018-05-06 20:20:43 +05:00
std::cerr << "nntpd for " << nntp->InstanceName() << " failed to bind to " << a << ": "<< strerror(errno) << std::endl;
2018-05-03 20:47:20 +05:00
return 1;
}
2017-10-17 19:29:56 +05:00
} catch (std::exception &ex)
{
2016-10-15 18:12:01 +05:00
std::cerr << "failed to bind: " << ex.what() << std::endl;
return 1;
}
2017-05-03 18:44:42 +05:00
loop->Run();
2018-05-03 20:47:20 +05:00
std::cerr << "Exiting" << std::endl;
2017-10-17 19:29:56 +05:00
}
else
{
2016-10-15 18:12:01 +05:00
std::cerr << "failed to open " << fname << std::endl;
return 1;
}
}