expandurl-mastodon/src/masto.cpp

361 lines
9.8 KiB
C++
Raw Normal View History

2018-05-11 06:57:41 +02:00
/* This file is part of expandurl-mastodon.
2019-04-20 01:47:23 +02:00
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
2018-05-11 06:57:41 +02:00
*
* 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 <fstream>
#include <cstdlib> // getenv()
#include <iostream>
2018-05-17 18:27:06 +02:00
#include <mutex>
#include <sstream>
#include <syslog.h>
#include <chrono>
2018-05-11 06:57:41 +02:00
#include "version.hpp"
#include "expandurl-mastodon.hpp"
using std::cout;
2018-05-11 06:57:41 +02:00
using std::string;
2019-04-20 01:47:23 +02:00
using std::uint8_t;
2018-05-11 06:57:41 +02:00
Listener::Listener()
: _instance("")
, _access_token("")
, _stream("")
, _ptr(nullptr)
, _running(false)
2018-05-26 23:52:22 +02:00
, _proxy("")
, _proxy_user("")
, _proxy_password("")
2018-05-27 16:08:49 +02:00
, _config(configfile.get_json())
{
2018-05-27 16:08:49 +02:00
read_config();
if (_config["access_token"].isNull())
{
syslog(LOG_INFO, "Attempting to register application and write config file.");
if (register_app())
{
syslog(LOG_INFO, "Registration successful.");
2018-05-27 16:08:49 +02:00
if (!configfile.write())
{
2018-05-27 16:08:49 +02:00
syslog(LOG_ERR, "Could not write %s.",
configfile.get_filepath().c_str());
std::exit(1);
}
}
else
{
syslog(LOG_ERR, "Could not register app.");
std::exit(2);
}
}
2019-04-20 01:47:23 +02:00
_masto = std::make_unique<Easy::API>(_instance, _access_token);
2018-05-27 16:08:49 +02:00
_masto->set_useragent(static_cast<const string>("expandurl-mastodon/") +
global::version);
set_proxy(*_masto);
}
2018-05-27 16:08:49 +02:00
Listener::~Listener()
2018-05-11 06:57:41 +02:00
{
}
2019-04-20 01:47:23 +02:00
void Listener::read_config()
{
2018-05-27 16:08:49 +02:00
_instance = _config["account"].asString();
_instance = _instance.substr(_instance.find('@') + 1);
_access_token = _config["access_token"].asString();
_proxy = _config["proxy"]["url"].asString();
_proxy_user = _config["proxy"]["user"].asString();
_proxy_password = _config["proxy"]["password"].asString();
2018-05-12 10:42:22 +02:00
}
2018-05-11 06:57:41 +02:00
2019-04-20 01:47:23 +02:00
void Listener::start()
2018-05-12 10:42:22 +02:00
{
2019-04-20 01:47:23 +02:00
Easy::API masto(_instance, _access_token);
_running = true;
2019-04-20 01:47:23 +02:00
masto.set_useragent(string("expandurl-mastodon/") + global::version);
set_proxy(masto);
masto.get_stream(Mastodon::API::v1::streaming_user, _ptr, _stream);
syslog(LOG_NOTICE, "Connecting to %s ...", _instance.c_str());
2018-05-11 06:57:41 +02:00
}
2019-04-20 01:47:23 +02:00
void Listener::stop()
2018-05-11 06:57:41 +02:00
{
if (!configfile.write())
{
syslog(LOG_ERR, "Could not write %s.",
configfile.get_filepath().c_str());
}
2018-05-11 06:57:41 +02:00
if (_ptr)
{
_ptr->cancel_stream();
_thread.join();
_ptr.reset();
2018-05-28 15:46:38 +02:00
_ptr = nullptr;
_stream = "";
2018-05-11 06:57:41 +02:00
}
2018-05-14 21:44:44 +02:00
else
{
syslog(LOG_DEBUG, "_ptr is false.");
2018-05-14 21:44:44 +02:00
}
2018-05-11 06:57:41 +02:00
}
const std::vector<Easy::Notification> Listener::get_new_messages()
2018-05-11 06:57:41 +02:00
{
using namespace std::chrono;
2018-05-11 06:57:41 +02:00
std::vector<Easy::Notification> v;
static system_clock::time_point lastping = system_clock::now();
2018-05-17 18:27:06 +02:00
std::lock_guard<std::mutex> lock(_ptr->get_mutex());
if (!_stream.empty())
{
for (const Easy::stream_event &event : Easy::parse_stream(_stream))
{
2019-04-20 01:47:23 +02:00
if (event.type == Easy::event_type::Notification)
{
2019-04-20 01:47:23 +02:00
Easy::Notification notif(event.data);
if (notif.type() == Easy::notification_type::Mention)
{
v.push_back(notif);
}
}
2019-04-20 01:47:23 +02:00
else if (event.type == Easy::event_type::Error)
{
constexpr uint8_t delay_after_error = 120;
syslog(LOG_DEBUG, "Connection lost.");
const Json::Value err;
syslog(LOG_ERR, "Connection terminated: Error %u",
err["error_code"].asUInt());
syslog(LOG_INFO, "Waiting for %u seconds", delay_after_error);
std::this_thread::sleep_for(std::chrono::seconds(delay_after_error));
_running = false;
}
}
_stream.clear();
lastping = system_clock::now();
}
else
2018-05-11 06:57:41 +02:00
{
// If the last keep-alive packet was received 25 seconds or more ago
2018-06-11 17:15:35 +02:00
if (duration_cast<seconds>(system_clock::now() - lastping).count() >= 25)
2018-05-11 06:57:41 +02:00
{
lastping = system_clock::now();
syslog(LOG_NOTICE, "Detected broken connection.");
2018-05-17 13:57:21 +02:00
_running = false;
2018-05-11 06:57:41 +02:00
}
}
return v;
}
const std::vector<Easy::Notification> Listener::catchup()
{
std::vector<Easy::Notification> v;
const string last_id = _config["last_id"].asString();
if (last_id != "")
{
syslog(LOG_DEBUG, "Catching up...");
2019-04-20 01:47:23 +02:00
parameters parameter =
{
{ "since_id", { last_id } },
{ "exclude_types", { "follow", "favourite", "reblog" } }
};
2019-04-20 01:47:23 +02:00
return_call ret;;
2019-04-20 01:47:23 +02:00
ret = _masto->get(API::v1::notifications, parameter);
2019-04-20 01:47:23 +02:00
if (ret)
{
2019-04-20 01:47:23 +02:00
for (const string str : Easy::json_array_to_vector(ret.answer))
{
v.push_back(Easy::Notification(str));
}
}
else
{
2019-04-20 01:47:23 +02:00
syslog(LOG_ERR, "Could not catch up: Error %u", ret.error_code);
}
}
return v;
}
2019-01-27 04:51:03 +01:00
Mastodon::Easy::Status Listener::get_status(const string &id)
2018-05-11 06:57:41 +02:00
{
2019-04-20 01:47:23 +02:00
return_call ret;
2018-05-11 06:57:41 +02:00
2019-04-20 01:47:23 +02:00
ret = _masto->get(API::v1::statuses_id, {{ "id", { id }}});
if (ret)
2018-05-11 06:57:41 +02:00
{
2019-04-20 01:47:23 +02:00
return Easy::Status(ret.answer);
2018-05-11 06:57:41 +02:00
}
else
{
2019-04-20 01:47:23 +02:00
syslog(LOG_ERR, "Error %u in %s.", ret.error_code, __FUNCTION__);
2018-05-11 06:57:41 +02:00
return Easy::Status();
}
}
2019-04-20 01:47:23 +02:00
bool Listener::send_reply(const Easy::Status &to_status,
const string &message)
2018-05-11 06:57:41 +02:00
{
2019-04-20 01:47:23 +02:00
Easy::return_entity<Easy::Status> ret;
2018-05-11 06:57:41 +02:00
Easy::Status new_status;
if (to_status.visibility() == Easy::visibility_type::Public)
2018-05-11 06:57:41 +02:00
{
new_status.visibility(Easy::visibility_type::Unlisted);
2018-05-11 06:57:41 +02:00
}
else
2018-05-11 06:57:41 +02:00
{
new_status.visibility(to_status.visibility());
2018-05-11 06:57:41 +02:00
}
new_status.in_reply_to_id(to_status.id());
new_status.content('@' + to_status.account().acct() + ' ' + message);
new_status.sensitive(to_status.sensitive());
new_status.spoiler_text(to_status.spoiler_text());
2018-05-11 06:57:41 +02:00
2019-04-20 01:47:23 +02:00
ret = _masto->send_post(new_status);
2018-05-11 06:57:41 +02:00
2019-04-20 01:47:23 +02:00
if (ret)
2018-05-11 06:57:41 +02:00
{
syslog(LOG_DEBUG, "Sent reply");
2018-05-11 06:57:41 +02:00
return true;
}
else
{
2019-04-20 01:47:23 +02:00
syslog(LOG_ERR, "Error %u in %s.", ret.error_code, __FUNCTION__);
2018-05-11 06:57:41 +02:00
return false;
}
}
2019-01-27 04:51:03 +01:00
const string Listener::get_parent_id(const Easy::Notification &notif)
{
2019-04-20 01:47:23 +02:00
return_call ret;
2018-05-27 01:24:11 +02:00
// Retry up to 2 times
for (std::uint_fast8_t retries = 1; retries <= 2; ++retries)
{
2018-05-27 01:24:11 +02:00
// Fetch full status
2019-04-20 01:47:23 +02:00
ret = _masto->get(API::v1::search, {{ "q", { notif.status().url() }}});
if (!ret)
2018-05-27 01:24:11 +02:00
{
syslog(LOG_ERR, "Error %u: Could not fetch status (in %s).",
2019-04-20 01:47:23 +02:00
ret.error_code, __FUNCTION__);
2018-05-27 01:24:11 +02:00
return 0;
}
2018-05-27 01:24:11 +02:00
ret = _masto->get(API::v1::statuses_id,
2019-04-20 01:47:23 +02:00
{{ "id", { notif.status().id() }}});
2019-04-20 01:47:23 +02:00
if (!ret)
2018-05-27 01:24:11 +02:00
{
syslog(LOG_ERR, "Error %u: Could not get status (in %s).",
2019-04-20 01:47:23 +02:00
ret.error_code, __FUNCTION__);
2018-05-27 01:24:11 +02:00
return 0;
}
else
{
2019-01-27 04:51:03 +01:00
_config["last_id"] = notif.id();
2019-04-20 01:47:23 +02:00
const Easy::Status s(ret.answer);
2018-05-27 01:24:11 +02:00
// If parent is found, return ID; else retry
2019-01-27 04:51:03 +01:00
if (!s.in_reply_to_id().empty())
2018-05-27 01:24:11 +02:00
{
return s.in_reply_to_id();
}
else
{
2018-05-29 19:15:53 +02:00
syslog(LOG_WARNING, "Could not get ID of replied-to post");
2018-05-27 01:24:11 +02:00
std::this_thread::sleep_for(std::chrono::seconds(2));
}
}
}
2018-05-27 01:24:11 +02:00
return 0;
}
2019-04-20 01:47:23 +02:00
bool Listener::stillrunning() const
{
return _running;
}
2019-04-20 01:47:23 +02:00
bool Listener::register_app()
{
cout << "Account (username@instance): ";
std::cin >> _instance;
_config["account"] = _instance;
_instance = _instance.substr(_instance.find('@') + 1);
2019-04-20 01:47:23 +02:00
_masto = std::make_unique<Easy::API>(_instance, "");
_masto->set_useragent(static_cast<const string>("expandurl-mastodon/") +
global::version);
2019-04-20 01:47:23 +02:00
return_call ret;
string client_id, client_secret, url;
ret = _masto->register_app1("expandurl-mastodon",
"urn:ietf:wg:oauth:2.0:oob",
"read write",
2018-06-04 21:04:38 +02:00
"https://schlomp.space/tastytea/expandurl-mastodon",
client_id,
client_secret,
url);
2019-04-20 01:47:23 +02:00
if (ret)
{
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);
2019-04-20 01:47:23 +02:00
if (ret)
{
_config["access_token"] = _access_token;
return true;
}
else
{
2019-04-20 01:47:23 +02:00
syslog(LOG_ERR, "register_app2(): %u", ret.error_code);
}
}
else
{
2019-04-20 01:47:23 +02:00
syslog(LOG_ERR, "register_app1(): %u", ret.error_code);
}
return false;
}
2018-05-26 23:52:22 +02:00
2019-04-20 01:47:23 +02:00
void Listener::set_proxy(Easy::API &masto)
2018-05-26 23:52:22 +02:00
{
if (!_proxy.empty())
{
if (!_proxy_user.empty())
{
masto.set_proxy(_proxy, _proxy_user + ':' + _proxy_password);
}
else
{
masto.set_proxy(_proxy);
}
}
}