Improved plugin modularity and protocol

This commit is contained in:
2025-12-14 11:03:02 +01:00
parent 51708ac050
commit 4eae3c046d
5 changed files with 28 additions and 68 deletions
-58
View File
@@ -1,58 +0,0 @@
import readline from 'readline';
async function createServerToken() {
return (await (await fetch('http://localhost:3000/api/auth', {
method: 'POST',
body: JSON.stringify({
intents: 'server',
server_ip: '127.0.0.1'
})
})).json()).token;
}
const token = await createServerToken();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const ws = new WebSocket('ws://localhost:7080');
ws.onopen = () => {
console.log('WebSocket connection established');
console.log('Initializing handshake');
ws.send(JSON.stringify({ version: '0.0.1', auth_token: token }))
};
ws.onmessage = (event) => {
const message = JSON.parse(event.data);
if (message.type === 'authenticated') {
console.log('Logged in with uuid: ' + message.params.messages);
console.log(message.params.messages);
}
console.log('Received:', message);
// ws.close();
};
function sendMessage(message) {
ws.send(JSON.stringify({ type: 'send_message', params: {
channel_id: 'general',
contents: message
}}));
}
function ask(question) {
return new Promise((resolve) => {
rl.question(question, resolve);
});
}
while (true) {
const message = await ask('');
if (message === 'q') {
ws.close();
process.exit();
}
sendMessage(message);
}
+1 -1
View File
@@ -11,7 +11,7 @@ use crate::plugin::types::{LoaderMessage, PluginMessage};
pub struct Plugin(TcpStream, BufReader<TcpStream>);
impl Plugin {
pub fn send(&mut self, m: LoaderMessage) {
pub fn send(&mut self, m: &LoaderMessage) {
self.0
.write(serde_json::to_string(&m).unwrap().as_bytes())
.unwrap();
+9 -1
View File
@@ -1,6 +1,9 @@
use serde::{Deserialize, Serialize};
use crate::types::message::{ClientMessage, WsMessage};
use crate::types::{
data::Message,
message::{ClientMessage, WsMessage},
};
#[derive(Debug, Serialize, Deserialize)]
pub struct PluginJson {
@@ -23,6 +26,11 @@ pub enum LoaderMessage {
user_id: String,
msg: WsMessage<ClientMessage>,
},
MessageSent {
user_id: String,
msg: Message,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
+8 -1
View File
@@ -1,6 +1,8 @@
use std::sync::Arc;
use crate::{server::Server, types, utils::client::Client};
use serde_json::ser;
use crate::{plugin::types::LoaderMessage, server::Server, types, utils::client::Client};
crate::logger!(LOGGER "Message Manager");
@@ -43,6 +45,11 @@ pub fn send(
});
}
server.send_plugin_message(&LoaderMessage::MessageSent {
user_id: client.get_uuid().unwrap_or_default(),
msg: msg,
});
Ok(())
}
+7 -4
View File
@@ -178,13 +178,10 @@ impl Server {
loop {
let req = client.read()?;
if let Some(r) = &req {
for p in self.plugins.lock().unwrap().iter_mut() {
p.send(LoaderMessage::Request {
self.send_plugin_message(&LoaderMessage::Request {
user_id: client.get_uuid().unwrap_or_default(),
msg: r.clone(),
});
}
self.wrap_err(&client, self.call_request(r, &client))?;
}
}
@@ -206,4 +203,10 @@ impl Server {
res
}
pub fn send_plugin_message(&self, msg: &LoaderMessage) {
for p in self.plugins.lock().unwrap().iter_mut() {
p.send(msg);
}
}
}