UsenetSearch/include/usenetsearch/UsenetClient.h

121 lines
2.9 KiB
C++

/*
Copyright© 2021 John Sennesael
UsenetSearch is Free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UsenetSearch is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "usenetsearch/SSLConnection.h"
#include "usenetsearch/TcpConnection.h"
#include <cstdint>
#include <codecvt>
#include <fstream>
#include <functional>
#include <limits>
#include <locale>
#include <memory>
#include <string>
#include <vector>
namespace usenetsearch {
class Application;
struct UsenetClientException: public UsenetSearchException
{
UsenetClientException(int errorCode, const std::string& message):
UsenetSearchException(errorCode, message){}
virtual ~UsenetClientException() = default;
};
struct NntpHeader
{
std::uint64_t articleID{0};
std::string subject;
/*
There's a lot more headers, but trying to limit it to the fields we care
to search to save on the storage space per article (there's going to be
a LOT of them).
*/
};
typedef std::vector<NntpHeader> NntpHeaders;
struct NntpMessage
{
std::uint16_t code{0};
std::string message;
};
struct NntpListEntry
{
static constexpr std::uint32_t NOT_INDEXED{
std::numeric_limits<std::uint32_t>::max()
};
std::uint64_t id{0};
std::uint32_t lastIndexedArticle{NOT_INDEXED};
std::uint64_t count{0};
std::uint64_t high{0};
std::uint64_t low{0};
std::string name;
std::string status;
};
class UsenetClient
{
Application& m_app;
std::unique_ptr<SSLConnection> m_ssl;
std::unique_ptr<TcpConnection> m_tcp;
bool m_useSSL{false};
bool IsError(const NntpMessage& msg) const;
NntpMessage ReadLine();
void Write(const std::wstring& message);
std::string ReadUntil(const std::string& deliminator);
public:
UsenetClient(Application& app);
void Authenticate(const std::wstring& user, const std::wstring& password);
void Connect(
const std::string& host,
std::uint16_t port,
bool useSSL = false
);
void Group(const std::wstring& newsgroup);
NntpHeader Head(std::uint64_t articleID);
void ProcessHeaders(
std::uint64_t startMessage,
std::function<void(std::shared_ptr<NntpHeaders>)> processFn,
std::uint64_t batchSize=100
);
std::unique_ptr<std::vector<NntpListEntry>> List();
std::unique_ptr<std::vector<std::uint64_t>> ListGroup(
const std::wstring& newsGroup
);
};
} // namespace usenetsearch