Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.
nntpchan/contrib/backends/nntpchan-daemon/libnntpchan/nntp_server.cpp

86 lines
1.6 KiB
C++
Raw Normal View History

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
}
2017-05-03 18:15:06 +05:00
IServerConn * NNTPServer::CreateConn(uv_stream_t * s)
2016-10-15 18:12:01 +05:00
{
CredDB_ptr creds;
2016-10-15 22:53:35 +05:00
std::ifstream i;
i.open(m_logindbpath);
if(i.is_open()) creds = std::make_shared<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;
}
void NNTPServer::SetFrontend(Frontend * f)
2017-05-03 22:33:04 +05:00
{
m_frontend.reset(f);
2017-05-03 22:33:04 +05:00
}
std::string NNTPServer::InstanceName() const
2016-10-18 17:17:40 +05:00
{
return m_servername;
2016-10-18 17:17:40 +05:00
}
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 + "\r\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
}