Rewrote config file management

This commit is contained in:
tastytea 2018-05-27 16:08:49 +02:00
parent 6dbddb6df2
commit 7938cfb30b
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
7 changed files with 209 additions and 56 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7)
project (expandurl-mastodon
VERSION 0.7.4
VERSION 0.8.0
LANGUAGES CXX
)
@ -9,6 +9,7 @@ find_package(CURL REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(CURLPP REQUIRED curlpp)
pkg_check_modules(JSONCPP REQUIRED jsoncpp)
pkg_check_modules(LIBXDG_BASEDIR REQUIRED libxdg-basedir)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
@ -27,10 +28,12 @@ include_directories(${PROJECT_BINARY_DIR})
include_directories(${CURL_INCLUDE_DIRS})
include_directories(${CURLPP_INCLUDE_DIRS})
include_directories(${JSONCPP_INCLUDE_DIRS})
include_directories(${LIBXDG_BASEDIR_INCLUDE_DIRS})
link_directories(${CURL_LIBRARY_DIRS})
link_directories(${CURLPP_LIBRARY_DIRS})
link_directories(${JSONCPP_LIBRARY_DIRS})
link_directories(${LIBXDG_BASEDIR_LIBRARY_DIRS})
# Write version in header
configure_file (
@ -42,5 +45,5 @@ file(GLOB sources src/*.cpp)
add_executable(expandurl-mastodon ${sources})
target_link_libraries(expandurl-mastodon
${CURLPP_LIBRARIES} ${JSONCPP_LIBRARIES}
mastodon-cpp pthread)
${LIBXDG_BASEDIR_LIBRARIES} mastodon-cpp pthread stdc++fs)
install(TARGETS expandurl-mastodon DESTINATION ${CMAKE_INSTALL_BINDIR})

View File

@ -27,6 +27,7 @@ or to [@tastytea@soc.ialis.me](https://soc.ialis.me/@tastytea).
* [curlpp](http://www.curlpp.org/) (tested: 0.8.1)
* [mastodon-cpp](https://github.com/tastytea/mastodon-cpp) (at least: 0.15.1)
* [jsoncpp](https://github.com/open-source-parsers/jsoncpp) (tested: 1.8.4)
* [libxdg-basedir](http://repo.or.cz/w/libxdg-basedir.git) (tested: 1.2.0)
## Get sourcecode

88
src/configjson.cpp Normal file
View File

@ -0,0 +1,88 @@
/* This file is part of expandurl-mastodon.
* Copyright © 2018 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 <fstream>
#include <experimental/filesystem>
#include <basedir.h>
#include <sstream>
#include "configjson.hpp"
using std::string;
namespace fs = std::experimental::filesystem;
ConfigJSON::ConfigJSON(const string &filename, const string &subdir)
: _json()
{
xdgHandle xdg;
xdgInitHandle(&xdg);
_filepath = xdgConfigHome(&xdg);
xdgWipeHandle(&xdg);
if (!subdir.empty())
{
_filepath += '/' + subdir;
if (!fs::exists(_filepath))
{
fs::create_directory(_filepath);
}
}
_filepath += '/' + filename;
}
const bool ConfigJSON::read()
{
std::ifstream file(_filepath);
if (file.is_open())
{
std::stringstream config;
config << file.rdbuf();
file.close();
config >> _json;
return true;
}
else
{
return false;
}
}
const bool ConfigJSON::write()
{
std::ofstream file(_filepath);
if (file.is_open())
{
const string config = _json.toStyledString();
file.write(config.c_str(), config.length());
file.close();
return true;
}
else
{
return false;
}
}
Json::Value &ConfigJSON::get_json()
{
return _json;
}
const string ConfigJSON::get_filepath() const
{
return _filepath;
}

82
src/configjson.hpp Normal file
View File

@ -0,0 +1,82 @@
/* This file is part of expandurl-mastodon.
* Copyright © 2018 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 CONFIGJSON_HPP
#define CONFIGJSON_HPP
#include <string>
#include <jsoncpp/json/json.h>
using std::string;
class ConfigJSON
{
public:
/*!
* @brief Checks if subdir is present, creates it if necessary
*
* Example:
* @code
* ConfigJSON config("test.json", "subdirectory");
* @endcode
*
* @param filename The name of the file, including extension
* @param subdir The subdir (optional)
*/
explicit ConfigJSON(const string &filename, const string &subdir = "");
/*!
* @brief Read the file
*
* @return `true` on success
*/
const bool read();
/*!
* @brief Write the file
*
* @return `true` on success
*/
const bool write();
/*!
* @brief Returns a reference to the config as Json::Value
*
* Example:
* @code
* Json::Value &json = config.get_json();
* @endcode
*/
Json::Value &get_json();
/*!
* @brief Gets the complete filepath
*/
const string get_filepath() const;
private:
/*!
* Holds the contents of the JSON file
*/
Json::Value _json;
/*!
* Complete filepath
*/
string _filepath;
};
#endif // CONFIGJSON_HPP

View File

@ -25,11 +25,14 @@
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/all.hpp>
#include <jsoncpp/json/json.h>
#include "configjson.hpp"
using std::string;
using Mastodon::API;
using Mastodon::Easy;
extern ConfigJSON configfile;
void signal_handler(int signum);
@ -92,13 +95,12 @@ private:
std::unique_ptr<API::http> _ptr;
std::thread _thread;
bool _running;
Json::Value _config;
const string _configfilepath;
string _proxy;
string _proxy_user;
string _proxy_password;
Json::Value &_config;
const bool read_config();
const void read_config();
const bool write_config();
const bool register_app();
const void set_proxy(Easy &masto);

View File

@ -21,12 +21,14 @@
#include <syslog.h>
#include <unistd.h> // getuid()
#include <curlpp/cURLpp.hpp>
#include "configjson.hpp"
#include "expandurl-mastodon.hpp"
using std::string;
using Mastodon::Easy;
bool running = true;
ConfigJSON configfile("expandurl-mastodon.json");
void signal_handler(int signum)
{
@ -50,6 +52,11 @@ void signal_handler(int signum)
int main(int argc, char *argv[])
{
if (!configfile.read())
{
syslog(LOG_WARNING, "Could not open %s.", configfile.get_filepath().c_str());
}
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
curlpp::initialize();

View File

@ -32,30 +32,22 @@ Listener::Listener()
, _stream("")
, _ptr(nullptr)
, _running(false)
, _configfilepath(static_cast<const string>(getenv("HOME")) +
"/.config/expandurl-mastodon.json")
, _proxy("")
, _proxy_user("")
, _proxy_password("")
, _config(configfile.get_json())
{
if (read_config())
read_config();
if (_config["access_token"].isNull())
{
_masto = std::make_unique<Easy>(_instance, _access_token);
_masto->set_useragent(static_cast<const string>("expandurl-mastodon/") +
global::version);
set_proxy(*_masto);
}
else
{
syslog(LOG_WARNING, "Could not open %s.", _configfilepath.c_str());
syslog(LOG_INFO, "Attempting to register application and write config file.");
if (register_app())
{
syslog(LOG_INFO, "Registration successful.");
if (!write_config())
if (!configfile.write())
{
syslog(LOG_ERR, "Could not write %s.", _configfilepath.c_str());
syslog(LOG_ERR, "Could not write %s.",
configfile.get_filepath().c_str());
std::exit(1);
}
}
@ -65,52 +57,30 @@ Listener::Listener()
std::exit(2);
}
}
_masto = std::make_unique<Easy>(_instance, _access_token);
_masto->set_useragent(static_cast<const string>("expandurl-mastodon/") +
global::version);
set_proxy(*_masto);
}
Listener::~Listener()
{
if (!write_config())
if (!configfile.write())
{
syslog(LOG_ERR, "Could not write %s.", _configfilepath.c_str());
syslog(LOG_ERR, "Could not write %s.",
configfile.get_filepath().c_str());
}
}
const bool Listener::read_config()
const void Listener::read_config()
{
std::ifstream file(_configfilepath);
if (file.is_open())
{
std::stringstream json;
json << file.rdbuf();
file.close();
json >> _config;
_instance = _config["account"].asString();
_instance = _instance.substr(_instance.find('@') + 1);
_access_token = _config["access_token"].asString();
_proxy = _config["proxy"]["url"].asString();
_proxy_user = _config["proxy"]["user"].asString();
_proxy_password = _config["proxy"]["password"].asString();
return true;
}
return false;
}
const bool Listener::write_config()
{
std::ofstream outfile(_configfilepath);
if (outfile.is_open())
{
outfile.write(_config.toStyledString().c_str(),
_config.toStyledString().length());
outfile.close();
return true;
}
return false;
_instance = _config["account"].asString();
_instance = _instance.substr(_instance.find('@') + 1);
_access_token = _config["access_token"].asString();
_proxy = _config["proxy"]["url"].asString();
_proxy_user = _config["proxy"]["user"].asString();
_proxy_password = _config["proxy"]["password"].asString();
}
const void Listener::start()