2018-05-11 02:21:44 +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 <iostream>
|
2018-05-11 06:57:41 +02:00
|
|
|
#include <chrono>
|
|
|
|
#include <csignal>
|
|
|
|
#include <regex>
|
2018-05-11 02:21:44 +02:00
|
|
|
#include <curlpp/cURLpp.hpp>
|
|
|
|
#include "expandurl-mastodon.hpp"
|
|
|
|
|
|
|
|
using std::cout;
|
2018-05-11 06:57:41 +02:00
|
|
|
using std::cerr;
|
2018-05-11 02:21:44 +02:00
|
|
|
using std::string;
|
2018-05-11 06:57:41 +02:00
|
|
|
using Mastodon::Easy;
|
|
|
|
|
|
|
|
bool running = true;
|
|
|
|
|
|
|
|
void signal_handler(int signum)
|
|
|
|
{
|
|
|
|
switch (signum)
|
|
|
|
{
|
|
|
|
case SIGINT:
|
2018-05-12 10:42:22 +02:00
|
|
|
case SIGTERM:
|
2018-05-11 06:57:41 +02:00
|
|
|
if (!running)
|
|
|
|
{
|
|
|
|
cout << "Forced close.\n";
|
|
|
|
exit(signum);
|
|
|
|
}
|
|
|
|
running = false;
|
2018-05-13 16:11:37 +02:00
|
|
|
cout << "Closing, this could take a few seconds...\n";
|
2018-05-11 06:57:41 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 02:21:44 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-05-11 06:57:41 +02:00
|
|
|
signal(SIGINT, signal_handler);
|
2018-05-12 10:42:22 +02:00
|
|
|
signal(SIGTERM, signal_handler);
|
2018-05-11 02:21:44 +02:00
|
|
|
curlpp::initialize();
|
|
|
|
|
2018-05-11 06:57:41 +02:00
|
|
|
Listener listener;
|
|
|
|
listener.start();
|
|
|
|
|
|
|
|
while (running)
|
|
|
|
{
|
2018-05-17 13:21:45 +02:00
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
2018-05-12 10:29:57 +02:00
|
|
|
if (!listener.stillrunning())
|
|
|
|
{
|
2018-05-12 14:24:53 +02:00
|
|
|
cout << "DEBUG: Reestablishing connection...\n";
|
2018-05-12 10:29:57 +02:00
|
|
|
listener.start();
|
|
|
|
}
|
2018-05-11 06:57:41 +02:00
|
|
|
|
|
|
|
for (Easy::Notification ¬if : listener.get_new_messages())
|
|
|
|
{
|
2018-05-12 14:24:53 +02:00
|
|
|
cout << "DEBUG: new messages\n";
|
2018-05-12 02:30:57 +02:00
|
|
|
const std::uint_fast64_t id = listener.get_parent_id(notif);
|
2018-05-12 14:24:53 +02:00
|
|
|
cout << "DEBUG: in_reply_to_id: " << id << '\n';
|
2018-05-11 06:57:41 +02:00
|
|
|
Easy::Status status;
|
2018-05-12 02:30:57 +02:00
|
|
|
|
2018-05-11 06:57:41 +02:00
|
|
|
if (id > 0)
|
|
|
|
{
|
|
|
|
status = listener.get_status(id);
|
2018-05-12 02:30:57 +02:00
|
|
|
if (status.valid())
|
2018-05-11 06:57:41 +02:00
|
|
|
{
|
2018-05-12 02:30:57 +02:00
|
|
|
string message = "";
|
|
|
|
for (const string &url : get_urls(status.content()))
|
|
|
|
{
|
|
|
|
message += url + " \n";
|
|
|
|
}
|
|
|
|
if (!message.empty())
|
2018-05-11 07:24:03 +02:00
|
|
|
{
|
2018-05-12 02:30:57 +02:00
|
|
|
if (!listener.send_reply(notif.status(), message))
|
|
|
|
{
|
2018-05-12 14:24:53 +02:00
|
|
|
cerr << "ERROR: could not send reply to " << id << '\n';
|
2018-05-12 02:30:57 +02:00
|
|
|
}
|
2018-05-11 07:24:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-12 02:30:57 +02:00
|
|
|
listener.send_reply(notif.status(),
|
|
|
|
"I couldn't find an URL in the message you "
|
|
|
|
"replied to. 😞");
|
2018-05-11 07:24:03 +02:00
|
|
|
}
|
2018-05-11 06:57:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-11 07:24:03 +02:00
|
|
|
listener.send_reply(notif.status(),
|
2018-05-12 02:30:57 +02:00
|
|
|
"I couldn't get the message you replied to. 😞");
|
2018-05-11 06:57:41 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-12 02:30:57 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
listener.send_reply(notif.status(),
|
|
|
|
"It seems that you didn't reply to a message?");
|
|
|
|
}
|
2018-05-11 06:57:41 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-11 02:21:44 +02:00
|
|
|
|
2018-05-12 10:42:22 +02:00
|
|
|
listener.stop();
|
2018-05-13 16:11:37 +02:00
|
|
|
curlpp::terminate();
|
2018-05-11 02:21:44 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|