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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
#![feature(proc_macro_hygiene, decl_macro, optin_builtin_traits, negative_impls)] #![feature(test)] #![deny(clippy::all)] //!# Bot-RS Core //! //! This library is used to implement Plugins and Plugin-Loaders for the Bot-RS platform. //! //! ## Implementing a Plugin //! //! There are two kinds of plugins: **Simple Plugin** and **Streamable Plugin**. //! While the **Simple Plugin** has a simple interface the **Streamable Plugin** enables the developer to implement a custom way to handle the incoming Stream of Messages and convert it into a Stream of outgoing messages. //! This also means sending asynchronous messages. While **Simple Plugins** only react to messages, **Streamable Plugins** can sand messages at any time. //! //! The process from creating the cargo project to building the library files will be described in the following sections. //! //! ### Implementing a Simple Plugin //! //! 0. Install required tools: //! - [rust and cargo through rustup](https:///rustup.rs/) //! - [cargo-edit](https:///github.com/killercup/cargo-edit#installation): To easily edit package dependencies //! //! 1. Create the Cargo project with `cargo new --lib --vcs git <project name>`. And change your working directory to it (`cd hello-plugin`). //! ```ignore //! cargo new --lib --vcs git hello-plugin //! cd hello-plugin //! ``` //! 2. Add the dependency of `bot-rs-core` to the project. Every plugin has to implement the **StreamablePlugin** trait. To simplify this the "derive" feature enables the derive-macro which generates a valid StreamablePlugin implementation from a struct which implements the **Plugin** trait. //! The derived code requires the plugin crate to have the dependency to the [futures crate](https:///crates.io/crates/futures). Which we'll also add. //! As the **Plugin** trait contains async functions it's also required to have an dependency to the [async_trait crate](https:///crates.io/crates/async-trait). //! To handle irc messages coming from twitch (only supported platform yet) a dependency to the [irc-rust crate](https:///crates.io/crates/irc-rust) is also required. //! To also be able to log messages we'll use the [log crate](https:///crates.io/crates/log) with its [env_logger](https:///crates.io/crates/env_logger) implementation for simplicity. //! ```ignore //! cargo add bot-rs-core --features derive && \ //! cargo add futures && \ //! cargo add async-trait && \ //! cargo add irc-rust && \ //! cargo add log && \ //! cargo add env_logger //! ``` //! 3. Add the following snippet to your `Cargo.toml` to compile the library to a loadable library file which will be loaded by a plugin-loader implementation. //! ```ignore //! [lib] //! crate-type = ["cdylib"] //! ``` //! 4. Now we can implement the actual plugin. Replace the contents of the library root file `src/lib.rs` with the following content: //! ```ignore //! // Simple logging facade //! #[macro_use] //! extern crate log; //! //! // Enables the derive-macro for StreamablePlugin. //! #[macro_use] //! extern crate bot_rs_core; //! //! use async_trait::async_trait; //! //! use bot_rs_core::Message; //! use bot_rs_core::plugin::{StreamablePlugin, Plugin, InvocationError, PluginInfo, PluginRegistrar}; //! use std::sync::Arc; //! use bot_rs_core::profile::Profile; //! //! // Reacts to an invocation of `!hello` with `Hello, @<sender name>!`. //! #[derive(StreamablePlugin)] //! struct HelloPlugin { //! profile: Profile //! } //! //! #[async_trait::async_trait] //! impl Plugin for HelloPlugin { //! async fn call(&self, msg: Message) -> Result<Vec<Message>, InvocationError> { //! // Check AccessRights before processing //! if let Some(false) = self.profile.rights().allowed(&msg) { //! // Only process messages not handled by filters or allowed //! return Ok(Vec::with_capacity(0)); //! } //! match &msg { //! Message::Irc(irc_message) => { //! match irc_message.get_command() { //! "PRIVMSG" => { //! let params = irc_message //! .params() //! .expect("missing params in PRIVMSG"); //! // First param in a PRIVMSG is the channel name //! let channel = params.iter() //! .next() //! .expect("no params in PRIVMSG"); //! let trailing = params.trailing; //! if trailing.is_none() { //! // Return no messages //! return Ok(Vec::with_capacity(0)); //! } //! let trailing = trailing.unwrap(); //! //! // Check if '!hello' command was called //! if !trailing.starts_with("!hello") { //! return Ok(Vec::with_capacity(0)); //! } //! //! let prefix = irc_message.prefix().expect("missing prefix in PRIVMSG"); //! let name = prefix.name(); //! //! Ok(vec![Message::Irc(irc_rust::Message::builder("PRIVMSG") //! .param(channel) //! .trailing(&format!("Hello, @{}!", name)) //! .build() //! .expect("failed to build irc message") //! )]) //! } //! } //! } //! } //! } //! //! // Return information about the plugin for identification. //! fn info(&self) -> PluginInfo { //! PluginInfo { //! name: "Hello Plugin".to_string(), //! version: env!("CARGO_PKG_VERSION").to_string(), //! authors: env!("CARGO_PKG_AUTHORS").to_string(), //! repo: option_env!("CARGO_PKG_REPOSITORY") //! .map(|repo| if repo.is_empty() { "No repo".to_string() } else { repo.to_string() }), //! commands: vec!["!hello".to_string()] //! } //! } //! } //! //! // This macro creates a static field which can be loaded by the plugin loader. //! bot_rs_core::export_command!(register); //! //! // The plugin loading mechanism uses this function for load and register. Initializing loggers and other dependencies has to be done here. //! extern "C" fn register(registrar: &mut PluginRegistrar) { //! env_logger::init(); //! // Is set on startup by Bot-RS CLI Tool //! let profile = Profile::active().unwrap(); //! registrar.register(Arc::new(HelloPlugin{ profile })) //! } //! ``` //! 5. (Optional) Optimize plugin file for size to reduce the file size produced through `cargo build`. To do this copy the following snippet to your `Cargo.toml`: //! ```ignore //! [profile.release] //! lto = true //! codegen-units = 1 //! opt-level = "z" //! ``` //! For more infos read this guide on reducing the size of rust binaries/libraries: [](https://github.com/johnthagen/min-sized-rust). //! 6. Building the plugin file: `cargo build --release` //! //! ### Implementing a StreamablePlugin //! //! 0. Install required tools: //! - [rust and cargo through rustup](https://rustup.rs/) //! - [cargo-edit](https://github.com/killercup/cargo-edit#installation): To easily edit package dependencies //! //! 1. Create the Cargo project with `cargo new --lib --vcs git <project name>`. And change your working directory to it (`cd hello-plugin`). //! ```ignore //! cargo new --lib --vcs git hello-plugin //! cd hello-plugin //! ``` //! 2. Add the dependency of `bot-rs-core` to the project. //! As the **StreamablePlugin** trait contains async functions it's also required to have an dependency to the [async_trait crate](https://crates.io/crates/async-trait). //! To handle irc messages coming from twitch (only supported platform yet) a dependency to the [irc-rust crate](https://crates.io/crates/irc-rust) is also required. //! To also be able to log messages we'll use the [log crate](https://crates.io/crates/log) with its [env_logger](https://crates.io/crates/env_logger) implementation for simplicity. //! This time we'll also require the dependency to the [futures crate](https://crates.io/crates/futures) for our StreamablePlugin implementation. //! ```ignore //! cargo add bot-rs-core && \ //! cargo add async-trait && \ //! cargo add irc-rust && \ //! cargo add log && \ //! cargo add env_logger && \ //! cargo add futures //! ``` //! 3. Add the following snippet to your `Cargo.toml` to compile the library to a loadable library file which will be loaded by a plugin-loader implementation. //! ```ignore //! [lib] //! crate-type = ["cdylib"] //! ``` //! 4. Now we can implement the actual plugin. Replace the contents of the library root file `src/lib.rs` with the following content: //! ```ignore //! // Simple logging facade //! #[macro_use] //! extern crate log; //! extern crate futures; //! //! // Enables the derive-macro for StreamablePlugin. //! #[macro_use] //! extern crate bot_rs_core; //! //! use async_trait::async_trait; //! //! use bot_rs_core::Message; //! use bot_rs_core::plugin::{StreamablePlugin, Plugin, InvocationError, PluginInfo, PluginRegistrar}; //! use futures::channel::mpsc::{UnboundedReceiver, UnboundedSender}; //! use std::sync::Arc; //! use futures::{StreamExt, SinkExt}; //! //! /// Reacts to an invocation of `!hello` with `Hello, @<sender name>!`. //! struct HelloPlugin; //! //! // For simplicity we'll be using the Plugin implementation showed in the previous section. But we'll implement the `StreamablePlugin` ourself this time. //! //! // <insert Plugin implementation of previous section here> //! //! // This implementation doesn'T require spawning new threads. This should be handled by the plugin-loader. //! #[async_trait] //! impl StreamablePlugin for HelloPlugin { //! async fn stream(&self, //! mut input: UnboundedReceiver<Message>, //! mut output: UnboundedSender<Vec<Message>>) //! -> Result<(), InvocationError> { //! // Read next message from input channel //! while let Some(msg) = input.next().await { //! // Call out Plugin implementation //! let results = self.call(msg).await?; //! // Send the results to the output channel //! output.send(results) //! .await.expect("failed to send results to output"); //! } //! Ok(()) //! } //! //! // Return information about the plugin for identification. //! fn info(&self) -> PluginInfo { //! Plugin::info(self) //! } //! } //! //! // This macro creates a static field which can be loaded by the plugin loader. //! export_command!(register); //! //! // The plugin loading mechanism uses this function for load and register. Initializing loggers and other dependencies has to be done here. //! extern "C" fn register(registrar: &mut PluginRegistrar) { //! env_logger::init(); //! registrar.register(Arc::new(HelloPlugin)) //! } //! ``` //! 5. (Optional) Optimize plugin file for size to reduce the file size produced through `cargo build`. To do this copy the following snippet to your `Cargo.toml`: //! ```ignore //! [profile.release] //! lto = true //! codegen-units = 1 //! opt-level = "z" //! ``` //! For more infos read this guide on reducing the size of rust binaries/libraries: [](https://github.com/johnthagen/min-sized-rust). //! 6. Building the plugin file: `cargo build --release` //! //! ## Plugin-Loader //! //! This is currently not publicly documented. The only implementation currently present is the [botrs cli](https://github.com/MoBlaa/bot-rs-cli). To use the plugins this cli tool is required. #[macro_use] extern crate log; #[macro_use] extern crate serde; #[macro_use] extern crate async_trait; #[cfg(feature = "derive")] extern crate bot_rs_core_derive; #[cfg(feature = "plugin-loader")] extern crate rocket; #[cfg(test)] extern crate test; #[cfg(feature = "twitch-api")] #[macro_use] extern crate derive_more; #[cfg(feature = "default")] pub mod auth; #[cfg(feature = "default")] pub mod command_access; #[cfg(feature = "default")] pub mod plugin; #[cfg(feature = "plugin-loader")] pub mod plugins; #[cfg(feature = "default")] pub mod profile; #[cfg(feature = "twitch-api")] pub mod twitch_api; #[cfg(feature = "twitch-api")] mod utils; #[cfg(feature = "derive")] pub use bot_rs_core_derive::*; use core::fmt; use std::fmt::{Display, Formatter}; pub const CORE_VERSION: &str = env!("CARGO_PKG_VERSION"); pub const RUSTC_VERSION: &str = env!("RUSTC_VERSION"); pub const ENV_JOINED_CHANNELS: &str = "BRS_JOINED_CHANNELS"; #[derive(Clone, Eq, PartialEq, Hash, Debug, Serialize, Deserialize)] pub enum Message { Irc(irc_rust::Message), } impl Display for Message { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { Message::Irc(msg) => { let str_msg = msg.to_string(); let byte_len = str_msg.bytes().len(); if byte_len > 512 { error!( "Raw IRC Message exceeds exceeds 512 Byte length: {}", byte_len ); Err(fmt::Error) } else { write!(f, "{}", msg) } } } } } impl From<irc_rust::Message> for Message { fn from(irc: irc_rust::Message) -> Self { Message::Irc(irc) } }