2016-10-15 21:37:59 +05:00
|
|
|
#include "nntp_handler.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace nntpchan
|
|
|
|
{
|
|
|
|
NNTPServerHandler::NNTPServerHandler(const std::string & storage) :
|
2016-10-15 22:53:35 +05:00
|
|
|
m_auth(nullptr),
|
2016-10-15 21:37:59 +05:00
|
|
|
m_store(storage),
|
|
|
|
m_authed(false),
|
|
|
|
m_state(eStateReadCommand)
|
|
|
|
{
|
|
|
|
}
|
2016-10-15 22:53:35 +05:00
|
|
|
|
|
|
|
NNTPServerHandler::~NNTPServerHandler()
|
|
|
|
{
|
|
|
|
if(m_auth) delete m_auth;
|
|
|
|
}
|
2017-04-14 15:24:53 +05:00
|
|
|
|
2016-10-15 21:37:59 +05:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NNTPServerHandler::HandleCommand(const std::deque<std::string> & command)
|
|
|
|
{
|
|
|
|
auto cmd = command[0];
|
|
|
|
std::transform(cmd.begin(), cmd.end(), cmd.begin(),
|
|
|
|
[](unsigned char ch) { return std::toupper(ch); });
|
|
|
|
std::size_t cmdlen = command.size();
|
|
|
|
std::cerr << "handle command [" << cmd << "] " << (int) cmdlen << std::endl;
|
|
|
|
if (cmd == "QUIT") {
|
|
|
|
Quit();
|
|
|
|
return;
|
|
|
|
} else if (cmd == "MODE" ) {
|
|
|
|
if(cmdlen == 1) {
|
|
|
|
// set mode
|
|
|
|
SwitchMode(command[1]);
|
|
|
|
} else if(cmdlen) {
|
|
|
|
// too many arguments
|
|
|
|
} else {
|
|
|
|
// get mode
|
|
|
|
}
|
2017-04-14 15:24:53 +05:00
|
|
|
|
2016-10-15 21:37:59 +05:00
|
|
|
} else {
|
|
|
|
// unknown command
|
|
|
|
QueueLine("500 Unknown Command");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NNTPServerHandler::SwitchMode(const std::string & mode)
|
|
|
|
{
|
2017-04-14 15:24:53 +05:00
|
|
|
if (mode == "READER") {
|
|
|
|
m_mode = mode;
|
|
|
|
if(PostingAllowed()) {
|
|
|
|
QueueLine("200 Posting is permitted yo");
|
|
|
|
} else {
|
|
|
|
QueueLine("201 Posting is not permitted yo");
|
|
|
|
}
|
|
|
|
} else if (mode == "STREAM") {
|
|
|
|
m_mode = mode;
|
|
|
|
if (PostingAllowed()) {
|
|
|
|
QueueLine("203 Streaming enabled");
|
|
|
|
} else {
|
|
|
|
QueueLine("483 Streaming Denied");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// unknown mode
|
|
|
|
QueueLine("500 Unknown mode");
|
|
|
|
}
|
2016-10-15 21:37:59 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
void NNTPServerHandler::Quit()
|
|
|
|
{
|
|
|
|
std::cerr << "quitting" << std::endl;
|
|
|
|
m_state = eStateQuit;
|
|
|
|
QueueLine("205 quitting");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NNTPServerHandler::Done()
|
|
|
|
{
|
|
|
|
return m_state == eStateQuit;
|
|
|
|
}
|
2016-10-15 22:53:35 +05:00
|
|
|
|
|
|
|
bool NNTPServerHandler::PostingAllowed()
|
|
|
|
{
|
|
|
|
return m_authed || m_auth == nullptr;
|
|
|
|
}
|
2017-04-14 15:24:53 +05:00
|
|
|
|
2016-10-15 22:53:35 +05:00
|
|
|
void NNTPServerHandler::Greet()
|
|
|
|
{
|
2017-04-14 15:24:53 +05:00
|
|
|
if(PostingAllowed())
|
2016-10-15 22:53:35 +05:00
|
|
|
QueueLine("200 Posting allowed");
|
|
|
|
else
|
|
|
|
QueueLine("201 Posting not allowed");
|
|
|
|
}
|
|
|
|
|
|
|
|
void NNTPServerHandler::SetAuth(NNTPCredentialDB *creds)
|
|
|
|
{
|
|
|
|
if(m_auth) delete m_auth;
|
|
|
|
m_auth = creds;
|
|
|
|
}
|
2016-10-15 21:37:59 +05:00
|
|
|
}
|