Archived
1
0

clang format

This commit is contained in:
Jeff Becker 2018-05-06 08:10:20 -04:00
parent c503cddd85
commit 242e996ded
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
9 changed files with 144 additions and 163 deletions

View File

@ -5,15 +5,13 @@
using namespace mstch; 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( std::string mstch::render(const std::string &tmplt, const node &root,
const std::string& tmplt, const std::map<std::string, std::string> &partials)
const node& root,
const std::map<std::string,std::string>& partials)
{ {
std::map<std::string, template_type> partial_templates; std::map<std::string, template_type> partial_templates;
for (auto& partial: partials) for (auto &partial : partials)
partial_templates.insert({partial.first, {partial.second}}); partial_templates.insert({partial.first, {partial.second}});
return render_context(root, partial_templates).render(tmplt); return render_context(root, partial_templates).render(tmplt);

View File

@ -16,12 +16,12 @@ const char *GetBase32SubstitutionTable() { return T32; }
static void iT64Build(void); static void iT64Build(void);
/* /*
* *
* BASE64 Substitution Table * BASE64 Substitution Table
* ------------------------- * -------------------------
* *
* Direct 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', 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', '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; } const char *GetBase64SubstitutionTable() { return T64; }
/* /*
* Reverse Substitution Table (built in run time) * Reverse Substitution Table (built in run time)
*/ */
static char iT64[256]; static char iT64[256];
static int isFirstTime = 1; static int isFirstTime = 1;
/* /*
* Padding * Padding
*/ */
static char P64 = '='; static char P64 = '=';
/* /*
* *
* ByteStreamToBase64 * ByteStreamToBase64
* ------------------ * ------------------
* *
* Converts binary encoded data to BASE64 format. * Converts binary encoded data to BASE64 format.
* *
*/ */
size_t /* Number of bytes in the encoded buffer */ 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 */ size_t InCount, /* Number of bytes in the input buffer */
char *OutBuffer, /* output buffer */ char *OutBuffer, /* output buffer */
size_t len /* length of output buffer */ size_t len /* length of output buffer */
) )
{ {
unsigned char *ps; unsigned char *ps;
@ -125,21 +125,21 @@ size_t /* Number of bytes in the encode
} }
/* /*
* *
* Base64ToByteStream * Base64ToByteStream
* ------------------ * ------------------
* *
* Converts BASE64 encoded data to binary format. If input buffer is * Converts BASE64 encoded data to binary format. If input buffer is
* not properly padded, buffer of negative length is returned * not properly padded, buffer of negative length is returned
* *
*/ */
size_t /* Number of output bytes */ 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 */ size_t InCount, /* Number of input bytes */
uint8_t *OutBuffer, /* output buffer length */ uint8_t *OutBuffer, /* output buffer length */
size_t len /* length of output buffer */ size_t len /* length of output buffer */
) )
{ {
unsigned char *ps; unsigned char *ps;
unsigned char *pd; unsigned char *pd;
@ -212,13 +212,13 @@ size_t Base32EncodingBufferSize(const size_t input_size)
return 8 * d.quot; return 8 * d.quot;
} }
/* /*
* *
* iT64 * iT64
* ---- * ----
* Reverse table builder. P64 character is replaced with 0 * Reverse table builder. P64 character is replaced with 0
* *
* *
*/ */
static void iT64Build() static void iT64Build()
{ {

View File

@ -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 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)
{
std::string Blake2B_base32(const std::string & str) crypto_generichash(h.data(), h.size(), d, l, nullptr, 0);
{ }
Blake2BDigest d;
Blake2B(reinterpret_cast<const uint8_t*>(str.c_str()), str.size(), d);
return B32Encode(d.data(), d.size());
}
std::string Blake2B_base32(const std::string &str)
{
Blake2BDigest 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); } Crypto::Crypto() { assert(sodium_init() == 0); }

View File

@ -17,57 +17,53 @@ typedef nntpchan::ev::KqueueLoop LoopImpl;
namespace nntpchan namespace nntpchan
{ {
namespace ev namespace ev
{
bool ev::Loop::BindTCP(const sockaddr *addr, ev::io *handler)
{
assert(handler->acceptable());
socklen_t slen;
switch (addr->sa_family)
{ {
bool ev::Loop::BindTCP(const sockaddr * addr, ev::io * handler) case AF_INET:
{ slen = sizeof(sockaddr_in);
assert(handler->acceptable()); break;
socklen_t slen; case AF_INET6:
switch(addr->sa_family) slen = sizeof(sockaddr_in6);
{ break;
case AF_INET: case AF_UNIX:
slen = sizeof(sockaddr_in); slen = sizeof(sockaddr_un);
break; break;
case AF_INET6: default:
slen = sizeof(sockaddr_in6); return false;
break; }
case AF_UNIX: int fd = socket(addr->sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0);
slen = sizeof(sockaddr_un); if (fd == -1)
break; {
default: return false;
return false;
}
int fd = socket(addr->sa_family, SOCK_STREAM | SOCK_NONBLOCK, 0);
if(fd == -1)
{
return false;
}
if(bind(fd, addr, slen) == -1)
{
::close(fd);
return false;
}
if (listen(fd, 5) == -1)
{
::close(fd);
return false;
}
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;
}
} }
if (bind(fd, addr, slen) == -1)
ev::Loop * NewMainLoop()
{ {
return new LoopImpl; ::close(fd);
return false;
} }
if (listen(fd, 5) == -1)
{
::close(fd);
return false;
}
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; }
} }

View File

@ -13,16 +13,15 @@ void LineReader::Data(const char *data, ssize_t l)
m_leftover = ""; m_leftover = "";
m_line << std::string(data, l); 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()); line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
HandleLine(line); HandleLine(line);
} }
if(m_line) if (m_line)
m_leftover = m_line.str(); m_leftover = m_line.str();
m_line.clear(); m_line.clear();
} }
bool LineReader::ShouldClose() { return m_close; } bool LineReader::ShouldClose() { return m_close; }
} }

View File

@ -10,8 +10,8 @@
namespace nntpchan namespace nntpchan
{ {
NNTPServerHandler::NNTPServerHandler(fs::path storage) NNTPServerHandler::NNTPServerHandler(fs::path storage)
: LineReader(1024), m_article(nullptr), m_auth(nullptr), m_store(storage), : LineReader(1024), m_article(nullptr), m_auth(nullptr), m_store(storage), m_authed(false),
m_authed(false), m_state(eStateReadCommand) m_state(eStateReadCommand)
{ {
} }
@ -52,7 +52,7 @@ void NNTPServerHandler::OnData(const char *data, ssize_t l)
if (m_state == eStateStoreArticle) if (m_state == eStateStoreArticle)
{ {
std::cerr << "storing " << l << " bytes" << std::endl; std::cerr << "storing " << l << " bytes" << std::endl;
if(strncmp(data, ".\r\n", l) == 0) if (strncmp(data, ".\r\n", l) == 0)
{ {
ArticleObtained(); ArticleObtained();
return; return;
@ -68,7 +68,7 @@ void NNTPServerHandler::OnData(const char *data, ssize_t l)
} }
ArticleObtained(); ArticleObtained();
diff += 5; diff += 5;
if(l - diff) if (l - diff)
Data(end + 5, l - diff); Data(end + 5, l - diff);
return; return;
} }

View File

@ -11,7 +11,7 @@
namespace nntpchan namespace nntpchan
{ {
NNTPServer::NNTPServer(ev::Loop * loop) : Server(loop), m_frontend(nullptr) {} NNTPServer::NNTPServer(ev::Loop *loop) : Server(loop), m_frontend(nullptr) {}
NNTPServer::~NNTPServer() {} 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 NNTPServer::OnAcceptError(int status) { std::cerr << "nntpserver::accept() " << strerror(status) << std::endl; }
void NNTPServerConn::Greet() void NNTPServerConn::Greet()
{ {
IConnHandler *handler = GetHandler(); IConnHandler *handler = GetHandler();

View File

@ -7,14 +7,12 @@
namespace nntpchan 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() void Server::close()
{ {
auto itr = m_conns.begin(); auto itr = m_conns.begin();
while(itr != m_conns.end()) while (itr != m_conns.end())
{ {
itr = m_conns.erase(itr); itr = m_conns.erase(itr);
} }
@ -30,12 +28,12 @@ bool Server::Bind(const std::string &addr)
void Server::OnAccept(int f) void Server::OnAccept(int f)
{ {
IServerConn *conn = CreateConn(f); IServerConn *conn = CreateConn(f);
if(!m_Loop->SetNonBlocking(conn)) if (!m_Loop->SetNonBlocking(conn))
{ {
conn->close(); conn->close();
delete conn; delete conn;
} }
else if(m_Loop->TrackConn(conn)) else if (m_Loop->TrackConn(conn))
{ {
m_conns.push_back(conn); m_conns.push_back(conn);
conn->Greet(); conn->Greet();
@ -51,7 +49,8 @@ void Server::OnAccept(int f)
int Server::accept() int Server::accept()
{ {
int res = ::accept(fd, nullptr, nullptr); int res = ::accept(fd, nullptr, nullptr);
if(res == -1) return res; if (res == -1)
return res;
OnAccept(res); OnAccept(res);
return res; return res;
} }
@ -69,7 +68,7 @@ void Server::RemoveConn(IServerConn *conn)
m_Loop->UntrackConn(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; } bool IConnHandler::HasNextLine() { return m_sendlines.size() > 0; }
@ -80,39 +79,34 @@ std::string IConnHandler::GetNextLine()
return line; 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; } 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); ssize_t readsz = ::read(fd, buf, sz);
if(readsz > 0) if (readsz > 0)
{ {
m_handler->OnData(buf, readsz); m_handler->OnData(buf, readsz);
} }
return readsz; return readsz;
} }
bool IServerConn::keepalive() bool IServerConn::keepalive() { return !m_handler->ShouldClose(); }
{
return !m_handler->ShouldClose();
}
int IServerConn::write(size_t avail) int IServerConn::write(size_t avail)
{ {
auto leftovers = m_writeLeftover.size(); auto leftovers = m_writeLeftover.size();
ssize_t written; ssize_t written;
if(leftovers) if (leftovers)
{ {
if(leftovers > avail) if (leftovers > avail)
{ {
leftovers = avail; leftovers = avail;
} }
written = ::write(fd, m_writeLeftover.c_str(), leftovers); written = ::write(fd, m_writeLeftover.c_str(), leftovers);
if(written > 0) if (written > 0)
{ {
avail -= written; avail -= written;
m_writeLeftover = m_writeLeftover.substr(written); m_writeLeftover = m_writeLeftover.substr(written);
@ -125,13 +119,13 @@ int IServerConn::write(size_t avail)
} }
do do
{ {
if(!m_handler->HasNextLine()) if (!m_handler->HasNextLine())
{ {
return written; return written;
} }
auto line = m_handler->GetNextLine(); auto line = m_handler->GetNextLine();
int wrote; int wrote;
if(line.size() <= avail) if (line.size() <= avail)
{ {
wrote = ::write(fd, line.c_str(), line.size()); wrote = ::write(fd, line.c_str(), line.size());
} }
@ -140,7 +134,7 @@ int IServerConn::write(size_t avail)
auto subline = line.substr(0, avail); auto subline = line.substr(0, avail);
wrote = ::write(fd, subline.c_str(), subline.size()); wrote = ::write(fd, subline.c_str(), subline.size());
} }
if(wrote > 0) if (wrote > 0)
{ {
written += wrote; written += wrote;
m_writeLeftover = line.substr(wrote); m_writeLeftover = line.substr(wrote);
@ -150,8 +144,7 @@ int IServerConn::write(size_t avail)
m_writeLeftover = line; m_writeLeftover = line;
return -1; return -1;
} }
} } while (avail > 0);
while(avail > 0);
return written; return written;
} }

View File

@ -7,8 +7,8 @@
namespace nntpchan namespace nntpchan
{ {
const fs::path posts_skiplist_dir = "posts"; const fs::path posts_skiplist_dir = "posts";
const fs::path threads_skiplist_dir = "threads"; const fs::path threads_skiplist_dir = "threads";
ArticleStorage::ArticleStorage(const fs::path &fpath) { SetPath(fpath); } ArticleStorage::ArticleStorage(const fs::path &fpath) { SetPath(fpath); }
@ -23,15 +23,13 @@ void ArticleStorage::SetPath(const fs::path &fpath)
errno = 0; errno = 0;
} }
bool ArticleStorage::init_skiplist(const std::string &subdir) const bool ArticleStorage::init_skiplist(const std::string &subdir) const
{ {
fs::path skiplist = skiplist_root(subdir); fs::path skiplist = skiplist_root(subdir);
const auto subdirs = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', const auto subdirs = {
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '2', '3', '4', '5', '6', '7',
'y', 'z', '2', '3', '4', '5', '6', '7', };
};
for (const auto &s : subdirs) for (const auto &s : subdirs)
fs::create_directories(skiplist / std::string(&s, 1)); fs::create_directories(skiplist / std::string(&s, 1));
return true; return true;
@ -85,13 +83,9 @@ void ArticleStorage::EnsureSymlinks(const std::string &msgid) const
skiplist_dir(posts_skiplist_dir, msgidhash); skiplist_dir(posts_skiplist_dir, msgidhash);
} }
fs::path ArticleStorage::skiplist_root(const std::string &name) const { return basedir / name; }
fs::path ArticleStorage::skiplist_root(const std::string & name ) const fs::path ArticleStorage::skiplist_dir(const fs::path &root, const std::string &name) const
{ {
return basedir / name; return root / name.substr(0, 1);
} }
fs::path ArticleStorage::skiplist_dir(const fs::path & root, const std::string & name ) const
{
return root / name.substr(0, 1) ;
}
} }