2018-05-11 02:21:44 +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 02:21:44 +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 <iostream>
|
2018-05-11 06:57:41 +02:00
|
|
|
#include <chrono>
|
|
|
|
#include <csignal>
|
|
|
|
#include <regex>
|
2019-04-20 01:47:23 +02:00
|
|
|
#include <numeric>
|
2018-05-22 13:44:41 +02:00
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h> // getuid()
|
2018-05-11 02:21:44 +02:00
|
|
|
#include <curlpp/cURLpp.hpp>
|
2018-05-27 16:08:49 +02:00
|
|
|
#include "configjson.hpp"
|
2018-05-11 02:21:44 +02:00
|
|
|
#include "expandurl-mastodon.hpp"
|
|
|
|
|
2019-04-20 01:47:23 +02:00
|
|
|
using namespace Mastodon;
|
|
|
|
|
2018-05-11 02:21:44 +02:00
|
|
|
using std::string;
|
2018-05-11 06:57:41 +02:00
|
|
|
|
|
|
|
bool running = true;
|
2018-05-27 16:08:49 +02:00
|
|
|
ConfigJSON configfile("expandurl-mastodon.json");
|
2018-05-11 06:57:41 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2018-05-22 13:44:41 +02:00
|
|
|
syslog(LOG_NOTICE, "Forced close.");
|
2018-05-11 06:57:41 +02:00
|
|
|
exit(signum);
|
|
|
|
}
|
|
|
|
running = false;
|
2018-05-22 13:44:41 +02:00
|
|
|
syslog(LOG_NOTICE, "Received signal %d, closing...", signum);
|
2018-05-22 19:10:06 +02:00
|
|
|
std::cerr << "Received signal " << signum << ", closing...\n";
|
2018-05-11 06:57:41 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 01:47:23 +02:00
|
|
|
int main()
|
2018-05-11 02:21:44 +02:00
|
|
|
{
|
2018-05-29 14:56:39 +02:00
|
|
|
signal(SIGINT, signal_handler);
|
|
|
|
signal(SIGTERM, signal_handler);
|
|
|
|
|
2018-05-27 16:08:49 +02:00
|
|
|
if (!configfile.read())
|
|
|
|
{
|
2019-01-27 04:51:03 +01:00
|
|
|
syslog(LOG_WARNING, "Could not open %s.",
|
|
|
|
configfile.get_filepath().c_str());
|
2018-05-27 16:08:49 +02:00
|
|
|
}
|
2018-05-29 14:56:39 +02:00
|
|
|
init_replacements();
|
2018-05-27 16:08:49 +02:00
|
|
|
|
2018-05-11 02:21:44 +02:00
|
|
|
curlpp::initialize();
|
2018-05-22 19:06:42 +02:00
|
|
|
openlog("expandurl-mastodon", LOG_CONS | LOG_NDELAY | LOG_PID, LOG_LOCAL1);
|
2018-05-24 11:19:59 +02:00
|
|
|
syslog(LOG_NOTICE, "Program started by user %d", getuid());
|
2018-05-11 02:21:44 +02:00
|
|
|
|
2018-05-11 06:57:41 +02:00
|
|
|
Listener listener;
|
|
|
|
listener.start();
|
2018-05-20 14:48:12 +02:00
|
|
|
std::vector<Easy::Notification> new_messages = listener.catchup();
|
2018-05-11 06:57:41 +02:00
|
|
|
|
|
|
|
while (running)
|
|
|
|
{
|
2018-06-11 05:44:06 +02:00
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(2));
|
2018-05-12 10:29:57 +02:00
|
|
|
if (!listener.stillrunning())
|
|
|
|
{
|
2018-05-17 13:57:21 +02:00
|
|
|
listener.stop();
|
2018-05-22 13:44:41 +02:00
|
|
|
syslog(LOG_DEBUG, "Reestablishing connection...");
|
2018-05-12 10:29:57 +02:00
|
|
|
listener.start();
|
2018-05-21 01:25:33 +02:00
|
|
|
new_messages = listener.catchup();
|
2018-05-12 10:29:57 +02:00
|
|
|
}
|
2018-05-11 06:57:41 +02:00
|
|
|
|
2018-05-20 14:48:12 +02:00
|
|
|
// Only get new messages if we don't have to catchup
|
|
|
|
if (new_messages.size() == 0)
|
2018-05-11 06:57:41 +02:00
|
|
|
{
|
2018-05-20 14:48:12 +02:00
|
|
|
new_messages = listener.get_new_messages();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Easy::Notification ¬if : new_messages)
|
|
|
|
{
|
2018-05-22 13:44:41 +02:00
|
|
|
syslog(LOG_DEBUG, "new message");
|
2019-01-27 04:51:03 +01:00
|
|
|
const string id = listener.get_parent_id(notif);
|
|
|
|
syslog(LOG_DEBUG, "in_reply_to_id: %s", id.c_str());
|
2018-05-11 06:57:41 +02:00
|
|
|
Easy::Status status;
|
2018-05-12 02:30:57 +02:00
|
|
|
|
2019-01-27 04:51:03 +01:00
|
|
|
if (!id.empty())
|
2018-05-11 06:57:41 +02:00
|
|
|
{
|
|
|
|
status = listener.get_status(id);
|
2018-05-12 02:30:57 +02:00
|
|
|
if (status.valid())
|
2018-05-11 06:57:41 +02:00
|
|
|
{
|
2019-04-20 01:47:23 +02:00
|
|
|
const std::vector<string> vec = get_urls(status.content());
|
|
|
|
const string message =
|
|
|
|
std::accumulate(vec.begin(), vec.end(), string(),
|
|
|
|
[](const string &s1, const string s2)
|
|
|
|
{ return s1 + s2 + " \n"; });
|
2018-05-12 02:30:57 +02:00
|
|
|
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))
|
|
|
|
{
|
2019-01-27 04:51:03 +01:00
|
|
|
syslog(LOG_ERR, "could not send reply to %s",
|
|
|
|
id.c_str());
|
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(),
|
2018-05-22 12:05:55 +02:00
|
|
|
"I couldn't find the message you replied to. 😞 \n"
|
|
|
|
"Maybe the federation is a bit wonky at the moment.");
|
2018-05-12 02:30:57 +02:00
|
|
|
}
|
2018-05-11 06:57:41 +02:00
|
|
|
}
|
2018-05-20 14:48:12 +02:00
|
|
|
new_messages.clear();
|
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-22 13:44:41 +02:00
|
|
|
closelog();
|
2018-05-13 16:11:37 +02:00
|
|
|
curlpp::terminate();
|
2018-05-11 02:21:44 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|