2017-05-03 18:15:06 +05:00
|
|
|
|
2016-10-15 18:12:01 +05:00
|
|
|
#include "nntp_server.hpp"
|
2016-10-15 22:53:35 +05:00
|
|
|
#include "nntp_auth.hpp"
|
2017-05-03 18:15:06 +05:00
|
|
|
#include "nntp_handler.hpp"
|
2016-10-15 18:12:01 +05:00
|
|
|
#include "net.hpp"
|
|
|
|
#include <cassert>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
namespace nntpchan
|
|
|
|
{
|
2017-05-03 18:15:06 +05:00
|
|
|
|
|
|
|
NNTPServer::NNTPServer(uv_loop_t * loop) : Server(loop), m_frontend(nullptr) {}
|
2016-10-15 18:12:01 +05:00
|
|
|
|
|
|
|
NNTPServer::~NNTPServer()
|
|
|
|
{
|
2016-10-18 17:17:40 +05:00
|
|
|
if (m_frontend) delete m_frontend;
|
|
|
|
}
|
|
|
|
|
2017-05-03 18:15:06 +05:00
|
|
|
IServerConn * NNTPServer::CreateConn(uv_stream_t * s)
|
2016-10-15 18:12:01 +05:00
|
|
|
{
|
2016-10-15 22:53:35 +05:00
|
|
|
NNTPCredentialDB * creds = nullptr;
|
|
|
|
|
|
|
|
std::ifstream i;
|
|
|
|
i.open(m_logindbpath);
|
|
|
|
if(i.is_open()) creds = new HashedFileDB(m_logindbpath);
|
2017-05-03 17:09:23 +05:00
|
|
|
|
2017-05-03 18:15:06 +05:00
|
|
|
NNTPServerHandler * handler = new NNTPServerHandler(m_storagePath);
|
|
|
|
if(creds)
|
|
|
|
handler->SetAuth(creds);
|
2016-10-15 18:12:01 +05:00
|
|
|
|
2017-05-03 18:15:06 +05:00
|
|
|
NNTPServerConn * conn = new NNTPServerConn(GetLoop(), s, this, handler);
|
|
|
|
return conn;
|
|
|
|
}
|
2016-10-15 18:12:01 +05:00
|
|
|
|
2016-10-15 22:53:35 +05:00
|
|
|
void NNTPServer::SetLoginDB(const std::string path)
|
|
|
|
{
|
|
|
|
m_logindbpath = path;
|
|
|
|
}
|
2017-05-03 17:09:23 +05:00
|
|
|
|
2016-10-15 22:53:35 +05:00
|
|
|
|
2016-10-15 18:12:01 +05:00
|
|
|
void NNTPServer::SetStoragePath(const std::string & path)
|
|
|
|
{
|
|
|
|
m_storagePath = path;
|
|
|
|
}
|
2016-10-18 17:17:40 +05:00
|
|
|
|
2017-05-03 22:33:04 +05:00
|
|
|
void NNTPServer::SetInstanceName(const std::string & name)
|
|
|
|
{
|
|
|
|
m_servername = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string NNTPServer::InstanceName() const
|
|
|
|
{
|
|
|
|
return m_servername;
|
|
|
|
}
|
|
|
|
|
2016-10-18 17:17:40 +05:00
|
|
|
void NNTPServer::SetFrontend(Frontend * f)
|
|
|
|
{
|
|
|
|
if(m_frontend) delete m_frontend;
|
|
|
|
m_frontend = f;
|
|
|
|
}
|
2017-05-03 17:09:23 +05:00
|
|
|
|
2017-05-03 18:15:06 +05:00
|
|
|
void NNTPServer::OnAcceptError(int status)
|
2016-10-15 18:12:01 +05:00
|
|
|
{
|
2017-05-03 18:15:06 +05:00
|
|
|
std::cerr << "nntpserver::accept() " << uv_strerror(status) << std::endl;
|
2016-10-15 18:12:01 +05:00
|
|
|
}
|
2017-05-03 17:09:23 +05:00
|
|
|
|
2016-10-15 18:12:01 +05:00
|
|
|
void NNTPServerConn::SendNextReply()
|
|
|
|
{
|
2017-05-03 18:15:06 +05:00
|
|
|
IConnHandler * handler = GetHandler();
|
2017-05-03 18:44:42 +05:00
|
|
|
while(handler->HasNextLine()) {
|
2017-05-03 18:15:06 +05:00
|
|
|
auto line = handler->GetNextLine();
|
|
|
|
SendString(line + "\n");
|
2016-10-15 18:12:01 +05:00
|
|
|
}
|
|
|
|
}
|
2016-10-15 21:37:59 +05:00
|
|
|
|
|
|
|
|
|
|
|
void NNTPServerConn::Greet()
|
2016-10-15 18:12:01 +05:00
|
|
|
{
|
2017-05-03 18:15:06 +05:00
|
|
|
IConnHandler * handler = GetHandler();
|
|
|
|
handler->Greet();
|
2016-10-15 22:53:35 +05:00
|
|
|
SendNextReply();
|
2016-10-15 18:12:01 +05:00
|
|
|
}
|
2017-05-03 17:09:23 +05:00
|
|
|
|
2016-10-15 18:12:01 +05:00
|
|
|
|
2017-05-03 18:15:06 +05:00
|
|
|
|
2016-10-15 18:12:01 +05:00
|
|
|
}
|