polychat_plugin/plugin/
init_plugin.rs

1use super::{PluginInfo, APIVersion, Message, SendStatus};
2use crate::types::Account;
3
4use libc::c_char;
5use std::ffi::CString;
6
7#[derive(Debug)]
8pub struct InitializedPlugin {
9    pub supported_api: APIVersion,
10    pub name: String,
11
12    pub create_account: extern fn() -> Account,
13    pub destroy_account: extern fn(acc: Account),
14    pub post_message: extern fn(msg: * const Message) -> SendStatus,
15    pub print: extern fn(acc: Account),
16}
17
18impl InitializedPlugin {
19    pub fn new(plugin: &PluginInfo) -> Result<InitializedPlugin, String> {
20        if plugin.create_account.is_none() {
21            return Err("create_account is not defined".to_string());
22        } else if plugin.destroy_account.is_none() {
23            return Err("destroy_account is not defined".to_string());
24        } else if plugin.post_message.is_none() {
25            return Err("post_message is not defined".to_string());
26        } else if plugin.print.is_none() {
27            return Err("print is not defined".to_string());
28        } else if plugin.name.is_null() {
29            return Err("name is not defined".to_string());
30        }
31
32        let name: String;
33        unsafe {
34            let name_res = CString::from_raw(plugin.name as *mut c_char).into_string();
35            if name_res.is_err() {
36                return Err("Could not decode plugin name".to_string());
37            }
38
39            name = name_res.unwrap();
40        }
41
42        Ok(InitializedPlugin {
43            supported_api: plugin.supported_api,
44            create_account: plugin.create_account.unwrap(),
45            destroy_account: plugin.destroy_account.unwrap(),
46            post_message: plugin.post_message.unwrap(),
47            print: plugin.print.unwrap(),
48            name,
49        })
50    }
51}