Added registration and config file creation

This commit is contained in:
tastytea 2018-05-20 21:14:41 +02:00
parent 51233c1059
commit 2efd88bc4d
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
3 changed files with 68 additions and 4 deletions

View File

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

View File

@ -97,6 +97,7 @@ private:
const bool read_config();
const bool write_config();
const bool register_app();
};
#endif // EXPANDURL_MASTODON_HPP

View File

@ -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<Easy>(_instance, "");
_masto->set_useragent(static_cast<const string>("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;
}