irccd  3.0.3
plugin.hpp
1 /*
2  * plugin.hpp -- JavaScript plugins for irccd
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_JS_PLUGIN_HPP
20 #define IRCCD_JS_PLUGIN_HPP
21 
27 #include <irccd/daemon/plugin.hpp>
28 #include <irccd/daemon/server.hpp>
29 
30 #include "duk.hpp"
31 
35 namespace irccd::js {
36 
37 class api;
38 
44 class plugin : public daemon::plugin {
45 public:
49  static inline const std::string_view config_property{DUK_HIDDEN_SYMBOL("config")};
50 
54  static inline const std::string_view templates_property{DUK_HIDDEN_SYMBOL("templates")};
55 
59  static inline const std::string_view paths_property{DUK_HIDDEN_SYMBOL("paths")};
60 
61 private:
62  // JavaScript context.
63  mutable duk::context context_;
64 
65  // Path to Javascript script file.
66  std::string path_;
67 
68  void push() noexcept;
69 
70  template <typename Value, typename... Args>
71  void push(Value&& value, Args&&... args);
72 
73  template <typename... Args>
74  void call(const std::string&, Args&&... args);
75 
76 public:
83  plugin(std::string id, std::string path);
84 
90  auto get_context() noexcept -> duk::context&;
91 
95  void open();
96 
100  auto get_name() const noexcept -> std::string_view override;
101 
105  auto get_author() const noexcept -> std::string_view override;
106 
110  auto get_license() const noexcept -> std::string_view override;
111 
115  auto get_summary() const noexcept -> std::string_view override;
116 
120  auto get_version() const noexcept -> std::string_view override;
121 
125  auto get_options() const -> map override;
126 
130  void set_options(const map& map) override;
131 
135  auto get_templates() const -> map override;
136 
140  void set_templates(const map& map) override;
141 
145  auto get_paths() const -> map override;
146 
150  void set_paths(const map& map) override;
151 
155  void handle_command(daemon::bot& bot, const daemon::message_event& event) override;
156 
160  void handle_connect(daemon::bot& bot, const daemon::connect_event& event) override;
161 
165  void handle_disconnect(daemon::bot& bot, const daemon::disconnect_event& event) override;
166 
170  void handle_invite(daemon::bot& bot, const daemon::invite_event& event) override;
171 
175  void handle_join(daemon::bot& bot, const daemon::join_event& event) override;
176 
180  void handle_kick(daemon::bot& bot, const daemon::kick_event& event) override;
181 
185  void handle_load(daemon::bot& bot) override;
186 
190  void handle_message(daemon::bot& bot, const daemon::message_event& event) override;
191 
195  void handle_me(daemon::bot& bot, const daemon::me_event& event) override;
196 
200  void handle_mode(daemon::bot& bot, const daemon::mode_event& event) override;
201 
205  void handle_names(daemon::bot& bot, const daemon::names_event& event) override;
206 
210  void handle_nick(daemon::bot& bot, const daemon::nick_event& event) override;
211 
215  void handle_notice(daemon::bot& bot, const daemon::notice_event& event) override;
216 
220  void handle_part(daemon::bot& bot, const daemon::part_event& event) override;
221 
225  void handle_reload(daemon::bot& bot) override;
226 
230  void handle_topic(daemon::bot& bot, const daemon::topic_event& event) override;
231 
235  void handle_unload(daemon::bot& bot) override;
236 
240  void handle_whois(daemon::bot& bot, const daemon::whois_event& event) override;
241 };
242 
248 class plugin_loader : public daemon::plugin_loader {
249 public:
253  using modules = std::vector<std::unique_ptr<api>>;
254 
255 private:
256  daemon::bot& bot_;
257  modules modules_;
258 
259 public:
268  std::vector<std::string> directories = {},
269  std::vector<std::string> extensions = {".js"}) noexcept;
270 
274  ~plugin_loader() noexcept;
275 
281  auto get_modules() const noexcept -> const modules&;
282 
288  auto get_modules() noexcept -> modules&;
289 
293  auto open(std::string_view id, std::string_view file) -> std::shared_ptr<daemon::plugin>;
294 };
295 
296 namespace duk {
297 
301 template <>
302 struct type_traits<daemon::whois_info> : public std::true_type {
309  static void push(duk_context* ctx, const daemon::whois_info& who);
310 };
311 
312 } // !duk
313 
314 } // !irccd::js
315 
316 #endif // !IRCCD_PLUGIN_JS_HPP
Irccd main instance.
Definition: bot.hpp:58
Abstract plugin.
Definition: plugin.hpp:61
std::unordered_map< std::string, std::string > map
Definition: plugin.hpp:68
RAII based Duktape handler.
Definition: duk.hpp:95
Object for Javascript to perform I/O.
Definition: file_api.hpp:56
Implementation for searching Javascript plugins.
Definition: plugin.hpp:248
plugin_loader(daemon::bot &bot, std::vector< std::string > directories={}, std::vector< std::string > extensions={".js"}) noexcept
std::vector< std::unique_ptr< api > > modules
The list of Javascript API modules.
Definition: plugin.hpp:253
Javascript plugins for irccd.
Definition: plugin.hpp:44
auto get_author() const noexcept -> std::string_view override
void handle_join(daemon::bot &bot, const daemon::join_event &event) override
void handle_mode(daemon::bot &bot, const daemon::mode_event &event) override
void handle_me(daemon::bot &bot, const daemon::me_event &event) override
void set_paths(const map &map) override
void set_options(const map &map) override
auto get_options() const -> map override
void handle_topic(daemon::bot &bot, const daemon::topic_event &event) override
void handle_command(daemon::bot &bot, const daemon::message_event &event) override
void handle_message(daemon::bot &bot, const daemon::message_event &event) override
void handle_names(daemon::bot &bot, const daemon::names_event &event) override
auto get_name() const noexcept -> std::string_view override
void handle_nick(daemon::bot &bot, const daemon::nick_event &event) override
void handle_load(daemon::bot &bot) override
static const std::string_view templates_property
Definition: plugin.hpp:54
void handle_whois(daemon::bot &bot, const daemon::whois_event &event) override
auto get_context() noexcept -> duk::context &
auto get_summary() const noexcept -> std::string_view override
void handle_notice(daemon::bot &bot, const daemon::notice_event &event) override
void handle_connect(daemon::bot &bot, const daemon::connect_event &event) override
void handle_part(daemon::bot &bot, const daemon::part_event &event) override
auto get_templates() const -> map override
void handle_unload(daemon::bot &bot) override
auto get_version() const noexcept -> std::string_view override
void handle_disconnect(daemon::bot &bot, const daemon::disconnect_event &event) override
void handle_reload(daemon::bot &bot) override
auto get_license() const noexcept -> std::string_view override
void handle_invite(daemon::bot &bot, const daemon::invite_event &event) override
auto get_paths() const -> map override
static const std::string_view config_property
Definition: plugin.hpp:49
void handle_kick(daemon::bot &bot, const daemon::kick_event &event) override
static const std::string_view paths_property
Definition: plugin.hpp:59
void set_templates(const map &map) override
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
Javascript namespace.
Definition: api.hpp:42
Definition: bot.hpp:253
Describe a whois information.
Definition: server.hpp:72
static void push(duk_context *ctx, const daemon::whois_info &who)
Operations on different types.
Definition: duk.hpp:254