diff options
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 75 |
1 files changed, 43 insertions, 32 deletions
diff --git a/src/main.cpp b/src/main.cpp index 488032d..5725584 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,15 +6,14 @@ #include <utils/xdg.hpp> #include <utils/reload.hpp> -#ifdef CARES_FOUND +#ifdef UDNS_FOUND # include <network/dns_handler.hpp> #endif #include <atomic> -#include <signal.h> -#ifdef USE_DATABASE -# include <litesql.hpp> -#endif +#include <csignal> + +#include <identd/identd_server.hpp> // A flag set by the SIGINT signal handler. static std::atomic<bool> stop(false); @@ -89,7 +88,7 @@ int main(int ac, char** av) #ifdef USE_DATABASE try { open_database(); - } catch (const litesql::DatabaseError&) { + } catch (...) { return 1; } #endif @@ -97,7 +96,7 @@ int main(int ac, char** av) // Block the signals we want to manage. They will be unblocked only during // the epoll_pwait or ppoll calls. This avoids some race conditions, // explained in man 2 pselect on linux - sigset_t mask; + sigset_t mask{}; sigemptyset(&mask); sigaddset(&mask, SIGINT); sigaddset(&mask, SIGTERM); @@ -126,13 +125,17 @@ int main(int ac, char** av) sigaction(SIGUSR2, &on_sigusr, nullptr); auto p = std::make_shared<Poller>(); + +#ifdef UDNS_FOUND + DNSHandler dns_handler(p); +#endif + auto xmpp_component = std::make_shared<BiboumiComponent>(p, hostname, password); xmpp_component->start(); -#ifdef CARES_FOUND - DNSHandler::instance.watch_dns_sockets(p); -#endif + IdentdServer identd(*xmpp_component, p, static_cast<uint16_t>(Config::get_int("identd_port", 113))); + auto timeout = TimedEventsManager::instance().get_timeout(); while (p->poll(timeout) != -1) { @@ -140,6 +143,7 @@ int main(int ac, char** av) // Check for empty irc_clients (not connected, or with no joined // channel) and remove them xmpp_component->clean(); + identd.clean(); if (stop) { log_info("Signal received, exiting..."); @@ -149,6 +153,10 @@ int main(int ac, char** av) exiting = true; stop.store(false); xmpp_component->shutdown(); +#ifdef UDNS_FOUND + dns_handler.destroy(); +#endif + identd.shutdown(); // Cancel the timer for a potential reconnection TimedEventsManager::instance().cancel("XMPP reconnection"); } @@ -162,26 +170,36 @@ int main(int ac, char** av) // happened because we sent something invalid to it and it decided to // close the connection. This is a bug that should be fixed, but we // still reconnect automatically instead of dropping everything - if (!exiting && xmpp_component->ever_auth && + if (!exiting && !xmpp_component->is_connected() && !xmpp_component->is_connecting()) { - if (xmpp_component->first_connection_try == true) - { // immediately re-try to connect - xmpp_component->reset(); - xmpp_component->start(); - } + if (xmpp_component->ever_auth) + { + static const std::string reconnect_name{"XMPP reconnection"}; + if (xmpp_component->first_connection_try == true) + { // immediately re-try to connect + xmpp_component->reset(); + xmpp_component->start(); + } + else if (!TimedEventsManager::instance().find_event(reconnect_name)) + { // Re-connecting failed, we now try only each few seconds + auto reconnect_later = [xmpp_component]() + { + xmpp_component->reset(); + xmpp_component->start(); + }; + TimedEvent event(std::chrono::steady_clock::now() + 2s, reconnect_later, reconnect_name); + TimedEventsManager::instance().add_event(std::move(event)); + } + } else - { // Re-connecting failed, we now try only each few seconds - auto reconnect_later = [xmpp_component]() { - xmpp_component->reset(); - xmpp_component->start(); - }; - TimedEvent event(std::chrono::steady_clock::now() + 2s, - reconnect_later, "XMPP reconnection"); - TimedEventsManager::instance().add_event(std::move(event)); - } +#ifdef UDNS_FOUND + dns_handler.destroy(); +#endif + identd.shutdown(); + } } // If the only existing connection is the one to the XMPP component: // close the XMPP stream. @@ -189,18 +207,11 @@ int main(int ac, char** av) xmpp_component->close(); if (exiting && p->size() == 1 && xmpp_component->is_document_open()) xmpp_component->close_document(); -#ifdef CARES_FOUND - if (!exiting) - DNSHandler::instance.watch_dns_sockets(p); -#endif if (exiting) // If we are exiting, do not wait for any timed event timeout = utils::no_timeout; else timeout = TimedEventsManager::instance().get_timeout(); } -#ifdef CARES_FOUND - DNSHandler::instance.destroy(); -#endif if (!xmpp_component->ever_auth) return 1; // To signal that the process did not properly start log_info("All connections cleanly closed, have a nice day."); |