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/src/nntp_handler.cpp

124 lines
2.8 KiB
C++
Raw Normal View History

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) :
2017-05-03 17:09:23 +05:00
LineReader(1024),
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");
}
}
2017-05-03 18:44:42 +05:00
void NNTPServerHandler::OnData(const char * data, ssize_t l)
{
Data(data, l);
}
2016-10-15 21:37:59 +05:00
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");
}
2017-05-03 18:15:06 +05:00
bool NNTPServerHandler::ShouldClose()
2016-10-15 21:37:59 +05:00
{
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
}