/* This file is part of mastorss. * Copyright © 2018, 2019 tastytea * * 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 . */ #include #include #include #include #include #include #include #include #include "mastorss.hpp" using std::cout; using std::cerr; using std::cin; using std::string; namespace fs = std::experimental::filesystem; std::uint16_t read_config(string &instance, string &access_token, string &feedurl) { bool config_changed = false; // Read config file, get access token std::ifstream file(filepath + "config-" + profile + ".json"); if (file.is_open()) { std::stringstream json; json << file.rdbuf(); file.close(); json >> config; 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(); } else { config[profile]["max_size"] = max_size; config_changed = true; } } else { cout << "Config file not found. Building new one.\n"; fs::create_directory(filepath); } if (instance.empty()) { cout << "Instance: "; std::getline(cin, instance); config[profile]["instance"] = instance; config_changed = true; } if (access_token.empty()) { cout << "No access token found.\n"; string client_id, client_secret, url; Mastodon::API masto(instance, ""); Mastodon::return_call ret = masto.register_app1("mastorss", "urn:ietf:wg:oauth:2.0:oob", "write", "https://schlomp.space/tastytea/mastorss", client_id, client_secret, url); if (!ret) { string code; cout << "Visit " << url << " to authorize this application.\n"; cout << "Insert code: "; std::getline(cin, code); ret = masto.register_app2(client_id, client_secret, "urn:ietf:wg:oauth:2.0:oob", code, access_token); if (!ret) { config[profile]["access_token"] = access_token; config_changed = true; } else { cerr << "Error code: " << ret.error_code << '\n'; return ret.error_code; } } else { cerr << "Error code: " << ret.error_code << '\n'; return ret.error_code; } } if (feedurl.empty()) { cout << "feedurl: "; std::getline(cin, feedurl); config[profile]["feedurl"] = feedurl; config_changed = true; } if (config[profile]["titles_only"].isNull()) { string titles_only; cout << "post only titles? [y/n]: "; std::getline(cin, titles_only); if (titles_only[0] == 'y') { config[profile]["titles_only"] = true; } else { config[profile]["titles_only"] = false; } config_changed = true; } if (config[profile]["titles_as_cw"].isNull()) { string titles_as_cw; cout << "Use titles as CW? [y/n]: "; std::getline(cin, titles_as_cw); if (titles_as_cw[0] == 'y') { config[profile]["titles_as_cw"] = true; } else { config[profile]["titles_as_cw"] = false; } config_changed = true; } if (config[profile]["append"].isNull()) { string append; cout << "Append this string to each post []: "; std::getline(cin, append); config[profile]["append"] = append; config_changed = true; } if (config[profile]["interval"].isNull()) { string interval; cout << "Interval between posts in seconds [60]: "; std::getline(cin, interval); if (interval.empty()) { interval = "60"; } config[profile]["interval"] = Json::Value::UInt64(std::stoul(interval)); config_changed = true; } if (config_changed) { write_config(); } return 0; } 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; }