UsenetSearch/include/usenetsearch/UsenetClient.h

98 lines
2.6 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 <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
{
};
struct NntpMessage
{
std::uint16_t code;
std::wstring message;
};
struct NntpListEntry
{
std::wstring name;
std::uint64_t high;
std::uint64_t low;
std::uint64_t count;
std::wstring status;
};
std::ostream& operator<<(std::ofstream& out, const NntpListEntry& obj);
std::ifstream& operator>>(std::ifstream& in, NntpListEntry& obj);
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::wstring ReadUntil(const std::wstring& deliminator);
public:
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& groupName);
std::unique_ptr<std::vector<NntpListEntry>> List();
/* whilst message id's are typically numbers, the rfc states they are unique
alphanumeric strings. */
std::unique_ptr<std::vector<std::wstring>> ListGroup(
const std::wstring& newsGroup
);
// use the ListGroup <newsgroup> command to get a list of article id's in a newsgroup
// then use the HEAD <article id> command to get the headers for each article
};
} // namespace usenetsearch