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/libnntpchan/staticfile_frontend.cpp

128 lines
3.7 KiB
C++
Raw Normal View History

2017-10-11 18:48:27 +05:00
#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 <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)
{
}
void StaticFileFrontend::ProcessNewMessage(const fs::path & fpath)
{
std::clog << "process message " << fpath << std::endl;
auto file = OpenFile(fpath, eRead);
if(file)
{
// read header
RawHeader header;
if(!ReadHeader(file, header))
{
std::clog << "failed to read mime header" << std::endl;
return;
}
// read body
2017-10-11 18:48:27 +05:00
std::map<std::string, std::any> thread_args;
auto findMsgidFunc = [](const std::pair<std::string, std::string> & item) -> bool {
auto lower = ToLower(item.first);
return (lower == "message-id") || (lower == "messageid");
};
auto msgid = std::find_if(header.begin(), header.end(), findMsgidFunc);
if(!IsValidMessageID(msgid->second))
{
2017-10-11 18:48:27 +05:00
std::clog << "invalid message-id: " << msgid->second << std::endl;
return;
}
2017-10-11 18:48:27 +05:00
std::string msgid_hash = sha1_hex(msgid->second);
2017-10-11 18:48:27 +05:00
fs::path threadFilePath = m_OutDir / fs::path("thread-" + msgid_hash + ".html");
if(m_TemplateEngine)
{
FileHandle_ptr out = OpenFile(threadFilePath, eWrite);
if(!m_TemplateEngine->WriteTemplate("thread.mustache", thread_args, out))
{
std::clog << "failed to write " << threadFilePath << std::endl;
return;
}
2017-10-11 18:48:27 +05:00
}
std::set<std::string> newsgroups_list;
auto findNewsgroupsFunc = [](const std::pair<std::string, std::string> & item) -> bool
{
return ToLower(item.first) == "newsgroups";
};
2017-10-11 18:48:27 +05:00
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);
}
for(const auto & name : newsgroups_list)
{
auto board = GetThreadsPaginated(name, 10, m_Pages);
uint32_t pageno = 0;
for(Threads_t threads : board)
{
2017-10-11 18:48:27 +05:00
std::map<std::string, std::any> board_args;
board_args["group"] = std::make_any<std::string>(name);
board_args["pageno"] = std::make_any<uint32_t>(pageno);
board_args["threads"] = std::make_any<Threads_t>(threads);
fs::path boardPageFilename(newsgroup + "-" + std::to_string(pageno) + ".html");
if(m_TemplateEngine)
{
2017-10-11 18:48:27 +05:00
FileHandle_ptr out = OpenFile(m_OutDir / boardPageFilename, eWrite);
m_TemplateEngine->WriteTemplate("board.mustache", board_args, out);
}
2017-10-11 18:48:27 +05:00
++pageno;
}
}
}
}
2017-10-11 18:48:27 +05:00
bool StaticFileFrontend::AcceptsNewsgroup(const std::string & newsgroup)
{
return IsValidNewsgroup(newsgroup);
}
bool StaticFileFrontend::AcceptsMessage(const std::string & msgid)
{
return IsValidMessageID(msgid);
}
StaticFileFrontend::BoardPage_t StaticFileFrontend::GetThreadsPaginated(const std::string & group, uint32_t perpage, uint32_t pages)
{
return {};
}
}