Archived
1
0

nntpchan-daeom code

This commit is contained in:
Jeff Becker
2017-10-13 07:58:41 -04:00
parent c583a03f81
commit e9c88ffd28
14 changed files with 328 additions and 84 deletions

View File

@@ -0,0 +1,21 @@
#ifndef NNTPCHAN_MESSAGE_HPP
#define NNTPCHAN_MESSAGE_HPP
#include <memory>
#include <nntpchan/model.hpp>
namespace nntpchan
{
struct MessageDB
{
using BoardPage = nntpchan::model::BoardPage;
using Thread = nntpchan::model::Thread;
virtual bool LoadBoardPage(BoardPage & board, const std::string & newsgroup, uint32_t perpage, uint32_t page) const = 0;
virtual bool FindThreadByHash(const std::string & hashhex, std::string & msgid) const = 0;
virtual bool LoadThread(Thread & thread, const std::string & rootmsgid) const = 0;
};
typedef std::unique_ptr<MessageDB> MessageDB_ptr;
}
#endif

View File

@@ -1,17 +1,20 @@
#ifndef NNTPCHAN_MODEL_HPP
#define NNTPCHAN_MODEL_HPP
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#include <variant>
#include <nntpchan/sanitize.hpp>
namespace nntpchan
{
namespace model
{
// MIME Header
typedef std::map<std::string, std::set<std::string> > PostHeader;
typedef std::map<std::string, std::vector<std::string> > PostHeader;
// text post contents
typedef std::string PostBody;
// single file attachment, (orig_filename, hexdigest, thumb_filename)
@@ -22,37 +25,50 @@ namespace nntpchan
typedef std::tuple<PostHeader, PostBody, Attachments> Post;
// a thread (many posts in post order)
typedef std::vector<Post> Thread;
static inline std::string & GetFilename(PostAttachment & att)
// 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 std::string & GetHexDigest(PostAttachment & att)
static inline const std::string & GetHexDigest(const PostAttachment & att)
{
return std::get<1>(att);
}
static inline std::string & GetThumbnail(PostAttachment & att)
static inline const std::string & GetThumbnail(const PostAttachment & att)
{
return std::get<2>(att);
}
static inline PostHeader & GetHeader(Post & post)
static inline const PostHeader & GetHeader(const Post & post)
{
return std::get<0>(post);
}
static inline PostBody & GetBody(Post & post)
static inline const PostBody & GetBody(const Post & post)
{
return std::get<1>(post);
}
static inline Attachments & GetAttachments(Post & 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)
{
std::string ival = ToLower(val);
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
return itr->second[0];
}
using Model = std::variant<Thread, BoardPage>;
}
}

View File

@@ -11,7 +11,7 @@ namespace nntpchan
class NNTPServerHandler : public LineReader, public IConnHandler
{
public:
NNTPServerHandler(const std::string & storage);
NNTPServerHandler(const fs::path & storage);
~NNTPServerHandler();
virtual bool ShouldClose();
@@ -51,7 +51,7 @@ namespace nntpchan
std::string m_articleName;
FileHandle_ptr m_article;
CredDB_ptr m_auth;
ArticleStorage m_store;
ArticleStorage_ptr m_store;
std::string m_mode;
bool m_authed;
State m_state;

View File

@@ -6,6 +6,7 @@ namespace nntpchan
{
std::string NNTPSanitizeLine(const std::string & str);
std::string ToLower(const std::string & str);
std::string StripWhitespaces(const std::string & str);
bool IsValidMessageID(const std::string & msgid);
bool IsValidNewsgroup(const std::string & group);
}

View File

@@ -1,6 +1,7 @@
#ifndef NNTPCHAN_STATICFILE_FRONTEND_HPP
#define NNTPCHAN_STATICFILE_FRONTEND_HPP
#include "frontend.hpp"
#include "message.hpp"
#include "template_engine.hpp"
#include "model.hpp"
#include <experimental/filesystem>
@@ -23,17 +24,7 @@ namespace nntpchan
bool AcceptsMessage(const std::string & msgid);
private:
typedef nntpchan::model::Thread Thread_t;
typedef std::vector<Thread_t> Threads_t;
typedef std::vector<Threads_t> BoardPage_t;
BoardPage_t GetThreadsPaginated(const std::string & group, uint32_t perpage, uint32_t pages);
private:
MessageDB_ptr m_MessageDB;
TemplateEngine_ptr m_TemplateEngine;
fs::path m_TemplateDir;
fs::path m_OutDir;

View File

@@ -4,36 +4,49 @@
#include <experimental/filesystem>
#include <string>
#include "file_handle.hpp"
#include "message.hpp"
namespace nntpchan
{
namespace fs = std::experimental::filesystem;
class ArticleStorage
class ArticleStorage : public MessageDB
{
public:
ArticleStorage();
ArticleStorage(const fs::path & fpath);
~ArticleStorage();
void SetPath(const fs::path & fpath);
FileHandle_ptr OpenWrite(const std::string & msgid);
FileHandle_ptr OpenRead(const std::string & msgid);
FileHandle_ptr OpenWrite(const std::string & msgid) const;
FileHandle_ptr OpenRead(const std::string & msgid) const;
/**
return true if we should accept a new message give its message id
*/
bool Accept(const std::string & msgid);
bool Accept(const std::string & msgid) const;
bool LoadBoardPage(BoardPage & board, const std::string & newsgroup, uint32_t perpage, uint32_t page) const;
bool FindThreadByHash(const std::string & hashhex, std::string & msgid) const;
bool LoadThread(Thread & thread, const std::string & rootmsgid) const;
/** ensure symlinks are formed for this article by message id */
void EnsureSymlinks(const std::string & msgid) const;
private:
void SetPath(const fs::path & fpath);
fs::path MessagePath(const std::string & msgid);
fs::path MessagePath(const std::string & msgid) const;
bool init_skiplist(const std::string & subdir) const;
fs::path skiplist_root(const std::string & name) const;
fs::path basedir;
};
typedef std::unique_ptr<ArticleStorage> ArticleStorage_ptr;
}

View File

@@ -1,6 +1,7 @@
#ifndef NNTPCHAN_TEMPLATE_ENGINE_HPP
#define NNTPCHAN_TEMPLATE_ENGINE_HPP
#include "file_handle.hpp"
#include "model.hpp"
#include <any>
#include <map>
#include <memory>
@@ -11,7 +12,7 @@ namespace nntpchan
struct TemplateEngine
{
using Args_t = std::map<std::string, std::any>;
typedef std::map<std::string, std::variant<nntpchan::model::Model, std::string>> Args_t;
virtual bool WriteTemplate(const fs::path & template_fpath, const Args_t & args, const FileHandle_ptr & out) = 0;
};