* more nntpchan-daemon code
* fix keepalive
This commit is contained in:
234
contrib/backends/nntpchan-daemon/libnntpchan/base64.cpp
Normal file
234
contrib/backends/nntpchan-daemon/libnntpchan/base64.cpp
Normal file
@@ -0,0 +1,234 @@
|
||||
#include "base64.hpp"
|
||||
|
||||
|
||||
// taken from i2pd
|
||||
namespace i2p
|
||||
{
|
||||
namespace data
|
||||
{
|
||||
static void iT64Build(void);
|
||||
|
||||
/*
|
||||
*
|
||||
* 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',
|
||||
'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)
|
||||
*/
|
||||
|
||||
static char iT64[256];
|
||||
static int isFirstTime = 1;
|
||||
|
||||
/*
|
||||
* Padding
|
||||
*/
|
||||
|
||||
static char P64 = '=';
|
||||
|
||||
/*
|
||||
*
|
||||
* ByteStreamToBase64
|
||||
* ------------------
|
||||
*
|
||||
* Converts binary encoded data to BASE64 format.
|
||||
*
|
||||
*/
|
||||
static size_t /* Number of bytes in the encoded buffer */
|
||||
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;
|
||||
unsigned char * pd;
|
||||
unsigned char acc_1;
|
||||
unsigned char acc_2;
|
||||
int i;
|
||||
int n;
|
||||
int m;
|
||||
size_t outCount;
|
||||
|
||||
ps = (unsigned char *)InBuffer;
|
||||
n = InCount/3;
|
||||
m = InCount%3;
|
||||
if (!m)
|
||||
outCount = 4*n;
|
||||
else
|
||||
outCount = 4*(n+1);
|
||||
if (outCount > len) return 0;
|
||||
pd = (unsigned char *)OutBuffer;
|
||||
for ( i = 0; i<n; i++ ){
|
||||
acc_1 = *ps++;
|
||||
acc_2 = (acc_1<<4)&0x30;
|
||||
acc_1 >>= 2; /* base64 digit #1 */
|
||||
*pd++ = T64[acc_1];
|
||||
acc_1 = *ps++;
|
||||
acc_2 |= acc_1 >> 4; /* base64 digit #2 */
|
||||
*pd++ = T64[acc_2];
|
||||
acc_1 &= 0x0f;
|
||||
acc_1 <<=2;
|
||||
acc_2 = *ps++;
|
||||
acc_1 |= acc_2>>6; /* base64 digit #3 */
|
||||
*pd++ = T64[acc_1];
|
||||
acc_2 &= 0x3f; /* base64 digit #4 */
|
||||
*pd++ = T64[acc_2];
|
||||
}
|
||||
if ( m == 1 ){
|
||||
acc_1 = *ps++;
|
||||
acc_2 = (acc_1<<4)&0x3f; /* base64 digit #2 */
|
||||
acc_1 >>= 2; /* base64 digit #1 */
|
||||
*pd++ = T64[acc_1];
|
||||
*pd++ = T64[acc_2];
|
||||
*pd++ = P64;
|
||||
*pd++ = P64;
|
||||
|
||||
}
|
||||
else if ( m == 2 ){
|
||||
acc_1 = *ps++;
|
||||
acc_2 = (acc_1<<4)&0x3f;
|
||||
acc_1 >>= 2; /* base64 digit #1 */
|
||||
*pd++ = T64[acc_1];
|
||||
acc_1 = *ps++;
|
||||
acc_2 |= acc_1 >> 4; /* base64 digit #2 */
|
||||
*pd++ = T64[acc_2];
|
||||
acc_1 &= 0x0f;
|
||||
acc_1 <<=2; /* base64 digit #3 */
|
||||
*pd++ = T64[acc_1];
|
||||
*pd++ = P64;
|
||||
}
|
||||
|
||||
return outCount;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* Base64ToByteStream
|
||||
* ------------------
|
||||
*
|
||||
* Converts BASE64 encoded data to binary format. If input buffer is
|
||||
* not properly padded, buffer of negative length is returned
|
||||
*
|
||||
*/
|
||||
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 */
|
||||
)
|
||||
{
|
||||
unsigned char * ps;
|
||||
unsigned char * pd;
|
||||
unsigned char acc_1;
|
||||
unsigned char acc_2;
|
||||
int i;
|
||||
int n;
|
||||
int m;
|
||||
size_t outCount;
|
||||
|
||||
if (isFirstTime) iT64Build();
|
||||
n = InCount/4;
|
||||
m = InCount%4;
|
||||
if (InCount && !m)
|
||||
outCount = 3*n;
|
||||
else {
|
||||
outCount = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
ps = (unsigned char *)(InBuffer + InCount - 1);
|
||||
while ( *ps-- == P64 ) outCount--;
|
||||
ps = (unsigned char *)InBuffer;
|
||||
|
||||
if (outCount > len) return -1;
|
||||
pd = OutBuffer;
|
||||
auto endOfOutBuffer = OutBuffer + outCount;
|
||||
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;
|
||||
|
||||
acc_2 <<= 4;
|
||||
acc_1 = iT64[*ps++];
|
||||
acc_2 |= acc_1 >> 2;
|
||||
*pd++ = acc_2;
|
||||
if (pd >= endOfOutBuffer) break;
|
||||
|
||||
acc_2 = iT64[*ps++];
|
||||
acc_2 |= acc_1 << 6;
|
||||
*pd++ = acc_2;
|
||||
}
|
||||
|
||||
return outCount;
|
||||
}
|
||||
|
||||
static size_t Base64EncodingBufferSize (const size_t input_size)
|
||||
{
|
||||
auto d = div (input_size, 3);
|
||||
if (d.rem) d.quot++;
|
||||
return 4*d.quot;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* iT64
|
||||
* ----
|
||||
* Reverse table builder. P64 character is replaced with 0
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
static void iT64Build()
|
||||
{
|
||||
int i;
|
||||
isFirstTime = 0;
|
||||
for ( i=0; i<256; i++ ) iT64[i] = -1;
|
||||
for ( i=0; i<64; i++ ) iT64[(int)T64[i]] = i;
|
||||
iT64[(int)P64] = 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
std::string B64Encode(const uint8_t * data, const std::size_t l)
|
||||
{
|
||||
std::string out;
|
||||
out.resize(i2p::data::Base64EncodingBufferSize(l));
|
||||
i2p::data::ByteStreamToBase64(data, l, &out[0], out.size());
|
||||
return out;
|
||||
}
|
||||
|
||||
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;
|
||||
out.shrink_to_fit();
|
||||
return true;
|
||||
}
|
||||
}
|
17
contrib/backends/nntpchan-daemon/libnntpchan/base64.hpp
Normal file
17
contrib/backends/nntpchan-daemon/libnntpchan/base64.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef NNTPCHAN_BASE64_HPP
|
||||
#define NNTPCHAN_BASE64_HPP
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
/** returns base64 encoded string */
|
||||
std::string B64Encode(const uint8_t * data, const std::size_t l);
|
||||
|
||||
/** @brief returns true if decode was successful */
|
||||
bool B64Decode(const std::string & data, std::vector<uint8_t> & out);
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
21
contrib/backends/nntpchan-daemon/libnntpchan/buffer.cpp
Normal file
21
contrib/backends/nntpchan-daemon/libnntpchan/buffer.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "buffer.hpp"
|
||||
#include <cstring>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
WriteBuffer::WriteBuffer(const char * b, const size_t s)
|
||||
{
|
||||
char * buf = new char[s];
|
||||
std::memcpy(buf, b, s);
|
||||
this->b = uv_buf_init(buf, s);
|
||||
w.data = this;
|
||||
};
|
||||
|
||||
WriteBuffer::WriteBuffer(const std::string & s) : WriteBuffer(s.c_str(), s.size()) {}
|
||||
|
||||
WriteBuffer::~WriteBuffer()
|
||||
{
|
||||
delete [] b.base;
|
||||
}
|
||||
}
|
||||
|
19
contrib/backends/nntpchan-daemon/libnntpchan/buffer.hpp
Normal file
19
contrib/backends/nntpchan-daemon/libnntpchan/buffer.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef NNTPCHAN_BUFFER_HPP
|
||||
#define NNTPCHAN_BUFFER_HPP
|
||||
#include <uv.h>
|
||||
#include <string>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
struct WriteBuffer
|
||||
{
|
||||
uv_write_t w;
|
||||
uv_buf_t b;
|
||||
|
||||
WriteBuffer(const std::string & s);
|
||||
WriteBuffer(const char * b, const size_t s);
|
||||
~WriteBuffer();
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
20
contrib/backends/nntpchan-daemon/libnntpchan/crypto.cpp
Normal file
20
contrib/backends/nntpchan-daemon/libnntpchan/crypto.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include "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);
|
||||
}
|
||||
|
||||
Crypto::Crypto()
|
||||
{
|
||||
assert(sodium_init() == 0);
|
||||
}
|
||||
|
||||
Crypto::~Crypto()
|
||||
{
|
||||
}
|
||||
}
|
22
contrib/backends/nntpchan-daemon/libnntpchan/crypto.hpp
Normal file
22
contrib/backends/nntpchan-daemon/libnntpchan/crypto.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef NNTPCHAN_CRYPTO_HPP
|
||||
#define NNTPCHAN_CRYPTO_HPP
|
||||
|
||||
#include <sodium/crypto_hash.h>
|
||||
#include <array>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
typedef std::array<uint8_t, crypto_hash_BYTES> SHA512Digest;
|
||||
|
||||
void SHA512(const uint8_t * d, std::size_t l, SHA512Digest & h);
|
||||
|
||||
/** global crypto initializer */
|
||||
struct Crypto
|
||||
{
|
||||
Crypto();
|
||||
~Crypto();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif
|
44
contrib/backends/nntpchan-daemon/libnntpchan/crypto_old.hpp
Normal file
44
contrib/backends/nntpchan-daemon/libnntpchan/crypto_old.hpp
Normal file
@@ -0,0 +1,44 @@
|
||||
|
||||
|
||||
#ifndef NNTPCHAN_CRYPTO_OLD_HPP
|
||||
#define NNTPCHAN_CRYPTO_OLD_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
extern "C" {
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t state[5];
|
||||
uint32_t count[2];
|
||||
unsigned char buffer[64];
|
||||
} SHA1_CTX;
|
||||
|
||||
void SHA1Transform(
|
||||
uint32_t state[5],
|
||||
const unsigned char buffer[64]
|
||||
);
|
||||
|
||||
void SHA1Init(
|
||||
SHA1_CTX * context
|
||||
);
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
27
contrib/backends/nntpchan-daemon/libnntpchan/event.cpp
Normal file
27
contrib/backends/nntpchan-daemon/libnntpchan/event.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "event.hpp"
|
||||
#include <cassert>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
Mainloop::Mainloop()
|
||||
{
|
||||
m_loop = uv_default_loop();
|
||||
assert(uv_loop_init(m_loop) == 0);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
26
contrib/backends/nntpchan-daemon/libnntpchan/event.hpp
Normal file
26
contrib/backends/nntpchan-daemon/libnntpchan/event.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef NNTPCHAN_EVENT_HPP
|
||||
#define NNTPCHAN_EVENT_HPP
|
||||
#include <uv.h>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
class Mainloop
|
||||
{
|
||||
public:
|
||||
|
||||
Mainloop();
|
||||
~Mainloop();
|
||||
|
||||
operator uv_loop_t * () const { return m_loop; }
|
||||
|
||||
void Run(uv_run_mode mode = UV_RUN_DEFAULT);
|
||||
void Stop();
|
||||
|
||||
private:
|
||||
|
||||
uv_loop_t * m_loop;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@@ -0,0 +1,57 @@
|
||||
#include "exec_frontend.hpp"
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
ExecFrontend::ExecFrontend(const std::string & fname) :
|
||||
m_exec(fname)
|
||||
{
|
||||
}
|
||||
|
||||
ExecFrontend::~ExecFrontend() {}
|
||||
|
||||
void ExecFrontend::ProcessNewMessage(const fs::path & fpath)
|
||||
{
|
||||
Exec({"post", fpath});
|
||||
}
|
||||
|
||||
bool ExecFrontend::AcceptsNewsgroup(const std::string & newsgroup)
|
||||
{
|
||||
return Exec({"newsgroup", newsgroup}) == 0;
|
||||
}
|
||||
|
||||
bool ExecFrontend::AcceptsMessage(const std::string & msgid)
|
||||
{
|
||||
return Exec({"msgid", msgid}) == 0;
|
||||
}
|
||||
|
||||
int ExecFrontend::Exec(std::deque<std::string> args)
|
||||
{
|
||||
// set up arguments
|
||||
const char ** cargs = new char const *[args.size() +2];
|
||||
std::size_t l = 0;
|
||||
cargs[l++] = m_exec.c_str();
|
||||
while (args.size()) {
|
||||
cargs[l++] = args.front().c_str();
|
||||
args.pop_front();
|
||||
}
|
||||
cargs[l] = 0;
|
||||
int retcode = 0;
|
||||
pid_t child = fork();
|
||||
if(child) {
|
||||
waitpid(child, &retcode, 0);
|
||||
} else {
|
||||
int r = execvpe(m_exec.c_str(),(char * const *) cargs, environ);
|
||||
if ( r == -1 ) {
|
||||
std::cout << strerror(errno) << std::endl;
|
||||
exit( errno );
|
||||
} else
|
||||
exit(r);
|
||||
}
|
||||
return retcode;
|
||||
}
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
#ifndef NNTPCHAN_EXEC_FRONTEND_HPP
|
||||
#define NNTPCHAN_EXEC_FRONTEND_HPP
|
||||
#include "frontend.hpp"
|
||||
#include <deque>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
class ExecFrontend : public Frontend
|
||||
{
|
||||
public:
|
||||
|
||||
ExecFrontend(const std::string & exe);
|
||||
|
||||
~ExecFrontend();
|
||||
|
||||
void ProcessNewMessage(const fs::path & fpath);
|
||||
bool AcceptsNewsgroup(const std::string & newsgroup);
|
||||
bool AcceptsMessage(const std::string & msgid);
|
||||
|
||||
private:
|
||||
|
||||
int Exec(std::deque<std::string> args);
|
||||
|
||||
private:
|
||||
std::string m_exec;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
22
contrib/backends/nntpchan-daemon/libnntpchan/file_handle.cpp
Normal file
22
contrib/backends/nntpchan-daemon/libnntpchan/file_handle.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "file_handle.hpp"
|
||||
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
FileHandle_ptr OpenFile(const fs::path & fname, FileMode mode)
|
||||
{
|
||||
std::fstream * f = new std::fstream;
|
||||
if(mode == eRead)
|
||||
{
|
||||
f->open(fname, std::ios::in);
|
||||
}
|
||||
else if (mode == eWrite)
|
||||
{
|
||||
f->open(fname, std::ios::out);
|
||||
}
|
||||
if(f->is_open())
|
||||
return FileHandle_ptr(f);
|
||||
delete f;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
23
contrib/backends/nntpchan-daemon/libnntpchan/file_handle.hpp
Normal file
23
contrib/backends/nntpchan-daemon/libnntpchan/file_handle.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef NNTPCHAN_FILE_HANDLE_HPP
|
||||
#define NNTPCHAN_FILE_HANDLE_HPP
|
||||
|
||||
#include <memory>
|
||||
#include <fstream>
|
||||
#include <experimental/filesystem>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
typedef std::unique_ptr<std::fstream> FileHandle_ptr;
|
||||
|
||||
enum FileMode
|
||||
{
|
||||
eRead,
|
||||
eWrite
|
||||
};
|
||||
|
||||
namespace fs = std::experimental::filesystem;
|
||||
|
||||
FileHandle_ptr OpenFile(const fs::path & fname, FileMode mode);
|
||||
}
|
||||
|
||||
#endif
|
29
contrib/backends/nntpchan-daemon/libnntpchan/frontend.hpp
Normal file
29
contrib/backends/nntpchan-daemon/libnntpchan/frontend.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef NNTPCHAN_FRONTEND_HPP
|
||||
#define NNTPCHAN_FRONTEND_HPP
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <experimental/filesystem>
|
||||
namespace nntpchan
|
||||
{
|
||||
|
||||
namespace fs = std::experimental::filesystem;
|
||||
/** @brief nntpchan frontend ui interface */
|
||||
class Frontend
|
||||
{
|
||||
public:
|
||||
|
||||
/** @brief process an inbound message stored at fpath that we have accepted. */
|
||||
virtual void ProcessNewMessage(const fs::path & fpath) = 0;
|
||||
|
||||
/** @brief return true if we take posts in a newsgroup */
|
||||
virtual bool AcceptsNewsgroup(const std::string & newsgroup) = 0;
|
||||
|
||||
/** @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;
|
||||
}
|
||||
|
||||
#endif
|
6
contrib/backends/nntpchan-daemon/libnntpchan/http.hpp
Normal file
6
contrib/backends/nntpchan-daemon/libnntpchan/http.hpp
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef NNTPCHAN_HTTP_HPP
|
||||
#define NNTPCHAN_HTTP_HPP
|
||||
|
||||
|
||||
|
||||
#endif
|
@@ -0,0 +1,5 @@
|
||||
#ifndef NNTPCHAN_HTTP_CLIENT_HPP
|
||||
#define NNTPCHAN_HTTP_CLIENT_HPP
|
||||
|
||||
|
||||
#endif
|
@@ -0,0 +1,6 @@
|
||||
#ifndef NNTPCHAN_HTTP_SERVER_HPP
|
||||
#define NNTPCHAN_HTTP_SERVER_HPP
|
||||
|
||||
|
||||
|
||||
#endif
|
186
contrib/backends/nntpchan-daemon/libnntpchan/ini.hpp
Normal file
186
contrib/backends/nntpchan-daemon/libnntpchan/ini.hpp
Normal file
@@ -0,0 +1,186 @@
|
||||
/**
|
||||
* The MIT License (MIT)
|
||||
* Copyright (c) <2015> <carriez.md@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef INI_HPP
|
||||
#define INI_HPP
|
||||
|
||||
#include <cassert>
|
||||
#include <map>
|
||||
#include <list>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
namespace INI {
|
||||
|
||||
struct Level
|
||||
{
|
||||
Level() : parent(NULL), depth(0) {}
|
||||
Level(Level* p) : parent(p), depth(0) {}
|
||||
|
||||
typedef std::map<std::string, std::string> value_map_t;
|
||||
typedef std::map<std::string, Level> section_map_t;
|
||||
typedef std::list<value_map_t::const_iterator> values_t;
|
||||
typedef std::list<section_map_t::const_iterator> sections_t;
|
||||
value_map_t values;
|
||||
section_map_t sections;
|
||||
values_t ordered_values; // original order in the ini file
|
||||
sections_t ordered_sections;
|
||||
Level* parent;
|
||||
size_t depth;
|
||||
|
||||
const std::string& operator[](const std::string& name) { return values[name]; }
|
||||
Level& operator()(const std::string& name) { return sections[name]; }
|
||||
};
|
||||
|
||||
class Parser
|
||||
{
|
||||
public:
|
||||
Parser(const char* fn);
|
||||
Parser(std::istream& f) : f_(&f), ln_(0) { parse(top_); }
|
||||
Level& top() { return top_; }
|
||||
void dump(std::ostream& s) { dump(s, top(), ""); }
|
||||
|
||||
private:
|
||||
void dump(std::ostream& s, const Level& l, const std::string& sname);
|
||||
void parse(Level& l);
|
||||
void parseSLine(std::string& sname, size_t& depth);
|
||||
void err(const char* s);
|
||||
|
||||
private:
|
||||
Level top_;
|
||||
std::ifstream f0_;
|
||||
std::istream* f_;
|
||||
std::string line_;
|
||||
size_t ln_;
|
||||
};
|
||||
|
||||
inline void
|
||||
Parser::err(const char* s)
|
||||
{
|
||||
char buf[256];
|
||||
sprintf(buf, "%s on line #%ld", s, ln_);
|
||||
throw std::runtime_error(buf);
|
||||
}
|
||||
|
||||
inline std::string trim(const std::string& s)
|
||||
{
|
||||
char p[] = " \t\r\n";
|
||||
long sp = 0;
|
||||
long ep = s.length() - 1;
|
||||
for (; sp <= ep; ++sp)
|
||||
if (!strchr(p, s[sp])) break;
|
||||
for (; ep >= 0; --ep)
|
||||
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)
|
||||
{
|
||||
if (!f0_)
|
||||
throw std::runtime_error(std::string("failed to open file: ") + fn);
|
||||
|
||||
parse(top_);
|
||||
}
|
||||
|
||||
inline void
|
||||
Parser::parseSLine(std::string& sname, size_t& depth)
|
||||
{
|
||||
depth = 0;
|
||||
for (; depth < line_.length(); ++depth)
|
||||
if (line_[depth] != '[') break;
|
||||
|
||||
sname = line_.substr(depth, line_.length() - 2*depth);
|
||||
}
|
||||
|
||||
inline void
|
||||
Parser::parse(Level& l)
|
||||
{
|
||||
while (std::getline(*f_, line_)) {
|
||||
++ln_;
|
||||
if (line_[0] == '#' || line_[0] == ';') continue;
|
||||
line_ = trim(line_);
|
||||
if (line_.empty()) continue;
|
||||
if (line_[0] == '[') {
|
||||
size_t depth;
|
||||
std::string sname;
|
||||
parseSLine(sname, depth);
|
||||
Level* lp = NULL;
|
||||
Level* parent = &l;
|
||||
if (depth > l.depth + 1)
|
||||
err("section with wrong depth");
|
||||
if (l.depth == depth-1)
|
||||
lp = &l.sections[sname];
|
||||
else {
|
||||
lp = l.parent;
|
||||
size_t n = l.depth - depth;
|
||||
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) {
|
||||
lp->depth = depth;
|
||||
lp->parent = parent;
|
||||
}
|
||||
parent->ordered_sections.push_back(parent->sections.find(sname));
|
||||
parse(*lp);
|
||||
} 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))));
|
||||
if (!res.second)
|
||||
err("duplicated key found");
|
||||
l.ordered_values.push_back(res.first);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
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) {
|
||||
assert((*it)->second.depth == l.depth+1);
|
||||
dump(s, (*it)->second, (*it)->first);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // INI_HPP
|
||||
|
12
contrib/backends/nntpchan-daemon/libnntpchan/io_handle.hpp
Normal file
12
contrib/backends/nntpchan-daemon/libnntpchan/io_handle.hpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef NNTPCHAN_IO_HANDLE_HPP
|
||||
#define NNTPCHAN_IO_HANDLE_HPP
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
typedef std::unique_ptr<std::iostream> IOHandle_ptr;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
39
contrib/backends/nntpchan-daemon/libnntpchan/line.cpp
Normal file
39
contrib/backends/nntpchan-daemon/libnntpchan/line.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "line.hpp"
|
||||
|
||||
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;
|
||||
// process leftovers
|
||||
std::size_t idx = 0;
|
||||
std::size_t pos = 0;
|
||||
while(l-- > 0) {
|
||||
char c = data[idx++];
|
||||
if(c == '\n') {
|
||||
OnLine(data, pos);
|
||||
pos = 0;
|
||||
data += idx;
|
||||
} else if (c == '\r' && data[idx] == '\n') {
|
||||
OnLine(data, pos);
|
||||
data += idx + 1;
|
||||
pos = 0;
|
||||
} else {
|
||||
pos ++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LineReader::OnLine(const char *d, const size_t l)
|
||||
{
|
||||
std::string line(d, l);
|
||||
HandleLine(line);
|
||||
}
|
||||
|
||||
bool LineReader::ShouldClose()
|
||||
{
|
||||
return m_close;
|
||||
}
|
||||
}
|
34
contrib/backends/nntpchan-daemon/libnntpchan/line.hpp
Normal file
34
contrib/backends/nntpchan-daemon/libnntpchan/line.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef NNTPCHAN_LINE_HPP
|
||||
#define NNTPCHAN_LINE_HPP
|
||||
#include "server.hpp"
|
||||
#include <stdint.h>
|
||||
namespace nntpchan
|
||||
{
|
||||
|
||||
/** @brief a buffered line reader */
|
||||
class LineReader
|
||||
{
|
||||
public:
|
||||
|
||||
LineReader(size_t lineLimit);
|
||||
|
||||
/** @brief queue inbound data from connection */
|
||||
void Data(const char * data, ssize_t s);
|
||||
|
||||
/** implements IConnHandler */
|
||||
virtual bool ShouldClose();
|
||||
|
||||
protected:
|
||||
/** @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;
|
||||
bool m_close;
|
||||
const size_t lineLimit;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
26
contrib/backends/nntpchan-daemon/libnntpchan/mime.cpp
Normal file
26
contrib/backends/nntpchan-daemon/libnntpchan/mime.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "mime.hpp"
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
bool ReadHeader(const FileHandle_ptr & file, RawHeader & header)
|
||||
{
|
||||
std::string line;
|
||||
while(std::getline(*file, line) && !(line == "\r" || line == ""))
|
||||
{
|
||||
std::string k, v;
|
||||
auto idx = line.find(": ");
|
||||
auto endidx = line.size() - 1;
|
||||
|
||||
while(line[endidx] == '\r') --endidx;
|
||||
|
||||
if(idx != std::string::npos && idx + 2 < endidx)
|
||||
{
|
||||
k = line.substr(0, idx);
|
||||
v = line.substr(idx+2, endidx);
|
||||
header[k] = v;
|
||||
}
|
||||
}
|
||||
return file->good();
|
||||
}
|
||||
|
||||
}
|
30
contrib/backends/nntpchan-daemon/libnntpchan/mime.hpp
Normal file
30
contrib/backends/nntpchan-daemon/libnntpchan/mime.hpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef NNTPCHAN_MIME_HPP
|
||||
#define NNTPCHAN_MIME_HPP
|
||||
#include "file_handle.hpp"
|
||||
#include "io_handle.hpp"
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
|
||||
typedef std::map<std::string, std::string> RawHeader;
|
||||
|
||||
bool ReadHeader(const FileHandle_ptr & f, RawHeader & h);
|
||||
|
||||
|
||||
struct MimePart
|
||||
{
|
||||
virtual RawHeader & Header() = 0;
|
||||
virtual IOHandle_ptr OpenPart() = 0;
|
||||
};
|
||||
|
||||
typedef std::unique_ptr<MimePart> MimePart_ptr;
|
||||
|
||||
typedef std::function<bool(MimePart_ptr)> PartReader;
|
||||
|
||||
bool ReadParts(const FileHandle_ptr & f, PartReader r);
|
||||
}
|
||||
|
||||
#endif
|
59
contrib/backends/nntpchan-daemon/libnntpchan/model.hpp
Normal file
59
contrib/backends/nntpchan-daemon/libnntpchan/model.hpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#ifndef NNTPCHAN_MODEL_HPP
|
||||
#define NNTPCHAN_MODEL_HPP
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
namespace model
|
||||
{
|
||||
// MIME Header
|
||||
typedef std::map<std::string, std::set<std::string> > PostHeader;
|
||||
// text post contents
|
||||
typedef std::string PostBody;
|
||||
// single file attachment, (orig_filename, hexdigest, thumb_filename)
|
||||
typedef std::tuple<std::string, std::string, std::string> PostAttachment;
|
||||
// all attachments on a post
|
||||
typedef std::vector<PostAttachment> Attachments;
|
||||
// a post (header, Post Text, Attachments)
|
||||
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)
|
||||
{
|
||||
return std::get<0>(att);
|
||||
}
|
||||
|
||||
static inline std::string & GetHexDigest(PostAttachment & att)
|
||||
{
|
||||
return std::get<1>(att);
|
||||
}
|
||||
|
||||
static inline std::string & GetThumbnail(PostAttachment & att)
|
||||
{
|
||||
return std::get<2>(att);
|
||||
}
|
||||
|
||||
static inline PostHeader & GetHeader(Post & post)
|
||||
{
|
||||
return std::get<0>(post);
|
||||
}
|
||||
|
||||
static inline PostBody & GetBody(Post & post)
|
||||
{
|
||||
return std::get<1>(post);
|
||||
}
|
||||
|
||||
static inline Attachments & GetAttachments(Post & post)
|
||||
{
|
||||
return std::get<2>(post);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
44
contrib/backends/nntpchan-daemon/libnntpchan/net.cpp
Normal file
44
contrib/backends/nntpchan-daemon/libnntpchan/net.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "net.hpp"
|
||||
#include <uv.h>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
std::string NetAddr::to_string()
|
||||
{
|
||||
std::string str("invalid");
|
||||
const size_t s = 128;
|
||||
char * buff = new char[s];
|
||||
if(uv_ip6_name(&addr, buff, s) == 0) {
|
||||
str = std::string(buff);
|
||||
delete [] buff;
|
||||
}
|
||||
std::stringstream ss;
|
||||
ss << "[" << str << "]:" << ntohs(addr.sin6_port);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
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) {
|
||||
throw std::runtime_error("invalid address: "+addr);
|
||||
}
|
||||
if (addr[0] != '[') {
|
||||
throw std::runtime_error("invalid address: "+addr);
|
||||
}
|
||||
auto p = addr.substr(n+2);
|
||||
int port = std::atoi(p.c_str());
|
||||
auto a = addr.substr(0, n);
|
||||
uv_ip6_addr(a.c_str(), port, &saddr.addr);
|
||||
return saddr;
|
||||
}
|
||||
}
|
23
contrib/backends/nntpchan-daemon/libnntpchan/net.hpp
Normal file
23
contrib/backends/nntpchan-daemon/libnntpchan/net.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef NNTPCHAN_NET_HPP
|
||||
#define NNTPCHAN_NET_HPP
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
#include <string>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
struct NetAddr
|
||||
{
|
||||
NetAddr();
|
||||
|
||||
sockaddr_in6 addr;
|
||||
operator sockaddr * () { return (sockaddr *) &addr; }
|
||||
operator const sockaddr * () const { return (sockaddr *) &addr; }
|
||||
std::string to_string();
|
||||
};
|
||||
|
||||
NetAddr ParseAddr(const std::string & addr);
|
||||
}
|
||||
|
||||
#endif
|
99
contrib/backends/nntpchan-daemon/libnntpchan/nntp_auth.cpp
Normal file
99
contrib/backends/nntpchan-daemon/libnntpchan/nntp_auth.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#include "nntp_auth.hpp"
|
||||
#include "crypto.hpp"
|
||||
#include "base64.hpp"
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
HashedCredDB::HashedCredDB() : LineReader(1024) {}
|
||||
|
||||
bool HashedCredDB::CheckLogin(const std::string & user, const std::string & passwd)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m_access);
|
||||
m_found = false;
|
||||
m_user = user;
|
||||
m_passwd = passwd;
|
||||
m_instream->seekg(0, std::ios::end);
|
||||
const auto l = m_instream->tellg();
|
||||
m_instream->seekg(0, std::ios::beg);
|
||||
char * buff = new char[l];
|
||||
// read file
|
||||
m_instream->read(buff, l);
|
||||
Data(buff, l);
|
||||
delete [] buff;
|
||||
return m_found;
|
||||
}
|
||||
|
||||
bool HashedCredDB::ProcessLine(const std::string & line)
|
||||
{
|
||||
// strip comments
|
||||
auto comment = line.find("#");
|
||||
std::string part = line;
|
||||
for (; comment != std::string::npos; comment = part.find("#")) {
|
||||
if(comment)
|
||||
part = part.substr(0, comment);
|
||||
else break;
|
||||
}
|
||||
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
|
||||
part = part.substr(idx+1);
|
||||
|
||||
idx = part.find(":");
|
||||
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;
|
||||
}
|
||||
|
||||
void HashedCredDB::HandleLine(const std::string &line)
|
||||
{
|
||||
if(m_found) return;
|
||||
if(ProcessLine(line))
|
||||
m_found = true;
|
||||
}
|
||||
|
||||
void HashedCredDB::SetStream(std::istream * s)
|
||||
{
|
||||
m_instream = s;
|
||||
}
|
||||
|
||||
std::string HashedCredDB::Hash(const std::string & data, const std::string & salt)
|
||||
{
|
||||
SHA512Digest h;
|
||||
std::string d = data + salt;
|
||||
SHA512((const uint8_t*)d.c_str(), d.size(), h);
|
||||
return B64Encode(h.data(), h.size());
|
||||
}
|
||||
|
||||
HashedFileDB::HashedFileDB(const std::string & fname) :
|
||||
m_fname(fname),
|
||||
f(nullptr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
HashedFileDB::~HashedFileDB()
|
||||
{
|
||||
}
|
||||
|
||||
void HashedFileDB::Close()
|
||||
{
|
||||
if(f.is_open())
|
||||
f.close();
|
||||
}
|
||||
|
||||
bool HashedFileDB::Open()
|
||||
{
|
||||
if(!f.is_open())
|
||||
f.open(m_fname);
|
||||
if(f.is_open()) {
|
||||
SetStream(&f);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
61
contrib/backends/nntpchan-daemon/libnntpchan/nntp_auth.hpp
Normal file
61
contrib/backends/nntpchan-daemon/libnntpchan/nntp_auth.hpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#ifndef NNTPCHAN_NNTP_AUTH_HPP
|
||||
#define NNTPCHAN_NNTP_AUTH_HPP
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include "line.hpp"
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
/** @brief nntp credential db interface */
|
||||
class NNTPCredentialDB
|
||||
{
|
||||
public:
|
||||
/** @brief open connection to database, return false on error otherwise return true */
|
||||
virtual bool Open() = 0;
|
||||
/** @brief close connection to database */
|
||||
virtual void Close() = 0;
|
||||
/** @brief return true if username password combo is correct */
|
||||
virtual bool CheckLogin(const std::string & user, const std::string & passwd) = 0;
|
||||
virtual ~NNTPCredentialDB() {}
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<NNTPCredentialDB> CredDB_ptr;
|
||||
|
||||
/** @brief nntp credential db using hashed+salted passwords */
|
||||
class HashedCredDB : public NNTPCredentialDB, public LineReader
|
||||
{
|
||||
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);
|
||||
|
||||
std::mutex m_access;
|
||||
std::string m_user, m_passwd;
|
||||
bool m_found;
|
||||
/** return true if we have a line that matches this username / password combo */
|
||||
std::istream * m_instream;
|
||||
};
|
||||
|
||||
class HashedFileDB : public HashedCredDB
|
||||
{
|
||||
public:
|
||||
HashedFileDB(const std::string & fname);
|
||||
~HashedFileDB();
|
||||
bool Open();
|
||||
void Close();
|
||||
private:
|
||||
std::string m_fname;
|
||||
std::ifstream f;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
214
contrib/backends/nntpchan-daemon/libnntpchan/nntp_handler.cpp
Normal file
214
contrib/backends/nntpchan-daemon/libnntpchan/nntp_handler.cpp
Normal file
@@ -0,0 +1,214 @@
|
||||
#include "nntp_handler.hpp"
|
||||
#include "sanitize.hpp"
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
NNTPServerHandler::NNTPServerHandler(const std::string & storage) :
|
||||
LineReader(1024),
|
||||
m_article(nullptr),
|
||||
m_auth(nullptr),
|
||||
m_store(storage),
|
||||
m_authed(false),
|
||||
m_state(eStateReadCommand)
|
||||
{
|
||||
}
|
||||
|
||||
NNTPServerHandler::~NNTPServerHandler()
|
||||
{
|
||||
}
|
||||
|
||||
void NNTPServerHandler::HandleLine(const std::string &line)
|
||||
{
|
||||
if(m_state == eStateReadCommand)
|
||||
{
|
||||
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(std::string(part));
|
||||
}
|
||||
if(command.size())
|
||||
HandleCommand(command);
|
||||
else
|
||||
QueueLine("501 Syntax error");
|
||||
}
|
||||
else if(m_state == eStateStoreArticle)
|
||||
{
|
||||
std::string l = line + "\r\n";
|
||||
OnData(l.c_str(), l.size());
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "invalid state" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void NNTPServerHandler::OnData(const char * data, ssize_t l)
|
||||
{
|
||||
if(l <= 0 ) return;
|
||||
if(m_state == eStateStoreArticle)
|
||||
{
|
||||
const char * end = strstr(data, "\r\n.\r\n");
|
||||
if(end)
|
||||
{
|
||||
std::size_t diff = end - data ;
|
||||
if(m_article)
|
||||
m_article->write(data, diff+2);
|
||||
ArticleObtained();
|
||||
diff += 5;
|
||||
Data(end+5, l-diff);
|
||||
return;
|
||||
}
|
||||
if(m_article)
|
||||
m_article->write(data, l);
|
||||
}
|
||||
else
|
||||
Data(data, l);
|
||||
}
|
||||
|
||||
void NNTPServerHandler::HandleCommand(const std::deque<std::string> & command)
|
||||
{
|
||||
auto cmd = command[0];
|
||||
std::transform(cmd.begin(), cmd.end(), cmd.begin(), ::toupper);
|
||||
std::size_t cmdlen = command.size();
|
||||
for(const auto & part : command)
|
||||
std::cerr << " " << part;
|
||||
std::cerr << std::endl;
|
||||
if (cmd == "QUIT") {
|
||||
Quit();
|
||||
return;
|
||||
}
|
||||
else if (cmd[0] == '5')
|
||||
{
|
||||
return;
|
||||
}
|
||||
else if (cmd == "MODE" ) {
|
||||
if(cmdlen == 2) {
|
||||
// set mode
|
||||
SwitchMode(command[1]);
|
||||
} else if(cmdlen) {
|
||||
// too many arguments
|
||||
QueueLine("500 too many arguments");
|
||||
} else {
|
||||
// get mode
|
||||
QueueLine("500 wrong arguments");
|
||||
}
|
||||
} 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) {
|
||||
const std::string & msgid = command[1];
|
||||
if(IsValidMessageID(msgid) && m_store.Accept(msgid))
|
||||
{
|
||||
QueueLine("238 "+msgid);
|
||||
return;
|
||||
}
|
||||
QueueLine("438 "+msgid);
|
||||
}
|
||||
else
|
||||
QueueLine("501 syntax error");
|
||||
} else if (cmd == "TAKETHIS") {
|
||||
if (cmdlen == 2)
|
||||
{
|
||||
const std::string & msgid = command[1];
|
||||
if(m_store.Accept(msgid))
|
||||
{
|
||||
m_article = m_store.OpenWrite(msgid);
|
||||
}
|
||||
m_articleName = msgid;
|
||||
EnterState(eStateStoreArticle);
|
||||
return;
|
||||
}
|
||||
QueueLine("501 invalid syntax");
|
||||
} else {
|
||||
// unknown command
|
||||
QueueLine("500 Unknown Command");
|
||||
}
|
||||
}
|
||||
|
||||
void NNTPServerHandler::ArticleObtained()
|
||||
{
|
||||
if(m_article)
|
||||
{
|
||||
m_article->flush();
|
||||
m_article->close();
|
||||
m_article = nullptr;
|
||||
QueueLine("239 "+m_articleName);
|
||||
std::cerr << "stored " << m_articleName << std::endl;
|
||||
}
|
||||
else
|
||||
QueueLine("439 "+m_articleName);
|
||||
m_articleName = "";
|
||||
EnterState(eStateReadCommand);
|
||||
}
|
||||
|
||||
void NNTPServerHandler::SwitchMode(const std::string & mode)
|
||||
{
|
||||
std::string m = mode;
|
||||
std::transform(m.begin(), m.end(), m.begin(), ::toupper);
|
||||
if (m == "READER") {
|
||||
m_mode = m;
|
||||
if(PostingAllowed()) {
|
||||
QueueLine("200 Posting is permitted yo");
|
||||
} else {
|
||||
QueueLine("201 Posting is not permitted yo");
|
||||
}
|
||||
} else if (m == "STREAM") {
|
||||
m_mode = m;
|
||||
if (PostingAllowed()) {
|
||||
QueueLine("203 Streaming enabled");
|
||||
} else {
|
||||
QueueLine("483 Streaming Denied");
|
||||
}
|
||||
} else {
|
||||
// unknown mode
|
||||
QueueLine("500 Unknown mode");
|
||||
}
|
||||
}
|
||||
|
||||
void NNTPServerHandler::EnterState(State st)
|
||||
{
|
||||
std::cerr << "enter state " << st << std::endl;
|
||||
m_state = st;
|
||||
}
|
||||
|
||||
void NNTPServerHandler::Quit()
|
||||
{
|
||||
EnterState(eStateQuit);
|
||||
QueueLine("205 quitting");
|
||||
}
|
||||
|
||||
bool NNTPServerHandler::ShouldClose()
|
||||
{
|
||||
return m_state == eStateQuit;
|
||||
}
|
||||
|
||||
bool NNTPServerHandler::PostingAllowed()
|
||||
{
|
||||
return m_authed || m_auth == nullptr;
|
||||
}
|
||||
|
||||
void NNTPServerHandler::Greet()
|
||||
{
|
||||
if(PostingAllowed())
|
||||
QueueLine("200 Posting allowed");
|
||||
else
|
||||
QueueLine("201 Posting not allowed");
|
||||
}
|
||||
|
||||
void NNTPServerHandler::SetAuth(CredDB_ptr creds)
|
||||
{
|
||||
m_auth = creds;
|
||||
}
|
||||
}
|
@@ -0,0 +1,62 @@
|
||||
#ifndef NNTPCHAN_NNTP_HANDLER_HPP
|
||||
#define NNTPCHAN_NNTP_HANDLER_HPP
|
||||
#include <deque>
|
||||
#include <string>
|
||||
#include "line.hpp"
|
||||
#include "nntp_auth.hpp"
|
||||
#include "storage.hpp"
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
class NNTPServerHandler : public LineReader, public IConnHandler
|
||||
{
|
||||
public:
|
||||
NNTPServerHandler(const std::string & storage);
|
||||
~NNTPServerHandler();
|
||||
|
||||
virtual bool ShouldClose();
|
||||
|
||||
void SetAuth(CredDB_ptr creds);
|
||||
|
||||
virtual void OnData(const char *, ssize_t);
|
||||
|
||||
void Greet();
|
||||
|
||||
protected:
|
||||
void HandleLine(const std::string & line);
|
||||
void HandleCommand(const std::deque<std::string> & command);
|
||||
private:
|
||||
|
||||
enum State {
|
||||
eStateReadCommand,
|
||||
eStateStoreArticle,
|
||||
eStateQuit
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
void EnterState(State st);
|
||||
|
||||
void ArticleObtained();
|
||||
|
||||
// handle quit command, this queues a reply
|
||||
void Quit();
|
||||
|
||||
// switch nntp modes, this queues a reply
|
||||
void SwitchMode(const std::string & mode);
|
||||
|
||||
bool PostingAllowed();
|
||||
|
||||
private:
|
||||
std::string m_articleName;
|
||||
FileHandle_ptr m_article;
|
||||
CredDB_ptr m_auth;
|
||||
ArticleStorage m_store;
|
||||
std::string m_mode;
|
||||
bool m_authed;
|
||||
State m_state;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif
|
85
contrib/backends/nntpchan-daemon/libnntpchan/nntp_server.cpp
Normal file
85
contrib/backends/nntpchan-daemon/libnntpchan/nntp_server.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
|
||||
#include "nntp_server.hpp"
|
||||
#include "nntp_auth.hpp"
|
||||
#include "nntp_handler.hpp"
|
||||
#include "net.hpp"
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
|
||||
NNTPServer::NNTPServer(uv_loop_t * loop) : Server(loop), m_frontend(nullptr) {}
|
||||
|
||||
NNTPServer::~NNTPServer()
|
||||
{
|
||||
}
|
||||
|
||||
IServerConn * NNTPServer::CreateConn(uv_stream_t * s)
|
||||
{
|
||||
CredDB_ptr creds;
|
||||
|
||||
std::ifstream i;
|
||||
i.open(m_logindbpath);
|
||||
if(i.is_open()) creds = std::make_shared<HashedFileDB>(m_logindbpath);
|
||||
|
||||
NNTPServerHandler * handler = new NNTPServerHandler(m_storagePath);
|
||||
if(creds)
|
||||
handler->SetAuth(creds);
|
||||
|
||||
NNTPServerConn * conn = new NNTPServerConn(GetLoop(), s, this, handler);
|
||||
return conn;
|
||||
}
|
||||
|
||||
void NNTPServer::SetLoginDB(const std::string path)
|
||||
{
|
||||
m_logindbpath = path;
|
||||
}
|
||||
|
||||
|
||||
void NNTPServer::SetStoragePath(const std::string & path)
|
||||
{
|
||||
m_storagePath = path;
|
||||
}
|
||||
|
||||
void NNTPServer::SetInstanceName(const std::string & name)
|
||||
{
|
||||
m_servername = name;
|
||||
}
|
||||
|
||||
void NNTPServer::SetFrontend(Frontend * f)
|
||||
{
|
||||
m_frontend.reset(f);
|
||||
}
|
||||
|
||||
std::string NNTPServer::InstanceName() const
|
||||
{
|
||||
return m_servername;
|
||||
}
|
||||
|
||||
void NNTPServer::OnAcceptError(int status)
|
||||
{
|
||||
std::cerr << "nntpserver::accept() " << uv_strerror(status) << std::endl;
|
||||
}
|
||||
|
||||
void NNTPServerConn::SendNextReply()
|
||||
{
|
||||
IConnHandler * handler = GetHandler();
|
||||
while(handler->HasNextLine()) {
|
||||
auto line = handler->GetNextLine();
|
||||
SendString(line + "\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void NNTPServerConn::Greet()
|
||||
{
|
||||
IConnHandler * handler = GetHandler();
|
||||
handler->Greet();
|
||||
SendNextReply();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
62
contrib/backends/nntpchan-daemon/libnntpchan/nntp_server.hpp
Normal file
62
contrib/backends/nntpchan-daemon/libnntpchan/nntp_server.hpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#ifndef NNTPCHAN_NNTP_SERVER_HPP
|
||||
#define NNTPCHAN_NNTP_SERVER_HPP
|
||||
#include <uv.h>
|
||||
#include <string>
|
||||
#include <deque>
|
||||
#include "frontend.hpp"
|
||||
#include "server.hpp"
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
|
||||
class NNTPServer : public Server
|
||||
{
|
||||
public:
|
||||
|
||||
NNTPServer(uv_loop_t * loop);
|
||||
|
||||
virtual ~NNTPServer();
|
||||
|
||||
void SetStoragePath(const std::string & path);
|
||||
|
||||
void SetLoginDB(const std::string path);
|
||||
|
||||
void SetInstanceName(const std::string & name);
|
||||
|
||||
std::string InstanceName() const;
|
||||
|
||||
void Close();
|
||||
|
||||
virtual IServerConn * CreateConn(uv_stream_t * s);
|
||||
|
||||
virtual void OnAcceptError(int status);
|
||||
|
||||
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; };
|
||||
|
||||
/** @brief send next queued reply */
|
||||
virtual void SendNextReply();
|
||||
|
||||
virtual void Greet();
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
24
contrib/backends/nntpchan-daemon/libnntpchan/sanitize.cpp
Normal file
24
contrib/backends/nntpchan-daemon/libnntpchan/sanitize.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "sanitize.hpp"
|
||||
#include <algorithm>
|
||||
#include <regex>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
std::string NNTPSanitize(const std::string & str)
|
||||
{
|
||||
}
|
||||
|
||||
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); } );
|
||||
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;
|
||||
}
|
||||
}
|
12
contrib/backends/nntpchan-daemon/libnntpchan/sanitize.hpp
Normal file
12
contrib/backends/nntpchan-daemon/libnntpchan/sanitize.hpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef NNTPCHAN_SANITIZE_HPP
|
||||
#define NNTPCHAN_SANITIZE_HPP
|
||||
#include <string>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
std::string NNTPSanitize(const std::string & str);
|
||||
std::string ToLower(const std::string & str);
|
||||
bool IsValidMessageID(const std::string & msgid);
|
||||
}
|
||||
|
||||
#endif
|
140
contrib/backends/nntpchan-daemon/libnntpchan/server.cpp
Normal file
140
contrib/backends/nntpchan-daemon/libnntpchan/server.cpp
Normal file
@@ -0,0 +1,140 @@
|
||||
#include "buffer.hpp"
|
||||
#include "server.hpp"
|
||||
#include "net.hpp"
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
Server::Server(uv_loop_t * loop)
|
||||
{
|
||||
m_loop = loop;
|
||||
uv_tcp_init(m_loop, &m_server);
|
||||
m_server.data = this;
|
||||
}
|
||||
|
||||
void Server::Close()
|
||||
{
|
||||
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;
|
||||
s->data = nullptr;
|
||||
});
|
||||
}
|
||||
|
||||
void Server::Bind(const std::string & addr)
|
||||
{
|
||||
auto saddr = ParseAddr(addr);
|
||||
assert(uv_tcp_bind(*this, saddr, 0) == 0);
|
||||
auto cb = [] (uv_stream_t * s, int status) {
|
||||
Server * self = (Server *) s->data;
|
||||
self->OnAccept(s, status);
|
||||
};
|
||||
assert(uv_listen(*this, 5, cb) == 0);
|
||||
}
|
||||
|
||||
void Server::OnAccept(uv_stream_t * s, int status)
|
||||
{
|
||||
if(status < 0) {
|
||||
OnAcceptError(status);
|
||||
return;
|
||||
}
|
||||
IServerConn * conn = CreateConn(s);
|
||||
assert(conn);
|
||||
m_conns.push_back(conn);
|
||||
conn->Greet();
|
||||
}
|
||||
|
||||
void Server::RemoveConn(IServerConn * conn)
|
||||
{
|
||||
auto itr = m_conns.begin();
|
||||
while(itr != m_conns.end())
|
||||
{
|
||||
if(*itr == conn)
|
||||
itr = m_conns.erase(itr);
|
||||
else
|
||||
++itr;
|
||||
}
|
||||
}
|
||||
|
||||
void IConnHandler::QueueLine(const std::string & line)
|
||||
{
|
||||
m_sendlines.push_back(line);
|
||||
}
|
||||
|
||||
bool IConnHandler::HasNextLine()
|
||||
{
|
||||
return m_sendlines.size() > 0;
|
||||
}
|
||||
|
||||
std::string IConnHandler::GetNextLine()
|
||||
{
|
||||
std::string line = m_sendlines[0];
|
||||
m_sendlines.pop_front();
|
||||
return line;
|
||||
}
|
||||
|
||||
IServerConn::IServerConn(uv_loop_t * l, uv_stream_t * st, Server * parent, IConnHandler * h)
|
||||
{
|
||||
m_loop = l;
|
||||
m_parent = parent;
|
||||
m_handler = h;
|
||||
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) {
|
||||
IServerConn * self = (IServerConn*) h->data;
|
||||
if(self == nullptr) return;
|
||||
b->base = self->m_readbuff;
|
||||
if (s > sizeof(self->m_readbuff))
|
||||
b->len = sizeof(self->m_readbuff);
|
||||
else
|
||||
b->len = s;
|
||||
}, [] (uv_stream_t * s, ssize_t nread, const uv_buf_t * b) {
|
||||
IServerConn * self = (IServerConn*) s->data;
|
||||
if(self == nullptr) return;
|
||||
if(nread > 0) {
|
||||
self->m_handler->OnData(b->base, nread);
|
||||
self->SendNextReply();
|
||||
if(self->m_handler->ShouldClose())
|
||||
self->Close();
|
||||
} else {
|
||||
if (nread != UV_EOF) {
|
||||
std::cerr << "error in nntp server conn alloc: ";
|
||||
std::cerr << uv_strerror(nread);
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
// got eof or error
|
||||
self->Close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
IServerConn::~IServerConn()
|
||||
{
|
||||
delete m_handler;
|
||||
}
|
||||
|
||||
void IServerConn::SendString(const std::string & str)
|
||||
{
|
||||
WriteBuffer * b = new WriteBuffer(str);
|
||||
uv_write(&b->w, (uv_stream_t*)&m_conn, &b->b, 1, [](uv_write_t * w, int status) {
|
||||
(void) status;
|
||||
WriteBuffer * wb = (WriteBuffer *) w->data;
|
||||
if(wb)
|
||||
delete wb;
|
||||
});
|
||||
}
|
||||
|
||||
void IServerConn::Close()
|
||||
{
|
||||
m_parent->RemoveConn(this);
|
||||
uv_close((uv_handle_t*)&m_conn, [] (uv_handle_t * s) {
|
||||
IServerConn * self = (IServerConn*) s->data;
|
||||
if(self)
|
||||
delete self;
|
||||
s->data = nullptr;
|
||||
});
|
||||
}
|
||||
}
|
99
contrib/backends/nntpchan-daemon/libnntpchan/server.hpp
Normal file
99
contrib/backends/nntpchan-daemon/libnntpchan/server.hpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#ifndef NNTPCHAN_SERVER_HPP
|
||||
#define NNTPCHAN_SERVER_HPP
|
||||
#include <uv.h>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
|
||||
class Server;
|
||||
|
||||
|
||||
struct IConnHandler
|
||||
{
|
||||
|
||||
virtual ~IConnHandler() {};
|
||||
|
||||
/** got inbound data */
|
||||
virtual void OnData(const char * data, ssize_t s) = 0;
|
||||
|
||||
/** get next line of data to send */
|
||||
std::string GetNextLine();
|
||||
|
||||
/** return true if we have a line to send */
|
||||
bool HasNextLine();
|
||||
|
||||
/** return true if we should close this connection otherwise return false */
|
||||
virtual bool ShouldClose() = 0;
|
||||
|
||||
/** queue a data send */
|
||||
void QueueLine(const std::string & line);
|
||||
|
||||
virtual void Greet() = 0;
|
||||
|
||||
|
||||
private:
|
||||
std::deque<std::string> m_sendlines;
|
||||
};
|
||||
|
||||
/** server connection handler interface */
|
||||
struct IServerConn
|
||||
{
|
||||
IServerConn(uv_loop_t * l, uv_stream_t * s, Server * parent, IConnHandler * h);
|
||||
virtual ~IServerConn();
|
||||
virtual void Close();
|
||||
virtual void Greet() = 0;
|
||||
virtual void SendNextReply() = 0;
|
||||
virtual bool IsTimedOut() = 0;
|
||||
void SendString(const std::string & str);
|
||||
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;
|
||||
Server * m_parent;
|
||||
IConnHandler * m_handler;
|
||||
char m_readbuff[65536];
|
||||
};
|
||||
|
||||
class Server
|
||||
{
|
||||
public:
|
||||
Server(uv_loop_t * loop);
|
||||
/** called after socket close, NEVER call directly */
|
||||
virtual ~Server() {}
|
||||
/** create connection handler from open stream */
|
||||
virtual IServerConn * CreateConn(uv_stream_t * s) = 0;
|
||||
/** close all sockets and stop */
|
||||
void Close();
|
||||
/** bind to address */
|
||||
void Bind(const std::string & addr);
|
||||
|
||||
typedef std::function<void(IServerConn *)> ConnVisitor;
|
||||
|
||||
/** visit all open connections */
|
||||
void VisitConns(ConnVisitor v);
|
||||
|
||||
/** remove connection from server, called after proper close */
|
||||
void RemoveConn(IServerConn * conn);
|
||||
|
||||
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; }
|
||||
operator uv_stream_t * () { return (uv_stream_t *) &m_server; }
|
||||
|
||||
void OnAccept(uv_stream_t * s, int status);
|
||||
std::deque<IServerConn *> m_conns;
|
||||
uv_tcp_t m_server;
|
||||
uv_loop_t * m_loop;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif
|
327
contrib/backends/nntpchan-daemon/libnntpchan/sha1.cpp
Normal file
327
contrib/backends/nntpchan-daemon/libnntpchan/sha1.cpp
Normal file
@@ -0,0 +1,327 @@
|
||||
/*
|
||||
SHA-1 in C
|
||||
By Steve Reid <steve@edmweb.com>
|
||||
100% Public Domain
|
||||
|
||||
Test Vectors (from FIPS PUB 180-1)
|
||||
"abc"
|
||||
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
|
||||
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
|
||||
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
|
||||
A million repetitions of "a"
|
||||
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
|
||||
*/
|
||||
|
||||
/* #define LITTLE_ENDIAN * This should be #define'd already, if true. */
|
||||
/* #define SHA1HANDSOFF * Copies data before messing with it. */
|
||||
|
||||
#include "crypto_old.hpp"
|
||||
#include <string>
|
||||
|
||||
extern "C" {
|
||||
#define SHA1HANDSOFF
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* 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))
|
||||
#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))
|
||||
|
||||
/* (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);
|
||||
|
||||
|
||||
/* Hash a single 512-bit block. This is the core of the algorithm. */
|
||||
|
||||
void SHA1Transform(
|
||||
uint32_t state[5],
|
||||
const unsigned char buffer[64]
|
||||
)
|
||||
{
|
||||
uint32_t a, b, c, d, e;
|
||||
|
||||
typedef union
|
||||
{
|
||||
unsigned char c[64];
|
||||
uint32_t l[16];
|
||||
} CHAR64LONG16;
|
||||
|
||||
#ifdef SHA1HANDSOFF
|
||||
CHAR64LONG16 block[1]; /* use array to appear as a pointer */
|
||||
|
||||
memcpy(block, buffer, 64);
|
||||
#else
|
||||
/* The following had better never be used because it causes the
|
||||
* pointer-to-const buffer to be cast into a pointer to non-const.
|
||||
* And the result is written through. I threw a "const" in, hoping
|
||||
* this will cause a diagnostic.
|
||||
*/
|
||||
CHAR64LONG16 *block = (const CHAR64LONG16 *) buffer;
|
||||
#endif
|
||||
/* Copy context->state[] to working vars */
|
||||
a = state[0];
|
||||
b = state[1];
|
||||
c = state[2];
|
||||
d = state[3];
|
||||
e = state[4];
|
||||
/* 4 rounds of 20 operations each. Loop unrolled. */
|
||||
R0(a, b, c, d, e, 0);
|
||||
R0(e, a, b, c, d, 1);
|
||||
R0(d, e, a, b, c, 2);
|
||||
R0(c, d, e, a, b, 3);
|
||||
R0(b, c, d, e, a, 4);
|
||||
R0(a, b, c, d, e, 5);
|
||||
R0(e, a, b, c, d, 6);
|
||||
R0(d, e, a, b, c, 7);
|
||||
R0(c, d, e, a, b, 8);
|
||||
R0(b, c, d, e, a, 9);
|
||||
R0(a, b, c, d, e, 10);
|
||||
R0(e, a, b, c, d, 11);
|
||||
R0(d, e, a, b, c, 12);
|
||||
R0(c, d, e, a, b, 13);
|
||||
R0(b, c, d, e, a, 14);
|
||||
R0(a, b, c, d, e, 15);
|
||||
R1(e, a, b, c, d, 16);
|
||||
R1(d, e, a, b, c, 17);
|
||||
R1(c, d, e, a, b, 18);
|
||||
R1(b, c, d, e, a, 19);
|
||||
R2(a, b, c, d, e, 20);
|
||||
R2(e, a, b, c, d, 21);
|
||||
R2(d, e, a, b, c, 22);
|
||||
R2(c, d, e, a, b, 23);
|
||||
R2(b, c, d, e, a, 24);
|
||||
R2(a, b, c, d, e, 25);
|
||||
R2(e, a, b, c, d, 26);
|
||||
R2(d, e, a, b, c, 27);
|
||||
R2(c, d, e, a, b, 28);
|
||||
R2(b, c, d, e, a, 29);
|
||||
R2(a, b, c, d, e, 30);
|
||||
R2(e, a, b, c, d, 31);
|
||||
R2(d, e, a, b, c, 32);
|
||||
R2(c, d, e, a, b, 33);
|
||||
R2(b, c, d, e, a, 34);
|
||||
R2(a, b, c, d, e, 35);
|
||||
R2(e, a, b, c, d, 36);
|
||||
R2(d, e, a, b, c, 37);
|
||||
R2(c, d, e, a, b, 38);
|
||||
R2(b, c, d, e, a, 39);
|
||||
R3(a, b, c, d, e, 40);
|
||||
R3(e, a, b, c, d, 41);
|
||||
R3(d, e, a, b, c, 42);
|
||||
R3(c, d, e, a, b, 43);
|
||||
R3(b, c, d, e, a, 44);
|
||||
R3(a, b, c, d, e, 45);
|
||||
R3(e, a, b, c, d, 46);
|
||||
R3(d, e, a, b, c, 47);
|
||||
R3(c, d, e, a, b, 48);
|
||||
R3(b, c, d, e, a, 49);
|
||||
R3(a, b, c, d, e, 50);
|
||||
R3(e, a, b, c, d, 51);
|
||||
R3(d, e, a, b, c, 52);
|
||||
R3(c, d, e, a, b, 53);
|
||||
R3(b, c, d, e, a, 54);
|
||||
R3(a, b, c, d, e, 55);
|
||||
R3(e, a, b, c, d, 56);
|
||||
R3(d, e, a, b, c, 57);
|
||||
R3(c, d, e, a, b, 58);
|
||||
R3(b, c, d, e, a, 59);
|
||||
R4(a, b, c, d, e, 60);
|
||||
R4(e, a, b, c, d, 61);
|
||||
R4(d, e, a, b, c, 62);
|
||||
R4(c, d, e, a, b, 63);
|
||||
R4(b, c, d, e, a, 64);
|
||||
R4(a, b, c, d, e, 65);
|
||||
R4(e, a, b, c, d, 66);
|
||||
R4(d, e, a, b, c, 67);
|
||||
R4(c, d, e, a, b, 68);
|
||||
R4(b, c, d, e, a, 69);
|
||||
R4(a, b, c, d, e, 70);
|
||||
R4(e, a, b, c, d, 71);
|
||||
R4(d, e, a, b, c, 72);
|
||||
R4(c, d, e, a, b, 73);
|
||||
R4(b, c, d, e, a, 74);
|
||||
R4(a, b, c, d, e, 75);
|
||||
R4(e, a, b, c, d, 76);
|
||||
R4(d, e, a, b, c, 77);
|
||||
R4(c, d, e, a, b, 78);
|
||||
R4(b, c, d, e, a, 79);
|
||||
/* Add the working vars back into context.state[] */
|
||||
state[0] += a;
|
||||
state[1] += b;
|
||||
state[2] += c;
|
||||
state[3] += d;
|
||||
state[4] += e;
|
||||
/* Wipe variables */
|
||||
a = b = c = d = e = 0;
|
||||
#ifdef SHA1HANDSOFF
|
||||
memset(block, '\0', sizeof(block));
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/* SHA1Init - Initialize new context */
|
||||
|
||||
void SHA1Init(
|
||||
SHA1_CTX * context
|
||||
)
|
||||
{
|
||||
/* SHA1 initialization constants */
|
||||
context->state[0] = 0x67452301;
|
||||
context->state[1] = 0xEFCDAB89;
|
||||
context->state[2] = 0x98BADCFE;
|
||||
context->state[3] = 0x10325476;
|
||||
context->state[4] = 0xC3D2E1F0;
|
||||
context->count[0] = context->count[1] = 0;
|
||||
}
|
||||
|
||||
|
||||
/* Run your data through this. */
|
||||
|
||||
void SHA1Update(
|
||||
SHA1_CTX * context,
|
||||
const unsigned char *data,
|
||||
uint32_t len
|
||||
)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
uint32_t j;
|
||||
|
||||
j = context->count[0];
|
||||
if ((context->count[0] += len << 3) < j)
|
||||
context->count[1]++;
|
||||
context->count[1] += (len >> 29);
|
||||
j = (j >> 3) & 63;
|
||||
if ((j + len) > 63)
|
||||
{
|
||||
memcpy(&context->buffer[j], data, (i = 64 - j));
|
||||
SHA1Transform(context->state, context->buffer);
|
||||
for (; i + 63 < len; i += 64)
|
||||
{
|
||||
SHA1Transform(context->state, &data[i]);
|
||||
}
|
||||
j = 0;
|
||||
}
|
||||
else
|
||||
i = 0;
|
||||
memcpy(&context->buffer[j], &data[i], len - i);
|
||||
}
|
||||
|
||||
|
||||
/* Add padding and return the message digest. */
|
||||
|
||||
void SHA1Final(
|
||||
unsigned char digest[20],
|
||||
SHA1_CTX * context
|
||||
)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
unsigned char finalcount[8];
|
||||
|
||||
unsigned char c;
|
||||
|
||||
#if 0 /* untested "improvement" by DHR */
|
||||
/* Convert context->count to a sequence of bytes
|
||||
* in finalcount. Second element first, but
|
||||
* big-endian order within element.
|
||||
* But we do it all backwards.
|
||||
*/
|
||||
unsigned char *fcp = &finalcount[8];
|
||||
|
||||
for (i = 0; i < 2; i++)
|
||||
{
|
||||
uint32_t t = context->count[i];
|
||||
|
||||
int j;
|
||||
|
||||
for (j = 0; j < 4; t >>= 8, j++)
|
||||
*--fcp = (unsigned char) t}
|
||||
#else
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
finalcount[i] = (unsigned char) ((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */
|
||||
}
|
||||
#endif
|
||||
c = 0200;
|
||||
SHA1Update(context, &c, 1);
|
||||
while ((context->count[0] & 504) != 448)
|
||||
{
|
||||
c = 0000;
|
||||
SHA1Update(context, &c, 1);
|
||||
}
|
||||
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);
|
||||
}
|
||||
/* 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)
|
||||
{
|
||||
SHA1_CTX ctx;
|
||||
size_t ii;
|
||||
|
||||
SHA1Init(&ctx);
|
||||
for (ii=0; ii<len; ii+=1)
|
||||
SHA1Update(&ctx, str + ii, 1);
|
||||
SHA1Final(hash_out, &ctx);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
|
||||
static inline char nibble_to_char(uint8_t n)
|
||||
{
|
||||
if(n >= 10)
|
||||
return n + 87;
|
||||
else
|
||||
return n + 48;
|
||||
}
|
||||
|
||||
std::string sha1_hex(const std::string & data)
|
||||
{
|
||||
uint8_t digest[20];
|
||||
const uint8_t * ptr = (uint8_t*) data.c_str();
|
||||
sha1(digest, ptr, data.size());
|
||||
std::string out;
|
||||
std::size_t idx = 0;
|
||||
while(idx < 20)
|
||||
{
|
||||
out += nibble_to_char((digest[idx] & 0xf0) >> 8) + nibble_to_char(digest[idx] & 0x0f);
|
||||
++idx;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
}
|
11
contrib/backends/nntpchan-daemon/libnntpchan/sha1.hpp
Normal file
11
contrib/backends/nntpchan-daemon/libnntpchan/sha1.hpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef NNTPCHAN_SHA1_HPP
|
||||
#define NNTPCHAN_SHA1_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
std::string sha1_hex(const std::string & data);
|
||||
}
|
||||
|
||||
#endif
|
@@ -0,0 +1,108 @@
|
||||
#include "staticfile_frontend.hpp"
|
||||
#include "file_handle.hpp"
|
||||
#include "sanitize.hpp"
|
||||
#include "mime.hpp"
|
||||
#include "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
|
||||
|
||||
// render templates
|
||||
if(m_TemplateEngine)
|
||||
{
|
||||
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);
|
||||
|
||||
std::string msgid_hash = sha1_hex(msgid->second);
|
||||
|
||||
fs::path threadFilePath = m_OutDir / fs::path("thread-" + msgid_hash + ".html");
|
||||
FileHandle_ptr out = OpenFile(threadFilePath, eWrite);
|
||||
if(!m_TemplateEngine->WriteTemplate("thread.mustache", thread_args, out))
|
||||
{
|
||||
std::clog << "failed to write " << threadFilePath << std::endl;
|
||||
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, ' '))
|
||||
{
|
||||
newsgroups_list.insert(NNTPSanitize(newsgroup));
|
||||
}
|
||||
|
||||
for(const auto & name : newsgroups_list)
|
||||
{
|
||||
auto board = GetThreadsPaginated(name, 10, m_Pages);
|
||||
uint32_t pageno = 0;
|
||||
for(Threads_t threads : board)
|
||||
{
|
||||
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");
|
||||
out = OpenFile(m_OutDir / boardPageFilename, eWrite);
|
||||
m_TemplateEngine->WriteTemplate("board.mustache", board_args, out);
|
||||
|
||||
++pageno;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
StaticFileFrontend::BoardPage_t StaticFileFrontend::GetThreadsPaginated(const std::string & group, uint32_t perpage, uint32_t pages)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
#ifndef NNTPCHAN_STATICFILE_FRONTEND_HPP
|
||||
#define NNTPCHAN_STATICFILE_FRONTEND_HPP
|
||||
#include "frontend.hpp"
|
||||
#include "template_engine.hpp"
|
||||
#include "model.hpp"
|
||||
#include <experimental/filesystem>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
|
||||
namespace fs = std::experimental::filesystem;
|
||||
|
||||
class StaticFileFrontend : public Frontend
|
||||
{
|
||||
public:
|
||||
|
||||
StaticFileFrontend(TemplateEngine * tmpl, const std::string & templateDir, const std::string & outDir, uint32_t pages);
|
||||
|
||||
~StaticFileFrontend();
|
||||
|
||||
void ProcessNewMessage(const fs::path & fpath);
|
||||
bool AcceptsNewsgroup(const std::string & newsgroup) { (void) newsgroup; return true; }
|
||||
bool AcceptsMessage(const std::string & msgid) { (void) msgid; return true; }
|
||||
|
||||
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:
|
||||
|
||||
TemplateEngine_ptr m_TemplateEngine;
|
||||
fs::path m_TemplateDir;
|
||||
fs::path m_OutDir;
|
||||
uint32_t m_Pages;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
47
contrib/backends/nntpchan-daemon/libnntpchan/storage.cpp
Normal file
47
contrib/backends/nntpchan-daemon/libnntpchan/storage.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "storage.hpp"
|
||||
#include "sanitize.hpp"
|
||||
#include <sstream>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
ArticleStorage::ArticleStorage()
|
||||
{
|
||||
}
|
||||
|
||||
ArticleStorage::ArticleStorage(const fs::path & fpath) {
|
||||
SetPath(fpath);
|
||||
}
|
||||
|
||||
ArticleStorage::~ArticleStorage()
|
||||
{
|
||||
}
|
||||
|
||||
void ArticleStorage::SetPath(const fs::path & fpath)
|
||||
{
|
||||
basedir = fpath;
|
||||
fs::create_directories(basedir);
|
||||
}
|
||||
|
||||
bool ArticleStorage::Accept(const std::string& msgid)
|
||||
{
|
||||
if (!IsValidMessageID(msgid)) return false;
|
||||
auto p = MessagePath(msgid);
|
||||
return !fs::exists(p);
|
||||
}
|
||||
|
||||
fs::path ArticleStorage::MessagePath(const std::string & msgid)
|
||||
{
|
||||
return basedir / msgid;
|
||||
}
|
||||
|
||||
FileHandle_ptr ArticleStorage::OpenRead(const std::string & msgid)
|
||||
{
|
||||
return OpenFile(MessagePath(msgid), eRead);
|
||||
}
|
||||
|
||||
FileHandle_ptr ArticleStorage::OpenWrite(const std::string & msgid)
|
||||
{
|
||||
return OpenFile(MessagePath(msgid), eWrite);
|
||||
}
|
||||
|
||||
}
|
40
contrib/backends/nntpchan-daemon/libnntpchan/storage.hpp
Normal file
40
contrib/backends/nntpchan-daemon/libnntpchan/storage.hpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef NNTPCHAN_STORAGE_HPP
|
||||
#define NNTPCHAN_STORAGE_HPP
|
||||
|
||||
#include <experimental/filesystem>
|
||||
#include <string>
|
||||
#include "file_handle.hpp"
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
|
||||
namespace fs = std::experimental::filesystem;
|
||||
|
||||
class ArticleStorage
|
||||
{
|
||||
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);
|
||||
|
||||
/**
|
||||
return true if we should accept a new message give its message id
|
||||
*/
|
||||
bool Accept(const std::string & msgid);
|
||||
|
||||
private:
|
||||
|
||||
fs::path MessagePath(const std::string & msgid);
|
||||
|
||||
fs::path basedir;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif
|
@@ -0,0 +1,35 @@
|
||||
#include "template_engine.hpp"
|
||||
#include "sanitize.hpp"
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
|
||||
struct MustacheTemplateEngine : public TemplateEngine
|
||||
{
|
||||
struct Impl
|
||||
{
|
||||
bool RenderFile(const std::string & fname, const Args_t & args, const FileHandle_ptr & out)
|
||||
{
|
||||
auto file = OpenFile(fname, eRead);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
virtual bool WriteTemplate(const std::string & fname, const Args_t & args, const FileHandle_ptr & out)
|
||||
{
|
||||
auto impl = std::make_unique<Impl>();
|
||||
return impl->RenderFile(fname, args, out);
|
||||
}
|
||||
};
|
||||
|
||||
TemplateEngine * CreateTemplateEngine(const std::string & dialect)
|
||||
{
|
||||
auto d = ToLower(dialect);
|
||||
if(d == "mustache")
|
||||
return new MustacheTemplateEngine;
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
#ifndef NNTPCHAN_TEMPLATE_ENGINE_HPP
|
||||
#define NNTPCHAN_TEMPLATE_ENGINE_HPP
|
||||
#include "file_handle.hpp"
|
||||
#include <any>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace nntpchan
|
||||
{
|
||||
|
||||
struct TemplateEngine
|
||||
{
|
||||
using Args_t = std::map<std::string, std::any>;
|
||||
virtual bool WriteTemplate(const std::string & template_fname, const Args_t & args, const FileHandle_ptr & out) = 0;
|
||||
};
|
||||
|
||||
TemplateEngine * CreateTemplateEngine(const std::string & dialect);
|
||||
|
||||
typedef std::unique_ptr<TemplateEngine> TemplateEngine_ptr;
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user