2018-05-11 06:57:41 +02:00
|
|
|
/* This file is part of expandurl-mastodon.
|
|
|
|
* Copyright © 2018 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
#include "version.hpp"
|
|
|
|
#include "expandurl-mastodon.hpp"
|
|
|
|
|
|
|
|
using std::cerr;
|
2018-05-12 14:24:53 +02:00
|
|
|
using std::cout;
|
2018-05-11 06:57:41 +02:00
|
|
|
using std::string;
|
|
|
|
|
|
|
|
Listener::Listener()
|
|
|
|
: _instance("")
|
|
|
|
, _access_token("")
|
|
|
|
, _stream("")
|
|
|
|
, _ptr(nullptr)
|
2018-05-12 10:29:57 +02:00
|
|
|
, _running(false)
|
2018-05-11 06:57:41 +02:00
|
|
|
{
|
|
|
|
const string filepath = static_cast<const string>(getenv("HOME")) +
|
|
|
|
"/.config/expandurl-mastodon.cfg";
|
|
|
|
std::ifstream file(filepath);
|
|
|
|
|
|
|
|
if (file.is_open())
|
|
|
|
{
|
|
|
|
std::getline(file, _instance);
|
|
|
|
_instance = _instance.substr(_instance.find('@') + 1);
|
|
|
|
std::getline(file, _access_token);
|
|
|
|
file.close();
|
2018-05-12 02:30:57 +02:00
|
|
|
|
|
|
|
_masto = std::make_unique<Easy>(_instance, _access_token);
|
|
|
|
_masto->set_useragent(static_cast<const string>("expandurl-mastodon/") +
|
|
|
|
global::version);
|
2018-05-11 06:57:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "ERROR: Could not open " << filepath << '\n';
|
2018-05-12 10:42:22 +02:00
|
|
|
exit(1);
|
2018-05-11 06:57:41 +02:00
|
|
|
}
|
2018-05-12 10:42:22 +02:00
|
|
|
}
|
2018-05-11 06:57:41 +02:00
|
|
|
|
2018-05-12 10:42:22 +02:00
|
|
|
const void Listener::start()
|
|
|
|
{
|
2018-05-11 06:57:41 +02:00
|
|
|
_thread = std::thread([=]
|
|
|
|
{
|
2018-05-12 10:29:57 +02:00
|
|
|
_running = true;
|
2018-05-11 06:57:41 +02:00
|
|
|
Easy masto(_instance, _access_token);
|
|
|
|
masto.set_useragent(static_cast<const string>("expandurl-mastodon/") +
|
|
|
|
global::version);
|
|
|
|
masto.get_stream(Mastodon::API::v1::streaming_user, _stream, _ptr);
|
2018-05-12 14:24:53 +02:00
|
|
|
cout << "DEBUG: Connection lost.\n";
|
2018-05-12 10:29:57 +02:00
|
|
|
_running = false;
|
2018-05-11 06:57:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const void Listener::stop()
|
|
|
|
{
|
|
|
|
if (_ptr)
|
|
|
|
{
|
|
|
|
_ptr->abort_stream();
|
|
|
|
_thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Easy::Notification> Listener::get_new_messages()
|
|
|
|
{
|
|
|
|
const string buffer = _stream;
|
|
|
|
_stream.clear();
|
|
|
|
|
|
|
|
std::vector<Easy::Notification> v;
|
|
|
|
for (const Easy::stream_event &event : Easy::parse_stream(buffer))
|
|
|
|
{
|
|
|
|
if (event.first == Easy::event_type::Notification)
|
|
|
|
{
|
|
|
|
v.push_back(Easy::Notification(event.second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mastodon::Easy::Status Listener::get_status(const std::uint_fast64_t &id)
|
|
|
|
{
|
|
|
|
std::uint_fast16_t ret;
|
|
|
|
string answer;
|
|
|
|
|
2018-05-12 04:17:34 +02:00
|
|
|
ret = _masto->get(API::v1::statuses_id, {{ "id", { std::to_string(id) }}},
|
2018-05-12 02:30:57 +02:00
|
|
|
answer);
|
2018-05-11 06:57:41 +02:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
return Easy::Status(answer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-12 14:24:53 +02:00
|
|
|
cerr << "ERROR: " << ret << " (in " << __FUNCTION__ << ")\n";
|
2018-05-11 06:57:41 +02:00
|
|
|
return Easy::Status();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-12 04:17:34 +02:00
|
|
|
const bool Listener::send_reply(const Easy::Status &to_status,
|
2018-05-11 06:57:41 +02:00
|
|
|
const string &message)
|
|
|
|
{
|
|
|
|
std::uint_fast16_t ret;
|
|
|
|
string answer;
|
2018-05-12 04:17:34 +02:00
|
|
|
const string id = std::to_string(to_status.id());
|
2018-05-11 06:57:41 +02:00
|
|
|
string strvisibility;
|
|
|
|
|
2018-05-12 04:17:34 +02:00
|
|
|
switch (to_status.visibility())
|
2018-05-11 06:57:41 +02:00
|
|
|
{
|
|
|
|
case Easy::visibility_type::Private:
|
|
|
|
strvisibility = "private";
|
|
|
|
break;
|
|
|
|
case Easy::visibility_type::Direct:
|
|
|
|
strvisibility = "direct";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
strvisibility = "unlisted";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Easy::parametermap parameters =
|
|
|
|
{
|
|
|
|
{ "in_reply_to_id", { id } },
|
|
|
|
{ "visibility", { strvisibility } },
|
2018-05-12 04:17:34 +02:00
|
|
|
{ "status", { '@' + to_status.account().acct() + ' ' + message } }
|
2018-05-11 06:57:41 +02:00
|
|
|
};
|
|
|
|
|
2018-05-12 04:17:34 +02:00
|
|
|
if (to_status.sensitive())
|
2018-05-11 06:57:41 +02:00
|
|
|
{
|
|
|
|
parameters.insert({ "sensitive", { "true" } });
|
|
|
|
}
|
|
|
|
|
2018-05-12 04:17:34 +02:00
|
|
|
if (!to_status.spoiler_text().empty())
|
2018-05-11 06:57:41 +02:00
|
|
|
{
|
2018-05-12 04:17:34 +02:00
|
|
|
parameters.insert({ "spoiler_text", { to_status.spoiler_text() } });
|
2018-05-11 06:57:41 +02:00
|
|
|
}
|
|
|
|
|
2018-05-12 04:17:34 +02:00
|
|
|
ret = _masto->post(API::v1::statuses, parameters, answer);
|
2018-05-11 06:57:41 +02:00
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
2018-05-12 14:24:53 +02:00
|
|
|
cout << "DEBUG: Sent reply: " << message << '\n';
|
2018-05-11 06:57:41 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-12 14:24:53 +02:00
|
|
|
cerr << "ERROR: " << ret << " (in " << __FUNCTION__ << ")\n";
|
2018-05-11 06:57:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-05-12 02:30:57 +02:00
|
|
|
|
|
|
|
const std::uint_fast64_t Listener::get_parent_id(Easy::Notification ¬if)
|
|
|
|
{
|
|
|
|
string answer;
|
|
|
|
std::uint_fast16_t ret;
|
|
|
|
|
|
|
|
ret = _masto->get(API::v1::statuses_id,
|
|
|
|
{{ "id", { std::to_string(notif.status().id()) }}},
|
|
|
|
answer);
|
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
cerr << "ERROR: " << ret << " (in " << __FUNCTION__ << ")\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Easy::Status s(answer);
|
|
|
|
return s.in_reply_to_id();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2018-05-12 10:29:57 +02:00
|
|
|
|
|
|
|
const bool Listener::stillrunning() const
|
|
|
|
{
|
|
|
|
return _running;
|
|
|
|
}
|