diff --git a/.gitignore b/.gitignore index a9d37c5..3f8a50c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ target Cargo.lock +.vs/ diff --git a/Cargo.toml b/Cargo.toml index 2868475..75cc050 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,6 @@ crate-type = ["rlib", "dylib"] [dependencies] libloading = "0.7.3" -polychat-plugin = "0.3.1" +polychat-plugin = "0.4.0" log = "0.4.14" walkdir = "2.3.2" diff --git a/src/lib.rs b/src/lib.rs index 2166845..1e721c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ +pub mod main; pub mod plugin; pub mod plugin_manager; \ No newline at end of file diff --git a/src/main/mod.rs b/src/main/mod.rs new file mode 100644 index 0000000..91fda21 --- /dev/null +++ b/src/main/mod.rs @@ -0,0 +1,56 @@ +extern crate polychat_plugin; + +use log::info; + +use crate::plugin::Plugin; + +use super::plugin_manager::PluginManager; + +use polychat_plugin::plugin::CoreInterface; + +pub struct Main { + plugin_manager: Option +} + +impl Main { + pub fn new() -> Main { + Main { + plugin_manager: None + } + } + + pub fn init<'a>(&'a mut self, path: &'a str) -> Result<(), &str>{ + let plugin_man = PluginManager::from(path)?; + self.plugin_manager = Some(plugin_man); + + Ok(()) + } + + pub fn is_initalized(&self) { + assert!(self.plugin_manager.is_some(), "Main was not initialized!"); + } + + pub fn get_plugin_names(&self) -> Vec { + self.is_initalized(); + let plugin_manager = self.plugin_manager.as_ref().expect("PluginManager is not initalized"); + + return plugin_manager.get_plugin_names(); + } + + pub fn get_plugin_by_name(&self, name: String) -> Result<&Plugin, &str> { + self.is_initalized(); + let plugin = self.plugin_manager.as_ref().unwrap().get_plugin_by_name(name); + + match plugin { + None => Err("Could not find plugin"), + Some(s) => Ok(s) + } + } +} + +impl CoreInterface for Main { + fn test(&self, test_msg: String) { + self.is_initalized(); + info!("[CoreInterface] test function called with {}", test_msg); + } +} \ No newline at end of file diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index c6bb03a..515d3dc 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -52,12 +52,12 @@ impl Plugin { (self.plugin_info.destroy_account)(account); } - pub fn post_message(&self, msg_body: String) -> SendStatus { + pub fn post_message(&self, account: Account, msg_body: String) -> SendStatus { let body_cstr = CString::new(msg_body).unwrap(); let msg = Message { body: body_cstr.as_ptr() }; - return (self.plugin_info.post_message)(&msg); + return (self.plugin_info.post_message)(account, &msg); } pub fn print(&self, account: Account) { @@ -67,6 +67,10 @@ impl Plugin { pub fn get_name(&self) -> &String { &self.plugin_info.name } + + pub fn get_protocol_name(&self) -> &String { + &self.plugin_info.protocol_name + } } fn new_from_loaded_lib(path: &str, lib: Library) -> Result{ diff --git a/src/plugin_manager/mod.rs b/src/plugin_manager/mod.rs index 0a7b58d..fe865ae 100644 --- a/src/plugin_manager/mod.rs +++ b/src/plugin_manager/mod.rs @@ -141,7 +141,7 @@ impl PluginManager { * Returns a vector of services that the PluginManager * currently supports. */ - pub fn get_services(&self) -> Vec { + pub fn get_plugin_names(&self) -> Vec { let mut output: Vec = Vec::::new(); for (key, _) in &self.plugin_map { @@ -150,6 +150,10 @@ impl PluginManager { return output; } + + pub fn get_plugin_by_name(&self, name: String) -> Option<&Plugin> { + return self.plugin_map.get::(name.as_ref()); + } } impl Drop for PluginManager { diff --git a/tests/dummy_plugin/Cargo.toml b/tests/dummy_plugin/Cargo.toml index 22723ce..36204fc 100644 --- a/tests/dummy_plugin/Cargo.toml +++ b/tests/dummy_plugin/Cargo.toml @@ -12,5 +12,5 @@ crate-type = ["cdylib"] [dependencies] -polychat-plugin = "0.3.1" +polychat-plugin = {path="../../../polychat-plugin"} libc = "0.2.113" diff --git a/tests/dummy_plugin/Makefile b/tests/dummy_plugin/Makefile new file mode 100644 index 0000000..e75f7a9 --- /dev/null +++ b/tests/dummy_plugin/Makefile @@ -0,0 +1,3 @@ +all: + cargo build + cp target/debug/libdummy_plugin.so ~/.polychat/plugins/dummy diff --git a/tests/dummy_plugin/dummy_plugin.rs b/tests/dummy_plugin/dummy_plugin.rs index a59c005..4cbb413 100644 --- a/tests/dummy_plugin/dummy_plugin.rs +++ b/tests/dummy_plugin/dummy_plugin.rs @@ -3,11 +3,13 @@ extern crate polychat_plugin; use std::boxed::Box; use std::ffi::CString; +use std::ffi::c_char; use polychat_plugin::types::Account; use polychat_plugin::plugin::PluginInfo; -use polychat_plugin::plugin::Message; +use polychat_plugin::plugin::{Message, Conversation}; use polychat_plugin::plugin::SendStatus; +use polychat_plugin::plugin::{AuthResult, AuthMethod}; extern "C" fn create_account() -> Account { Box::into_raw(Box::new(5)) as Account @@ -20,7 +22,7 @@ extern "C" fn print(acc: Account) { } } -extern "C" fn post_message(msg: * const Message) -> SendStatus { +extern "C" fn post_message(acc: Account, msg: * const Message) -> SendStatus { unsafe { println!("Instructed to post message with body {}", *(*msg).body); } @@ -33,11 +35,20 @@ extern "C" fn destroy_account(acc: Account) { } } +extern "C" fn login(acc: Account, method: * const AuthMethod, passwd: *const c_char) -> AuthResult { + return AuthResult::Success; +} + +extern "C" fn request_messages(acc: Account, conv: Conversation, ts: u64, limit: u32) {} + #[no_mangle] unsafe extern "C" fn initialize(info: *mut PluginInfo) { (*info).name = CString::new("dummy").expect("Could not create CString").into_raw(); + (*info).protocol_name = CString::new("Pseduo-Protocol").expect("Could not create CString").into_raw(); (*info).create_account = Some(create_account); (*info).destroy_account = Some(destroy_account); (*info).post_message = Some(post_message); (*info).print = Some(print); + (*info).request_messages = Some(request_messages); + (*info).login = Some(login); } diff --git a/tests/plugin_manager.rs b/tests/plugin_manager.rs index d327e9d..7690499 100644 --- a/tests/plugin_manager.rs +++ b/tests/plugin_manager.rs @@ -73,7 +73,7 @@ fn double_free_never_happens() { fn get_services_returns_services() { let path = get_dummy_plugin(); let plugin_manager = PluginManager::from(path.as_str()).unwrap(); - let services = plugin_manager.get_services(); + let services = plugin_manager.get_plugin_names(); assert_eq!(services, ["dummy"]); } \ No newline at end of file