UsenetSearch/include/usenetsearch/UsenetClient.h

119 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 <cstdint>
#include <codecvt>
#include <fstream>
#include <functional>
#include <locale>
#include <memory>
#include <string>
#include <vector>
#include "usenetsearch/SSLConnection.h"
#include "usenetsearch/TcpConnection.h"
namespace usenetsearch {
struct UsenetClientException: public UsenetSearchException
{
UsenetClientException(int errorCode, const std::string& message):
UsenetSearchException(errorCode, message){}
virtual ~UsenetClientException() = default;
};
struct NntpHeader
{
std::uint64_t articleID;
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;
std::string message;
};
struct NntpListEntry
{
std::string name;
std::uint64_t high;
std::uint64_t low;
std::uint64_t count;
std::string status;
};
class UsenetClient
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> m_conv;
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:
/* Expected flow:
* Connect
* Authenticate
* List() to get a list of newsgroups
* for every newsgroup:
* XZHDR subject 0-
* uncompress result.
*/
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