This commit is contained in:
shenjack 2024-03-15 01:03:24 +08:00
parent de09257249
commit ec9cd625d1
Signed by: shenjack
GPG Key ID: 7B1134A979775551
11 changed files with 78 additions and 25 deletions

2
Cargo.lock generated
View File

@ -648,7 +648,7 @@ dependencies = [
[[package]] [[package]]
name = "ica-rs" name = "ica-rs"
version = "0.5.1" version = "0.5.2"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"chrono", "chrono",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "ica-rs" name = "ica-rs"
version = "0.5.1" version = "0.5.2"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -6,7 +6,7 @@ use serde_json::Value as JsonValue;
use crate::data_struct::ica::messages::{At, Message, NewMessage}; use crate::data_struct::ica::messages::{At, Message, NewMessage};
use crate::data_struct::ica::{MessageId, UserId}; use crate::data_struct::ica::{MessageId, UserId};
use crate::ica::client::BotStatus; use crate::MainStatus;
impl Serialize for At { impl Serialize for At {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@ -34,7 +34,7 @@ impl<'de> Deserialize<'de> for At {
pub trait MessageTrait { pub trait MessageTrait {
fn is_reply(&self) -> bool; fn is_reply(&self) -> bool;
fn is_from_self(&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 self.sender_id() == qq_id
} }
fn msg_id(&self) -> &MessageId; fn msg_id(&self) -> &MessageId;

View File

@ -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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@ -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::messages::{DeleteMessage, SendMessage};
use crate::data_struct::ica::online_data::OnlineData;
use crate::MainStatus; use crate::MainStatus;
use colored::Colorize; use colored::Colorize;
@ -8,7 +6,7 @@ use ed25519_dalek::{Signature, Signer, SigningKey};
use rust_socketio::asynchronous::Client; use rust_socketio::asynchronous::Client;
use rust_socketio::Payload; use rust_socketio::Payload;
use serde_json::Value; use serde_json::Value;
use tracing::{debug, warn}; use tracing::{debug, span, warn, Level};
/// "安全" 的 发送一条消息 /// "安全" 的 发送一条消息
pub async fn send_message(client: &Client, message: &SendMessage) -> bool { 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 fetch_history(client: &Client, roomd_id: RoomId) -> bool { false }
pub async fn sign_callback(payload: Payload, client: Client) { 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 { let require_data = match payload {
Payload::Text(json_value) => Some(json_value), Payload::Text(json_value) => Some(json_value),

View File

@ -6,8 +6,8 @@ use tracing::{info, warn};
use crate::data_struct::ica::all_rooms::Room; use crate::data_struct::ica::all_rooms::Room;
use crate::data_struct::ica::messages::{Message, MessageTrait, NewMessage}; use crate::data_struct::ica::messages::{Message, MessageTrait, NewMessage};
use crate::data_struct::ica::online_data::OnlineData; use crate::data_struct::ica::online_data::OnlineData;
use crate::ica::client::{send_message, BotStatus}; use crate::ica::client::send_message;
use crate::{py, VERSION}; use crate::{py, MainStatus, ICA_VERSION, MATRIX_VERSION, VERSION};
/// 获取在线数据 /// 获取在线数据
pub async fn get_online_data(payload: Payload, _client: Client) { 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() { if let Some(value) = values.first() {
let online_data = OnlineData::new_from_json(value); let online_data = OnlineData::new_from_json(value);
info!("update_online_data {}", format!("{:?}", online_data).cyan()); 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() { if let Some(value) = values.first() {
let message: NewMessage = serde_json::from_value(value.clone()).unwrap(); 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; return;
} }
info!("add_message {}", message.to_string().cyan()); 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() { 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; send_message(&client, &reply).await;
} }
// python 插件 // python 插件
@ -75,8 +78,8 @@ pub async fn update_all_room(payload: Payload, _client: Client) {
if let Some(value) = values.first() { if let Some(value) = values.first() {
if let Some(raw_rooms) = value.as_array() { if let Some(raw_rooms) = value.as_array() {
let rooms: Vec<Room> = raw_rooms.iter().map(Room::new_from_json).collect(); let rooms: Vec<Room> = raw_rooms.iter().map(Room::new_from_json).collect();
BotStatus::update_rooms(rooms.clone());
info!("update_all_room {}", rooms.len()); info!("update_all_room {}", rooms.len());
MainStatus::global_ica_status_mut().update_rooms(rooms);
} }
} }
} }

View File

@ -21,7 +21,7 @@ pub static mut MAIN_STATUS: status::BotStatus = status::BotStatus {
pub type MainStatus = status::BotStatus; pub type MainStatus = status::BotStatus;
pub const VERSION: &str = env!("CARGO_PKG_VERSION"); 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"; pub const MATRIX_VERSION: &str = "0.1.0";
#[macro_export] #[macro_export]

View File

@ -45,12 +45,8 @@ pub fn get_func<'py>(py_module: &'py PyAny, path: &PathBuf, name: &'py str) -> O
pub fn verify_plugins() { pub fn verify_plugins() {
let mut need_reload_files: Vec<PathBuf> = Vec::new(); let mut need_reload_files: Vec<PathBuf> = Vec::new();
let plugin_path = BotStatus::get_config().py_plugin_path.as_ref(); let plugin_path = MainStatus::global_config().py().plugin_path.clone();
if plugin_path.is_none() {
warn!("未配置 Python 插件路径");
return;
}
let plugin_path = plugin_path.unwrap();
for entry in std::fs::read_dir(plugin_path).unwrap().flatten() { for entry in std::fs::read_dir(plugin_path).unwrap().flatten() {
let path = entry.path(); let path = entry.path();
if let Some(ext) = path.extension() { if let Some(ext) = path.extension() {

View File

@ -74,8 +74,7 @@ impl TryFrom<RawPyPlugin> for PyPlugin {
Ok(config) => { Ok(config) => {
if config.is_instance_of::<PyTuple>() { if config.is_instance_of::<PyTuple>() {
let (config, default) = config.extract::<(String, String)>().unwrap(); let (config, default) = config.extract::<(String, String)>().unwrap();
let base_path = let base_path = MainStatus::global_config().py().config_path;
BotStatus::get_config().py_config_path.as_ref().unwrap();
let mut base_path: PathBuf = PathBuf::from(base_path); let mut base_path: PathBuf = PathBuf::from(base_path);
@ -117,6 +116,13 @@ impl TryFrom<RawPyPlugin> for PyPlugin {
))) )))
} }
} }
} else if config.is_none() {
// 没有配置文件
Ok(PyPlugin {
file_path: path,
changed_time,
py_module: module.into_py(py),
})
} else { } else {
warn!( warn!(
"加载 Python 插件 {:?} 的配置文件信息时失败:返回的不是 [str, str]", "加载 Python 插件 {:?} 的配置文件信息时失败:返回的不是 [str, str]",
@ -246,7 +252,7 @@ pub fn load_py_file(path: &PathBuf) -> std::io::Result<RawPyPlugin> {
pub fn init_py() { pub fn init_py() {
// 从 全局配置中获取 python 插件路径 // 从 全局配置中获取 python 插件路径
let span = span!(Level::INFO, "Init Python Plugin"); let span = span!(Level::INFO, "Init Python Plugin");
let enter = span.enter(); let _enter = span.enter();
let global_config = MainStatus::global_config().py(); let global_config = MainStatus::global_config().py();

View File

@ -27,6 +27,16 @@ impl BotStatus {
pub fn static_init(config: BotConfig) { pub fn static_init(config: BotConfig) {
unsafe { 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); MAIN_STATUS.config = Some(config);
} }
} }
@ -38,11 +48,18 @@ impl BotStatus {
pub fn global_matrix_status() -> &'static matrix::MainStatus { pub fn global_matrix_status() -> &'static matrix::MainStatus {
unsafe { MAIN_STATUS.matrix_status.as_ref().unwrap() } 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 { pub mod ica {
use crate::data_struct::ica::all_rooms::Room; 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)] #[derive(Debug, Clone)]
pub struct MainStatus { pub struct MainStatus {
@ -57,6 +74,11 @@ pub mod ica {
/// 在线数据 (Icalingua 信息) /// 在线数据 (Icalingua 信息)
pub online_status: OnlineData, pub online_status: OnlineData,
} }
impl MainStatus {
pub fn update_rooms(&mut self, room: Vec<Room>) { self.rooms = room; }
pub fn update_online_status(&mut self, status: OnlineData) { self.online_status = status; }
}
} }
pub mod matrix { pub mod matrix {

View File

@ -1,5 +1,10 @@
# 更新日志 # 更新日志
## 0.5.1/2
重构了一整波, 还没改 `ica-typing.py` 的代码
但至少能用了
## 0.5.0 ## 0.5.0
准备接入 `Matrix` 准备接入 `Matrix`