Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target
Cargo.lock
.vs/
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod main;
pub mod plugin;
pub mod plugin_manager;
56 changes: 56 additions & 0 deletions src/main/mod.rs
Original file line number Diff line number Diff line change
@@ -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<PluginManager>
}

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<String> {
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);
}
}
8 changes: 6 additions & 2 deletions src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<Plugin, String>{
Expand Down
6 changes: 5 additions & 1 deletion src/plugin_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl PluginManager {
* Returns a vector of services that the PluginManager
* currently supports.
*/
pub fn get_services(&self) -> Vec<String> {
pub fn get_plugin_names(&self) -> Vec<String> {
let mut output: Vec<String> = Vec::<String>::new();

for (key, _) in &self.plugin_map {
Expand All @@ -150,6 +150,10 @@ impl PluginManager {

return output;
}

pub fn get_plugin_by_name(&self, name: String) -> Option<&Plugin> {
return self.plugin_map.get::<str>(name.as_ref());
}
}

impl Drop for PluginManager {
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy_plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ crate-type = ["cdylib"]


[dependencies]
polychat-plugin = "0.3.1"
polychat-plugin = {path="../../../polychat-plugin"}
libc = "0.2.113"
3 changes: 3 additions & 0 deletions tests/dummy_plugin/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
all:
cargo build
cp target/debug/libdummy_plugin.so ~/.polychat/plugins/dummy
15 changes: 13 additions & 2 deletions tests/dummy_plugin/dummy_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion tests/plugin_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
}