~singpolyma/biboumi

ref: ad22be41719e29b075d707eb964fee1c4c30cf77 biboumi/src/main.cpp -rw-r--r-- 6.7 KiB
ad22be41 — louiz’ Do not fail to build if litesql is not there 7 years ago
                                                                                
d600a284 Florent Le Coz
d9d30dd7 Florent Le Coz
f0d9273d Florent Le Coz
3afb63a6 Florent Le Coz
ea0b2f2b Florent Le Coz
4c1b9abe louiz’
3afb63a6 Florent Le Coz
b86547dc Florent Le Coz
4c1b9abe louiz’
ad22be41 louiz’
9167cdf1 Florent Le Coz
d872c2b4 louiz’
3afb63a6 Florent Le Coz
ae02e58b louiz’
8fd27466 Florent Le Coz
ae02e58b louiz’
3afb63a6 Florent Le Coz
bf7b05ef Florent Le Coz
b60cbda4 Florent Le Coz
58000c36 louiz’
db503b23 louiz’
b60cbda4 Florent Le Coz
f89361c3 louiz’
d9d30dd7 Florent Le Coz
3afb63a6 Florent Le Coz
d9d30dd7 Florent Le Coz
5c9d2c23 Florent Le Coz
d9d30dd7 Florent Le Coz
5f82c937 Florent Le Coz
3afb63a6 Florent Le Coz
8fd27466 Florent Le Coz
5f82c937 Florent Le Coz
8fd27466 Florent Le Coz
f0d9273d Florent Le Coz
64c1b28c Florent Le Coz
f89361c3 louiz’
46ff7366 louiz’
085a48ea louiz’
33cdc601 Florent Le Coz
46ff7366 louiz’
b60cbda4 Florent Le Coz
46ff7366 louiz’
f0d9273d Florent Le Coz
b60cbda4 Florent Le Coz
46ff7366 louiz’
df59a091 Florent Le Coz
f0d9273d Florent Le Coz
ad22be41 louiz’
4c1b9abe louiz’
ad22be41 louiz’
4c1b9abe louiz’
897b281e Florent Le Coz
3afb63a6 Florent Le Coz
8fd27466 Florent Le Coz
e8671042 louiz’
3afb63a6 Florent Le Coz
8fd27466 Florent Le Coz
e8671042 louiz’
6804f315 Florent Le Coz
8fd27466 Florent Le Coz
3afb63a6 Florent Le Coz
d5324ac7 Florent Le Coz
d872c2b4 louiz’
d5324ac7 Florent Le Coz
99aba566 Florent Le Coz
d872c2b4 louiz’
b86547dc Florent Le Coz
5c9d2c23 Florent Le Coz
5507adbe Florent Le Coz
3afb63a6 Florent Le Coz
5c9d2c23 Florent Le Coz
e8e592d1 Florent Le Coz
d872c2b4 louiz’
3afb63a6 Florent Le Coz
9167cdf1 Florent Le Coz
3afb63a6 Florent Le Coz
5f82c937 Florent Le Coz
3afb63a6 Florent Le Coz
d872c2b4 louiz’
d5324ac7 Florent Le Coz
a50ca30e Florent Le Coz
3afb63a6 Florent Le Coz
8fd27466 Florent Le Coz
7115aa3b Florent Le Coz
5f82c937 Florent Le Coz
8fd27466 Florent Le Coz
ffc820e2 Florent Le Coz
d872c2b4 louiz’
cdc3183d Florent Le Coz
ffc820e2 Florent Le Coz
a50ca30e Florent Le Coz
d872c2b4 louiz’
a50ca30e Florent Le Coz
d872c2b4 louiz’
a50ca30e Florent Le Coz
3afb63a6 Florent Le Coz
99aba566 Florent Le Coz
5507adbe Florent Le Coz
3afb63a6 Florent Le Coz
b86547dc Florent Le Coz
a50ca30e Florent Le Coz
3afb63a6 Florent Le Coz
d5324ac7 Florent Le Coz
810ea19f Florent Le Coz
03a8accc Florent Le Coz
64c1b28c Florent Le Coz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <xmpp/biboumi_component.hpp>
#include <utils/timed_events.hpp>
#include <network/poller.hpp>
#include <config/config.hpp>
#include <logger/logger.hpp>
#include <utils/xdg.hpp>
#include <utils/reload.hpp>

#ifdef CARES_FOUND
# include <network/dns_handler.hpp>
#endif

#include <atomic>
#include <signal.h>
#ifdef USE_DATABASE
# include <litesql.hpp>
#endif

#include <identd/identd_server.hpp>

// A flag set by the SIGINT signal handler.
static std::atomic<bool> stop(false);
// Flag set by the SIGUSR1/2 signal handler.
static std::atomic<bool> reload(false);
// A flag indicating that we are wanting to exit the process. i.e: if this
// flag is set and all connections are closed, we can exit properly.
static bool exiting = false;

/**
 * Provide an helpful message to help the user write a minimal working
 * configuration file.
 */
int config_help(const std::string& missing_option)
{
  if (!missing_option.empty())
    log_error("Configuration error: empty value for option ", missing_option, ".");
  log_error("Please provide a configuration file filled like this:\n\n"
            "hostname=irc.example.com\npassword=S3CR3T");
  return 1;
}

int display_help()
{
  std::cout << "Usage: biboumi [configuration_file]" << std::endl;
  return 0;
}

static void sigint_handler(int sig, siginfo_t*, void*)
{
  // In 2 seconds, repeat the same signal, to force the exit
  TimedEventsManager::instance().add_event(TimedEvent(std::chrono::steady_clock::now() + 2s,
                                    [sig]() { raise(sig); }));
  stop.store(true);
}

static void sigusr_handler(int, siginfo_t*, void*)
{
  reload.store(true);
}

int main(int ac, char** av)
{
  if (ac > 1)
    {
      const std::string arg = av[1];
      if (arg.size() >= 2 && arg[0] == '-' && arg[1] == '-')
        {
          if (arg == "--help")
            return display_help();
          else
            {
              std::cerr << "Unknow command line option: " << arg << std::endl;
              return 1;
            }
        }
    }
  const std::string conf_filename = ac > 1 ? av[1] : xdg_config_path("biboumi.cfg");
  std::cout << "Using configuration file: " << conf_filename << std::endl;

  if (!Config::read_conf(conf_filename))
    return config_help("");

  const std::string password = Config::get("password", "");
  if (password.empty())
    return config_help("password");
  const std::string hostname = Config::get("hostname", "");
  if (hostname.empty())
    return config_help("hostname");


#ifdef USE_DATABASE
  try {
    open_database();
  } catch (const litesql::DatabaseError&) {
    return 1;
  }
#endif

  // 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;
  sigemptyset(&mask);
  sigaddset(&mask, SIGINT);
  sigaddset(&mask, SIGTERM);
  sigaddset(&mask, SIGUSR1);
  sigaddset(&mask, SIGUSR2);
  sigprocmask(SIG_BLOCK, &mask, nullptr);

  // Install the signals used to exit the process cleanly, or reload the
  // config
  struct sigaction on_sigint;
  on_sigint.sa_sigaction = &sigint_handler;
  // All signals must be blocked while a signal handler is running
  sigfillset(&on_sigint.sa_mask);
  // we want to catch that signal only once.
  // Sending SIGINT again will "force" an exit
  on_sigint.sa_flags = SA_RESETHAND;
  sigaction(SIGINT, &on_sigint, nullptr);
  sigaction(SIGTERM, &on_sigint, nullptr);

  // Install a signal to reload the config on SIGUSR1/2
  struct sigaction on_sigusr;
  on_sigusr.sa_sigaction = &sigusr_handler;
  sigfillset(&on_sigusr.sa_mask);
  on_sigusr.sa_flags = 0;
  sigaction(SIGUSR1, &on_sigusr, nullptr);
  sigaction(SIGUSR2, &on_sigusr, nullptr);

  auto p = std::make_shared<Poller>();

  auto xmpp_component =
    std::make_shared<BiboumiComponent>(p, hostname, password);
  xmpp_component->start();

  IdentdServer identd(*xmpp_component, p, static_cast<uint16_t>(Config::get_int("identd_port", 113)));

#ifdef CARES_FOUND
  DNSHandler::instance.watch_dns_sockets(p);
#endif
  auto timeout = TimedEventsManager::instance().get_timeout();
  while (p->poll(timeout) != -1)
  {
    TimedEventsManager::instance().execute_expired_events();
    // 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...");
#ifdef SYSTEMD_FOUND
      sd_notify(0, "STOPPING=1");
#endif
      exiting = true;
      stop.store(false);
      xmpp_component->shutdown();
      identd.shutdown();
      // Cancel the timer for a potential reconnection
      TimedEventsManager::instance().cancel("XMPP reconnection");
    }
    if (reload)
    {
      log_info("Signal received, reloading the config...");
      ::reload_process();
      reload.store(false);
    }
    // Reconnect to the XMPP server if this was not intended.  This may have
    // 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->is_connected() &&
        !xmpp_component->is_connecting())
    {
      if (xmpp_component->ever_auth)
        {
          if (xmpp_component->first_connection_try == true)
            { // immediately re-try to connect
              xmpp_component->reset();
              xmpp_component->start();
            }
          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));
            }
        }
      else
        identd.shutdown();
    }
    // If the only existing connection is the one to the XMPP component:
    // close the XMPP stream.
    if (exiting && xmpp_component->is_connecting())
      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.");
  return 0;
}