From 2efd88bc4dc409def0aeef69b1e85ed7ce9f496d Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 20 May 2018 21:14:41 +0200 Subject: [PATCH] Added registration and config file creation --- CMakeLists.txt | 2 +- src/expandurl-mastodon.hpp | 1 + src/masto.cpp | 69 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f012776..9ca127c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (expandurl-mastodon - VERSION 0.4.0 + VERSION 0.5.0 LANGUAGES CXX ) diff --git a/src/expandurl-mastodon.hpp b/src/expandurl-mastodon.hpp index 2b391b4..64cf809 100644 --- a/src/expandurl-mastodon.hpp +++ b/src/expandurl-mastodon.hpp @@ -97,6 +97,7 @@ private: const bool read_config(); const bool write_config(); + const bool register_app(); }; #endif // EXPANDURL_MASTODON_HPP diff --git a/src/masto.cpp b/src/masto.cpp index ad1e97f..068ac9d 100644 --- a/src/masto.cpp +++ b/src/masto.cpp @@ -44,8 +44,22 @@ Listener::Listener() } else { - cerr << "ERROR: Could not open " << _configfilepath << ".\n"; - exit(1); + cerr << "WARNING: Could not open " << _configfilepath << ".\n"; + cout << "Attempting to register application and write config file.\n"; + if (register_app()) + { + cout << "DEBUG: registration successful\n"; + if (!write_config()) + { + cerr << "ERROR: Could not write " << _configfilepath << ".\n"; + std::exit(1); + } + } + else + { + cerr << "ERROR: Could not register app.\n"; + std::exit(2); + } } } @@ -53,7 +67,7 @@ Listener::~Listener() { if (!write_config()) { - cerr << "ERROR: Could not open " << _configfilepath << ".\n"; + cerr << "ERROR: Could not write " << _configfilepath << ".\n"; } } @@ -301,3 +315,52 @@ const bool Listener::stillrunning() const { return _running; } + +const bool Listener::register_app() +{ + cout << "Account (username@instance): "; + std::cin >> _instance; + _config["account"] = _instance; + _instance = _instance.substr(_instance.find('@') + 1); + + _masto = std::make_unique(_instance, ""); + _masto->set_useragent(static_cast("expandurl-mastodon/") + + global::version); + + std::uint_fast16_t ret; + string client_id, client_secret, url; + ret = _masto->register_app1("expandurl-mastodon", + "urn:ietf:wg:oauth:2.0:oob", + "read write", + "https://github.com/tastytea/expandurl-mastodon", + client_id, + client_secret, + url); + if (ret == 0) + { + string code; + cout << "Visit " << url << " to authorize this application.\n"; + cout << "Paste the authorization code here: "; + std::cin >> code; + ret = _masto->register_app2(client_id, + client_secret, + "urn:ietf:wg:oauth:2.0:oob", + code, + _access_token); + if (ret == 0) + { + _config["access_token"] = _access_token; + return true; + } + else + { + cerr << "ERROR: register_app2(): " << ret << '\n'; + } + } + else + { + cerr << "ERROR: register_app1(): " << ret << '\n'; + } + + return false; +}