expandurl-mastodon/src/main.cpp

118 lines
3.3 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 <curlpp/cURLpp.hpp>
#include "expandurl-mastodon.hpp"
using std::cout;
2018-05-11 06:57:41 +02:00
using std::cerr;
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;
cout << "Closing, this could take a few seconds...\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-11 06:57:41 +02:00
Listener listener;
listener.start();
while (running)
{
std::this_thread::sleep_for(std::chrono::seconds(5));
if (!listener.stillrunning())
{
cout << "DEBUG: Reestablishing connection...\n";
listener.start();
}
2018-05-11 06:57:41 +02:00
for (Easy::Notification &notif : listener.get_new_messages())
{
cout << "DEBUG: new messages\n";
const std::uint_fast64_t id = listener.get_parent_id(notif);
cout << "DEBUG: in_reply_to_id: " << id << '\n';
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))
{
cerr << "ERROR: could not send reply to " << id << '\n';
}
}
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(),
"It seems that you didn't reply to a message?");
}
2018-05-11 06:57:41 +02:00
}
}
2018-05-12 10:42:22 +02:00
listener.stop();
curlpp::terminate();
return 0;
}