expandurl-mastodon/src/main.cpp

134 lines
4.0 KiB
C++
Raw Normal View History

/* 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>
#include <syslog.h>
#include <unistd.h> // getuid()
#include <curlpp/cURLpp.hpp>
#include "expandurl-mastodon.hpp"
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)
{
syslog(LOG_NOTICE, "Forced close.");
2018-05-11 06:57:41 +02:00
exit(signum);
}
running = false;
syslog(LOG_NOTICE, "Received signal %d, closing...", signum);
std::cerr << "Received signal " << signum << ", closing...\n";
2018-05-11 06:57:41 +02:00
break;
default:
break;
}
}
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);
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 06:57:41 +02:00
Listener listener;
listener.start();
std::vector<Easy::Notification> new_messages = listener.catchup();
2018-05-11 06:57:41 +02:00
while (running)
{
std::this_thread::sleep_for(std::chrono::seconds(5));
if (!listener.stillrunning())
{
2018-05-17 13:57:21 +02:00
listener.stop();
syslog(LOG_DEBUG, "Reestablishing connection...");
listener.start();
syslog(LOG_NOTICE, "Reestablished connection.");
2018-05-21 01:25:33 +02:00
new_messages = listener.catchup();
}
2018-05-11 06:57:41 +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
{
new_messages = listener.get_new_messages();
}
for (Easy::Notification &notif : new_messages)
{
syslog(LOG_DEBUG, "new message");
const std::uint_fast64_t id = listener.get_parent_id(notif);
syslog(LOG_DEBUG, "in_reply_to_id: %lu", id);
2018-05-11 06:57:41 +02:00
Easy::Status status;
2018-05-11 06:57:41 +02:00
if (id > 0)
{
status = listener.get_status(id);
if (status.valid())
2018-05-11 06:57:41 +02:00
{
string message = "";
for (const string &url : get_urls(status.content()))
{
message += url + " \n";
}
if (!message.empty())
{
if (!listener.send_reply(notif.status(), message))
{
syslog(LOG_ERR, "could not send reply to %lu", id);
}
}
else
{
listener.send_reply(notif.status(),
"I couldn't find an URL in the message you "
"replied to. 😞");
}
2018-05-11 06:57:41 +02:00
}
else
{
listener.send_reply(notif.status(),
"I couldn't get the message you replied to. 😞");
2018-05-11 06:57:41 +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-11 06:57:41 +02:00
}
new_messages.clear();
2018-05-11 06:57:41 +02:00
}
2018-05-12 10:42:22 +02:00
listener.stop();
closelog();
curlpp::terminate();
return 0;
}