2018-02-18 14:27:01 +01:00
|
|
|
/* This file is part of mastorss.
|
|
|
|
* 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 <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <cstdint>
|
2018-04-14 13:57:03 +02:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <experimental/filesystem>
|
|
|
|
#include <jsoncpp/json/json.h>
|
2018-04-09 18:16:17 +02:00
|
|
|
#include <mastodon-cpp/mastodon-cpp.hpp>
|
2018-02-18 14:27:01 +01:00
|
|
|
#include "mastorss.hpp"
|
|
|
|
|
|
|
|
using std::cout;
|
|
|
|
using std::cerr;
|
|
|
|
using std::cin;
|
|
|
|
using std::string;
|
2018-04-14 13:57:03 +02:00
|
|
|
namespace fs = std::experimental::filesystem;
|
2018-02-18 14:27:01 +01:00
|
|
|
|
2018-03-15 13:20:26 +01:00
|
|
|
std::uint16_t read_config(string &instance, string &access_token, string &feedurl)
|
2018-02-18 14:27:01 +01:00
|
|
|
{
|
|
|
|
bool config_changed = false;
|
|
|
|
|
|
|
|
// Read config file, get access token
|
2018-04-14 13:57:03 +02:00
|
|
|
std::ifstream file(filepath + "config-" + profile + ".json");
|
|
|
|
if (file.is_open())
|
|
|
|
{
|
|
|
|
std::stringstream json;
|
|
|
|
json << file.rdbuf();
|
|
|
|
file.close();
|
|
|
|
json >> config;
|
|
|
|
|
2018-04-14 14:10:14 +02:00
|
|
|
instance = config[profile]["instance"].asString();
|
|
|
|
access_token = config[profile]["access_token"].asString();
|
|
|
|
feedurl = config[profile]["feedurl"].asString();
|
|
|
|
if (!config[profile]["max_size"].isNull())
|
|
|
|
{
|
|
|
|
max_size = config[profile]["max_size"].asUInt();
|
|
|
|
}
|
2018-04-14 15:19:54 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
config[profile]["max_size"] = max_size;
|
|
|
|
config_changed = true;
|
|
|
|
}
|
2018-02-18 14:27:01 +01:00
|
|
|
}
|
2018-04-14 13:57:03 +02:00
|
|
|
else
|
2018-02-18 14:27:01 +01:00
|
|
|
{
|
2018-04-14 13:57:03 +02:00
|
|
|
cout << "Config file not found. Building new one.\n";
|
|
|
|
fs::create_directory(filepath);
|
2018-02-18 14:27:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (instance.empty())
|
|
|
|
{
|
|
|
|
cout << "Instance: ";
|
2018-09-20 06:23:44 +02:00
|
|
|
std::getline(cin, instance);
|
2018-04-14 14:10:14 +02:00
|
|
|
config[profile]["instance"] = instance;
|
2018-02-18 14:27:01 +01:00
|
|
|
config_changed = true;
|
|
|
|
}
|
|
|
|
if (access_token.empty())
|
|
|
|
{
|
|
|
|
cout << "No access token found.\n";
|
|
|
|
string client_id, client_secret, url;
|
|
|
|
Mastodon::API masto(instance, "");
|
2018-03-15 13:20:26 +01:00
|
|
|
std::uint16_t ret = masto.register_app1("mastorss",
|
2018-02-18 14:27:01 +01:00
|
|
|
"urn:ietf:wg:oauth:2.0:oob",
|
|
|
|
"write",
|
2018-06-04 21:02:07 +02:00
|
|
|
"https://schlomp.space/tastytea/mastorss",
|
2018-02-18 14:27:01 +01:00
|
|
|
client_id,
|
|
|
|
client_secret,
|
|
|
|
url);
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
string code;
|
|
|
|
cout << "Visit " << url << " to authorize this application.\n";
|
|
|
|
cout << "Insert code: ";
|
2018-09-20 06:23:44 +02:00
|
|
|
std::getline(cin, code);
|
2018-02-18 14:27:01 +01:00
|
|
|
|
2018-04-14 13:57:03 +02:00
|
|
|
ret = masto.register_app2(client_id,
|
|
|
|
client_secret,
|
|
|
|
"urn:ietf:wg:oauth:2.0:oob",
|
|
|
|
code,
|
|
|
|
access_token);
|
2018-02-18 14:27:01 +01:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
2018-04-14 14:10:14 +02:00
|
|
|
config[profile]["access_token"] = access_token;
|
2018-02-18 14:27:01 +01:00
|
|
|
config_changed = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "Error code: " << ret << '\n';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "Error code: " << ret << '\n';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if (feedurl.empty())
|
|
|
|
{
|
|
|
|
cout << "feedurl: ";
|
2018-09-20 06:23:44 +02:00
|
|
|
std::getline(cin, feedurl);
|
2018-04-14 14:10:14 +02:00
|
|
|
config[profile]["feedurl"] = feedurl;
|
|
|
|
config_changed = true;
|
|
|
|
}
|
2018-04-14 14:35:21 +02:00
|
|
|
if (config[profile]["titles_only"].isNull())
|
2018-04-14 14:10:14 +02:00
|
|
|
{
|
|
|
|
string titles_only;
|
|
|
|
cout << "post only titles? [y/n]: ";
|
2018-09-20 06:23:44 +02:00
|
|
|
std::getline(cin, titles_only);
|
2018-04-14 14:10:14 +02:00
|
|
|
if (titles_only[0] == 'y')
|
|
|
|
{
|
|
|
|
config[profile]["titles_only"] = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
config[profile]["titles_only"] = false;
|
|
|
|
}
|
2018-02-18 14:27:01 +01:00
|
|
|
config_changed = true;
|
|
|
|
}
|
2018-08-24 17:54:46 +02:00
|
|
|
if (config[profile]["titles_as_cw"].isNull())
|
|
|
|
{
|
|
|
|
string titles_as_cw;
|
|
|
|
cout << "Use titles as CW? [y/n]: ";
|
2018-09-20 06:23:44 +02:00
|
|
|
std::getline(cin, titles_as_cw);
|
2018-08-24 17:54:46 +02:00
|
|
|
if (titles_as_cw[0] == 'y')
|
|
|
|
{
|
|
|
|
config[profile]["titles_as_cw"] = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
config[profile]["titles_as_cw"] = false;
|
|
|
|
}
|
|
|
|
config_changed = true;
|
|
|
|
}
|
2018-05-11 01:17:53 +02:00
|
|
|
if (config[profile]["append"].isNull())
|
|
|
|
{
|
|
|
|
string append;
|
|
|
|
cout << "Append this string to each post []: ";
|
2018-09-20 06:23:44 +02:00
|
|
|
std::getline(cin, append);
|
2018-05-11 01:17:53 +02:00
|
|
|
config[profile]["append"] = append;
|
|
|
|
config_changed = true;
|
|
|
|
}
|
2018-09-20 05:06:10 +02:00
|
|
|
if (config[profile]["interval"].isNull())
|
|
|
|
{
|
|
|
|
string interval;
|
|
|
|
cout << "Interval between posts in seconds [60]: ";
|
2018-09-20 06:23:44 +02:00
|
|
|
std::getline(cin, interval);
|
2018-09-20 05:06:10 +02:00
|
|
|
if (interval.empty())
|
|
|
|
{
|
|
|
|
interval = "60";
|
|
|
|
}
|
|
|
|
config[profile]["interval"] = std::stoul(interval);
|
|
|
|
config_changed = true;
|
|
|
|
}
|
2018-02-18 14:27:01 +01:00
|
|
|
if (config_changed)
|
|
|
|
{
|
2018-04-14 13:57:03 +02:00
|
|
|
write_config();
|
2018-02-18 14:27:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-04-14 13:57:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
const bool write_config()
|
|
|
|
{
|
|
|
|
std::ofstream outfile(filepath + "config-" + profile + ".json");
|
|
|
|
if (outfile.is_open())
|
|
|
|
{
|
|
|
|
outfile.write(config.toStyledString().c_str(),
|
|
|
|
config.toStyledString().length());
|
|
|
|
outfile.close();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|