irccd  3.0.3
bot.hpp
1 /*
2  * bot.hpp -- main bot class
3  *
4  * Copyright (c) 2013-2019 David Demelier <markand@malikania.fr>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef IRCCD_DAEMON_BOT_HPP
20 #define IRCCD_DAEMON_BOT_HPP
21 
27 #include <irccd/sysconfig.hpp>
28 
29 #include <memory>
30 #include <system_error>
31 
32 #include <boost/asio/io_service.hpp>
33 
34 #include <irccd/config.hpp>
35 
36 namespace irccd {
37 
41 namespace daemon {
42 
43 namespace logger {
44 
45 class filter;
46 class sink;
47 
48 } // !logger
49 
50 class plugin_service;
51 class rule_service;
52 class server_service;
53 class transport_service;
54 
58 class bot {
59 private:
60  // Configuration.
61  config config_;
62 
63  // Main io service.
64  boost::asio::io_service& service_;
65 
66  // Tells if the configuration has already been called.
67  bool loaded_{false};
68 
69  // Custom logger.
70  std::unique_ptr<logger::sink> sink_;
71  std::unique_ptr<logger::filter> filter_;
72 
73  // Services.
74  std::unique_ptr<server_service> server_service_;
75  std::unique_ptr<transport_service> tpt_service_;
76  std::unique_ptr<rule_service> rule_service_;
77  std::unique_ptr<plugin_service> plugin_service_;
78 
79  // Not copyable and not movable because services have references.
80  bot(const bot&) = delete;
81  bot(bot&&) = delete;
82 
83  void operator=(const bot&) = delete;
84  void operator=(bot&&) = delete;
85 
86  // Load functions.
87  void load_logs_file(const ini::section&);
88  void load_logs_syslog();
89  void load_logs();
90  void load_templates();
91 
92 public:
103  bot(boost::asio::io_service& service, std::string config = "");
104 
108  ~bot();
109 
115  auto get_config() const noexcept -> const config&;
116 
122  void set_config(config cfg) noexcept;
123 
129  auto get_service() const noexcept -> const boost::asio::io_service&;
130 
136  auto get_service() noexcept -> boost::asio::io_service&;
137 
143  auto get_log() const noexcept -> const logger::sink&;
144 
150  auto get_log() noexcept -> logger::sink&;
151 
158  void set_log(std::unique_ptr<logger::sink> sink) noexcept;
159 
165  auto get_servers() noexcept -> server_service&;
166 
172  auto get_transports() noexcept -> transport_service&;
173 
179  auto get_rules() noexcept -> rule_service&;
180 
186  auto get_plugins() noexcept -> plugin_service&;
187 
191  void load() noexcept;
192 };
193 
197 class bot_error : public std::system_error {
198 public:
202  enum error {
204  no_error = 0,
205 
207  not_irccd,
208 
210  incompatible_version,
211 
213  auth_required,
214 
216  invalid_auth,
217 
219  invalid_message,
220 
222  invalid_command,
223 
226  };
227 
231  using system_error::system_error;
232 };
233 
239 auto bot_category() noexcept -> const std::error_category&;
240 
247 auto make_error_code(bot_error::error e) noexcept -> std::error_code;
248 
249 } // !daemon
250 
251 } // !irccd
252 
253 namespace std {
254 
255 template <>
256 struct is_error_code_enum<irccd::daemon::bot_error::error> : public std::true_type {
257 };
258 
259 } // !std
260 
261 #endif // !IRCCD_DAEMON_IRCCD_HPP
irccd::daemon::bot::load
void load() noexcept
irccd::daemon::logger::sink
Interface to implement new logger mechanisms.
Definition: logger.hpp:121
irccd::daemon::bot::set_config
void set_config(config cfg) noexcept
irccd::daemon::plugin_service
Manage plugins.
Definition: plugin_service.hpp:50
irccd::daemon::rule_service
Store and solve rules.
Definition: rule_service.hpp:47
irccd::daemon::bot::get_config
auto get_config() const noexcept -> const config &
irccd::daemon::server_service
Manage IRC servers.
Definition: server_service.hpp:51
irccd::daemon::logger::filter
Filter messages before printing them.
Definition: logger.hpp:283
irccd::config
Read .ini configuration file for irccd.
Definition: config.hpp:39
irccd::daemon::bot::get_rules
auto get_rules() noexcept -> rule_service &
irccd::daemon::make_error_code
auto make_error_code(bot_error::error e) noexcept -> std::error_code
irccd::daemon::bot_error::error
error
Irccd related errors.
Definition: bot.hpp:202
irccd::daemon::transport_service
Manage transport servers and clients.
Definition: transport_service.hpp:49
irccd::ini::section
Section that contains one or more options.
Definition: ini.hpp:295
irccd::daemon::bot::get_transports
auto get_transports() noexcept -> transport_service &
irccd::daemon::bot_error::incomplete_message
@ incomplete_message
Definition: bot.hpp:225
irccd::daemon::bot
Irccd main instance.
Definition: bot.hpp:58
irccd::daemon::bot::bot
bot(boost::asio::io_service &service, std::string config="")
irccd::daemon::bot_category
auto bot_category() noexcept -> const std::error_category &
irccd
Parent namespace.
Definition: acceptor.hpp:43
std
Definition: bot.hpp:253
irccd::daemon::bot::get_plugins
auto get_plugins() noexcept -> plugin_service &
irccd::daemon::bot::~bot
~bot()
irccd::daemon::bot::set_log
void set_log(std::unique_ptr< logger::sink > sink) noexcept
irccd::daemon::bot::get_servers
auto get_servers() noexcept -> server_service &
irccd::daemon::bot_error
Irccd error.
Definition: bot.hpp:197
irccd::daemon::bot::get_log
auto get_log() const noexcept -> const logger::sink &
irccd::daemon::logger::logger
Logger object.
Definition: logger.hpp:84
irccd::daemon::bot::get_service
auto get_service() const noexcept -> const boost::asio::io_service &