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

101 lines
2.2 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) :
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;
}
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
}
} else {
// unknown command
QueueLine("500 Unknown Command");
}
}
void NNTPServerHandler::SwitchMode(const std::string & mode)
{
2016-10-15 22:53:35 +05:00
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;
}
void NNTPServerHandler::Greet()
{
if(PostingAllowed())
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
}