2022-06-04 19:34:53 +00:00
|
|
|
use servers::{
|
2022-08-04 14:35:33 +00:00
|
|
|
async_trait,
|
2022-06-23 09:07:42 +00:00
|
|
|
plugins::{Command, Event, Plugin, PluginManagerType, Registrar, Result},
|
2022-08-04 14:35:33 +00:00
|
|
|
tcp::Client,
|
2022-06-04 19:34:53 +00:00
|
|
|
};
|
2022-06-04 18:10:21 +00:00
|
|
|
|
|
|
|
struct PluginTest;
|
|
|
|
|
2022-06-21 10:33:13 +00:00
|
|
|
/// Create a new plugin
|
2022-06-04 18:10:21 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl Plugin for PluginTest {
|
2022-06-20 21:22:45 +00:00
|
|
|
/// Name of the plugin.
|
2022-06-04 18:10:21 +00:00
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"test"
|
|
|
|
}
|
|
|
|
|
2022-06-20 21:22:45 +00:00
|
|
|
/// A function will be executed when plugin loading.
|
|
|
|
/// Usally used for initialization.
|
2022-06-04 18:10:21 +00:00
|
|
|
async fn on_plugin_load(&self) {}
|
|
|
|
}
|
|
|
|
|
2022-06-21 10:33:13 +00:00
|
|
|
/// Create a new command
|
2022-06-04 18:10:21 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl Command for PluginTest {
|
2022-06-20 21:22:45 +00:00
|
|
|
/// Command name
|
2022-06-04 18:10:21 +00:00
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"/test"
|
|
|
|
}
|
|
|
|
|
2022-06-20 21:22:45 +00:00
|
|
|
/// Help message of the command
|
2022-06-04 18:10:21 +00:00
|
|
|
fn help(&self) -> &'static str {
|
2022-07-29 19:55:21 +00:00
|
|
|
"Test command from plugin"
|
2022-06-04 18:10:21 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 21:22:45 +00:00
|
|
|
/// Command function
|
2022-06-23 09:07:42 +00:00
|
|
|
async fn execute(
|
|
|
|
&self,
|
|
|
|
client: &mut Client,
|
|
|
|
_args: Vec<&str>,
|
|
|
|
_commands: &PluginManagerType,
|
|
|
|
) -> Result<()> {
|
2022-07-01 19:32:20 +00:00
|
|
|
client.send("content").await?;
|
2022-06-23 09:07:42 +00:00
|
|
|
|
|
|
|
Ok(())
|
2022-06-04 18:10:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 21:22:45 +00:00
|
|
|
/// Create a new event
|
2022-06-16 17:31:09 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl Event for PluginTest {
|
2022-06-20 21:22:45 +00:00
|
|
|
/// Event name (onConnect or onSend)
|
2022-06-16 17:31:09 +00:00
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"onConnect"
|
|
|
|
}
|
|
|
|
|
2022-06-20 21:22:45 +00:00
|
|
|
/// Event function
|
2022-06-23 09:07:42 +00:00
|
|
|
async fn execute(&self, client: &mut Client) -> Result<()> {
|
2022-07-29 19:55:21 +00:00
|
|
|
client
|
2022-07-29 20:10:49 +00:00
|
|
|
.send(format!("Welcome {}", client.peer_addr().unwrap()))
|
2022-07-29 19:55:21 +00:00
|
|
|
.await?;
|
2022-06-23 09:07:42 +00:00
|
|
|
|
|
|
|
Ok(())
|
2022-06-16 17:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 21:41:08 +00:00
|
|
|
/// Register plugin
|
2022-06-04 18:10:21 +00:00
|
|
|
#[no_mangle]
|
2022-06-20 21:22:45 +00:00
|
|
|
pub fn plugin_entry(registrar: &mut dyn Registrar) {
|
|
|
|
registrar.register_plugin(Box::new(PluginTest));
|
|
|
|
registrar.register_command(Box::new(PluginTest));
|
|
|
|
registrar.register_event(Box::new(PluginTest));
|
2022-06-04 18:10:21 +00:00
|
|
|
}
|