irccd  3.0.3
plugin.hpp
1 /*
2  * plugin.hpp -- irccd JavaScript plugin interface
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_PLUGIN_HPP
20 #define IRCCD_DAEMON_PLUGIN_HPP
21 
27 #include <irccd/sysconfig.hpp>
28 
29 #include <memory>
30 #include <string>
31 #include <string_view>
32 #include <system_error>
33 #include <unordered_map>
34 #include <vector>
35 
36 namespace irccd::daemon {
37 
38 class bot;
39 
40 struct connect_event;
41 struct disconnect_event;
42 struct invite_event;
43 struct join_event;
44 struct kick_event;
45 struct me_event;
46 struct message_event;
47 struct mode_event;
48 struct names_event;
49 struct nick_event;
50 struct notice_event;
51 struct part_event;
52 struct topic_event;
53 struct whois_event;
54 
61 class plugin : public std::enable_shared_from_this<plugin> {
62 public:
68  using map = std::unordered_map<std::string, std::string>;
69 
70 private:
71  std::string id_;
72 
73 public:
80  plugin(std::string id) noexcept;
81 
85  virtual ~plugin() = default;
86 
92  auto get_id() const noexcept -> const std::string&;
93 
99  virtual auto get_name() const noexcept -> std::string_view = 0;
100 
106  virtual auto get_author() const noexcept -> std::string_view;
107 
113  virtual auto get_license() const noexcept -> std::string_view;
114 
120  virtual auto get_summary() const noexcept -> std::string_view;
121 
127  virtual auto get_version() const noexcept -> std::string_view;
128 
134  virtual auto get_options() const -> map;
135 
141  virtual void set_options(const map& map);
142 
148  virtual auto get_templates() const -> map;
149 
155  virtual void set_templates(const map& map);
156 
162  virtual auto get_paths() const -> map;
163 
169  virtual void set_paths(const map& map);
170 
179  virtual void handle_command(bot& bot, const message_event& event);
180 
187  virtual void handle_connect(bot& bot, const connect_event& event);
188 
196 
203  virtual void handle_invite(bot& bot, const invite_event& event);
204 
211  virtual void handle_join(bot& bot, const join_event& event);
212 
219  virtual void handle_kick(bot& bot, const kick_event& event);
220 
226  virtual void handle_load(bot& bot);
227 
234  virtual void handle_message(bot& bot, const message_event& event);
235 
242  virtual void handle_me(bot& bot, const me_event& event);
243 
250  virtual void handle_mode(bot& bot, const mode_event& event);
251 
258  virtual void handle_names(bot& bot, const names_event& event);
259 
266  virtual void handle_nick(bot& bot, const nick_event& event);
267 
274  virtual void handle_notice(bot& bot, const notice_event& event);
275 
282  virtual void handle_part(bot& bot, const part_event& event);
283 
289  virtual void handle_reload(bot& bot);
290 
297  virtual void handle_topic(bot& bot, const topic_event& event);
298 
304  virtual void handle_unload(bot& bot);
305 
312  virtual void handle_whois(bot& bot, const whois_event& event);
313 };
314 
329 private:
330  std::vector<std::string> directories_;
331  std::vector<std::string> extensions_;
332 
333 public:
345  plugin_loader(std::vector<std::string> directories = {},
346  std::vector<std::string> extensions = {}) noexcept;
347 
351  virtual ~plugin_loader() = default;
352 
359  virtual auto is_supported(std::string_view path) noexcept -> bool;
360 
372  virtual auto open(std::string_view id, std::string_view file) -> std::shared_ptr<plugin> = 0;
373 
381  virtual auto find(std::string_view id) -> std::shared_ptr<plugin>;
382 };
383 
388 class plugin_error : public std::system_error {
389 public:
393  enum error {
395  no_error = 0,
396 
398  invalid_identifier,
399 
401  not_found,
402 
404  exec_error,
405 
408  };
409 
410 private:
411  std::string id_;
412  std::string message_;
413  std::string what_;
414 
415 public:
423  plugin_error(error code, std::string id, std::string message = "");
424 
430  auto get_id() const noexcept -> const std::string&;
431 
437  auto get_message() const noexcept -> const std::string&;
438 
444  auto what() const noexcept -> const char* override;
445 };
446 
452 auto plugin_category() -> const std::error_category&;
453 
460 auto make_error_code(plugin_error::error e) -> std::error_code;
461 
462 } // !irccd::daemon
463 
468 namespace std {
469 
470 template <>
471 struct is_error_code_enum<irccd::daemon::plugin_error::error> : public std::true_type {
472 };
473 
474 } // !std
475 
480 #endif // !IRCCD_DAEMON_PLUGIN_HPP
Irccd main instance.
Definition: bot.hpp:58
Plugin error.
Definition: plugin.hpp:388
plugin_error(error code, std::string id, std::string message="")
error
Plugin related errors.
Definition: plugin.hpp:393
@ already_exists
Definition: plugin.hpp:407
auto get_id() const noexcept -> const std::string &
Abstract interface for searching plugins.
Definition: plugin.hpp:328
plugin_loader(std::vector< std::string > directories={}, std::vector< std::string > extensions={}) noexcept
virtual auto open(std::string_view id, std::string_view file) -> std::shared_ptr< plugin >=0
virtual auto find(std::string_view id) -> std::shared_ptr< plugin >
virtual auto is_supported(std::string_view path) noexcept -> bool
virtual ~plugin_loader()=default
Abstract plugin.
Definition: plugin.hpp:61
virtual void handle_reload(bot &bot)
virtual void set_options(const map &map)
virtual ~plugin()=default
virtual void handle_unload(bot &bot)
virtual void handle_notice(bot &bot, const notice_event &event)
virtual void handle_disconnect(bot &bot, const disconnect_event &event)
virtual auto get_paths() const -> map
virtual void handle_kick(bot &bot, const kick_event &event)
virtual void handle_message(bot &bot, const message_event &event)
virtual void set_templates(const map &map)
virtual void handle_nick(bot &bot, const nick_event &event)
virtual auto get_name() const noexcept -> std::string_view=0
std::unordered_map< std::string, std::string > map
Definition: plugin.hpp:68
virtual void handle_me(bot &bot, const me_event &event)
virtual void handle_invite(bot &bot, const invite_event &event)
virtual auto get_templates() const -> map
virtual void handle_part(bot &bot, const part_event &event)
plugin(std::string id) noexcept
virtual auto get_license() const noexcept -> std::string_view
virtual auto get_author() const noexcept -> std::string_view
virtual auto get_version() const noexcept -> std::string_view
virtual void handle_whois(bot &bot, const whois_event &event)
virtual auto get_options() const -> map
virtual void handle_load(bot &bot)
virtual void handle_mode(bot &bot, const mode_event &event)
virtual void handle_connect(bot &bot, const connect_event &event)
virtual void handle_topic(bot &bot, const topic_event &event)
virtual void handle_join(bot &bot, const join_event &event)
virtual void set_paths(const map &map)
virtual void handle_names(bot &bot, const names_event &event)
auto get_id() const noexcept -> const std::string &
virtual auto get_summary() const noexcept -> std::string_view
virtual void handle_command(bot &bot, const message_event &event)
std::variant< std::monostate, connect_event, disconnect_event, invite_event, join_event, kick_event, me_event, message_event, mode_event, names_event, nick_event, notice_event, part_event, topic_event, whois_event > event
Store all possible events.
Definition: server.hpp:247
Main irccd namespace.
Definition: bot.hpp:41
auto plugin_category() -> const std::error_category &
auto make_error_code(bot_error::error e) noexcept -> std::error_code
Parent namespace.
Definition: acceptor.hpp:43
Definition: bot.hpp:253
Connection success event.
Definition: server.hpp:84
Connection success event.
Definition: server.hpp:92
Invite event.
Definition: server.hpp:100
Join event.
Definition: server.hpp:111
Kick event.
Definition: server.hpp:121
CTCP action event.
Definition: server.hpp:144
Message event.
Definition: server.hpp:133
Mode event.
Definition: server.hpp:155
Names listing event.
Definition: server.hpp:169
Nick change event.
Definition: server.hpp:179
Notice event.
Definition: server.hpp:189
Part event.
Definition: server.hpp:200
Topic event.
Definition: server.hpp:211
Whois event.
Definition: server.hpp:222