switched confuig file format from cfg to json

Cette révision appartient à :
tastytea 2018-05-20 14:25:20 +02:00
Parent 211a255048
révision b361753f51
Signé par: tastytea
ID de la clé GPG: 59346E0EA35C67E5
4 fichiers modifiés avec 47 ajouts et 14 suppressions

Voir le fichier

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7)
project (expandurl-mastodon
VERSION 0.3.3
VERSION 0.4.0
LANGUAGES CXX
)

Voir le fichier

@ -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.

Voir le fichier

@ -24,6 +24,7 @@
#include <cstdint>
#include <mastodon-cpp/mastodon-cpp.hpp>
#include <mastodon-cpp/easy/all.hpp>
#include <jsoncpp/json/json.h>
using std::string;
using Mastodon::API;
@ -75,6 +76,7 @@ public:
const void stop();
std::vector<Easy::Notification> get_new_messages();
const std::vector<Easy::Notification> 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 &notif);
@ -89,6 +91,9 @@ private:
std::unique_ptr<API::http> _ptr;
std::thread _thread;
bool _running;
Json::Value _config;
const bool read_config();
};
#endif // EXPANDURL_MASTODON_HPP

Voir le fichier

@ -18,6 +18,7 @@
#include <cstdlib> // getenv()
#include <iostream>
#include <mutex>
#include <sstream>
#include "version.hpp"
#include "expandurl-mastodon.hpp"
@ -32,28 +33,45 @@ Listener::Listener()
, _ptr(nullptr)
, _running(false)
{
const string filepath = static_cast<const string>(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<Easy>(_instance, _access_token);
_masto->set_useragent(static_cast<const string>("expandurl-mastodon/") +
global::version);
}
else
{
cerr << "ERROR: Could not open " << filepath << '\n';
exit(1);
}
}
const bool Listener::read_config()
{
const string filepath = static_cast<const string>(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<Easy::Notification> Listener::get_new_messages()
return v;
}
const std::vector<Easy::Notification> Listener::catchup()
{
//
}
Mastodon::Easy::Status Listener::get_status(const std::uint_fast64_t &id)
{
std::uint_fast16_t ret;