Print --help, --version and implement downloading.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
tastytea 2019-12-20 01:14:23 +01:00
parent 39ad18efc0
commit 7bba87575f
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
6 changed files with 329 additions and 8 deletions

View File

@ -2,7 +2,7 @@
:doctype: manpage
:Author: tastytea
:Email: tastytea@tastytea.de
:Date: 2019-12-16
:Date: 2019-12-20
:Revision: 0.0.0
:man source: mastorss
:man manual: General Commands Manual
@ -13,19 +13,44 @@ mastorss - Another RSS to Mastodon bot.
== SYNOPSIS
*mastorss* profile
*mastorss* [--help|--version] <profile>
// == DESCRIPTION
// == OPTIONS
== OPTIONS
*--help*::
Show help message.
*--version*::
Show version, copyright and license.
// == EXAMPLES
// == PROTOCOL SUPPORT
== PROTOCOL SUPPORT
// Currently only HTTP and HTTPS are supported.
Currently only HTTP and HTTPS are supported.
// == PROXY SUPPORT
// == PROXY SERVERS
// Since mastorss is built on libcurl, it respects the same proxy environment
// variables. See *curl*(1), section _ENVIRONMENT_.
// .Tunnel connections through tor.
// ================================================================================
// [source,shell]
// --------------------------------------------------------------------------------
// ALL_PROXY="socks4a://[::1]:9050" mastorss example
// --------------------------------------------------------------------------------
// ================================================================================
== PROXY SERVERS
mastorss supports HTTP proxies set via the environment variable
_http_proxy_. Accepted formats are: _\http://[user[:password]@]host[:port]/_ or
_[user[:password]@]host[:port]_. No SOCKS proxy support yet, sorry.
Example: `http_proxy="http://localhost:3128/" mastorss`
== FILES
@ -39,12 +64,13 @@ mastorss - Another RSS to Mastodon bot.
|===========================================================
| Code | Explanation
| 1 | Could not send post for unknown reasons.
| 1 | No profile specified.
| 2 | Network error.
|===========================================================
== SEE ALSO
*crontab*(1), *crontab*(5)
*curl*(1), *crontab*(1), *crontab*(5)
== REPORTING BUGS

75
src/document.cpp Normal file
View File

@ -0,0 +1,75 @@
/* This file is part of mastorss.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program 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, version 3.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "document.hpp"
#include "exceptions.hpp"
#include "version.hpp"
#include <restclient-cpp/connection.h>
#include <restclient-cpp/restclient.h>
#include <string>
#include <utility>
using namespace mastorss;
using std::string;
using std::move;
Document::Document(string uri)
: _uri{move(uri)}
{
RestClient::init();
download();
}
Document::~Document()
{
RestClient::disable();
}
void Document::download()
{
RestClient::Connection connection(_uri);
connection.SetUserAgent(string("mastorss/").append(version));
connection.FollowRedirects(true, 10);
RestClient::Response response{connection.get("")};
switch (response.code)
{
case 200:
{
_raw_doc = response.body;
break;
}
case 301:
case 308:
{
// TODO(tastytea): Handle permanent redirections.
throw std::runtime_error{"Permanent redirect, "
"no solution implemented yet."};
}
case -1:
{
throw CURLException{errno};
}
default:
{
throw HTTPException{response.code};
}
}
}

44
src/document.hpp Normal file
View File

@ -0,0 +1,44 @@
/* This file is part of mastorss.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program 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, version 3.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MASTORSS_DOCUMENT_HPP
#define MASTORSS_DOCUMENT_HPP
#include <string>
namespace mastorss
{
using std::string;
class Document
{
public:
explicit Document(string uri);
~Document();
Document(const Document &other) = default;
Document &operator=(const Document &other) = delete;
Document(Document &&other) = default;
Document &operator=(Document &&other) = delete;
void download();
private:
const string _uri;
string _raw_doc;
};
} // namespace mastorss
#endif // MASTORSS_DOCUMENT_HPP

43
src/exceptions.cpp Normal file
View File

@ -0,0 +1,43 @@
/* This file is part of mastorss.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program 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, version 3.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "exceptions.hpp"
#include <string>
using namespace mastorss;
using std::string;
using std::to_string;
HTTPException::HTTPException(const int error)
: error_code{static_cast<uint16_t>(error)}
{}
const char *HTTPException::what() const noexcept
{
static const string error_string{"HTTP error: " + to_string(error_code)};
return error_string.c_str();
}
CURLException::CURLException(const int error)
: error_code{static_cast<uint16_t>(error)}
{}
const char *CURLException::what() const noexcept
{
static const string error_string{"libCURL error: " + to_string(error_code)};
return error_string.c_str();
}

49
src/exceptions.hpp Normal file
View File

@ -0,0 +1,49 @@
/* This file is part of mastorss.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
*
* This program 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, version 3.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MASTORSS_EXCEPTIONS_HPP
#define MASTORSS_EXCEPTIONS_HPP
#include <cstdint>
#include <exception>
namespace mastorss
{
using std::uint16_t;
using std::exception;
class HTTPException : public exception
{
public:
const uint16_t error_code;
explicit HTTPException(const int error);
virtual const char *what() const noexcept;
};
class CURLException : public exception
{
public:
const uint16_t error_code;
explicit CURLException(const int error);
virtual const char *what() const noexcept;
};
} // namespace mastorss
#endif // MASTORSS_EXCEPTIONS_HPP

84
src/main.cpp Normal file
View File

@ -0,0 +1,84 @@
#include "version.hpp"
#include "document.hpp"
#include "exceptions.hpp"
#include <iostream>
#include <string_view>
#include <vector>
using namespace mastorss;
using std::cout;
using std::cerr;
using std::string_view;
using std::vector;
namespace mastorss
{
namespace error
{
constexpr int noprofile = 1;
constexpr int network = 2;
} // namespace error
void print_version();
void print_help(const string_view &command);
void print_version()
{
cout << "mastorss " << version << "\n"
"Copyright (C) 2019 tastytea <tastytea@tastytea.de>\n"
"License GPLv3: GNU GPL version 3 "
"<https://www.gnu.org/licenses/gpl-3.0.html>.\n"
"This program comes with ABSOLUTELY NO WARRANTY. "
"This is free software,\n"
"and you are welcome to redistribute it under certain conditions.\n";
}
void print_help(const string_view &command)
{
cerr << "Usage: " << command << " [--version] <profile>\n";
}
} // namespace mastorss
int main(int argc, char *argv[])
{
const vector<string_view> args(argv, argv + argc);
if (args.size() == 1)
{
print_help(args[0]);
return error::noprofile;
}
if (args.size() > 1)
{
if (args[1] == "--version")
{
print_version();
}
else if (args[1] == "--help")
{
print_help(args[0]);
}
else
{
try
{
Document doc("https://ip.tastytea.de/");
}
catch (const HTTPException &e)
{
cerr << e.what() << '\n';
return error::network;
}
catch (const CURLException &e)
{
cerr << e.what() << '\n';
return error::network;
}
}
}
return 0;
}