From 4f3bc5cf6ef7db79fe3097506daf7709f78d89c3 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Tue, 18 Oct 2016 07:03:51 -0400 Subject: [PATCH] more recent changes --- contrib/backends/nntpchan-daemon/Makefile | 2 +- .../{nntpchan.cpp => nntpd.cpp} | 23 ++++++++++- .../nntpchan-daemon/src/exec_frontend.hpp | 9 +++++ .../backends/nntpchan-daemon/src/frontend.hpp | 25 ++++++++++++ contrib/frontends/py/README.md | 11 ++++++ contrib/frontends/py/frontend.py | 38 ++++++++++++++++--- contrib/frontends/py/nntpchan/config.py | 11 ++++++ contrib/frontends/py/nntpchan/db.py | 16 ++++++++ contrib/js/neochan/init.coffee.js | 5 +++ 9 files changed, 133 insertions(+), 7 deletions(-) rename contrib/backends/nntpchan-daemon/{nntpchan.cpp => nntpd.cpp} (70%) create mode 100644 contrib/backends/nntpchan-daemon/src/exec_frontend.hpp create mode 100644 contrib/backends/nntpchan-daemon/src/frontend.hpp create mode 100644 contrib/frontends/py/nntpchan/config.py create mode 100644 contrib/frontends/py/nntpchan/db.py create mode 100644 contrib/js/neochan/init.coffee.js diff --git a/contrib/backends/nntpchan-daemon/Makefile b/contrib/backends/nntpchan-daemon/Makefile index c4cbfe3..7151fc6 100644 --- a/contrib/backends/nntpchan-daemon/Makefile +++ b/contrib/backends/nntpchan-daemon/Makefile @@ -22,7 +22,7 @@ CXXFLAGS := -std=c++14 -Wall -Wextra $(INC_FLAGS) -g all: $(EXE) $(TOOL) $(EXE): $(OBJECTS) - $(CXX) -o $(EXE) $(LD_FLAGS) $(OBJECTS) $(CXXFLAGS) nntpchan.cpp + $(CXX) -o $(EXE) $(LD_FLAGS) $(OBJECTS) $(CXXFLAGS) nntpd.cpp $(TOOL): $(OBJECTS) $(CXX) -o $(TOOL) $(LD_FLAGS) $(OBJECTS) $(CXXFLAGS) tool.cpp diff --git a/contrib/backends/nntpchan-daemon/nntpchan.cpp b/contrib/backends/nntpchan-daemon/nntpd.cpp similarity index 70% rename from contrib/backends/nntpchan-daemon/nntpchan.cpp rename to contrib/backends/nntpchan-daemon/nntpd.cpp index 976d361..339bc30 100644 --- a/contrib/backends/nntpchan-daemon/nntpchan.cpp +++ b/contrib/backends/nntpchan-daemon/nntpd.cpp @@ -3,6 +3,7 @@ #include "storage.hpp" #include "nntp_server.hpp" #include "event.hpp" +#include "exec_frontend.hpp" #include #include @@ -57,8 +58,28 @@ int main(int argc, char * argv[]) { nntp.SetLoginDB(nntpconf["authdb"]); } - auto & a = nntpconf["bind"]; + if ( level.sections.find("frontend") != level.sections.end()) { + // frontend enabled + auto & frontconf = level.sections["frontend"]; + if (frontconf.find("type") == frontconf.end()) { + std::cerr << "frontend section provided but 'type' value not provided" << std::endl; + return 1; + } + auto & ftype = frontconf.find("type"); + if (ftype == "exec") { + if (frontconf.find("exec") == frontconf.end()) { + std::cerr << "exec frontend specified but no 'exec' value provided" << std::endl; + return 1; + } + nntp.SetFrontend(new nntpchan::ExecFrontend(loop, frontconf["exec"])); + } else { + std::cerr << "unknown frontend type '" << ftype << "'" << std::endl; + } + + } + auto & a = nntpconf["bind"]; + try { nntp.Bind(a); } catch ( std::exception & ex ) { diff --git a/contrib/backends/nntpchan-daemon/src/exec_frontend.hpp b/contrib/backends/nntpchan-daemon/src/exec_frontend.hpp new file mode 100644 index 0000000..3cfbd4c --- /dev/null +++ b/contrib/backends/nntpchan-daemon/src/exec_frontend.hpp @@ -0,0 +1,9 @@ +#ifndef NNTPCHAN_EXEC_FRONTEND_HPP +#define NNTPCHAN_EXEC_FRONTEND_HPP + +namespace nntpchan +{ + +} + +#endif diff --git a/contrib/backends/nntpchan-daemon/src/frontend.hpp b/contrib/backends/nntpchan-daemon/src/frontend.hpp new file mode 100644 index 0000000..55699a4 --- /dev/null +++ b/contrib/backends/nntpchan-daemon/src/frontend.hpp @@ -0,0 +1,25 @@ +#ifndef NNTPCHAN_FRONTEND_HPP +#define NNTPCHAN_FRONTEND_HPP + +namespace nntpchan +{ + /** @brief nntpchan frontend ui interface */ + class Frontend + { + public: + virtual ~Frontend() {} + + /** @brief process an inbound message stored at fpath that we have accepted. */ + void ProcessNewMessage(const std::string & fpath) = 0; + + /** @brief return true if we take posts in a newsgroup */ + bool AcceptsNewsgroup(const std::string & newsgroup) = 0; + + /** @brief return true if we will accept a message given its message-id */ + bool AcceptsMessage(const std::string & msgid) = 0; + + + }; +} + +#endif diff --git a/contrib/frontends/py/README.md b/contrib/frontends/py/README.md index cacb381..474ffc1 100644 --- a/contrib/frontends/py/README.md +++ b/contrib/frontends/py/README.md @@ -2,6 +2,8 @@ ## usage ## +### srndv2 unstable ### + add to nntpchan.json hooks section: { @@ -9,3 +11,12 @@ add to nntpchan.json hooks section: "exec": "/path/to/frontend.py" } +### nntpd ### + +add this to nntpchan.ini + + + ... + [frontend] + type=exec + exec=/path/to/frontend.py diff --git a/contrib/frontends/py/frontend.py b/contrib/frontends/py/frontend.py index cdce5e2..d4c583e 100644 --- a/contrib/frontends/py/frontend.py +++ b/contrib/frontends/py/frontend.py @@ -1,9 +1,37 @@ #!/usr/bin/env python3 -import nntpchan +from nntpchan import message +from nntpchan import db +import logging +import os import sys + if __name__ == "__main__": - msgid = sys.argv[1] - group = sys.argv[2] - if nntpchan.addArticle(msgid, group): - nntpchan.regenerate(msgid, group) + lvl = logging.INFO + if 'NNTPCHAN_DEBUG' in os.environ: + lvl = logging.DEBUG + logging.basicConfig(level=lvl) + l = logging.getLogger(__name__) + cmd = sys.argv[1] + if cmd == 'post': + fpath = sys.argv[2] + msg = None + if not os.path.exists(fpath): + print("{} does not exist".format(fpath)) + exit(1) + with open(fpath) as f: + msg = message.parse(f) + if msg: + l.debug("loaded {}".format(fpath)) + elif cmd == 'newsgroup': + if db.allowsNewsgroup(sys.argv[2]): + exit(0) + else: + exit(1) + elif cmd == 'msgid': + if db.allowsMessage(sys.argv[2]): + exit(0) + else: + exit(1) + elif cmd == 'init': + db.init() diff --git a/contrib/frontends/py/nntpchan/config.py b/contrib/frontends/py/nntpchan/config.py new file mode 100644 index 0000000..b377d20 --- /dev/null +++ b/contrib/frontends/py/nntpchan/config.py @@ -0,0 +1,11 @@ +# +# please edit this file to contain the required information +# + + +""" path to article storage directory """ +storage = '/path/to/storage/' + +""" database connector url """ +dburl = 'postgresql://user:password@localhost/database' +#dburl = 'sqlite:///path/to/nntpchan.sqlite3' diff --git a/contrib/frontends/py/nntpchan/db.py b/contrib/frontends/py/nntpchan/db.py new file mode 100644 index 0000000..583fe1d --- /dev/null +++ b/contrib/frontends/py/nntpchan/db.py @@ -0,0 +1,16 @@ +from nntpchan import config + +import sqlalchemy + +def allowsMessage(msgid): + return True + +def allowsNewsgroup(group): + return True + + + +def init(): + """ + initialize db backend + """ diff --git a/contrib/js/neochan/init.coffee.js b/contrib/js/neochan/init.coffee.js new file mode 100644 index 0000000..49caa98 --- /dev/null +++ b/contrib/js/neochan/init.coffee.js @@ -0,0 +1,5 @@ +// Generated by CoffeeScript 1.11.1 +(function() { + + +}).call(this);