clang format
This commit is contained in:
parent
c503cddd85
commit
242e996ded
@ -5,15 +5,13 @@
|
||||
|
||||
using namespace mstch;
|
||||
|
||||
std::function<std::string(const std::string&)> mstch::config::escape;
|
||||
std::function<std::string(const std::string &)> mstch::config::escape;
|
||||
|
||||
std::string mstch::render(
|
||||
const std::string& tmplt,
|
||||
const node& root,
|
||||
const std::map<std::string,std::string>& partials)
|
||||
std::string mstch::render(const std::string &tmplt, const node &root,
|
||||
const std::map<std::string, std::string> &partials)
|
||||
{
|
||||
std::map<std::string, template_type> partial_templates;
|
||||
for (auto& partial: partials)
|
||||
for (auto &partial : partials)
|
||||
partial_templates.insert({partial.first, {partial.second}});
|
||||
|
||||
return render_context(root, partial_templates).render(tmplt);
|
||||
|
@ -16,12 +16,12 @@ const char *GetBase32SubstitutionTable() { return T32; }
|
||||
static void iT64Build(void);
|
||||
|
||||
/*
|
||||
*
|
||||
* BASE64 Substitution Table
|
||||
* -------------------------
|
||||
*
|
||||
* Direct Substitution Table
|
||||
*/
|
||||
*
|
||||
* BASE64 Substitution Table
|
||||
* -------------------------
|
||||
*
|
||||
* 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',
|
||||
@ -31,33 +31,33 @@ static const char T64[64] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', '
|
||||
const char *GetBase64SubstitutionTable() { return T64; }
|
||||
|
||||
/*
|
||||
* Reverse Substitution Table (built in run time)
|
||||
*/
|
||||
* Reverse Substitution Table (built in run time)
|
||||
*/
|
||||
|
||||
static char iT64[256];
|
||||
static int isFirstTime = 1;
|
||||
|
||||
/*
|
||||
* Padding
|
||||
*/
|
||||
* Padding
|
||||
*/
|
||||
|
||||
static char P64 = '=';
|
||||
|
||||
/*
|
||||
*
|
||||
* ByteStreamToBase64
|
||||
* ------------------
|
||||
*
|
||||
* Converts binary encoded data to BASE64 format.
|
||||
*
|
||||
*/
|
||||
*
|
||||
* ByteStreamToBase64
|
||||
* ------------------
|
||||
*
|
||||
* Converts binary encoded data to BASE64 format.
|
||||
*
|
||||
*/
|
||||
|
||||
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 */
|
||||
)
|
||||
)
|
||||
|
||||
{
|
||||
unsigned char *ps;
|
||||
@ -125,21 +125,21 @@ size_t /* Number of bytes in the encode
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* Base64ToByteStream
|
||||
* ------------------
|
||||
*
|
||||
* Converts BASE64 encoded data to binary format. If input buffer is
|
||||
* not properly padded, buffer of negative length is returned
|
||||
*
|
||||
*/
|
||||
*
|
||||
* Base64ToByteStream
|
||||
* ------------------
|
||||
*
|
||||
* Converts BASE64 encoded data to binary format. If input buffer is
|
||||
* not properly padded, buffer of negative length is returned
|
||||
*
|
||||
*/
|
||||
|
||||
size_t /* Number of output bytes */
|
||||
Base64ToByteStream(const char *InBuffer, /* BASE64 encoded buffer */
|
||||
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 */
|
||||
)
|
||||
)
|
||||
{
|
||||
unsigned char *ps;
|
||||
unsigned char *pd;
|
||||
@ -212,13 +212,13 @@ size_t Base32EncodingBufferSize(const size_t input_size)
|
||||
return 8 * d.quot;
|
||||
}
|
||||
/*
|
||||
*
|
||||
* iT64
|
||||
* ----
|
||||
* Reverse table builder. P64 character is replaced with 0
|
||||
*
|
||||
*
|
||||
*/
|
||||
*
|
||||
* iT64
|
||||
* ----
|
||||
* Reverse table builder. P64 character is replaced with 0
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
static void iT64Build()
|
||||
{
|
||||
|
@ -7,15 +7,17 @@ namespace nntpchan
|
||||
{
|
||||
void SHA512(const uint8_t *d, const std::size_t l, SHA512Digest &h) { crypto_hash(h.data(), d, l); }
|
||||
|
||||
void Blake2B(const uint8_t *d, std::size_t l, Blake2BDigest & h) { crypto_generichash(h.data(), h.size(), d, l, nullptr, 0); }
|
||||
void Blake2B(const uint8_t *d, std::size_t l, Blake2BDigest &h)
|
||||
{
|
||||
crypto_generichash(h.data(), h.size(), d, l, nullptr, 0);
|
||||
}
|
||||
|
||||
std::string Blake2B_base32(const std::string & str)
|
||||
{
|
||||
std::string Blake2B_base32(const std::string &str)
|
||||
{
|
||||
Blake2BDigest d;
|
||||
Blake2B(reinterpret_cast<const uint8_t*>(str.c_str()), str.size(), d);
|
||||
Blake2B(reinterpret_cast<const uint8_t *>(str.c_str()), str.size(), d);
|
||||
return B32Encode(d.data(), d.size());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Crypto::Crypto() { assert(sodium_init() == 0); }
|
||||
|
||||
|
@ -17,13 +17,13 @@ typedef nntpchan::ev::KqueueLoop LoopImpl;
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
namespace ev
|
||||
{
|
||||
bool ev::Loop::BindTCP(const sockaddr * addr, ev::io * handler)
|
||||
{
|
||||
namespace ev
|
||||
{
|
||||
bool ev::Loop::BindTCP(const sockaddr *addr, ev::io *handler)
|
||||
{
|
||||
assert(handler->acceptable());
|
||||
socklen_t slen;
|
||||
switch(addr->sa_family)
|
||||
switch (addr->sa_family)
|
||||
{
|
||||
case AF_INET:
|
||||
slen = sizeof(sockaddr_in);
|
||||
@ -38,12 +38,12 @@ namespace nntpchan
|
||||
return false;
|
||||
}
|
||||
int fd = socket(addr->sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0);
|
||||
if(fd == -1)
|
||||
if (fd == -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if(bind(fd, addr, slen) == -1)
|
||||
if (bind(fd, addr, slen) == -1)
|
||||
{
|
||||
::close(fd);
|
||||
return false;
|
||||
@ -57,17 +57,13 @@ namespace nntpchan
|
||||
|
||||
handler->fd = fd;
|
||||
return TrackConn(handler);
|
||||
}
|
||||
|
||||
bool Loop::SetNonBlocking(ev::io * handler)
|
||||
{
|
||||
return fcntl(handler->fd, F_SETFL, fcntl(handler->fd, F_GETFL, 0) | O_NONBLOCK) != -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ev::Loop * NewMainLoop()
|
||||
{
|
||||
return new LoopImpl;
|
||||
}
|
||||
}
|
||||
|
||||
bool Loop::SetNonBlocking(ev::io *handler)
|
||||
{
|
||||
return fcntl(handler->fd, F_SETFL, fcntl(handler->fd, F_GETFL, 0) | O_NONBLOCK) != -1;
|
||||
}
|
||||
}
|
||||
|
||||
ev::Loop *NewMainLoop() { return new LoopImpl; }
|
||||
}
|
@ -13,16 +13,15 @@ void LineReader::Data(const char *data, ssize_t l)
|
||||
m_leftover = "";
|
||||
m_line << std::string(data, l);
|
||||
|
||||
for(std::string line; std::getline(m_line, line); )
|
||||
for (std::string line; std::getline(m_line, line);)
|
||||
{
|
||||
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
|
||||
HandleLine(line);
|
||||
}
|
||||
if(m_line)
|
||||
if (m_line)
|
||||
m_leftover = m_line.str();
|
||||
m_line.clear();
|
||||
}
|
||||
|
||||
|
||||
bool LineReader::ShouldClose() { return m_close; }
|
||||
}
|
||||
|
@ -10,8 +10,8 @@
|
||||
namespace nntpchan
|
||||
{
|
||||
NNTPServerHandler::NNTPServerHandler(fs::path storage)
|
||||
: LineReader(1024), m_article(nullptr), m_auth(nullptr), m_store(storage),
|
||||
m_authed(false), m_state(eStateReadCommand)
|
||||
: LineReader(1024), m_article(nullptr), m_auth(nullptr), m_store(storage), m_authed(false),
|
||||
m_state(eStateReadCommand)
|
||||
{
|
||||
}
|
||||
|
||||
@ -52,7 +52,7 @@ void NNTPServerHandler::OnData(const char *data, ssize_t l)
|
||||
if (m_state == eStateStoreArticle)
|
||||
{
|
||||
std::cerr << "storing " << l << " bytes" << std::endl;
|
||||
if(strncmp(data, ".\r\n", l) == 0)
|
||||
if (strncmp(data, ".\r\n", l) == 0)
|
||||
{
|
||||
ArticleObtained();
|
||||
return;
|
||||
@ -68,7 +68,7 @@ void NNTPServerHandler::OnData(const char *data, ssize_t l)
|
||||
}
|
||||
ArticleObtained();
|
||||
diff += 5;
|
||||
if(l - diff)
|
||||
if (l - diff)
|
||||
Data(end + 5, l - diff);
|
||||
return;
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
namespace nntpchan
|
||||
{
|
||||
|
||||
NNTPServer::NNTPServer(ev::Loop * loop) : Server(loop), m_frontend(nullptr) {}
|
||||
NNTPServer::NNTPServer(ev::Loop *loop) : Server(loop), m_frontend(nullptr) {}
|
||||
|
||||
NNTPServer::~NNTPServer() {}
|
||||
|
||||
@ -43,7 +43,6 @@ std::string NNTPServer::InstanceName() const { return m_servername; }
|
||||
|
||||
void NNTPServer::OnAcceptError(int status) { std::cerr << "nntpserver::accept() " << strerror(status) << std::endl; }
|
||||
|
||||
|
||||
void NNTPServerConn::Greet()
|
||||
{
|
||||
IConnHandler *handler = GetHandler();
|
||||
|
@ -7,14 +7,12 @@
|
||||
namespace nntpchan
|
||||
{
|
||||
|
||||
Server::Server(ev::Loop * loop) : ev::io(-1), m_Loop(loop)
|
||||
{
|
||||
}
|
||||
Server::Server(ev::Loop *loop) : ev::io(-1), m_Loop(loop) {}
|
||||
|
||||
void Server::close()
|
||||
{
|
||||
auto itr = m_conns.begin();
|
||||
while(itr != m_conns.end())
|
||||
while (itr != m_conns.end())
|
||||
{
|
||||
itr = m_conns.erase(itr);
|
||||
}
|
||||
@ -30,12 +28,12 @@ bool Server::Bind(const std::string &addr)
|
||||
void Server::OnAccept(int f)
|
||||
{
|
||||
IServerConn *conn = CreateConn(f);
|
||||
if(!m_Loop->SetNonBlocking(conn))
|
||||
if (!m_Loop->SetNonBlocking(conn))
|
||||
{
|
||||
conn->close();
|
||||
delete conn;
|
||||
}
|
||||
else if(m_Loop->TrackConn(conn))
|
||||
else if (m_Loop->TrackConn(conn))
|
||||
{
|
||||
m_conns.push_back(conn);
|
||||
conn->Greet();
|
||||
@ -51,7 +49,8 @@ void Server::OnAccept(int f)
|
||||
int Server::accept()
|
||||
{
|
||||
int res = ::accept(fd, nullptr, nullptr);
|
||||
if(res == -1) return res;
|
||||
if (res == -1)
|
||||
return res;
|
||||
OnAccept(res);
|
||||
return res;
|
||||
}
|
||||
@ -69,7 +68,7 @@ void Server::RemoveConn(IServerConn *conn)
|
||||
m_Loop->UntrackConn(conn);
|
||||
}
|
||||
|
||||
void IConnHandler::QueueLine(const std::string &line) { m_sendlines.push_back(line+"\r\n"); }
|
||||
void IConnHandler::QueueLine(const std::string &line) { m_sendlines.push_back(line + "\r\n"); }
|
||||
|
||||
bool IConnHandler::HasNextLine() { return m_sendlines.size() > 0; }
|
||||
|
||||
@ -80,39 +79,34 @@ std::string IConnHandler::GetNextLine()
|
||||
return line;
|
||||
}
|
||||
|
||||
IServerConn::IServerConn(int fd, Server *parent, IConnHandler *h) : ev::io(fd), m_parent(parent), m_handler(h)
|
||||
{
|
||||
}
|
||||
IServerConn::IServerConn(int fd, Server *parent, IConnHandler *h) : ev::io(fd), m_parent(parent), m_handler(h) {}
|
||||
|
||||
IServerConn::~IServerConn() { delete m_handler; }
|
||||
|
||||
int IServerConn::read(char * buf, size_t sz)
|
||||
int IServerConn::read(char *buf, size_t sz)
|
||||
{
|
||||
ssize_t readsz = ::read(fd, buf, sz);
|
||||
if(readsz > 0)
|
||||
if (readsz > 0)
|
||||
{
|
||||
m_handler->OnData(buf, readsz);
|
||||
}
|
||||
return readsz;
|
||||
}
|
||||
|
||||
bool IServerConn::keepalive()
|
||||
{
|
||||
return !m_handler->ShouldClose();
|
||||
}
|
||||
bool IServerConn::keepalive() { return !m_handler->ShouldClose(); }
|
||||
|
||||
int IServerConn::write(size_t avail)
|
||||
{
|
||||
auto leftovers = m_writeLeftover.size();
|
||||
ssize_t written;
|
||||
if(leftovers)
|
||||
if (leftovers)
|
||||
{
|
||||
if(leftovers > avail)
|
||||
if (leftovers > avail)
|
||||
{
|
||||
leftovers = avail;
|
||||
}
|
||||
written = ::write(fd, m_writeLeftover.c_str(), leftovers);
|
||||
if(written > 0)
|
||||
if (written > 0)
|
||||
{
|
||||
avail -= written;
|
||||
m_writeLeftover = m_writeLeftover.substr(written);
|
||||
@ -125,13 +119,13 @@ int IServerConn::write(size_t avail)
|
||||
}
|
||||
do
|
||||
{
|
||||
if(!m_handler->HasNextLine())
|
||||
if (!m_handler->HasNextLine())
|
||||
{
|
||||
return written;
|
||||
}
|
||||
auto line = m_handler->GetNextLine();
|
||||
int wrote;
|
||||
if(line.size() <= avail)
|
||||
if (line.size() <= avail)
|
||||
{
|
||||
wrote = ::write(fd, line.c_str(), line.size());
|
||||
}
|
||||
@ -140,7 +134,7 @@ int IServerConn::write(size_t avail)
|
||||
auto subline = line.substr(0, avail);
|
||||
wrote = ::write(fd, subline.c_str(), subline.size());
|
||||
}
|
||||
if(wrote > 0)
|
||||
if (wrote > 0)
|
||||
{
|
||||
written += wrote;
|
||||
m_writeLeftover = line.substr(wrote);
|
||||
@ -150,8 +144,7 @@ int IServerConn::write(size_t avail)
|
||||
m_writeLeftover = line;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
while(avail > 0);
|
||||
} while (avail > 0);
|
||||
return written;
|
||||
}
|
||||
|
||||
|
@ -7,8 +7,8 @@
|
||||
namespace nntpchan
|
||||
{
|
||||
|
||||
const fs::path posts_skiplist_dir = "posts";
|
||||
const fs::path threads_skiplist_dir = "threads";
|
||||
const fs::path posts_skiplist_dir = "posts";
|
||||
const fs::path threads_skiplist_dir = "threads";
|
||||
|
||||
ArticleStorage::ArticleStorage(const fs::path &fpath) { SetPath(fpath); }
|
||||
|
||||
@ -23,15 +23,13 @@ void ArticleStorage::SetPath(const fs::path &fpath)
|
||||
errno = 0;
|
||||
}
|
||||
|
||||
|
||||
bool ArticleStorage::init_skiplist(const std::string &subdir) const
|
||||
{
|
||||
fs::path skiplist = skiplist_root(subdir);
|
||||
const auto subdirs = { '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', '2', '3', '4', '5', '6', '7',
|
||||
};
|
||||
const auto subdirs = {
|
||||
'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', '2', '3', '4', '5', '6', '7',
|
||||
};
|
||||
for (const auto &s : subdirs)
|
||||
fs::create_directories(skiplist / std::string(&s, 1));
|
||||
return true;
|
||||
@ -85,13 +83,9 @@ void ArticleStorage::EnsureSymlinks(const std::string &msgid) const
|
||||
skiplist_dir(posts_skiplist_dir, msgidhash);
|
||||
}
|
||||
|
||||
|
||||
fs::path ArticleStorage::skiplist_root(const std::string & name ) const
|
||||
{
|
||||
return basedir / name;
|
||||
}
|
||||
fs::path ArticleStorage::skiplist_dir(const fs::path & root, const std::string & name ) const
|
||||
{
|
||||
return root / name.substr(0, 1) ;
|
||||
}
|
||||
fs::path ArticleStorage::skiplist_root(const std::string &name) const { return basedir / name; }
|
||||
fs::path ArticleStorage::skiplist_dir(const fs::path &root, const std::string &name) const
|
||||
{
|
||||
return root / name.substr(0, 1);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user