Archived
1
0

clang format

This commit is contained in:
Jeff Becker 2017-10-17 10:29:56 -04:00
parent 85248787fc
commit 841c5c6afe
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
69 changed files with 2381 additions and 2448 deletions

View File

@ -0,0 +1,14 @@
TabWidth: 2
UseTab: Never
ColumnLimit: 120
IndentWidth: 2
Language: Cpp
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
BeforeElse: true

View File

@ -31,14 +31,16 @@ PKGS := libuv libsodium
LD_FLAGS := $(shell pkg-config --libs $(PKGS)) -lstdc++fs
INC_FLAGS := $(shell pkg-config --cflags $(PKGS)) -I$(HEADERS_PATH)
CXXFLAGS := -std=c++17 -Wall -Wextra -Werror -pedantic $(INC_FLAGS)
REQUIRED_CXXFLAGS = -std=c++17 -Wall -Wextra -Werror -pedantic $(INC_FLAGS)
DEBUG = 1
ifeq ($(DEBUG),1)
CXXFLAGS += -g
REQUIRED_CXXFLAGS += -g
endif
CXXFLAGS += $(REQUIRED_CXXFLAGS)
NNTPCHAN_LIB = $(REPO)/libnntpchan.a
MUSTACHE_LIB = $(REPO)/libmustache.a

View File

@ -26,15 +26,16 @@
#define INI_HPP
#include <cassert>
#include <map>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <stdexcept>
#include <string>
#include <cstring>
#include <iostream>
#include <fstream>
namespace INI {
namespace INI
{
struct Level
{
@ -78,8 +79,7 @@ private:
size_t ln_;
};
inline void
Parser::err(const char* s)
inline void Parser::err(const char *s)
{
char buf[256];
sprintf(buf, "%s on line #%ld", s, ln_);
@ -92,14 +92,15 @@ inline std::string trim(const std::string& s)
long sp = 0;
long ep = s.length() - 1;
for (; sp <= ep; ++sp)
if (!strchr(p, s[sp])) break;
if (!strchr(p, s[sp]))
break;
for (; ep >= 0; --ep)
if (!strchr(p, s[ep])) break;
if (!strchr(p, s[ep]))
break;
return s.substr(sp, ep - sp + 1);
}
inline
Parser::Parser(const char* fn) : f0_(fn), f_(&f0_), ln_(0)
inline Parser::Parser(const char *fn) : f0_(fn), f_(&f0_), ln_(0)
{
if (!f0_)
throw std::runtime_error(std::string("failed to open file: ") + fn);
@ -107,25 +108,28 @@ Parser::Parser(const char* fn) : f0_(fn), f_(&f0_), ln_(0)
parse(top_);
}
inline void
Parser::parseSLine(std::string& sname, size_t& depth)
inline void Parser::parseSLine(std::string &sname, size_t &depth)
{
depth = 0;
for (; depth < line_.length(); ++depth)
if (line_[depth] != '[') break;
if (line_[depth] != '[')
break;
sname = line_.substr(depth, line_.length() - 2 * depth);
}
inline void
Parser::parse(Level& l)
inline void Parser::parse(Level &l)
{
while (std::getline(*f_, line_))
{
while (std::getline(*f_, line_)) {
++ln_;
if (line_[0] == '#' || line_[0] == ';') continue;
if (line_[0] == '#' || line_[0] == ';')
continue;
line_ = trim(line_);
if (line_.empty()) continue;
if (line_[0] == '[') {
if (line_.empty())
continue;
if (line_[0] == '[')
{
size_t depth;
std::string sname;
parseSLine(sname, depth);
@ -135,28 +139,32 @@ Parser::parse(Level& l)
err("section with wrong depth");
if (l.depth == depth - 1)
lp = &l.sections[sname];
else {
else
{
lp = l.parent;
size_t n = l.depth - depth;
for (size_t i = 0; i < n; ++i) lp = lp->parent;
for (size_t i = 0; i < n; ++i)
lp = lp->parent;
parent = lp;
lp = &lp->sections[sname];
}
if (lp->depth != 0)
err("duplicate section name on the same level");
if (!lp->parent) {
if (!lp->parent)
{
lp->depth = depth;
lp->parent = parent;
}
parent->ordered_sections.push_back(parent->sections.find(sname));
parse(*lp);
} else {
}
else
{
size_t n = line_.find('=');
if (n == std::string::npos)
err("no '=' found");
std::pair<Level::value_map_t::const_iterator, bool> res =
l.values.insert(std::make_pair(trim(line_.substr(0, n)),
trim(line_.substr(n+1, line_.length()-n-1))));
l.values.insert(std::make_pair(trim(line_.substr(0, n)), trim(line_.substr(n + 1, line_.length() - n - 1))));
if (!res.second)
err("duplicated key found");
l.ordered_values.push_back(res.first);
@ -164,23 +172,26 @@ Parser::parse(Level& l)
}
}
inline void
Parser::dump(std::ostream& s, const Level& l, const std::string& sname)
inline void Parser::dump(std::ostream &s, const Level &l, const std::string &sname)
{
if (!sname.empty()) s << '\n';
for (size_t i = 0; i < l.depth; ++i) s << '[';
if (!sname.empty()) s << sname;
for (size_t i = 0; i < l.depth; ++i) s << ']';
if (!sname.empty()) s << std::endl;
if (!sname.empty())
s << '\n';
for (size_t i = 0; i < l.depth; ++i)
s << '[';
if (!sname.empty())
s << sname;
for (size_t i = 0; i < l.depth; ++i)
s << ']';
if (!sname.empty())
s << std::endl;
for (Level::values_t::const_iterator it = l.ordered_values.begin(); it != l.ordered_values.end(); ++it)
s << (*it)->first << '=' << (*it)->second << std::endl;
for (Level::sections_t::const_iterator it = l.ordered_sections.begin(); it != l.ordered_sections.end(); ++it) {
for (Level::sections_t::const_iterator it = l.ordered_sections.begin(); it != l.ordered_sections.end(); ++it)
{
assert((*it)->second.depth == l.depth + 1);
dump(s, (*it)->second, (*it)->first);
}
}
}
#endif // INI_HPP

View File

@ -1,18 +1,19 @@
#include "ini.hpp"
#include <nntpchan/crypto.hpp>
#include <nntpchan/storage.hpp>
#include <nntpchan/nntp_server.hpp>
#include <nntpchan/event.hpp>
#include <nntpchan/exec_frontend.hpp>
#include <nntpchan/nntp_server.hpp>
#include <nntpchan/staticfile_frontend.hpp>
#include <nntpchan/storage.hpp>
#include <vector>
#include <string>
#include <vector>
int main(int argc, char * argv[]) {
if (argc != 2) {
int main(int argc, char *argv[])
{
if (argc != 2)
{
std::cerr << "usage: " << argv[0] << " config.ini" << std::endl;
return 1;
}
@ -23,20 +24,22 @@ int main(int argc, char * argv[]) {
nntpchan::NNTPServer nntp(loop);
std::string fname(argv[1]);
std::ifstream i(fname);
if(i.is_open()) {
if (i.is_open())
{
INI::Parser conf(i);
std::vector<std::string> requiredSections = {"nntp", "articles"};
auto &level = conf.top();
for ( const auto & section : requiredSections ) {
if(level.sections.find(section) == level.sections.end()) {
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;
@ -45,7 +48,8 @@ int main(int argc, char * argv[]) {
auto &storeconf = level.sections["articles"].values;
if (storeconf.find("store_path") == storeconf.end()) {
if (storeconf.find("store_path") == storeconf.end())
{
std::cerr << "storage section does not have 'store_path' value" << std::endl;
return 1;
}
@ -54,19 +58,22 @@ int main(int argc, char * argv[]) {
auto &nntpconf = level.sections["nntp"].values;
if (nntpconf.find("bind") == nntpconf.end()) {
if (nntpconf.find("bind") == nntpconf.end())
{
std::cerr << "nntp section does not have 'bind' value" << std::endl;
return 1;
}
if(nntpconf.find("instance_name") == nntpconf.end()) {
if (nntpconf.find("instance_name") == nntpconf.end())
{
std::cerr << "nntp section lacks 'instance_name' value" << std::endl;
return 1;
}
nntp.SetInstanceName(nntpconf["instance_name"]);
if (nntpconf.find("authdb") != nntpconf.end()) {
if (nntpconf.find("authdb") != nntpconf.end())
{
nntp.SetLoginDB(nntpconf["authdb"]);
}
@ -91,9 +98,7 @@ int main(int argc, char * argv[]) {
}
else if (ftype == "staticfile")
{
auto required = {
"template_dir", "out_dir", "template_dialect", "max_pages"
};
auto required = {"template_dir", "out_dir", "template_dialect", "max_pages"};
for (const auto &opt : required)
{
if (frontconf.find(opt) == frontconf.end())
@ -108,21 +113,23 @@ int main(int argc, char * argv[]) {
std::cerr << "max_pages invalid value '" << frontconf["max_pages"] << "'" << std::endl;
return 1;
}
nntp.SetFrontend(new nntpchan::StaticFileFrontend(nntpchan::CreateTemplateEngine(frontconf["template_dialect"]), frontconf["template_dir"], frontconf["out_dir"], maxPages));
nntp.SetFrontend(new nntpchan::StaticFileFrontend(nntpchan::CreateTemplateEngine(frontconf["template_dialect"]),
frontconf["template_dir"], frontconf["out_dir"], maxPages));
}
else
{
std::cerr << "unknown frontend type '" << ftype << "'" << std::endl;
return 1;
}
}
auto &a = nntpconf["bind"];
try {
try
{
nntp.Bind(a);
} catch ( std::exception & ex ) {
} catch (std::exception &ex)
{
std::cerr << "failed to bind: " << ex.what() << std::endl;
return 1;
}
@ -130,11 +137,10 @@ int main(int argc, char * argv[]) {
std::cerr << "nntpd for " << nntp.InstanceName() << " bound to " << a << std::endl;
loop.Run();
} else {
}
else
{
std::cerr << "failed to open " << fname << std::endl;
return 1;
}
}

View File

@ -10,8 +10,6 @@ namespace nntpchan
/** @brief returns true if decode was successful */
bool B64Decode(const std::string &data, std::vector<uint8_t> &out);
}
#endif

View File

@ -1,7 +1,7 @@
#ifndef NNTPCHAN_BUFFER_HPP
#define NNTPCHAN_BUFFER_HPP
#include <uv.h>
#include <string>
#include <uv.h>
namespace nntpchan
{

View File

@ -1,8 +1,8 @@
#ifndef NNTPCHAN_CRYPTO_HPP
#define NNTPCHAN_CRYPTO_HPP
#include <sodium/crypto_hash.h>
#include <array>
#include <sodium/crypto_hash.h>
namespace nntpchan
{
@ -18,5 +18,4 @@ namespace nntpchan
};
}
#endif

View File

@ -7,7 +7,6 @@ namespace nntpchan
class Mainloop
{
public:
Mainloop();
~Mainloop();
@ -17,9 +16,7 @@ namespace nntpchan
void Stop();
private:
uv_loop_t *m_loop;
};
}

View File

@ -8,7 +8,6 @@ namespace nntpchan
class ExecFrontend : public Frontend
{
public:
ExecFrontend(const std::string &exe);
~ExecFrontend();
@ -18,12 +17,10 @@ namespace nntpchan
bool AcceptsMessage(const std::string &msgid);
private:
int Exec(std::deque<std::string> args);
private:
std::string m_exec;
};
}

View File

@ -1,9 +1,9 @@
#ifndef NNTPCHAN_FILE_HANDLE_HPP
#define NNTPCHAN_FILE_HANDLE_HPP
#include <memory>
#include <fstream>
#include <experimental/filesystem>
#include <fstream>
#include <memory>
namespace nntpchan
{

View File

@ -1,8 +1,8 @@
#ifndef NNTPCHAN_FRONTEND_HPP
#define NNTPCHAN_FRONTEND_HPP
#include <string>
#include <memory>
#include <experimental/filesystem>
#include <memory>
#include <string>
namespace nntpchan
{
@ -11,7 +11,6 @@ namespace nntpchan
class Frontend
{
public:
/** @brief process an inbound message stored at fpath that we have accepted. */
virtual void ProcessNewMessage(const fs::path &fpath) = 0;
@ -20,7 +19,6 @@ namespace nntpchan
/** @brief return true if we will accept a message given its message-id */
virtual bool AcceptsMessage(const std::string &msgid) = 0;
};
typedef std::unique_ptr<Frontend> Frontend_ptr;

View File

@ -1,6 +1,4 @@
#ifndef NNTPCHAN_HTTP_HPP
#define NNTPCHAN_HTTP_HPP
#endif

View File

@ -1,5 +1,4 @@
#ifndef NNTPCHAN_HTTP_CLIENT_HPP
#define NNTPCHAN_HTTP_CLIENT_HPP
#endif

View File

@ -1,6 +1,4 @@
#ifndef NNTPCHAN_HTTP_SERVER_HPP
#define NNTPCHAN_HTTP_SERVER_HPP
#endif

View File

@ -1,12 +1,11 @@
#ifndef NNTPCHAN_IO_HANDLE_HPP
#define NNTPCHAN_IO_HANDLE_HPP
#include <memory>
#include <iostream>
#include <memory>
namespace nntpchan
{
typedef std::unique_ptr<std::iostream> IOHandle_ptr;
}
#endif

View File

@ -9,7 +9,6 @@ namespace nntpchan
class LineReader
{
public:
LineReader(size_t lineLimit);
/** @brief queue inbound data from connection */
@ -22,7 +21,6 @@ namespace nntpchan
/** @brief handle a line from the client */
virtual void HandleLine(const std::string &line) = 0;
private:
void OnLine(const char *d, const size_t l);
std::string m_leftovers;

View File

@ -13,7 +13,6 @@ namespace nntpchan
bool ReadHeader(const FileHandle_ptr &f, RawHeader &h);
struct MimePart
{
virtual RawHeader &Header() = 0;

View File

@ -2,12 +2,12 @@
#define NNTPCHAN_MODEL_HPP
#include <algorithm>
#include <map>
#include <nntpchan/sanitize.hpp>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#include <variant>
#include <nntpchan/sanitize.hpp>
#include <vector>
namespace nntpchan
{
@ -28,40 +28,24 @@ namespace nntpchan
// a board page is many threads in bump order
typedef std::vector<Thread> BoardPage;
static inline const std::string & GetFilename(const PostAttachment & att)
{
return std::get<0>(att);
}
static inline const std::string &GetFilename(const PostAttachment &att) { return std::get<0>(att); }
static inline const std::string & GetHexDigest(const PostAttachment & att)
{
return std::get<1>(att);
}
static inline const std::string &GetHexDigest(const PostAttachment &att) { return std::get<1>(att); }
static inline const std::string & GetThumbnail(const PostAttachment & att)
{
return std::get<2>(att);
}
static inline const std::string &GetThumbnail(const PostAttachment &att) { return std::get<2>(att); }
static inline const PostHeader & GetHeader(const Post & post)
{
return std::get<0>(post);
}
static inline const PostHeader &GetHeader(const Post &post) { return std::get<0>(post); }
static inline const PostBody & GetBody(const Post & post)
{
return std::get<1>(post);
}
static inline const PostBody &GetBody(const Post &post) { return std::get<1>(post); }
static inline const Attachments & GetAttachments(const Post & post)
{
return std::get<2>(post);
}
static inline const Attachments &GetAttachments(const Post &post) { return std::get<2>(post); }
static inline const std::string & HeaderIFind(const PostHeader & header, const std::string & val, const std::string & fallback)
static inline const std::string &HeaderIFind(const PostHeader &header, const std::string &val,
const std::string &fallback)
{
std::string ival = ToLower(val);
auto itr = std::find_if(header.begin(), header.end(), [ival](const auto & item) -> bool { return ToLower(item.first) == ival; });
auto itr = std::find_if(header.begin(), header.end(),
[ival](const auto &item) -> bool { return ToLower(item.first) == ival; });
if (itr == std::end(header))
return fallback;
else

View File

@ -1,9 +1,9 @@
#ifndef NNTPCHAN_NET_HPP
#define NNTPCHAN_NET_HPP
#include <sys/types.h>
#include <netinet/in.h>
#include <string>
#include <sys/types.h>
namespace nntpchan
{

View File

@ -1,11 +1,11 @@
#ifndef NNTPCHAN_NNTP_AUTH_HPP
#define NNTPCHAN_NNTP_AUTH_HPP
#include <string>
#include <iostream>
#include <fstream>
#include <mutex>
#include <memory>
#include "line.hpp"
#include <fstream>
#include <iostream>
#include <memory>
#include <mutex>
#include <string>
namespace nntpchan
{
@ -30,11 +30,13 @@ namespace nntpchan
public:
HashedCredDB();
bool CheckLogin(const std::string &user, const std::string &passwd);
protected:
void SetStream(std::istream *i);
std::string Hash(const std::string &data, const std::string &salt);
void HandleLine(const std::string &line);
private:
bool ProcessLine(const std::string &line);
@ -52,6 +54,7 @@ namespace nntpchan
~HashedFileDB();
bool Open();
void Close();
private:
std::string m_fname;
std::ifstream f;

View File

@ -1,10 +1,10 @@
#ifndef NNTPCHAN_NNTP_HANDLER_HPP
#define NNTPCHAN_NNTP_HANDLER_HPP
#include <deque>
#include <string>
#include "line.hpp"
#include "nntp_auth.hpp"
#include "storage.hpp"
#include <deque>
#include <string>
namespace nntpchan
{
@ -25,16 +25,16 @@ namespace nntpchan
protected:
void HandleLine(const std::string &line);
void HandleCommand(const std::deque<std::string> &command);
private:
enum State {
private:
enum State
{
eStateReadCommand,
eStateStoreArticle,
eStateQuit
};
private:
void EnterState(State st);
void ArticleObtained();
@ -58,5 +58,4 @@ namespace nntpchan
};
}
#endif

View File

@ -1,10 +1,10 @@
#ifndef NNTPCHAN_NNTP_SERVER_HPP
#define NNTPCHAN_NNTP_SERVER_HPP
#include <uv.h>
#include <string>
#include <deque>
#include "frontend.hpp"
#include "server.hpp"
#include <deque>
#include <string>
#include <uv.h>
namespace nntpchan
{
@ -12,7 +12,6 @@ namespace nntpchan
class NNTPServer : public Server
{
public:
NNTPServer(uv_loop_t *loop);
virtual ~NNTPServer();
@ -34,19 +33,16 @@ namespace nntpchan
void SetFrontend(Frontend *f);
private:
std::string m_logindbpath;
std::string m_storagePath;
std::string m_servername;
Frontend_ptr m_frontend;
};
class NNTPServerConn : public IServerConn
{
public:
NNTPServerConn(uv_loop_t *l, uv_stream_t *s, Server *parent, IConnHandler *h) : IServerConn(l, s, parent, h) {}
virtual bool IsTimedOut() { return false; };
@ -55,7 +51,6 @@ namespace nntpchan
virtual void SendNextReply();
virtual void Greet();
};
}

View File

@ -1,16 +1,15 @@
#ifndef NNTPCHAN_SERVER_HPP
#define NNTPCHAN_SERVER_HPP
#include <uv.h>
#include <deque>
#include <functional>
#include <string>
#include <uv.h>
namespace nntpchan
{
class Server;
struct IConnHandler
{
@ -33,7 +32,6 @@ namespace nntpchan
virtual void Greet() = 0;
private:
std::deque<std::string> m_sendlines;
};
@ -51,6 +49,7 @@ namespace nntpchan
Server *Parent() { return m_parent; };
IConnHandler *GetHandler() { return m_handler; };
uv_loop_t *GetLoop() { return m_loop; };
private:
uv_tcp_t m_conn;
uv_loop_t *m_loop;
@ -82,6 +81,7 @@ namespace nntpchan
protected:
uv_loop_t *GetLoop() { return m_loop; }
virtual void OnAcceptError(int status) = 0;
private:
operator uv_handle_t *() { return (uv_handle_t *)&m_server; }
operator uv_tcp_t *() { return &m_server; }
@ -94,5 +94,4 @@ namespace nntpchan
};
}
#endif

View File

@ -2,8 +2,8 @@
#define NNTPCHAN_STATICFILE_FRONTEND_HPP
#include "frontend.hpp"
#include "message.hpp"
#include "template_engine.hpp"
#include "model.hpp"
#include "template_engine.hpp"
#include <experimental/filesystem>
namespace nntpchan
@ -14,7 +14,6 @@ namespace nntpchan
class StaticFileFrontend : public Frontend
{
public:
StaticFileFrontend(TemplateEngine *tmpl, const std::string &templateDir, const std::string &outDir, uint32_t pages);
~StaticFileFrontend();

View File

@ -1,10 +1,10 @@
#ifndef NNTPCHAN_STORAGE_HPP
#define NNTPCHAN_STORAGE_HPP
#include <experimental/filesystem>
#include <string>
#include "file_handle.hpp"
#include "message.hpp"
#include <experimental/filesystem>
#include <string>
namespace nntpchan
{
@ -14,7 +14,6 @@ namespace nntpchan
class ArticleStorage : public MessageDB
{
public:
ArticleStorage();
ArticleStorage(const fs::path &fpath);
~ArticleStorage();
@ -43,11 +42,9 @@ namespace nntpchan
fs::path skiplist_root(const std::string &name) const;
fs::path basedir;
};
typedef std::unique_ptr<ArticleStorage> ArticleStorage_ptr;
}
#endif

View File

@ -6,35 +6,29 @@ using namespace mstch;
const mstch::node render_context::null_node;
render_context::push::push(render_context& context, const mstch::node& node):
m_context(context)
render_context::push::push(render_context &context, const mstch::node &node) : m_context(context)
{
context.m_nodes.emplace_front(node);
context.m_node_ptrs.emplace_front(&node);
context.m_state.push(std::unique_ptr<render_state>(new outside_section));
}
render_context::push::~push() {
render_context::push::~push()
{
m_context.m_nodes.pop_front();
m_context.m_node_ptrs.pop_front();
m_context.m_state.pop();
}
std::string render_context::push::render(const template_type& templt) {
return m_context.render(templt);
}
std::string render_context::push::render(const template_type &templt) { return m_context.render(templt); }
render_context::render_context(
const mstch::node& node,
const std::map<std::string, template_type>& partials):
m_partials(partials), m_nodes(1, node), m_node_ptrs(1, &node)
render_context::render_context(const mstch::node &node, const std::map<std::string, template_type> &partials)
: m_partials(partials), m_nodes(1, node), m_node_ptrs(1, &node)
{
m_state.push(std::unique_ptr<render_state>(new outside_section));
}
const mstch::node& render_context::find_node(
const std::string& token,
std::list<node const*> current_nodes)
const mstch::node &render_context::find_node(const std::string &token, std::list<node const *> current_nodes)
{
if (token != "." && token.find('.') != std::string::npos)
return find_node(token.substr(token.rfind('.') + 1),
@ -46,16 +40,14 @@ const mstch::node& render_context::find_node(
return null_node;
}
const mstch::node& render_context::get_node(const std::string& token) {
return find_node(token, m_node_ptrs);
}
const mstch::node &render_context::get_node(const std::string &token) { return find_node(token, m_node_ptrs); }
std::string render_context::render(
const template_type& templt, const std::string& prefix)
std::string render_context::render(const template_type &templt, const std::string &prefix)
{
std::string output;
bool prev_eol = true;
for (auto& token: templt) {
for (auto &token : templt)
{
if (prev_eol && prefix.length() != 0)
output += m_state.top()->render(*this, {prefix});
output += m_state.top()->render(*this, token);
@ -64,9 +56,7 @@ std::string render_context::render(
return output;
}
std::string render_context::render_partial(
const std::string& partial_name, const std::string& prefix)
std::string render_context::render_partial(const std::string &partial_name, const std::string &prefix)
{
return m_partials.count(partial_name) ?
render(m_partials.at(partial_name), prefix) : "";
return m_partials.count(partial_name) ? render(m_partials.at(partial_name), prefix) : "";
}

View File

@ -3,49 +3,45 @@
#include <deque>
#include <list>
#include <sstream>
#include <string>
#include <stack>
#include <string>
#include <mstch/mstch.hpp>
#include "state/render_state.hpp"
#include "template_type.hpp"
#include <mstch/mstch.hpp>
namespace mstch {
namespace mstch
{
class render_context {
class render_context
{
public:
class push {
class push
{
public:
push(render_context &context, const mstch::node &node = {});
~push();
std::string render(const template_type &templt);
private:
render_context &m_context;
};
render_context(
const mstch::node& node,
const std::map<std::string, template_type>& partials);
render_context(const mstch::node &node, const std::map<std::string, template_type> &partials);
const mstch::node &get_node(const std::string &token);
std::string render(
const template_type& templt, const std::string& prefix = "");
std::string render_partial(
const std::string& partial_name, const std::string& prefix);
template<class T, class... Args>
void set_state(Args&& ... args) {
m_state.top() = std::unique_ptr<render_state>(
new T(std::forward<Args>(args)...));
std::string render(const template_type &templt, const std::string &prefix = "");
std::string render_partial(const std::string &partial_name, const std::string &prefix);
template <class T, class... Args> void set_state(Args &&... args)
{
m_state.top() = std::unique_ptr<render_state>(new T(std::forward<Args>(args)...));
}
private:
static const mstch::node null_node;
const mstch::node& find_node(
const std::string& token,
std::list<node const*> current_nodes);
const mstch::node &find_node(const std::string &token, std::list<node const *> current_nodes);
std::map<std::string, template_type> m_partials;
std::deque<mstch::node> m_nodes;
std::list<const mstch::node *> m_node_ptrs;
std::stack<std::unique_ptr<render_state>> m_state;
};
}

View File

@ -1,18 +1,20 @@
#include "in_section.hpp"
#include "outside_section.hpp"
#include "../visitor/is_node_empty.hpp"
#include "../visitor/render_section.hpp"
#include "outside_section.hpp"
using namespace mstch;
in_section::in_section(type type, const token& start_token):
m_type(type), m_start_token(start_token), m_skipped_openings(0)
in_section::in_section(type type, const token &start_token)
: m_type(type), m_start_token(start_token), m_skipped_openings(0)
{
}
std::string in_section::render(render_context& ctx, const token& token) {
std::string in_section::render(render_context &ctx, const token &token)
{
if (token.token_type() == token::type::section_close)
if (token.name() == m_start_token.name() && m_skipped_openings == 0) {
if (token.name() == m_start_token.name() && m_skipped_openings == 0)
{
auto &node = ctx.get_node(m_start_token.name());
std::string out;
@ -23,10 +25,10 @@ std::string in_section::render(render_context& ctx, const token& token) {
ctx.set_state<outside_section>();
return out;
} else
}
else
m_skipped_openings--;
else if (token.token_type() == token::type::inverted_section_open ||
token.token_type() == token::type::section_open)
else if (token.token_type() == token::type::inverted_section_open || token.token_type() == token::type::section_open)
m_skipped_openings++;
m_section << token;

View File

@ -3,14 +3,20 @@
#include <sstream>
#include <vector>
#include "render_state.hpp"
#include "../template_type.hpp"
#include "render_state.hpp"
namespace mstch {
namespace mstch
{
class in_section: public render_state {
class in_section : public render_state
{
public:
enum class type { inverted, normal };
enum class type
{
inverted,
normal
};
in_section(type type, const token &start_token);
std::string render(render_context &context, const token &token) override;
@ -20,5 +26,4 @@ class in_section: public render_state {
template_type m_section;
int m_skipped_openings;
};
}

View File

@ -1,16 +1,16 @@
#include "outside_section.hpp"
#include "../render_context.hpp"
#include "../visitor/render_node.hpp"
#include "in_section.hpp"
#include "../render_context.hpp"
using namespace mstch;
std::string outside_section::render(
render_context& ctx, const token& token)
std::string outside_section::render(render_context &ctx, const token &token)
{
using flag = render_node::flag;
switch (token.token_type()) {
switch (token.token_type())
{
case token::type::section_open:
ctx.set_state<in_section>(in_section::type::normal, token);
break;

View File

@ -2,11 +2,12 @@
#include "render_state.hpp"
namespace mstch {
namespace mstch
{
class outside_section: public render_state {
class outside_section : public render_state
{
public:
std::string render(render_context &context, const token &token) override;
};
}

View File

@ -4,14 +4,15 @@
#include "../token.hpp"
namespace mstch {
namespace mstch
{
class render_context;
class render_state {
class render_state
{
public:
virtual ~render_state() {}
virtual std::string render(render_context &context, const token &token) = 0;
};
}

View File

@ -2,57 +2,58 @@
using namespace mstch;
template_type::template_type(const std::string& str, const delim_type& delims):
m_open(delims.first), m_close(delims.second)
template_type::template_type(const std::string &str, const delim_type &delims)
: m_open(delims.first), m_close(delims.second)
{
tokenize(str);
strip_whitespace();
}
template_type::template_type(const std::string& str):
m_open("{{"), m_close("}}")
template_type::template_type(const std::string &str) : m_open("{{"), m_close("}}")
{
tokenize(str);
strip_whitespace();
}
void template_type::process_text(citer begin, citer end) {
void template_type::process_text(citer begin, citer end)
{
if (begin == end)
return;
auto start = begin;
for (auto it = begin; it != end; ++it)
if (*it == '\n' || it == end - 1) {
if (*it == '\n' || it == end - 1)
{
m_tokens.push_back({{start, it + 1}});
start = it + 1;
}
}
void template_type::tokenize(const std::string& tmp) {
void template_type::tokenize(const std::string &tmp)
{
citer beg = tmp.begin();
auto npos = std::string::npos;
for (std::size_t cur_pos = 0; cur_pos < tmp.size();) {
for (std::size_t cur_pos = 0; cur_pos < tmp.size();)
{
auto open_pos = tmp.find(m_open, cur_pos);
auto close_pos = tmp.find(
m_close, open_pos == npos ? open_pos : open_pos + 1);
auto close_pos = tmp.find(m_close, open_pos == npos ? open_pos : open_pos + 1);
if (close_pos != npos && open_pos != npos) {
if (*(beg + open_pos + m_open.size()) == '{' &&
*(beg + close_pos + m_close.size()) == '}')
if (close_pos != npos && open_pos != npos)
{
if (*(beg + open_pos + m_open.size()) == '{' && *(beg + close_pos + m_close.size()) == '}')
++close_pos;
process_text(beg + cur_pos, beg + open_pos);
cur_pos = close_pos + m_close.size();
m_tokens.push_back({{beg + open_pos, beg + close_pos + m_close.size()},
m_open.size(), m_close.size()});
m_tokens.push_back({{beg + open_pos, beg + close_pos + m_close.size()}, m_open.size(), m_close.size()});
if (cur_pos == tmp.size()) {
if (cur_pos == tmp.size())
{
m_tokens.push_back({{""}});
m_tokens.back().eol(true);
}
if (*(beg + open_pos + m_open.size()) == '=' &&
*(beg + close_pos - 1) == '=')
if (*(beg + open_pos + m_open.size()) == '=' && *(beg + close_pos - 1) == '=')
{
auto tok_beg = beg + open_pos + m_open.size() + 1;
auto tok_end = beg + close_pos - 1;
@ -61,27 +62,32 @@ void template_type::tokenize(const std::string& tmp) {
m_open = {front_skip, beg + tmp.find(' ', front_skip - beg)};
m_close = {beg + tmp.rfind(' ', back_skip - beg) + 1, back_skip + 1};
}
} else {
}
else
{
process_text(beg + cur_pos, tmp.end());
cur_pos = close_pos;
}
}
}
void template_type::strip_whitespace() {
void template_type::strip_whitespace()
{
auto line_begin = m_tokens.begin();
bool has_tag = false, non_space = false;
for (auto it = m_tokens.begin(); it != m_tokens.end(); ++it) {
for (auto it = m_tokens.begin(); it != m_tokens.end(); ++it)
{
auto type = (*it).token_type();
if (type != token::type::text && type != token::type::variable &&
type != token::type::unescaped_variable)
if (type != token::type::text && type != token::type::variable && type != token::type::unescaped_variable)
has_tag = true;
else if (!(*it).ws_only())
non_space = true;
if ((*it).eol()) {
if (has_tag && !non_space) {
if ((*it).eol())
{
if (has_tag && !non_space)
{
store_prefixes(line_begin);
auto c = line_begin;
@ -96,9 +102,9 @@ void template_type::strip_whitespace() {
}
}
void template_type::store_prefixes(std::vector<token>::iterator beg) {
void template_type::store_prefixes(std::vector<token>::iterator beg)
{
for (auto cur = beg; !(*cur).eol(); ++cur)
if ((*cur).token_type() == token::type::partial &&
cur != beg && (*(cur - 1)).ws_only())
if ((*cur).token_type() == token::type::partial && cur != beg && (*(cur - 1)).ws_only())
(*cur).partial_prefix((*(cur - 1)).raw());
}

View File

@ -6,9 +6,11 @@
#include "token.hpp"
#include "utils.hpp"
namespace mstch {
namespace mstch
{
class template_type {
class template_type
{
public:
template_type() = default;
template_type(const std::string &str);
@ -26,5 +28,4 @@ class template_type {
void tokenize(const std::string &tmp);
void store_prefixes(std::vector<token>::iterator beg);
};
}

View File

@ -3,38 +3,53 @@
using namespace mstch;
token::type token::token_info(char c) {
switch (c) {
case '>': return type::partial;
case '^': return type::inverted_section_open;
case '/': return type::section_close;
case '&': return type::unescaped_variable;
case '#': return type::section_open;
case '!': return type::comment;
default: return type::variable;
token::type token::token_info(char c)
{
switch (c)
{
case '>':
return type::partial;
case '^':
return type::inverted_section_open;
case '/':
return type::section_close;
case '&':
return type::unescaped_variable;
case '#':
return type::section_open;
case '!':
return type::comment;
default:
return type::variable;
}
}
token::token(const std::string& str, std::size_t left, std::size_t right):
m_raw(str), m_eol(false), m_ws_only(false)
token::token(const std::string &str, std::size_t left, std::size_t right) : m_raw(str), m_eol(false), m_ws_only(false)
{
if (left != 0 && right != 0)
{
if (str[left] == '=' && str[str.size() - right - 1] == '=')
{
if (left != 0 && right != 0) {
if (str[left] == '=' && str[str.size() - right - 1] == '=') {
m_type = type::delimiter_change;
} else if (str[left] == '{' && str[str.size() - right - 1] == '}') {
}
else if (str[left] == '{' && str[str.size() - right - 1] == '}')
{
m_type = type::unescaped_variable;
m_name = {first_not_ws(str.begin() + left + 1, str.end() - right),
first_not_ws(str.rbegin() + 1 + right, str.rend() - left) + 1};
} else {
}
else
{
auto c = first_not_ws(str.begin() + left, str.end() - right);
m_type = token_info(*c);
if (m_type != type::variable)
c = first_not_ws(c + 1, str.end() - right);
m_name = {c, first_not_ws(str.rbegin() + right, str.rend() - left) + 1};
m_delims = {{str.begin(), str.begin() + left},
{str.end() - right, str.end()}};
m_delims = {{str.begin(), str.begin() + left}, {str.end() - right, str.end()}};
}
} else {
}
else
{
m_type = type::text;
m_eol = (str.size() > 0 && str[str.size() - 1] == '\n');
m_ws_only = (str.find_first_not_of(" \r\n\t") == std::string::npos);

View File

@ -2,15 +2,25 @@
#include <string>
namespace mstch {
namespace mstch
{
using delim_type = std::pair<std::string, std::string>;
class token {
class token
{
public:
enum class type {
text, variable, section_open, section_close, inverted_section_open,
unescaped_variable, comment, partial, delimiter_change
enum class type
{
text,
variable,
section_open,
section_close,
inverted_section_open,
unescaped_variable,
comment,
partial,
delimiter_change
};
token(const std::string &str, std::size_t left = 0, std::size_t right = 0);
type token_type() const { return m_type; };
@ -18,9 +28,7 @@ class token {
const std::string &name() const { return m_name; };
const std::string &partial_prefix() const { return m_partial_prefix; };
const delim_type &delims() const { return m_delims; };
void partial_prefix(const std::string& p_partial_prefix) {
m_partial_prefix = p_partial_prefix;
};
void partial_prefix(const std::string &p_partial_prefix) { m_partial_prefix = p_partial_prefix; };
bool eol() const { return m_eol; }
void eol(bool eol) { m_eol = eol; }
bool ws_only() const { return m_ws_only; }
@ -35,5 +43,4 @@ class token {
bool m_ws_only;
type token_info(char c);
};
}

View File

@ -1,23 +1,26 @@
#include "utils.hpp"
#include "mstch/mstch.hpp"
mstch::citer mstch::first_not_ws(mstch::citer begin, mstch::citer end) {
mstch::citer mstch::first_not_ws(mstch::citer begin, mstch::citer end)
{
for (auto it = begin; it != end; ++it)
if (*it != ' ') return it;
if (*it != ' ')
return it;
return end;
}
mstch::citer mstch::first_not_ws(mstch::criter begin, mstch::criter end) {
mstch::citer mstch::first_not_ws(mstch::criter begin, mstch::criter end)
{
for (auto rit = begin; rit != end; ++rit)
if (*rit != ' ') return --(rit.base());
if (*rit != ' ')
return --(rit.base());
return --(end.base());
}
mstch::criter mstch::reverse(mstch::citer it) {
return std::reverse_iterator<mstch::citer>(it);
}
mstch::criter mstch::reverse(mstch::citer it) { return std::reverse_iterator<mstch::citer>(it); }
std::string mstch::html_escape(const std::string& str) {
std::string mstch::html_escape(const std::string &str)
{
if (mstch::config::escape)
return mstch::config::escape(str);
@ -30,14 +33,28 @@ std::string mstch::html_escape(const std::string& str) {
};
for (auto it = str.begin(); it != str.end(); ++it)
switch (*it) {
case '&': add_escape("&amp;", it); break;
case '\'': add_escape("&#39;", it); break;
case '"': add_escape("&quot;", it); break;
case '<': add_escape("&lt;", it); break;
case '>': add_escape("&gt;", it); break;
case '/': add_escape("&#x2F;", it); break;
default: break;
switch (*it)
{
case '&':
add_escape("&amp;", it);
break;
case '\'':
add_escape("&#39;", it);
break;
case '"':
add_escape("&quot;", it);
break;
case '<':
add_escape("&lt;", it);
break;
case '>':
add_escape("&gt;", it);
break;
case '/':
add_escape("&#x2F;", it);
break;
default:
break;
}
return out + std::string{start, str.end()};

View File

@ -1,9 +1,10 @@
#pragma once
#include <string>
#include <boost/variant/apply_visitor.hpp>
#include <string>
namespace mstch {
namespace mstch
{
using citer = std::string::const_iterator;
using criter = std::string::const_reverse_iterator;
@ -13,11 +14,8 @@ citer first_not_ws(criter begin, criter end);
std::string html_escape(const std::string &str);
criter reverse(citer it);
template<class... Args>
auto visit(Args&&... args) -> decltype(boost::apply_visitor(
std::forward<Args>(args)...))
template <class... Args> auto visit(Args &&... args) -> decltype(boost::apply_visitor(std::forward<Args>(args)...))
{
return boost::apply_visitor(std::forward<Args>(args)...);
}
}

View File

@ -2,34 +2,25 @@
#include <boost/variant/static_visitor.hpp>
#include "mstch/mstch.hpp"
#include "has_token.hpp"
#include "mstch/mstch.hpp"
namespace mstch {
class get_token: public boost::static_visitor<const mstch::node&> {
public:
get_token(const std::string& token, const mstch::node& node):
m_token(token), m_node(node)
namespace mstch
{
}
template<class T>
const mstch::node& operator()(const T&) const {
return m_node;
}
class get_token : public boost::static_visitor<const mstch::node &>
{
public:
get_token(const std::string &token, const mstch::node &node) : m_token(token), m_node(node) {}
const mstch::node& operator()(const map& map) const {
return map.at(m_token);
}
template <class T> const mstch::node &operator()(const T &) const { return m_node; }
const mstch::node& operator()(const std::shared_ptr<object>& object) const {
return object->at(m_token);
}
const mstch::node &operator()(const map &map) const { return map.at(m_token); }
const mstch::node &operator()(const std::shared_ptr<object> &object) const { return object->at(m_token); }
private:
const std::string &m_token;
const mstch::node &m_node;
};
}

View File

@ -4,28 +4,21 @@
#include "mstch/mstch.hpp"
namespace mstch {
namespace mstch
{
class has_token: public boost::static_visitor<bool> {
class has_token : public boost::static_visitor<bool>
{
public:
has_token(const std::string& token): m_token(token) {
}
has_token(const std::string &token) : m_token(token) {}
template<class T>
bool operator()(const T&) const {
return m_token == ".";
}
template <class T> bool operator()(const T &) const { return m_token == "."; }
bool operator()(const map& map) const {
return map.count(m_token) == 1;
}
bool operator()(const map &map) const { return map.count(m_token) == 1; }
bool operator()(const std::shared_ptr<object>& object) const {
return object->has(m_token);
}
bool operator()(const std::shared_ptr<object> &object) const { return object->has(m_token); }
private:
const std::string &m_token;
};
}

View File

@ -4,38 +4,24 @@
#include "mstch/mstch.hpp"
namespace mstch {
namespace mstch
{
class is_node_empty: public boost::static_visitor<bool> {
class is_node_empty : public boost::static_visitor<bool>
{
public:
template<class T>
bool operator()(const T&) const {
return false;
}
template <class T> bool operator()(const T &) const { return false; }
bool operator()(const std::nullptr_t&) const {
return true;
}
bool operator()(const std::nullptr_t &) const { return true; }
bool operator()(const int& value) const {
return value == 0;
}
bool operator()(const int &value) const { return value == 0; }
bool operator()(const double& value) const {
return value == 0;
}
bool operator()(const double &value) const { return value == 0; }
bool operator()(const bool& value) const {
return !value;
}
bool operator()(const bool &value) const { return !value; }
bool operator()(const std::string& value) const {
return value == "";
}
bool operator()(const std::string &value) const { return value == ""; }
bool operator()(const array& array) const {
return array.size() == 0;
}
bool operator()(const array &array) const { return array.size() == 0; }
};
}

View File

@ -1,50 +1,47 @@
#pragma once
#include <sstream>
#include <boost/variant/static_visitor.hpp>
#include <sstream>
#include "../render_context.hpp"
#include "mstch/mstch.hpp"
#include "../utils.hpp"
#include "mstch/mstch.hpp"
namespace mstch {
class render_node: public boost::static_visitor<std::string> {
public:
enum class flag { none, escape_html };
render_node(render_context& ctx, flag p_flag = flag::none):
m_ctx(ctx), m_flag(p_flag)
namespace mstch
{
}
template<class T>
std::string operator()(const T&) const {
return "";
}
class render_node : public boost::static_visitor<std::string>
{
public:
enum class flag
{
none,
escape_html
};
render_node(render_context &ctx, flag p_flag = flag::none) : m_ctx(ctx), m_flag(p_flag) {}
std::string operator()(const int& value) const {
return std::to_string(value);
}
template <class T> std::string operator()(const T &) const { return ""; }
std::string operator()(const double& value) const {
std::string operator()(const int &value) const { return std::to_string(value); }
std::string operator()(const double &value) const
{
std::stringstream ss;
ss << value;
return ss.str();
}
std::string operator()(const bool& value) const {
return value ? "true" : "false";
}
std::string operator()(const bool &value) const { return value ? "true" : "false"; }
std::string operator()(const lambda& value) const {
template_type interpreted{value([this](const mstch::node& n) {
return visit(render_node(m_ctx), n);
})};
std::string operator()(const lambda &value) const
{
template_type interpreted{value([this](const mstch::node &n) { return visit(render_node(m_ctx), n); })};
auto rendered = render_context::push(m_ctx).render(interpreted);
return (m_flag == flag::escape_html) ? html_escape(rendered) : rendered;
}
std::string operator()(const std::string& value) const {
std::string operator()(const std::string &value) const
{
return (m_flag == flag::escape_html) ? html_escape(value) : value;
}
@ -52,5 +49,4 @@ class render_node: public boost::static_visitor<std::string> {
render_context &m_ctx;
flag m_flag;
};
}

View File

@ -3,47 +3,49 @@
#include <boost/variant/static_visitor.hpp>
#include "../render_context.hpp"
#include "mstch/mstch.hpp"
#include "../utils.hpp"
#include "mstch/mstch.hpp"
#include "render_node.hpp"
namespace mstch {
namespace mstch
{
class render_section: public boost::static_visitor<std::string> {
class render_section : public boost::static_visitor<std::string>
{
public:
enum class flag { none, keep_array };
render_section(
render_context& ctx,
const template_type& section,
const delim_type& delims,
flag p_flag = flag::none):
m_ctx(ctx), m_section(section), m_delims(delims), m_flag(p_flag)
enum class flag
{
none,
keep_array
};
render_section(render_context &ctx, const template_type &section, const delim_type &delims, flag p_flag = flag::none)
: m_ctx(ctx), m_section(section), m_delims(delims), m_flag(p_flag)
{
}
template<class T>
std::string operator()(const T& t) const {
template <class T> std::string operator()(const T &t) const
{
return render_context::push(m_ctx, t).render(m_section);
}
std::string operator()(const lambda& fun) const {
std::string operator()(const lambda &fun) const
{
std::string section_str;
for (auto &token : m_section)
section_str += token.raw();
template_type interpreted{fun([this](const mstch::node& n) {
return visit(render_node(m_ctx), n);
}, section_str), m_delims};
template_type interpreted{fun([this](const mstch::node &n) { return visit(render_node(m_ctx), n); }, section_str),
m_delims};
return render_context::push(m_ctx).render(interpreted);
}
std::string operator()(const array& array) const {
std::string operator()(const array &array) const
{
std::string out;
if (m_flag == flag::keep_array)
return render_context::push(m_ctx, array).render(m_section);
else
for (auto &item : array)
out += visit(render_section(
m_ctx, m_section, m_delims, flag::keep_array), item);
out += visit(render_section(m_ctx, m_section, m_delims, flag::keep_array), item);
return out;
}
@ -53,5 +55,4 @@ class render_section: public boost::static_visitor<std::string> {
const delim_type &m_delims;
flag m_flag;
};
}

View File

@ -1,6 +1,5 @@
#include <nntpchan/base64.hpp>
// taken from i2pd
namespace i2p
{
@ -16,17 +15,10 @@ namespace data
* Direct Substitution Table
*/
static const char T64[64] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
};
static const char T64[64] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
/*
* Reverse Substitution Table (built in run time)
@ -50,8 +42,7 @@ namespace data
*
*/
static size_t /* Number of bytes in the encoded buffer */
ByteStreamToBase64 (
const uint8_t * InBuffer, /* Input buffer, binary data */
ByteStreamToBase64(const uint8_t *InBuffer, /* Input buffer, binary data */
size_t InCount, /* Number of bytes in the input buffer */
char *OutBuffer, /* output buffer */
size_t len /* length of output buffer */
@ -74,9 +65,11 @@ namespace data
outCount = 4 * n;
else
outCount = 4 * (n + 1);
if (outCount > len) return 0;
if (outCount > len)
return 0;
pd = (unsigned char *)OutBuffer;
for ( i = 0; i<n; i++ ){
for (i = 0; i < n; i++)
{
acc_1 = *ps++;
acc_2 = (acc_1 << 4) & 0x30;
acc_1 >>= 2; /* base64 digit #1 */
@ -92,7 +85,8 @@ namespace data
acc_2 &= 0x3f; /* base64 digit #4 */
*pd++ = T64[acc_2];
}
if ( m == 1 ){
if (m == 1)
{
acc_1 = *ps++;
acc_2 = (acc_1 << 4) & 0x3f; /* base64 digit #2 */
acc_1 >>= 2; /* base64 digit #1 */
@ -100,9 +94,9 @@ namespace data
*pd++ = T64[acc_2];
*pd++ = P64;
*pd++ = P64;
}
else if ( m == 2 ){
else if (m == 2)
{
acc_1 = *ps++;
acc_2 = (acc_1 << 4) & 0x3f;
acc_1 >>= 2; /* base64 digit #1 */
@ -128,10 +122,8 @@ namespace data
* not properly padded, buffer of negative length is returned
*
*/
static
ssize_t /* Number of output bytes */
Base64ToByteStream (
const char * InBuffer, /* BASE64 encoded buffer */
static ssize_t /* Number of output bytes */
Base64ToByteStream(const char *InBuffer, /* BASE64 encoded buffer */
size_t InCount, /* Number of input bytes */
uint8_t *OutBuffer, /* output buffer length */
size_t len /* length of output buffer */
@ -146,36 +138,43 @@ namespace data
int m;
size_t outCount;
if (isFirstTime) iT64Build();
if (isFirstTime)
iT64Build();
n = InCount / 4;
m = InCount % 4;
if (InCount && !m)
outCount = 3 * n;
else {
else
{
outCount = 0;
return 0;
}
ps = (unsigned char *)(InBuffer + InCount - 1);
while ( *ps-- == P64 ) outCount--;
while (*ps-- == P64)
outCount--;
ps = (unsigned char *)InBuffer;
if (outCount > len) return -1;
if (outCount > len)
return -1;
pd = OutBuffer;
auto endOfOutBuffer = OutBuffer + outCount;
for ( i = 0; i < n; i++ ){
for (i = 0; i < n; i++)
{
acc_1 = iT64[*ps++];
acc_2 = iT64[*ps++];
acc_1 <<= 2;
acc_1 |= acc_2 >> 4;
*pd++ = acc_1;
if (pd >= endOfOutBuffer) break;
if (pd >= endOfOutBuffer)
break;
acc_2 <<= 4;
acc_1 = iT64[*ps++];
acc_2 |= acc_1 >> 2;
*pd++ = acc_2;
if (pd >= endOfOutBuffer) break;
if (pd >= endOfOutBuffer)
break;
acc_2 = iT64[*ps++];
acc_2 |= acc_1 << 6;
@ -188,7 +187,8 @@ namespace data
static size_t Base64EncodingBufferSize(const size_t input_size)
{
auto d = div(input_size, 3);
if (d.rem) d.quot++;
if (d.rem)
d.quot++;
return 4 * d.quot;
}
@ -205,12 +205,12 @@ namespace data
{
int i;
isFirstTime = 0;
for ( i=0; i<256; i++ ) iT64[i] = -1;
for ( i=0; i<64; i++ ) iT64[(int)T64[i]] = i;
for (i = 0; i < 256; i++)
iT64[i] = -1;
for (i = 0; i < 64; i++)
iT64[(int)T64[i]] = i;
iT64[(int)P64] = 0;
}
}
}
@ -227,7 +227,8 @@ namespace nntpchan
bool B64Decode(const std::string &data, std::vector<uint8_t> &out)
{
out.resize(data.size());
if(i2p::data::Base64ToByteStream(data.c_str(), data.size(), &out[0], out.size()) == -1) return false;
if (i2p::data::Base64ToByteStream(data.c_str(), data.size(), &out[0], out.size()) == -1)
return false;
out.shrink_to_fit();
return true;
}

View File

@ -1,5 +1,5 @@
#include <nntpchan/buffer.hpp>
#include <cstring>
#include <nntpchan/buffer.hpp>
namespace nntpchan
{
@ -13,9 +13,5 @@ namespace nntpchan
WriteBuffer::WriteBuffer(const std::string &s) : WriteBuffer(s.c_str(), s.size()) {}
WriteBuffer::~WriteBuffer()
{
delete [] b.base;
WriteBuffer::~WriteBuffer() { delete[] b.base; }
}
}

View File

@ -1,20 +1,12 @@
#include <cassert>
#include <nntpchan/crypto.hpp>
#include <sodium.h>
#include <cassert>
namespace nntpchan
{
void SHA512(const uint8_t * d, const std::size_t l, SHA512Digest & h)
{
crypto_hash(h.data(), d, l);
}
void SHA512(const uint8_t *d, const std::size_t l, SHA512Digest &h) { crypto_hash(h.data(), d, l); }
Crypto::Crypto()
{
assert(sodium_init() == 0);
}
Crypto::Crypto() { assert(sodium_init() == 0); }
Crypto::~Crypto()
{
}
Crypto::~Crypto() {}
}

View File

@ -14,31 +14,15 @@ typedef struct
unsigned char buffer[64];
} SHA1_CTX;
void SHA1Transform(
uint32_t state[5],
const unsigned char buffer[64]
);
void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]);
void SHA1Init(
SHA1_CTX * context
);
void SHA1Init(SHA1_CTX *context);
void SHA1Update(
SHA1_CTX * context,
const unsigned char *data,
uint32_t len
);
void SHA1Update(SHA1_CTX *context, const unsigned char *data, uint32_t len);
void SHA1Final(
unsigned char digest[20],
SHA1_CTX * context
);
void sha1(
uint8_t *hash_out,
const uint8_t *str,
size_t len);
void SHA1Final(unsigned char digest[20], SHA1_CTX *context);
void sha1(uint8_t *hash_out, const uint8_t *str, size_t len);
}
#endif

View File

@ -1,5 +1,5 @@
#include <nntpchan/event.hpp>
#include <cassert>
#include <nntpchan/event.hpp>
namespace nntpchan
{
@ -9,19 +9,9 @@ namespace nntpchan
assert(uv_loop_init(m_loop) == 0);
}
Mainloop::~Mainloop()
{
uv_loop_close(m_loop);
}
Mainloop::~Mainloop() { uv_loop_close(m_loop); }
void Mainloop::Stop()
{
uv_stop(m_loop);
}
void Mainloop::Run(uv_run_mode mode)
{
assert(uv_run(m_loop, mode) == 0);
}
void Mainloop::Stop() { uv_stop(m_loop); }
void Mainloop::Run(uv_run_mode mode) { assert(uv_run(m_loop, mode) == 0); }
}

View File

@ -1,33 +1,21 @@
#include <nntpchan/exec_frontend.hpp>
#include <cstring>
#include <iostream>
#include <errno.h>
#include <unistd.h>
#include <iostream>
#include <nntpchan/exec_frontend.hpp>
#include <sys/wait.h>
#include <unistd.h>
namespace nntpchan
{
ExecFrontend::ExecFrontend(const std::string & fname) :
m_exec(fname)
{
}
ExecFrontend::ExecFrontend(const std::string &fname) : m_exec(fname) {}
ExecFrontend::~ExecFrontend() {}
void ExecFrontend::ProcessNewMessage(const fs::path & fpath)
{
Exec({"post", fpath});
}
void ExecFrontend::ProcessNewMessage(const fs::path &fpath) { Exec({"post", fpath}); }
bool ExecFrontend::AcceptsNewsgroup(const std::string & newsgroup)
{
return Exec({"newsgroup", newsgroup}) == 0;
}
bool ExecFrontend::AcceptsNewsgroup(const std::string &newsgroup) { return Exec({"newsgroup", newsgroup}) == 0; }
bool ExecFrontend::AcceptsMessage(const std::string & msgid)
{
return Exec({"msgid", msgid}) == 0;
}
bool ExecFrontend::AcceptsMessage(const std::string &msgid) { return Exec({"msgid", msgid}) == 0; }
int ExecFrontend::Exec(std::deque<std::string> args)
{
@ -35,21 +23,27 @@ namespace nntpchan
const char **cargs = new char const *[args.size() + 2];
std::size_t l = 0;
cargs[l++] = m_exec.c_str();
while (args.size()) {
while (args.size())
{
cargs[l++] = args.front().c_str();
args.pop_front();
}
cargs[l] = 0;
int retcode = 0;
pid_t child = fork();
if(child) {
if (child)
{
waitpid(child, &retcode, 0);
} else {
}
else
{
int r = execvpe(m_exec.c_str(), (char *const *)cargs, environ);
if ( r == -1 ) {
if (r == -1)
{
std::cout << strerror(errno) << std::endl;
exit(errno);
} else
}
else
exit(r);
}
return retcode;

View File

@ -1,6 +1,5 @@
#include <nntpchan/file_handle.hpp>
namespace nntpchan
{
FileHandle_ptr OpenFile(const fs::path &fname, FileMode mode)

View File

@ -1,26 +1,34 @@
#include <nntpchan/line.hpp>
namespace nntpchan {
namespace nntpchan
{
LineReader::LineReader(size_t limit) : m_close(false), lineLimit(limit) {}
void LineReader::Data(const char *data, ssize_t l)
{
if(l <= 0) return;
if (l <= 0)
return;
// process leftovers
std::size_t idx = 0;
std::size_t pos = 0;
while(l-- > 0) {
while (l-- > 0)
{
char c = data[idx++];
if(c == '\n') {
if (c == '\n')
{
OnLine(data, pos);
pos = 0;
data += idx;
} else if (c == '\r' && data[idx] == '\n') {
}
else if (c == '\r' && data[idx] == '\n')
{
OnLine(data, pos);
data += idx + 1;
pos = 0;
} else {
}
else
{
pos++;
}
}
@ -33,8 +41,5 @@ namespace nntpchan {
HandleLine(line);
}
bool LineReader::ShouldClose()
{
return m_close;
}
bool LineReader::ShouldClose() { return m_close; }
}

View File

@ -11,7 +11,8 @@ namespace nntpchan
auto idx = line.find(": ");
auto endidx = line.size() - 1;
while(line[endidx] == '\r') --endidx;
while (line[endidx] == '\r')
--endidx;
if (idx != std::string::npos && idx + 2 < endidx)
{
@ -22,5 +23,4 @@ namespace nntpchan
}
return file->good();
}
}

View File

@ -1,8 +1,8 @@
#include <cstring>
#include <nntpchan/net.hpp>
#include <uv.h>
#include <sstream>
#include <stdexcept>
#include <cstring>
#include <uv.h>
namespace nntpchan
{
@ -11,7 +11,8 @@ namespace nntpchan
std::string str("invalid");
const size_t s = 128;
char *buff = new char[s];
if(uv_ip6_name(&addr, buff, s) == 0) {
if (uv_ip6_name(&addr, buff, s) == 0)
{
str = std::string(buff);
delete[] buff;
}
@ -20,19 +21,18 @@ namespace nntpchan
return ss.str();
}
NetAddr::NetAddr()
{
std::memset(&addr, 0, sizeof(addr));
}
NetAddr::NetAddr() { std::memset(&addr, 0, sizeof(addr)); }
NetAddr ParseAddr(const std::string &addr)
{
NetAddr saddr;
auto n = addr.rfind("]:");
if (n == std::string::npos) {
if (n == std::string::npos)
{
throw std::runtime_error("invalid address: " + addr);
}
if (addr[0] != '[') {
if (addr[0] != '[')
{
throw std::runtime_error("invalid address: " + addr);
}
auto p = addr.substr(n + 2);

View File

@ -1,9 +1,9 @@
#include <nntpchan/nntp_auth.hpp>
#include <nntpchan/crypto.hpp>
#include <nntpchan/base64.hpp>
#include <array>
#include <iostream>
#include <fstream>
#include <iostream>
#include <nntpchan/base64.hpp>
#include <nntpchan/crypto.hpp>
#include <nntpchan/nntp_auth.hpp>
namespace nntpchan
{
@ -31,19 +31,25 @@ namespace nntpchan
// strip comments
auto comment = line.find("#");
std::string part = line;
for (; comment != std::string::npos; comment = part.find("#")) {
for (; comment != std::string::npos; comment = part.find("#"))
{
if (comment)
part = part.substr(0, comment);
else break;
else
break;
}
if(!part.size()) return false; // empty line after comments
if (!part.size())
return false; // empty line after comments
auto idx = part.find(":");
if (idx == std::string::npos) return false; // bad format
if (m_user != part.substr(0, idx)) return false; // username mismatch
if (idx == std::string::npos)
return false; // bad format
if (m_user != part.substr(0, idx))
return false; // username mismatch
part = part.substr(idx + 1);
idx = part.find(":");
if (idx == std::string::npos) return false; // bad format
if (idx == std::string::npos)
return false; // bad format
std::string cred = part.substr(0, idx);
std::string salt = part.substr(idx + 1);
return Hash(m_passwd, salt) == cred;
@ -51,15 +57,13 @@ namespace nntpchan
void HashedCredDB::HandleLine(const std::string &line)
{
if(m_found) return;
if (m_found)
return;
if (ProcessLine(line))
m_found = true;
}
void HashedCredDB::SetStream(std::istream * s)
{
m_instream = s;
}
void HashedCredDB::SetStream(std::istream *s) { m_instream = s; }
std::string HashedCredDB::Hash(const std::string &data, const std::string &salt)
{
@ -69,16 +73,9 @@ namespace nntpchan
return B64Encode(h.data(), h.size());
}
HashedFileDB::HashedFileDB(const std::string & fname) :
m_fname(fname),
f(nullptr)
{
HashedFileDB::HashedFileDB(const std::string &fname) : m_fname(fname), f(nullptr) {}
}
HashedFileDB::~HashedFileDB()
{
}
HashedFileDB::~HashedFileDB() {}
void HashedFileDB::Close()
{
@ -90,7 +87,8 @@ namespace nntpchan
{
if (!f.is_open())
f.open(m_fname);
if(f.is_open()) {
if (f.is_open())
{
SetStream(&f);
return true;
}

View File

@ -1,27 +1,21 @@
#include <nntpchan/nntp_handler.hpp>
#include <nntpchan/sanitize.hpp>
#include <algorithm>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>
#include <nntpchan/nntp_handler.hpp>
#include <nntpchan/sanitize.hpp>
#include <sstream>
#include <string>
namespace nntpchan
{
NNTPServerHandler::NNTPServerHandler(const fs::path & storage) :
LineReader(1024),
m_article(nullptr),
m_auth(nullptr),
m_store(std::make_unique<ArticleStorage>(storage)),
m_authed(false),
m_state(eStateReadCommand)
NNTPServerHandler::NNTPServerHandler(const fs::path &storage)
: LineReader(1024), m_article(nullptr), m_auth(nullptr), m_store(std::make_unique<ArticleStorage>(storage)),
m_authed(false), m_state(eStateReadCommand)
{
}
NNTPServerHandler::~NNTPServerHandler()
{
}
NNTPServerHandler::~NNTPServerHandler() {}
void NNTPServerHandler::HandleLine(const std::string &line)
{
@ -30,8 +24,10 @@ namespace nntpchan
std::deque<std::string> command;
std::istringstream s;
s.str(line);
for (std::string part; std::getline(s, part, ' '); ) {
if(part.size()) command.push_back(part);
for (std::string part; std::getline(s, part, ' ');)
{
if (part.size())
command.push_back(part);
}
if (command.size())
HandleCommand(command);
@ -51,7 +47,8 @@ namespace nntpchan
void NNTPServerHandler::OnData(const char *data, ssize_t l)
{
if(l <= 0 ) return;
if (l <= 0)
return;
if (m_state == eStateStoreArticle)
{
const char *end = strstr(data, "\r\n.\r\n");
@ -86,7 +83,8 @@ namespace nntpchan
for (const auto &part : command)
std::cerr << " " << part;
std::cerr << std::endl;
if (cmd == "QUIT") {
if (cmd == "QUIT")
{
Quit();
return;
}
@ -94,26 +92,37 @@ namespace nntpchan
{
return;
}
else if (cmd == "MODE" ) {
if(cmdlen == 2) {
else if (cmd == "MODE")
{
if (cmdlen == 2)
{
// set mode
SwitchMode(command[1]);
} else if(cmdlen) {
}
else if (cmdlen)
{
// too many arguments
QueueLine("500 too many arguments");
} else {
}
else
{
// get mode
QueueLine("500 wrong arguments");
}
} else if(cmd == "CAPABILITIES") {
}
else if (cmd == "CAPABILITIES")
{
QueueLine("101 I support the following:");
QueueLine("READER");
QueueLine("IMPLEMENTATION nntpchan-daemon");
QueueLine("VERSION 2");
QueueLine("STREAMING");
QueueLine(".");
} else if (cmd == "CHECK") {
if(cmdlen >= 2) {
}
else if (cmd == "CHECK")
{
if (cmdlen >= 2)
{
const std::string &msgid = command[1];
if (IsValidMessageID(msgid) && m_store->Accept(msgid))
{
@ -124,7 +133,9 @@ namespace nntpchan
}
else
QueueLine("501 syntax error");
} else if (cmd == "TAKETHIS") {
}
else if (cmd == "TAKETHIS")
{
if (cmdlen >= 2)
{
const std::string &msgid = command[1];
@ -137,7 +148,9 @@ namespace nntpchan
return;
}
QueueLine("501 invalid syntax");
} else {
}
else
{
// unknown command
QueueLine("500 Unknown Command");
}
@ -162,21 +175,32 @@ namespace nntpchan
{
std::string m = mode;
std::transform(m.begin(), m.end(), m.begin(), ::toupper);
if (m == "READER") {
if (m == "READER")
{
m_mode = m;
if(PostingAllowed()) {
if (PostingAllowed())
{
QueueLine("200 Posting is permitted yo");
} else {
}
else
{
QueueLine("201 Posting is not permitted yo");
}
} else if (m == "STREAM") {
}
else if (m == "STREAM")
{
m_mode = m;
if (PostingAllowed()) {
if (PostingAllowed())
{
QueueLine("203 Streaming enabled");
} else {
}
else
{
QueueLine("483 Streaming Denied");
}
} else {
}
else
{
// unknown mode
QueueLine("500 Unknown mode");
}
@ -194,15 +218,9 @@ namespace nntpchan
QueueLine("205 quitting");
}
bool NNTPServerHandler::ShouldClose()
{
return m_state == eStateQuit;
}
bool NNTPServerHandler::ShouldClose() { return m_state == eStateQuit; }
bool NNTPServerHandler::PostingAllowed()
{
return m_authed || m_auth == nullptr;
}
bool NNTPServerHandler::PostingAllowed() { return m_authed || m_auth == nullptr; }
void NNTPServerHandler::Greet()
{
@ -212,8 +230,5 @@ namespace nntpchan
QueueLine("201 Posting not allowed");
}
void NNTPServerHandler::SetAuth(CredDB_ptr creds)
{
m_auth = creds;
}
void NNTPServerHandler::SetAuth(CredDB_ptr creds) { m_auth = creds; }
}

View File

@ -1,10 +1,10 @@
#include <nntpchan/nntp_server.hpp>
#include <nntpchan/nntp_auth.hpp>
#include <nntpchan/nntp_handler.hpp>
#include <nntpchan/net.hpp>
#include <cassert>
#include <iostream>
#include <nntpchan/net.hpp>
#include <nntpchan/nntp_auth.hpp>
#include <nntpchan/nntp_handler.hpp>
#include <nntpchan/nntp_server.hpp>
#include <sstream>
namespace nntpchan
@ -12,9 +12,7 @@ namespace nntpchan
NNTPServer::NNTPServer(uv_loop_t *loop) : Server(loop), m_frontend(nullptr) {}
NNTPServer::~NNTPServer()
{
}
NNTPServer::~NNTPServer() {}
IServerConn *NNTPServer::CreateConn(uv_stream_t *s)
{
@ -22,7 +20,8 @@ namespace nntpchan
std::ifstream i;
i.open(m_logindbpath);
if(i.is_open()) creds = std::make_shared<HashedFileDB>(m_logindbpath);
if (i.is_open())
creds = std::make_shared<HashedFileDB>(m_logindbpath);
NNTPServerHandler *handler = new NNTPServerHandler(m_storagePath);
if (creds)
@ -32,54 +31,32 @@ namespace nntpchan
return conn;
}
void NNTPServer::SetLoginDB(const std::string path)
{
m_logindbpath = path;
}
void NNTPServer::SetLoginDB(const std::string path) { m_logindbpath = path; }
void NNTPServer::SetStoragePath(const std::string &path) { m_storagePath = path; }
void NNTPServer::SetStoragePath(const std::string & path)
{
m_storagePath = path;
}
void NNTPServer::SetInstanceName(const std::string &name) { m_servername = name; }
void NNTPServer::SetInstanceName(const std::string & name)
{
m_servername = name;
}
void NNTPServer::SetFrontend(Frontend *f) { m_frontend.reset(f); }
void NNTPServer::SetFrontend(Frontend * f)
{
m_frontend.reset(f);
}
std::string NNTPServer::InstanceName() const { return m_servername; }
std::string NNTPServer::InstanceName() const
{
return m_servername;
}
void NNTPServer::OnAcceptError(int status)
{
std::cerr << "nntpserver::accept() " << uv_strerror(status) << std::endl;
}
void NNTPServer::OnAcceptError(int status) { std::cerr << "nntpserver::accept() " << uv_strerror(status) << std::endl; }
void NNTPServerConn::SendNextReply()
{
IConnHandler *handler = GetHandler();
while(handler->HasNextLine()) {
while (handler->HasNextLine())
{
auto line = handler->GetNextLine();
SendString(line + "\r\n");
}
}
void NNTPServerConn::Greet()
{
IConnHandler *handler = GetHandler();
handler->Greet();
SendNextReply();
}
}

View File

@ -1,14 +1,15 @@
#include <nntpchan/sanitize.hpp>
#include <algorithm>
#include <regex>
#include <cctype>
#include <nntpchan/sanitize.hpp>
#include <regex>
namespace nntpchan
{
std::string NNTPSanitizeLine(const std::string &str)
{
if(str == ".") return " .";
if (str == ".")
return " .";
std::string sane;
sane += str;
const char ch = ' ';
@ -19,23 +20,18 @@ namespace nntpchan
std::string ToLower(const std::string &str)
{
std::string lower = str;
std::transform(lower.begin(), lower.end(), lower.begin(), [](unsigned char ch) -> unsigned char { return std::tolower(ch); } );
std::transform(lower.begin(), lower.end(), lower.begin(),
[](unsigned char ch) -> unsigned char { return std::tolower(ch); });
return lower;
}
static const std::regex re_ValidMessageID("^<[a-zA-Z0-9$\\._]{2,128}@[a-zA-Z0-9\\-\\.]{2,63}>$");
bool IsValidMessageID(const std::string & msgid)
{
return std::regex_search(msgid, re_ValidMessageID) == 1;
}
bool IsValidMessageID(const std::string &msgid) { return std::regex_search(msgid, re_ValidMessageID) == 1; }
static const std::regex re_ValidNewsgroup("^[a-zA-Z][a-zA-Z0-9.]{1,128}$");
bool IsValidNewsgroup(const std::string & msgid)
{
return std::regex_search(msgid, re_ValidNewsgroup) == 1;
}
bool IsValidNewsgroup(const std::string &msgid) { return std::regex_search(msgid, re_ValidNewsgroup) == 1; }
std::string StripWhitespaces(const std::string &str)
{

View File

@ -1,8 +1,8 @@
#include <nntpchan/buffer.hpp>
#include <nntpchan/server.hpp>
#include <nntpchan/net.hpp>
#include <cassert>
#include <iostream>
#include <nntpchan/buffer.hpp>
#include <nntpchan/net.hpp>
#include <nntpchan/server.hpp>
namespace nntpchan
{
@ -18,7 +18,8 @@ namespace nntpchan
std::cout << "Close server" << std::endl;
uv_close((uv_handle_t *)&m_server, [](uv_handle_t *s) {
Server *self = (Server *)s->data;
if (self) delete self;
if (self)
delete self;
s->data = nullptr;
});
}
@ -36,7 +37,8 @@ namespace nntpchan
void Server::OnAccept(uv_stream_t *s, int status)
{
if(status < 0) {
if (status < 0)
{
OnAcceptError(status);
return;
}
@ -58,15 +60,9 @@ namespace nntpchan
}
}
void IConnHandler::QueueLine(const std::string & line)
{
m_sendlines.push_back(line);
}
void IConnHandler::QueueLine(const std::string &line) { m_sendlines.push_back(line); }
bool IConnHandler::HasNextLine()
{
return m_sendlines.size() > 0;
}
bool IConnHandler::HasNextLine() { return m_sendlines.size() > 0; }
std::string IConnHandler::GetNextLine()
{
@ -83,25 +79,33 @@ namespace nntpchan
uv_tcp_init(l, &m_conn);
m_conn.data = this;
uv_accept(st, (uv_stream_t *)&m_conn);
uv_read_start((uv_stream_t*) &m_conn, [] (uv_handle_t * h, size_t s, uv_buf_t * b) {
uv_read_start((uv_stream_t *)&m_conn,
[](uv_handle_t *h, size_t s, uv_buf_t *b) {
IServerConn *self = (IServerConn *)h->data;
if(self == nullptr) return;
if (self == nullptr)
return;
b->base = new char[s];
}, [] (uv_stream_t * s, ssize_t nread, const uv_buf_t * b) {
},
[](uv_stream_t *s, ssize_t nread, const uv_buf_t *b) {
IServerConn *self = (IServerConn *)s->data;
if(self == nullptr) {
if (self == nullptr)
{
if (b->base)
delete[] b->base;
return;
}
if(nread > 0) {
if (nread > 0)
{
self->m_handler->OnData(b->base, nread);
self->SendNextReply();
if (self->m_handler->ShouldClose())
self->Close();
delete[] b->base;
} else {
if (nread != UV_EOF) {
}
else
{
if (nread != UV_EOF)
{
std::cerr << "error in nntp server conn alloc: ";
std::cerr << uv_strerror(nread);
std::cerr << std::endl;
@ -112,10 +116,7 @@ namespace nntpchan
});
}
IServerConn::~IServerConn()
{
delete m_handler;
}
IServerConn::~IServerConn() { delete m_handler; }
void IServerConn::SendString(const std::string &str)
{

View File

@ -27,42 +27,45 @@ extern "C" {
/* for uint32_t */
#include <stdint.h>
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
/* blk0() and blk() perform the initial expand. */
/* I got the idea of expanding during the round function from SSLeay */
#if BYTE_ORDER == LITTLE_ENDIAN
#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
|(rol(block->l[i],8)&0x00FF00FF))
#define blk0(i) (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | (rol(block->l[i], 8) & 0x00FF00FF))
#elif BYTE_ORDER == BIG_ENDIAN
#define blk0(i) block->l[i]
#else
#error "Endianness not defined!"
#endif
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
^block->l[(i+2)&15]^block->l[i&15],1))
#define blk(i) \
(block->l[i & 15] = \
rol(block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] ^ block->l[(i + 2) & 15] ^ block->l[i & 15], 1))
/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
#define R0(v, w, x, y, z, i) \
z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
w = rol(w, 30);
#define R1(v, w, x, y, z, i) \
z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
w = rol(w, 30);
#define R2(v, w, x, y, z, i) \
z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \
w = rol(w, 30);
#define R3(v, w, x, y, z, i) \
z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
w = rol(w, 30);
#define R4(v, w, x, y, z, i) \
z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
w = rol(w, 30);
/* Hash a single 512-bit block. This is the core of the algorithm. */
void SHA1Transform(
uint32_t state[5],
const unsigned char buffer[64]
)
void SHA1Transform(uint32_t state[5], const unsigned char buffer[64])
{
uint32_t a, b, c, d, e;
typedef union
{
typedef union {
unsigned char c[64];
uint32_t l[16];
} CHAR64LONG16;
@ -179,12 +182,9 @@ void SHA1Transform(
#endif
}
/* SHA1Init - Initialize new context */
void SHA1Init(
SHA1_CTX * context
)
void SHA1Init(SHA1_CTX *context)
{
/* SHA1 initialization constants */
context->state[0] = 0x67452301;
@ -195,14 +195,9 @@ void SHA1Init(
context->count[0] = context->count[1] = 0;
}
/* Run your data through this. */
void SHA1Update(
SHA1_CTX * context,
const unsigned char *data,
uint32_t len
)
void SHA1Update(SHA1_CTX *context, const unsigned char *data, uint32_t len)
{
uint32_t i;
@ -228,13 +223,9 @@ void SHA1Update(
memcpy(&context->buffer[j], &data[i], len - i);
}
/* Add padding and return the message digest. */
void SHA1Final(
unsigned char digest[20],
SHA1_CTX * context
)
void SHA1Final(unsigned char digest[20], SHA1_CTX *context)
{
unsigned i;
@ -261,7 +252,8 @@ void SHA1Final(
#else
for (i = 0; i < 8; i++)
{
finalcount[i] = (unsigned char) ((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */
finalcount[i] =
(unsigned char)((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */
}
#endif
c = 0200;
@ -274,18 +266,14 @@ void SHA1Final(
SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
for (i = 0; i < 20; i++)
{
digest[i] = (unsigned char)
((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
digest[i] = (unsigned char)((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
}
/* Wipe variables */
memset(context, '\0', sizeof(*context));
memset(&finalcount, '\0', sizeof(finalcount));
}
void sha1(
uint8_t *hash_out,
const uint8_t *str,
size_t len)
void sha1(uint8_t *hash_out, const uint8_t *str, size_t len)
{
SHA1_CTX ctx;
size_t ii;
@ -295,7 +283,6 @@ void sha1(
SHA1Update(&ctx, str + ii, 1);
SHA1Final(hash_out, &ctx);
}
}
namespace nntpchan

View File

@ -1,22 +1,19 @@
#include <nntpchan/staticfile_frontend.hpp>
#include <nntpchan/file_handle.hpp>
#include <nntpchan/sanitize.hpp>
#include <nntpchan/mime.hpp>
#include <nntpchan/sha1.hpp>
#include <any>
#include <iostream>
#include <nntpchan/file_handle.hpp>
#include <nntpchan/mime.hpp>
#include <nntpchan/sanitize.hpp>
#include <nntpchan/sha1.hpp>
#include <nntpchan/staticfile_frontend.hpp>
#include <set>
#include <sstream>
namespace nntpchan
{
StaticFileFrontend::StaticFileFrontend(TemplateEngine * tmpl, const std::string & templateDir, const std::string & outDir, uint32_t pages) :
m_TemplateEngine(tmpl),
m_TemplateDir(templateDir),
m_OutDir(outDir),
m_Pages(pages)
StaticFileFrontend::StaticFileFrontend(TemplateEngine *tmpl, const std::string &templateDir, const std::string &outDir,
uint32_t pages)
: m_TemplateEngine(tmpl), m_TemplateDir(templateDir), m_OutDir(outDir), m_Pages(pages)
{
}
@ -36,7 +33,6 @@ namespace nntpchan
// read body
auto findMsgidFunc = [](const std::pair<std::string, std::string> &item) -> bool {
auto lower = ToLower(item.first);
return (lower == "message-id") || (lower == "messageid");
@ -85,10 +81,29 @@ namespace nntpchan
}
}
std::string rootmsgid_hash = sha1_hex(rootmsgid);
std::set<std::string> newsgroups_list;
auto findNewsgroupsFunc = [](const std::pair<std::string, std::string> &item) -> bool {
return ToLower(item.first) == "newsgroups";
};
auto group = std::find_if(header.begin(), header.end(), findNewsgroupsFunc);
if (group == std::end(header))
{
std::clog << "no newsgroups header" << std::endl;
return;
}
std::istringstream input(group->second);
std::string newsgroup;
while (std::getline(input, newsgroup, ' '))
{
if (IsValidNewsgroup(newsgroup))
newsgroups_list.insert(newsgroup);
}
fs::path threadFilePath = m_OutDir / fs::path("thread-" + rootmsgid_hash + ".html");
nntpchan::model::Thread thread;
@ -114,27 +129,6 @@ namespace nntpchan
return;
}
}
std::set<std::string> newsgroups_list;
auto findNewsgroupsFunc = [](const std::pair<std::string, std::string> & item) -> bool
{
return ToLower(item.first) == "newsgroups";
};
auto group = std::find_if(header.begin(), header.end(), findNewsgroupsFunc);
if(group == std::end(header))
{
std::clog << "no newsgroups header" << std::endl;
return;
}
std::istringstream input(group->second);
std::string newsgroup;
while(std::getline(input, newsgroup, ' '))
{
if(IsValidNewsgroup(newsgroup))
newsgroups_list.insert(newsgroup);
}
nntpchan::model::BoardPage page;
for (const auto &name : newsgroups_list)
{
@ -172,15 +166,7 @@ namespace nntpchan
}
}
bool StaticFileFrontend::AcceptsNewsgroup(const std::string &newsgroup) { return IsValidNewsgroup(newsgroup); }
bool StaticFileFrontend::AcceptsNewsgroup(const std::string & newsgroup)
{
return IsValidNewsgroup(newsgroup);
}
bool StaticFileFrontend::AcceptsMessage(const std::string & msgid)
{
return IsValidMessageID(msgid);
}
bool StaticFileFrontend::AcceptsMessage(const std::string &msgid) { return IsValidMessageID(msgid); }
}

View File

@ -1,21 +1,14 @@
#include <nntpchan/storage.hpp>
#include <nntpchan/sanitize.hpp>
#include <cassert>
#include <nntpchan/sanitize.hpp>
#include <nntpchan/storage.hpp>
#include <sstream>
namespace nntpchan
{
ArticleStorage::ArticleStorage()
{
}
ArticleStorage::ArticleStorage(const fs::path & fpath) {
SetPath(fpath);
}
ArticleStorage::ArticleStorage(const fs::path &fpath) { SetPath(fpath); }
ArticleStorage::~ArticleStorage()
{
}
ArticleStorage::~ArticleStorage() {}
void ArticleStorage::SetPath(const fs::path &fpath)
{
@ -36,24 +29,43 @@ namespace nntpchan
bool ArticleStorage::Accept(const std::string &msgid) const
{
if (!IsValidMessageID(msgid)) return false;
if (!IsValidMessageID(msgid))
return false;
auto p = MessagePath(msgid);
return !fs::exists(p);
}
fs::path ArticleStorage::MessagePath(const std::string & msgid) const
{
return basedir / msgid;
}
fs::path ArticleStorage::MessagePath(const std::string &msgid) const { return basedir / msgid; }
FileHandle_ptr ArticleStorage::OpenRead(const std::string & msgid) const
{
return OpenFile(MessagePath(msgid), eRead);
}
FileHandle_ptr ArticleStorage::OpenRead(const std::string &msgid) const { return OpenFile(MessagePath(msgid), eRead); }
FileHandle_ptr ArticleStorage::OpenWrite(const std::string &msgid) const
{
return OpenFile(MessagePath(msgid), eWrite);
}
bool ArticleStorage::LoadBoardPage(BoardPage &board, const std::string &newsgroup, uint32_t perpage,
uint32_t page) const
{
(void)board;
(void)newsgroup;
(void)perpage;
(void)page;
return false;
}
bool ArticleStorage::FindThreadByHash(const std::string &hashhex, std::string &msgid) const
{
(void)hashhex;
(void)msgid;
return false;
}
bool ArticleStorage::LoadThread(Thread &thread, const std::string &rootmsgid) const
{
(void)thread;
(void)rootmsgid;
return false;
}
/** ensure symlinks are formed for this article by message id */
void ArticleStorage::EnsureSymlinks(const std::string &msgid) const { (void)msgid; }
}

View File

@ -1,13 +1,16 @@
#include <nntpchan/template_engine.hpp>
#include <nntpchan/sanitize.hpp>
#include <mstch/mstch.hpp>
#include <iostream>
#include <mstch/mstch.hpp>
#include <nntpchan/sanitize.hpp>
#include <nntpchan/template_engine.hpp>
#include <sstream>
namespace nntpchan
{
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template <class... Ts> struct overloaded : Ts...
{
using Ts::operator()...;
};
template <class... Ts> overloaded(Ts...)->overloaded<Ts...>;
namespace mustache = mstch;
@ -77,10 +80,8 @@ namespace nntpchan
mustache::map obj;
for (const auto &item : args)
{
std::visit(overloaded {
[&obj, item](const nntpchan::model::Model & m) {
std::visit(overloaded {
[&obj, item](const nntpchan::model::BoardPage & p) {
std::visit(overloaded{[&obj, item](const nntpchan::model::Model &m) {
std::visit(overloaded{[&obj, item](const nntpchan::model::BoardPage &p) {
mustache::array threads;
for (const auto &thread : p)
{
@ -90,13 +91,11 @@ namespace nntpchan
},
[&obj, item](const nntpchan::model::Thread &t) {
obj[item.first] = thread_to_map(t);
}
}, m);
}
,[&obj, item](const std::string & str) {
obj[item.first] = str;
}
}, item.second);
}},
m);
},
[&obj, item](const std::string &str) { obj[item.first] = str; }},
item.second);
}
std::string str = mustache::render(m_tmplString, obj);
@ -141,7 +140,8 @@ namespace nntpchan
for (const auto &fname : partial_files)
{
auto file = OpenFile(dir / fs::path(fname + std::string(".html")), eRead);
if(!file) {
if (!file)
{
std::clog << "no such partial: " << fname << std::endl;
return false;
}

View File

@ -1,9 +1,7 @@
#include <nntpchan/exec_frontend.hpp>
#include <nntpchan/sanitize.hpp>
#include <cassert>
#include <iostream>
#include <nntpchan/exec_frontend.hpp>
#include <nntpchan/sanitize.hpp>
int main(int, char *[])
{

View File

@ -3,19 +3,16 @@
#include <cassert>
#include <cstring>
#include <string>
#include <iostream>
#include <sodium.h>
#include <string>
static void print_help(const std::string &exename)
{
std::cout << "usage: " << exename << " [help|gen|check]" << std::endl;
}
static void print_long_help()
{
}
static void print_long_help() {}
static void gen_passwd(const std::string &username, const std::string &passwd)
{
@ -29,18 +26,20 @@ static void gen_passwd(const std::string & username, const std::string & passwd)
std::cout << username << ":" << hash << ":" << salt << std::endl;
}
static bool check_cred(const std::string &cred, const std::string &passwd)
{
auto idx = cred.find(":");
if(idx == std::string::npos || idx == 0) return false;
if (idx == std::string::npos || idx == 0)
return false;
std::string part = cred.substr(idx + 1);
idx = part.find(":");
if(idx == std::string::npos || idx == 0) return false;
if (idx == std::string::npos || idx == 0)
return false;
std::string salt = part.substr(idx + 1);
std::string hash = part.substr(0, idx);
std::vector<uint8_t> h;
if(!nntpchan::B64Decode(hash, h)) return false;
if (!nntpchan::B64Decode(hash, h))
return false;
nntpchan::SHA512Digest d;
std::string l = passwd + salt;
nntpchan::SHA512((const uint8_t *)l.data(), l.size(), d);
@ -50,36 +49,46 @@ static bool check_cred(const std::string & cred, const std::string & passwd)
int main(int argc, char *argv[])
{
assert(sodium_init() == 0);
if(argc == 1) {
if (argc == 1)
{
print_help(argv[0]);
return 1;
}
std::string cmd(argv[1]);
if (cmd == "help") {
if (cmd == "help")
{
print_long_help();
return 0;
}
if (cmd == "gen") {
if(argc == 4) {
if (cmd == "gen")
{
if (argc == 4)
{
gen_passwd(argv[2], argv[3]);
return 0;
} else {
}
else
{
std::cout << "usage: " << argv[0] << " gen username password" << std::endl;
return 1;
}
}
if(cmd == "check" ) {
if (cmd == "check")
{
std::string cred;
std::cout << "credential: ";
if(!std::getline(std::cin, cred)) {
if (!std::getline(std::cin, cred))
{
return 1;
}
std::string passwd;
std::cout << "password: ";
if(!std::getline(std::cin, passwd)) {
if (!std::getline(std::cin, passwd))
{
return 1;
}
if(check_cred(cred, passwd)) {
if (check_cred(cred, passwd))
{
std::cout << "okay" << std::endl;
return 0;
}

View File

@ -1,9 +1,7 @@
#include <nntpchan/exec_frontend.hpp>
#include <nntpchan/sanitize.hpp>
#include <cassert>
#include <iostream>
#include <nntpchan/exec_frontend.hpp>
#include <nntpchan/sanitize.hpp>
int main(int, char *[])
{