mirror of
https://git.ngram.ca/OpenJam/rc-servers
synced 2026-08-23 23:08:52 +00:00
Add basic C FFI for chat plugins and corresponding plugin loader
This commit is contained in:
@@ -35,7 +35,8 @@ async fn main() -> std::io::Result<()> {
|
||||
let cubes = oj_rc_core::persist::config::ConfigImpl::load(&args.assets).expect("Bad config data");
|
||||
let users = std::sync::Arc::new(oj_rc_core::persist::user::UserImpl::load(&args.data, &cubes).await.expect("Bad user data"));
|
||||
|
||||
let chat_system = state::chat::ChatImpl::new(<oj_rc_core::ConfigImpl as ConfigProvider<()>>::chat_system_config(&cubes)).expect("Bad chat config data");
|
||||
let chat_plugin_path = std::path::PathBuf::from(&args.data).join("plugins/chat");
|
||||
let chat_system = state::chat::ChatImpl::new(<oj_rc_core::ConfigImpl as ConfigProvider<()>>::chat_system_config(&cubes), chat_plugin_path).expect("Bad chat config data");
|
||||
|
||||
let server = std::sync::Arc::new(polariton_server::Server::new(operations::handler(chat_system.clone(), &cubes), polariton_server::events::EventsHandler::new()));
|
||||
|
||||
|
||||
@@ -51,3 +51,43 @@ impl oj_rc_plugins::chat::ChatProvider for ProviderWrapper {
|
||||
tokio::task::spawn(Self::do_send_message(self.provider.clone(), message.to_owned(), channel.to_owned(), username.to_owned()));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_chat_plugins(from_dir: impl AsRef<std::path::Path>) -> Vec<Box<dyn oj_rc_plugins::chat::ChatPlugin>> {
|
||||
let path_ref = from_dir.as_ref();
|
||||
if path_ref.exists() {
|
||||
log::warn!("Chat plugins are experimental and insecure");
|
||||
} else {
|
||||
log::info!("Not loading chat plugins; {} does not exist", path_ref.display());
|
||||
return Vec::default();
|
||||
}
|
||||
match path_ref.read_dir() {
|
||||
Ok(dir) => {
|
||||
let mut plugins = Vec::new();
|
||||
for entry in dir {
|
||||
match entry {
|
||||
Ok(entry) => {
|
||||
if entry.path().is_file() {
|
||||
match oj_rc_plugins::chat::ChatCPlugin::new(entry.path()) {
|
||||
Ok(plugin) => {
|
||||
plugins.push(Box::new(plugin) as Box<dyn oj_rc_plugins::chat::ChatPlugin>);
|
||||
},
|
||||
Err(e) => {
|
||||
log::warn!("Failed to load chat plugin {}: {}", entry.path().display(), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
log::warn!("Failed to read entry in {}: {}", path_ref.display(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
plugins
|
||||
},
|
||||
Err(e) => {
|
||||
log::error!("Failed to load chat plugins from {}: {}", path_ref.display(), e);
|
||||
Vec::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ pub struct ChatProvider {
|
||||
}
|
||||
|
||||
impl ChatProvider {
|
||||
pub fn new(conf: oj_rc_core::persist::config::ChatSystemConfig) -> std::io::Result<Self> {
|
||||
pub fn new(conf: oj_rc_core::persist::config::ChatSystemConfig, dir: impl AsRef<std::path::Path>) -> std::io::Result<Self> {
|
||||
Ok(Self {
|
||||
chat_system: std::sync::Arc::new(tokio::sync::RwLock::new(crate::state::chat::ChatSystem::new(conf)?)),
|
||||
chat_system: std::sync::Arc::new(tokio::sync::RwLock::new(crate::state::chat::ChatSystem::new(conf, dir)?)),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -198,12 +198,12 @@ impl ChatSystem {
|
||||
handle.send_private_message(response);
|
||||
}
|
||||
|
||||
pub fn new(config: oj_rc_core::persist::config::ChatSystemConfig) -> std::io::Result<Self> {
|
||||
pub fn new(config: oj_rc_core::persist::config::ChatSystemConfig, dir: impl AsRef<std::path::Path>) -> std::io::Result<Self> {
|
||||
Ok(Self {
|
||||
chats: HashMap::new(),
|
||||
online_users: HashMap::new(),
|
||||
config: super::ChatSystemConfig::from_persist(config)?,
|
||||
plugin: crate::PluginWrapper::new(vec![]), // TODO construct plugins
|
||||
plugin: crate::PluginWrapper::new(crate::plugin_wrapper::load_chat_plugins(dir)),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user