diff --git a/Cargo.lock b/Cargo.lock index 4cb358c..bd95fa3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -648,7 +648,7 @@ dependencies = [ [[package]] name = "ica-rs" -version = "0.5.1" +version = "0.5.2" dependencies = [ "anyhow", "chrono", diff --git a/ica-rs/Cargo.toml b/ica-rs/Cargo.toml index fce5ed1..4185c71 100644 --- a/ica-rs/Cargo.toml +++ b/ica-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ica-rs" -version = "0.5.1" +version = "0.5.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/ica-rs/src/data_struct/ica/messages/msg_trait.rs b/ica-rs/src/data_struct/ica/messages/msg_trait.rs index 81be868..b7aaa4b 100644 --- a/ica-rs/src/data_struct/ica/messages/msg_trait.rs +++ b/ica-rs/src/data_struct/ica/messages/msg_trait.rs @@ -6,7 +6,7 @@ use serde_json::Value as JsonValue; use crate::data_struct::ica::messages::{At, Message, NewMessage}; use crate::data_struct::ica::{MessageId, UserId}; -use crate::ica::client::BotStatus; +use crate::MainStatus; impl Serialize for At { fn serialize(&self, serializer: S) -> Result @@ -34,7 +34,7 @@ impl<'de> Deserialize<'de> for At { pub trait MessageTrait { fn is_reply(&self) -> bool; fn is_from_self(&self) -> bool { - let qq_id = BotStatus::get_online_data().qqid; + let qq_id = MainStatus::global_ica_status().online_status.qqid; self.sender_id() == qq_id } fn msg_id(&self) -> &MessageId; diff --git a/ica-rs/src/data_struct/ica/online_data.rs b/ica-rs/src/data_struct/ica/online_data.rs index c832fd9..781cb22 100644 --- a/ica-rs/src/data_struct/ica/online_data.rs +++ b/ica-rs/src/data_struct/ica/online_data.rs @@ -141,6 +141,26 @@ impl OnlineData { } } +impl Default for OnlineData { + fn default() -> Self { + OnlineData { + bkn: -1, + nick: "UNKNOWN".to_string(), + online: false, + qqid: -1, + icalingua_info: IcalinguaInfo { + ica_version: "UNKNOWN".to_string(), + os_info: "UNKNOWN".to_string(), + resident_set_size: "UNKNOWN".to_string(), + heap_used: "UNKNOWN".to_string(), + load: "UNKNOWN".to_string(), + server_node: "UNKNOWN".to_string(), + client_count: 1, + }, + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/ica-rs/src/ica/client.rs b/ica-rs/src/ica/client.rs index 1dae7f4..09f4877 100644 --- a/ica-rs/src/ica/client.rs +++ b/ica-rs/src/ica/client.rs @@ -1,6 +1,4 @@ -use crate::data_struct::ica::all_rooms::Room; use crate::data_struct::ica::messages::{DeleteMessage, SendMessage}; -use crate::data_struct::ica::online_data::OnlineData; use crate::MainStatus; use colored::Colorize; @@ -8,7 +6,7 @@ use ed25519_dalek::{Signature, Signer, SigningKey}; use rust_socketio::asynchronous::Client; use rust_socketio::Payload; use serde_json::Value; -use tracing::{debug, warn}; +use tracing::{debug, span, warn, Level}; /// "安全" 的 发送一条消息 pub async fn send_message(client: &Client, message: &SendMessage) -> bool { @@ -46,6 +44,9 @@ pub async fn delete_message(client: &Client, message: &DeleteMessage) -> bool { // pub async fn fetch_history(client: &Client, roomd_id: RoomId) -> bool { false } pub async fn sign_callback(payload: Payload, client: Client) { + let span = span!(Level::INFO, "signing icalingua"); + let _guard = span.enter(); + // 获取数据 let require_data = match payload { Payload::Text(json_value) => Some(json_value), diff --git a/ica-rs/src/ica/events.rs b/ica-rs/src/ica/events.rs index f91ecc5..83fd39a 100644 --- a/ica-rs/src/ica/events.rs +++ b/ica-rs/src/ica/events.rs @@ -6,8 +6,8 @@ use tracing::{info, warn}; use crate::data_struct::ica::all_rooms::Room; use crate::data_struct::ica::messages::{Message, MessageTrait, NewMessage}; use crate::data_struct::ica::online_data::OnlineData; -use crate::ica::client::{send_message, BotStatus}; -use crate::{py, VERSION}; +use crate::ica::client::send_message; +use crate::{py, MainStatus, ICA_VERSION, MATRIX_VERSION, VERSION}; /// 获取在线数据 pub async fn get_online_data(payload: Payload, _client: Client) { @@ -15,7 +15,7 @@ pub async fn get_online_data(payload: Payload, _client: Client) { if let Some(value) = values.first() { let online_data = OnlineData::new_from_json(value); info!("update_online_data {}", format!("{:?}", online_data).cyan()); - BotStatus::update_online_data(online_data); + MainStatus::global_ica_status_mut().update_online_status(online_data); } } } @@ -26,15 +26,18 @@ pub async fn add_message(payload: Payload, client: Client) { if let Some(value) = values.first() { let message: NewMessage = serde_json::from_value(value.clone()).unwrap(); // 检测是否在过滤列表内 - if BotStatus::get_ica_config().filter_list.contains(&message.msg.sender_id) { + if MainStatus::global_config().ica().filter_list.contains(&message.msg.sender_id) { return; } + info!("add_message {}", message.to_string().cyan()); - // info!("add_message {}", format!("{:#?}", message).cyan()); // 就在这里处理掉最基本的消息 // 之后的处理交给插件 if message.content().eq("/bot-rs") && !message.is_from_self() && !message.is_reply() { - let reply = message.reply_with(&format!("ica-async-rs pong v{}", VERSION)); + let reply = message.reply_with(&format!( + "shenbot v{}\nica-async-rs pong v{}\nmatrix v{}", + VERSION, ICA_VERSION, MATRIX_VERSION + )); send_message(&client, &reply).await; } // python 插件 @@ -75,8 +78,8 @@ pub async fn update_all_room(payload: Payload, _client: Client) { if let Some(value) = values.first() { if let Some(raw_rooms) = value.as_array() { let rooms: Vec = raw_rooms.iter().map(Room::new_from_json).collect(); - BotStatus::update_rooms(rooms.clone()); info!("update_all_room {}", rooms.len()); + MainStatus::global_ica_status_mut().update_rooms(rooms); } } } diff --git a/ica-rs/src/main.rs b/ica-rs/src/main.rs index 6023744..24a17cc 100644 --- a/ica-rs/src/main.rs +++ b/ica-rs/src/main.rs @@ -21,7 +21,7 @@ pub static mut MAIN_STATUS: status::BotStatus = status::BotStatus { pub type MainStatus = status::BotStatus; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); -pub const ICA_VERSION: &str = "0.5.0"; +pub const ICA_VERSION: &str = "0.5.2"; pub const MATRIX_VERSION: &str = "0.1.0"; #[macro_export] diff --git a/ica-rs/src/py/call.rs b/ica-rs/src/py/call.rs index 89b074d..5ca988c 100644 --- a/ica-rs/src/py/call.rs +++ b/ica-rs/src/py/call.rs @@ -45,12 +45,8 @@ pub fn get_func<'py>(py_module: &'py PyAny, path: &PathBuf, name: &'py str) -> O pub fn verify_plugins() { let mut need_reload_files: Vec = Vec::new(); - let plugin_path = BotStatus::get_config().py_plugin_path.as_ref(); - if plugin_path.is_none() { - warn!("未配置 Python 插件路径"); - return; - } - let plugin_path = plugin_path.unwrap(); + let plugin_path = MainStatus::global_config().py().plugin_path.clone(); + for entry in std::fs::read_dir(plugin_path).unwrap().flatten() { let path = entry.path(); if let Some(ext) = path.extension() { diff --git a/ica-rs/src/py/mod.rs b/ica-rs/src/py/mod.rs index ce35ba6..435303e 100644 --- a/ica-rs/src/py/mod.rs +++ b/ica-rs/src/py/mod.rs @@ -74,8 +74,7 @@ impl TryFrom for PyPlugin { Ok(config) => { if config.is_instance_of::() { let (config, default) = config.extract::<(String, String)>().unwrap(); - let base_path = - BotStatus::get_config().py_config_path.as_ref().unwrap(); + let base_path = MainStatus::global_config().py().config_path; let mut base_path: PathBuf = PathBuf::from(base_path); @@ -117,6 +116,13 @@ impl TryFrom for PyPlugin { ))) } } + } else if config.is_none() { + // 没有配置文件 + Ok(PyPlugin { + file_path: path, + changed_time, + py_module: module.into_py(py), + }) } else { warn!( "加载 Python 插件 {:?} 的配置文件信息时失败:返回的不是 [str, str]", @@ -246,7 +252,7 @@ pub fn load_py_file(path: &PathBuf) -> std::io::Result { pub fn init_py() { // 从 全局配置中获取 python 插件路径 let span = span!(Level::INFO, "Init Python Plugin"); - let enter = span.enter(); + let _enter = span.enter(); let global_config = MainStatus::global_config().py(); diff --git a/ica-rs/src/status.rs b/ica-rs/src/status.rs index a5bf3fe..ef00dc8 100644 --- a/ica-rs/src/status.rs +++ b/ica-rs/src/status.rs @@ -27,6 +27,16 @@ impl BotStatus { pub fn static_init(config: BotConfig) { unsafe { + MAIN_STATUS.ica_status = Some(ica::MainStatus { + enable: config.check_ica(), + qq_login: false, + current_loaded_messages_count: 0, + rooms: Vec::new(), + online_status: ica::OnlineData::default(), + }); + MAIN_STATUS.matrix_status = Some(matrix::MainStatus { + enable: config.check_matrix(), + }); MAIN_STATUS.config = Some(config); } } @@ -38,11 +48,18 @@ impl BotStatus { pub fn global_matrix_status() -> &'static matrix::MainStatus { unsafe { MAIN_STATUS.matrix_status.as_ref().unwrap() } } + + pub fn global_ica_status_mut() -> &'static mut ica::MainStatus { + unsafe { MAIN_STATUS.ica_status.as_mut().unwrap() } + } + pub fn global_matrix_status_mut() -> &'static mut matrix::MainStatus { + unsafe { MAIN_STATUS.matrix_status.as_mut().unwrap() } + } } pub mod ica { use crate::data_struct::ica::all_rooms::Room; - use crate::data_struct::ica::online_data::OnlineData; + pub use crate::data_struct::ica::online_data::OnlineData; #[derive(Debug, Clone)] pub struct MainStatus { @@ -57,6 +74,11 @@ pub mod ica { /// 在线数据 (Icalingua 信息) pub online_status: OnlineData, } + + impl MainStatus { + pub fn update_rooms(&mut self, room: Vec) { self.rooms = room; } + pub fn update_online_status(&mut self, status: OnlineData) { self.online_status = status; } + } } pub mod matrix { diff --git a/news.md b/news.md index f510dd9..7dbd065 100644 --- a/news.md +++ b/news.md @@ -1,5 +1,10 @@ # 更新日志 +## 0.5.1/2 + +重构了一整波, 还没改 `ica-typing.py` 的代码 +但至少能用了 + ## 0.5.0 准备接入 `Matrix`