diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e05b10..2a68802 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,17 +1,26 @@ cmake_minimum_required (VERSION 3.7) include(GNUInstallDirs) -project (rss2mastodon - VERSION 0.2.0 +project (mastorss + VERSION 0.2.1 LANGUAGES CXX ) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) +include_directories(${MY_SOURCE_DIR}/src) +include_directories(${PROJECT_BINARY_DIR}) + +# Write version in header +configure_file ( + "${PROJECT_SOURCE_DIR}/src/version.hpp.in" + "${PROJECT_BINARY_DIR}/version.hpp" +) + include(FindCURL) find_package(CURL REQUIRED) -add_executable(rss2mastodon src/rss2mastodon.cpp src/http.cpp) -target_link_libraries(rss2mastodon mastodon-cpp boost_system boost_filesystem ssl crypto ${CURL_LIBRARIES} curlpp) -install(TARGETS rss2mastodon DESTINATION ${CMAKE_INSTALL_BINDIR}) +add_executable(mastorss src/mastorss.cpp src/http.cpp) +target_link_libraries(mastorss mastodon-cpp boost_system boost_filesystem ssl crypto ${CURL_LIBRARIES} curlpp) +install(TARGETS mastorss DESTINATION ${CMAKE_INSTALL_BINDIR}) diff --git a/README.md b/README.md index d789b81..1e8a902 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -**rss2mastodon** dumps RSS feeds into a mastodon account. +**mastorss** dumps RSS feeds into a mastodon account. It is hacked together and generally only extended/fixed when it fails. Use at your own risk. @@ -35,7 +35,7 @@ Install with `make install`. # Usage -Put watchwords.json into `~/.config/rss2mastodon/`. Launch with profile name. +Put watchwords.json into `~/.config/mastorss/`. Launch with profile name. In the first run only the newest entry is tooted. The profile can't be named "global". diff --git a/src/http.cpp b/src/http.cpp index ee43211..4ef8bfc 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -1,4 +1,4 @@ -/* This file is part of rss2mastodon. +/* This file is part of mastorss. * Copyright © 2018 tastytea * * This program is free software: you can redistribute it and/or modify @@ -24,7 +24,7 @@ #include #include #include -#include "rss2mastodon.hpp" +#include "mastorss.hpp" using std::string; using std::cerr; @@ -44,6 +44,7 @@ const std::uint16_t http_get(const string &feedurl, string &answer, const string "Connection: close", }); + oss << request; answer = oss.str(); diff --git a/src/rss2mastodon.cpp b/src/mastorss.cpp similarity index 78% rename from src/rss2mastodon.cpp rename to src/mastorss.cpp index e79a5bc..f82b2e6 100644 --- a/src/rss2mastodon.cpp +++ b/src/mastorss.cpp @@ -1,4 +1,4 @@ -/* This file is part of rss2mastodon. +/* This file is part of mastorss. * Copyright © 2018 tastytea * * This program is free software: you can redistribute it and/or modify @@ -29,18 +29,20 @@ #include #include #include -#include "rss2mastodon.hpp" +#include "version.hpp" +#include "mastorss.hpp" namespace pt = boost::property_tree; using Mastodon::API; using std::cout; using std::cerr; +using std::cin; using std::string; std::uint16_t max_size = 500; -const string filepath = string(getenv("HOME")) + "/.config/rss2mastodon/"; +const string filepath = string(getenv("HOME")) + "/.config/mastorss/"; -void read_config(pt::ptree &config, const string &profile, string &instance, string &access_token, string &feedurl) +std::uint16_t read_config(pt::ptree &config, const string &profile, string &instance, string &access_token, string &feedurl) { bool config_changed = false; @@ -62,21 +64,58 @@ void read_config(pt::ptree &config, const string &profile, string &instance, str if (instance.empty()) { cout << "Instance: "; - std::cin >> instance; + cin >> instance; config.put(profile + ".instance", instance); config_changed = true; } if (access_token.empty()) { - cout << "access_token: "; - std::cin >> access_token; - config.put(profile + ".access_token", access_token); - config_changed = true; + cout << "No access token found.\n"; + string client_id, client_secret, url; + Mastodon::API masto(instance, ""); + std::uint16_t ret = masto.register_app1(instance, + "mastorss", + "urn:ietf:wg:oauth:2.0:oob", + "write", + "", + client_id, + client_secret, + url); + if (ret == 0) + { + string code; + cout << "Visit " << url << " to authorize this application.\n"; + cout << "Insert code: "; + cin >> code; + + masto.register_app2(instance, + client_id, + client_secret, + "urn:ietf:wg:oauth:2.0:oob", + code, + access_token); + if (ret == 0) + { + config.put(profile + ".access_token", access_token); + config_changed = true; + } + else + { + cerr << "Error code: " << ret << '\n'; + return ret; + } + } + else + { + cerr << "Error code: " << ret << '\n'; + return ret; + } + } if (feedurl.empty()) { cout << "feedurl: "; - std::cin >> feedurl; + cin >> feedurl; config.put(profile + ".feedurl", feedurl); config_changed = true; } @@ -84,6 +123,8 @@ void read_config(pt::ptree &config, const string &profile, string &instance, str { pt::write_json(filepath + "config-" + profile + ".json", config); } + + return 0; } std::vector parse_website(const string &profile, const string &xml) @@ -206,7 +247,7 @@ int main(int argc, char *argv[]) string last_entry = config.get(profile + ".last_entry", ""); std::vector entries; //FIXME: User-Agent - ret = http_get(feedurl, answer, "rss2mastodon/experimental"); + ret = http_get(feedurl, answer, "mastorss/" + (string)global::version); if (ret != 0) { return ret; diff --git a/src/rss2mastodon.hpp b/src/mastorss.hpp similarity index 65% rename from src/rss2mastodon.hpp rename to src/mastorss.hpp index 8658d5c..5d5cfac 100644 --- a/src/rss2mastodon.hpp +++ b/src/mastorss.hpp @@ -1,5 +1,5 @@ -#ifndef RSS2MASTODON_HPP -#define RSS2MASTODON_HPP +#ifndef mastorss_HPP +#define mastorss_HPP #include #include @@ -9,11 +9,11 @@ namespace pt = boost::property_tree; using std::string; -void read_config(pt::ptree &config, const string &profile, string &instance, string &access_token, string &feedurl); +std::uint16_t read_config(pt::ptree &config, const string &profile, string &instance, string &access_token, string &feedurl); std::vector parse_website(const string &profile, const string &xml); // http.cpp const std::uint16_t http_get(const string &feedurl, string &answer, const string &useragent = ""); -#endif // RSS2MASTODON_HPP +#endif // mastorss_HPP diff --git a/src/version.hpp.in b/src/version.hpp.in new file mode 100644 index 0000000..cec7915 --- /dev/null +++ b/src/version.hpp.in @@ -0,0 +1,9 @@ +#ifndef VERSION_HPP +#define VERSION_HPP + +namespace global +{ + static constexpr char version[] = "@PROJECT_VERSION@"; +} + +#endif // VERSION_HPP