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/nntpd.cpp

101 lines
2.5 KiB
C++
Raw Normal View History

2016-10-15 18:12:01 +05:00
#include "ini.hpp"
#include "storage.hpp"
#include "nntp_server.hpp"
#include "event.hpp"
2016-10-18 16:03:51 +05:00
#include "exec_frontend.hpp"
2016-10-15 18:12:01 +05:00
#include <vector>
#include <string>
int main(int argc, char * argv[]) {
if (argc != 2) {
std::cerr << "usage: " << argv[0] << " config.ini" << std::endl;
return 1;
}
nntpchan::Mainloop loop;
2017-05-03 18:44:42 +05:00
2016-10-15 18:12:01 +05:00
nntpchan::NNTPServer nntp(loop);
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);
if(i.is_open()) {
INI::Parser conf(i);
2017-05-03 18:44:42 +05:00
2016-10-15 18:12:01 +05:00
std::vector<std::string> requiredSections = {"nntp", "storage"};
2017-05-03 18:44:42 +05:00
2016-10-15 18:12:01 +05:00
auto & level = conf.top();
2017-05-03 18:44:42 +05:00
2016-10-15 18:12:01 +05:00
for ( const auto & section : requiredSections ) {
if(level.sections.find(section) == level.sections.end()) {
std::cerr << "config file " << fname << " does not have required section: ";
std::cerr << section << std::endl;
return 1;
}
}
auto & storeconf = level.sections["storage"].values;
if (storeconf.find("path") == storeconf.end()) {
std::cerr << "storage section does not have 'path' value" << std::endl;
return 1;
}
nntp.SetStoragePath(storeconf["path"]);
2017-05-03 18:44:42 +05:00
2016-10-15 18:12:01 +05:00
auto & nntpconf = level.sections["nntp"].values;
if (nntpconf.find("bind") == nntpconf.end()) {
std::cerr << "nntp section does not have 'bind' value" << std::endl;
return 1;
}
2016-10-15 22:53:35 +05:00
if (nntpconf.find("authdb") != nntpconf.end()) {
nntp.SetLoginDB(nntpconf["authdb"]);
}
2016-10-18 16:03:51 +05:00
if ( level.sections.find("frontend") != level.sections.end()) {
// frontend enabled
2016-10-18 17:17:40 +05:00
auto & frontconf = level.sections["frontend"].values;
2016-10-18 16:03:51 +05:00
if (frontconf.find("type") == frontconf.end()) {
std::cerr << "frontend section provided but 'type' value not provided" << std::endl;
return 1;
}
2016-10-18 17:17:40 +05:00
auto ftype = frontconf["type"];
2016-10-18 16:03:51 +05:00
if (ftype == "exec") {
if (frontconf.find("exec") == frontconf.end()) {
std::cerr << "exec frontend specified but no 'exec' value provided" << std::endl;
return 1;
}
2016-10-18 17:17:40 +05:00
nntp.SetFrontend(new nntpchan::ExecFrontend(frontconf["exec"]));
2016-10-18 16:03:51 +05:00
} else {
std::cerr << "unknown frontend type '" << ftype << "'" << std::endl;
}
2017-05-03 18:44:42 +05:00
2016-10-18 16:03:51 +05:00
}
2017-05-03 18:44:42 +05:00
2016-10-18 16:03:51 +05:00
auto & a = nntpconf["bind"];
2016-10-15 18:12:01 +05:00
try {
2017-05-03 18:44:42 +05:00
nntp.Bind(a);
2016-10-15 18:12:01 +05:00
} catch ( std::exception & ex ) {
std::cerr << "failed to bind: " << ex.what() << std::endl;
return 1;
}
2017-05-03 18:44:42 +05:00
std::cerr << "run mainloop" << std::endl;
loop.Run();
std::cerr << "stopped mainloop" << std::endl;
2016-10-15 18:12:01 +05:00
} else {
std::cerr << "failed to open " << fname << std::endl;
return 1;
}
2017-05-03 18:44:42 +05:00
2016-10-15 18:12:01 +05:00
}