From b361753f51bb661aedb26bedacab5763bb37e200 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 20 May 2018 14:25:20 +0200 Subject: [PATCH] switched confuig file format from cfg to json --- CMakeLists.txt | 2 +- README.md | 11 +++++++--- src/expandurl-mastodon.hpp | 5 +++++ src/masto.cpp | 43 +++++++++++++++++++++++++++++--------- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 24b9021..f012776 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (expandurl-mastodon - VERSION 0.3.3 + VERSION 0.4.0 LANGUAGES CXX ) diff --git a/README.md b/README.md index bc86146..d021c6b 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ or to [@tastytea@soc.ialis.me](https://soc.ialis.me/@tastytea). * [cmake](https://cmake.org/) (tested: 3.9.6) * [curlpp](http://www.curlpp.org/) (tested: 0.8.1) * [mastodon-cpp](https://github.com/tastytea/mastodon-cpp) (at least: 0.13.1) + * [jsoncpp](https://github.com/open-source-parsers/jsoncpp) (tested: 1.8.4) ## Get sourcecode @@ -39,12 +40,16 @@ Install with `make install`. # Usage +**The config file has changed from cfg to JSON in 0.4.0.** + You will need to generate an access token yourself at the moment. Create a config file with your account and access token in -`${HOME}/.config/expandurl-mastodon.cfg`: +`${HOME}/.config/expandurl-mastodon.json`: - expandurl@example.social - abc123 + { + "account": "expandurl@example.social", + "access_tocken": "abc123" + } Now start expandurl-mastodon without parameters. diff --git a/src/expandurl-mastodon.hpp b/src/expandurl-mastodon.hpp index d5abdc6..64e0de3 100644 --- a/src/expandurl-mastodon.hpp +++ b/src/expandurl-mastodon.hpp @@ -24,6 +24,7 @@ #include #include #include +#include using std::string; using Mastodon::API; @@ -75,6 +76,7 @@ public: const void stop(); std::vector get_new_messages(); + const std::vector catchup(); Easy::Status get_status(const std::uint_fast64_t &id); const bool send_reply(const Easy::Status &to_status, const string &message); const std::uint_fast64_t get_parent_id(Easy::Notification ¬if); @@ -89,6 +91,9 @@ private: std::unique_ptr _ptr; std::thread _thread; bool _running; + Json::Value _config; + + const bool read_config(); }; #endif // EXPANDURL_MASTODON_HPP diff --git a/src/masto.cpp b/src/masto.cpp index fb06748..2d708a3 100644 --- a/src/masto.cpp +++ b/src/masto.cpp @@ -18,6 +18,7 @@ #include // getenv() #include #include +#include #include "version.hpp" #include "expandurl-mastodon.hpp" @@ -32,28 +33,45 @@ Listener::Listener() , _ptr(nullptr) , _running(false) { - const string filepath = static_cast(getenv("HOME")) + - "/.config/expandurl-mastodon.cfg"; - std::ifstream file(filepath); - if (file.is_open()) + if (read_config()) { - std::getline(file, _instance); - _instance = _instance.substr(_instance.find('@') + 1); - std::getline(file, _access_token); - file.close(); - _masto = std::make_unique(_instance, _access_token); _masto->set_useragent(static_cast("expandurl-mastodon/") + global::version); } else { - cerr << "ERROR: Could not open " << filepath << '\n'; exit(1); } } +const bool Listener::read_config() +{ + const string filepath = static_cast(getenv("HOME")) + + "/.config/expandurl-mastodon.json"; + std::ifstream file(filepath); + + 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(); + } + else + { + cerr << "ERROR: Could not open " << filepath << ".\n"; + return false; + } + + return true; +} + const void Listener::start() { _thread = std::thread([=] @@ -126,6 +144,11 @@ std::vector Listener::get_new_messages() return v; } +const std::vector Listener::catchup() +{ + // +} + Mastodon::Easy::Status Listener::get_status(const std::uint_fast64_t &id) { std::uint_fast16_t ret;