继续进行一个很逆天的重构

This commit is contained in:
shenjack 2024-03-13 01:17:50 +08:00
parent 53e652aa7b
commit f6e760e234
Signed by: shenjack
GPG Key ID: 7B1134A979775551
19 changed files with 454 additions and 44 deletions

311
Cargo.lock generated
View File

@ -23,6 +23,15 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234"
[[package]]
name = "aho-corasick"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
dependencies = [
"memchr",
]
[[package]]
name = "android-tzdata"
version = "0.1.1"
@ -38,6 +47,18 @@ dependencies = [
"libc",
]
[[package]]
name = "as_variant"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f38fa22307249f86fb7fad906fcae77f2564caeb56d7209103c551cd1cf4798f"
[[package]]
name = "assign"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f093eed78becd229346bf859eec0aa4dd7ddde0757287b2b4107a1f09c80002"
[[package]]
name = "async-stream"
version = "0.3.5"
@ -196,6 +217,12 @@ version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8"
[[package]]
name = "const_panic"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6051f239ecec86fde3410901ab7860d458d160371533842974fc61f96d15879b"
[[package]]
name = "core-foundation"
version = "0.9.4"
@ -621,6 +648,7 @@ dependencies = [
"futures-util",
"hex",
"pyo3",
"ruma",
"rust_socketio",
"serde",
"serde_json",
@ -648,6 +676,7 @@ checksum = "233cf39063f058ea2caae4091bf4a3ef70a653afbc026f5c4a4135d114e3c177"
dependencies = [
"equivalent",
"hashbrown",
"serde",
]
[[package]]
@ -686,6 +715,44 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "js_int"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d937f95470b270ce8b8950207715d71aa8e153c0d44c6684d59397ed4949160a"
dependencies = [
"serde",
]
[[package]]
name = "js_option"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68421373957a1593a767013698dbf206e2b221eefe97a44d98d18672ff38423c"
dependencies = [
"serde",
]
[[package]]
name = "konst"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d712a8c49d4274f8d8a5cf61368cb5f3c143d149882b1a2918129e53395fdb0"
dependencies = [
"const_panic",
"konst_kernel",
"typewit",
]
[[package]]
name = "konst_kernel"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dac6ea8c376b6e208a81cf39b8e82bebf49652454d98a4829e907dac16ef1790"
dependencies = [
"typewit",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -720,6 +787,12 @@ version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "maplit"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d"
[[package]]
name = "memchr"
version = "2.7.1"
@ -960,6 +1033,15 @@ version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro-crate"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e8366a6159044a37876a2b9817124296703c586a5c92e2c53751fa06d8d43e8"
dependencies = [
"toml_edit 0.20.7",
]
[[package]]
name = "proc-macro2"
version = "1.0.78"
@ -1080,6 +1162,35 @@ dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "regex"
version = "1.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "reqwest"
version = "0.11.24"
@ -1122,6 +1233,139 @@ dependencies = [
"winreg",
]
[[package]]
name = "ruma"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2779c38df072964c63476259d9300efb07d0d1a7178c6469893636ce0c547a36"
dependencies = [
"assign",
"js_int",
"js_option",
"ruma-client",
"ruma-client-api",
"ruma-common",
"ruma-events",
]
[[package]]
name = "ruma-client"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f5c81ad06f591b128cbb61694b3bb90d3c5cecff6be9332ef492fa4ed5fcd8c"
dependencies = [
"assign",
"async-stream",
"async-trait",
"bytes",
"futures-core",
"http",
"reqwest",
"ruma-client-api",
"ruma-common",
"serde",
"serde_html_form",
"serde_json",
"tracing",
]
[[package]]
name = "ruma-client-api"
version = "0.17.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "641837258fa214a70823477514954ef0f5d3bc6ae8e1d5d85081856a33103386"
dependencies = [
"assign",
"bytes",
"http",
"js_int",
"js_option",
"maplit",
"ruma-common",
"ruma-events",
"serde",
"serde_html_form",
"serde_json",
]
[[package]]
name = "ruma-common"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bca4c33c50e47b4cdceeac71bdef0c04153b0e29aa992d9030ec14a62323e85"
dependencies = [
"as_variant",
"base64",
"bytes",
"form_urlencoded",
"http",
"indexmap",
"js_int",
"konst",
"percent-encoding",
"rand",
"regex",
"ruma-identifiers-validation",
"ruma-macros",
"serde",
"serde_html_form",
"serde_json",
"thiserror",
"tracing",
"url",
"uuid",
"wildmatch",
]
[[package]]
name = "ruma-events"
version = "0.27.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d20a52770e5a9fb30b7a1c14ba8b3dcf76dadc01674e58e40094f78e6bd5e3f1"
dependencies = [
"as_variant",
"indexmap",
"js_int",
"js_option",
"percent-encoding",
"regex",
"ruma-common",
"ruma-identifiers-validation",
"ruma-macros",
"serde",
"serde_json",
"thiserror",
"tracing",
"url",
"wildmatch",
]
[[package]]
name = "ruma-identifiers-validation"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf8ad1259274f2f57c20901bd1cc5e4a8f23169d1c1d887b6338b02f058e9b41"
dependencies = [
"js_int",
"thiserror",
]
[[package]]
name = "ruma-macros"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0280534a4b3e34416f883285fac4f9c408cd0b737890ae66f3e7a7056d14be80"
dependencies = [
"once_cell",
"proc-macro-crate",
"proc-macro2",
"quote",
"ruma-identifiers-validation",
"serde",
"syn",
"toml",
]
[[package]]
name = "rust_engineio"
version = "0.4.4"
@ -1274,6 +1518,19 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_html_form"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50437e6a58912eecc08865e35ea2e8d365fbb2db0debb1c8bb43bf1faf055f25"
dependencies = [
"form_urlencoded",
"indexmap",
"itoa",
"ryu",
"serde",
]
[[package]]
name = "serde_json"
version = "1.0.114"
@ -1605,7 +1862,7 @@ dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
"toml_edit 0.22.6",
]
[[package]]
@ -1617,6 +1874,17 @@ dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.20.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70f427fce4d84c72b5b732388bf4a9f4531b53f74e2887e3ecb2481f68f66d81"
dependencies = [
"indexmap",
"toml_datetime",
"winnow 0.5.40",
]
[[package]]
name = "toml_edit"
version = "0.22.6"
@ -1627,7 +1895,7 @@ dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
"winnow 0.6.2",
]
[[package]]
@ -1726,6 +1994,21 @@ version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "typewit"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6fb9ae6a3cafaf0a5d14c2302ca525f9ae8e07a0f0e6949de88d882c37a6e24"
dependencies = [
"typewit_proc_macros",
]
[[package]]
name = "typewit_proc_macros"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e36a83ea2b3c704935a01b4642946aadd445cea40b10935e3f8bd8052b8193d6"
[[package]]
name = "unicode-bidi"
version = "0.3.15"
@ -1770,6 +2053,15 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]]
name = "uuid"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a"
dependencies = [
"getrandom",
]
[[package]]
name = "valuable"
version = "0.1.0"
@ -1892,6 +2184,12 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "wildmatch"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "017f0a8ed8331210d91b7a4c30d4edef8f21a65c02f2540496e2e79725f6d8a8"
[[package]]
name = "winapi"
version = "0.3.9"
@ -2055,6 +2353,15 @@ version = "0.52.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0770833d60a970638e989b3fa9fd2bb1aaadcf88963d1659fd7d9990196ed2d6"
[[package]]
name = "winnow"
version = "0.5.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876"
dependencies = [
"memchr",
]
[[package]]
name = "winnow"
version = "0.6.2"

View File

@ -3,7 +3,10 @@
py_plugin_path = "/path/to/your/plugin"
py_config_path = "/path/to/your/config"
# 填写 [ica] 但不填写此项则不启用 ica
enable_ica = true # 是否启用 ica
# 填写 [matrix] 但不填写此项则不启用 matrix
enable_matrix = true # 是否启用 matrix
[ica]
@ -20,3 +23,13 @@ notice_start = true # 是否在启动 bot 后通知
admin_list = [0] # 机器人的管理员
# 过滤的人
filter_list = [0]
[matrix]
home_server = "" # matrix 服务器地址
bot_id = "" # 机器人的 id
bot_password = "" # 机器人的密码
# 启动时通知的房间
notice_room = [""] # 启动 bot 后通知的房间
notice_start = true # 是否在启动 bot 后通知

View File

@ -5,23 +5,42 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
ed25519 = "2.2.3"
ed25519-dalek = "2.1.1"
hex = "0.4.3"
rust_socketio = { version = "0.4.4", features = ["async"]}
[features]
default = ["ica", "matrix"]
ica = ["dep:ed25519", "dep:ed25519-dalek", "dep:hex", "dep:rust_socketio"]
matrix = ["dep:ruma"]
[dependencies]
# matrix
ruma = { version = "0.9.4", features = [
"client-api-c",
"client-ext-client-api",
"client-reqwest",
"rand",
], optional = true }
# ica
ed25519 = { version = "2.2.3", optional = true }
ed25519-dalek = { version = "2.1.1", optional = true }
hex = { version = "0.4.3", optional = true }
rust_socketio = { version = "0.4.4", features = ["async"], optional = true }
# data
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
chrono = "0.4.34"
toml = "0.8.10"
colored = "2.1.0"
# runtime
tokio = { version = "1.0", features = ["full"] }
futures-util = "0.3.30"
pyo3 = "0.20.3"
# async 这玩意以后在搞
# pyo3-async = "0.3.2"
# pyo3-asyncio = { version = "0.20.0", features = ["attributes", "tokio-runtime"] }
# log
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["time"] }

View File

@ -3,6 +3,7 @@ use std::fs;
use serde::Deserialize;
use toml::from_str;
use tracing::warn;
/// Icalingua bot 的配置
#[derive(Debug, Clone, Deserialize)]
@ -23,15 +24,32 @@ pub struct IcaConfig {
pub filter_list: Vec<i64>,
}
/// Matrix 配置
#[derive(Debug, Clone, Deserialize)]
pub struct MatrixConfig {
/// home server
pub home_server: String,
/// bot_id
pub bot_id: String,
/// bot password
pub bot_password: String,
/// 提醒的房间
pub notice_room: Vec<String>,
/// 是否提醒
pub notice_start: bool,
}
/// 主配置
#[derive(Debug, Clone, Deserialize)]
pub struct BotConfig {
/// 是否启用 icalingua
pub enable_ica: bool,
pub enable_ica: Option<bool>,
/// Ica 配置
pub ica: Option<IcaConfig>,
/// 是否启用 Matrix
pub enable_matrix: Option<bool>,
/// Matrix 配置
// TODO: MatrixConfig
pub matrix: Option<MatrixConfig>,
/// Python 插件路径
pub py_plugin_path: Option<String>,
/// Python 配置文件路径
@ -51,5 +69,53 @@ impl BotConfig {
Self::new_from_path(config_file_path)
}
/// 检查是否启用 ica
pub fn check_ica(&self) -> bool {
match self.enable_ica {
Some(enable) => {
if enable {
if let None = self.ica {
warn!("enable_ica 为 true 但未填写 [ica] 配置\n将不启用 ica");
false
} else {
true
}
} else {
false
}
}
None => {
if let Some(_) = self.ica {
warn!("未填写 enable_ica 但填写了 [ica] 配置\n将不启用 ica");
}
false
}
}
}
/// 检查是否启用 Matrix
pub fn check_matrix(&self) -> bool {
match self.enable_matrix {
Some(enable) => {
if enable {
if let None = self.matrix {
warn!("enable_matrix 为 true 但未填写 [matrix] 配置\n将不启用 Matrix");
false
} else {
true
}
} else {
false
}
}
None => {
if let Some(_) = self.matrix {
warn!("未填写 enable_matrix 但填写了 [matrix] 配置\n将不启用 Matrix");
}
false
}
}
}
pub fn ica(&self) -> IcaConfig { self.ica.clone().expect("No ica config found") }
}

View File

@ -1,5 +1,5 @@
use crate::ica::data_struct::messages::{At, LastMessage};
use crate::ica::data_struct::RoomId;
use crate::data_struct::ica::messages::{At, LastMessage};
use crate::data_struct::ica::RoomId;
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;

View File

@ -1,5 +1,5 @@
use crate::ica::data_struct::files::MessageFile;
use crate::ica::data_struct::{MessageId, RoomId, UserId};
use crate::data_struct::ica::files::MessageFile;
use crate::data_struct::ica::{MessageId, RoomId, UserId};
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};

View File

@ -4,9 +4,9 @@ use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use crate::client::BotStatus;
use crate::ica::data_struct::messages::{At, Message, NewMessage};
use crate::ica::data_struct::{MessageId, UserId};
use crate::data_struct::ica::messages::{At, Message, NewMessage};
use crate::data_struct::ica::{MessageId, UserId};
use crate::ica::client::BotStatus;
impl Serialize for At {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>

View File

@ -0,0 +1 @@
pub mod ica;

View File

@ -1,4 +1,4 @@
pub mod data_struct;
pub mod client;
pub mod events;
use futures_util::FutureExt;
@ -13,7 +13,7 @@ pub async fn start_ica(config: &IcaConfig, stop_reciver: tokio::sync::oneshot::R
let socket = ClientBuilder::new(config.host.clone())
.transport_type(TransportType::Websocket)
.on_any(wrap_any_callback!(events::any_event))
.on("requireAuth", wrap_callback!(crate::client::sign_callback))
.on("requireAuth", wrap_callback!(client::sign_callback))
.on("message", wrap_callback!(events::connect_callback))
.on("authSucceed", wrap_callback!(events::connect_callback))
.on("authFailed", wrap_callback!(events::connect_callback))
@ -32,7 +32,7 @@ pub async fn start_ica(config: &IcaConfig, stop_reciver: tokio::sync::oneshot::R
if config.notice_start {
for room in config.notice_room.iter() {
let startup_msg = data_struct::messages::SendMessage::new(
let startup_msg = crate::data_struct::ica::messages::SendMessage::new(
format!("ica-async-rs bot v{}", crate::VERSION),
room.clone(),
None,

View File

@ -1,7 +1,7 @@
use crate::config::{BotConfig, IcaConfig};
use crate::ica::data_struct::all_rooms::Room;
use crate::ica::data_struct::messages::{DeleteMessage, SendMessage};
use crate::ica::data_struct::online_data::OnlineData;
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::ClientStatus_Global;
use colored::Colorize;

View File

@ -3,10 +3,10 @@ use rust_socketio::asynchronous::Client;
use rust_socketio::{Event, Payload};
use tracing::{info, warn};
use crate::client::{send_message, BotStatus};
use crate::ica::data_struct::all_rooms::Room;
use crate::ica::data_struct::messages::{Message, MessageTrait, NewMessage};
use crate::ica::data_struct::online_data::OnlineData;
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};
/// 获取在线数据

View File

@ -1,16 +1,18 @@
use std::time::Duration;
mod config;
mod data_struct;
#[cfg(feature = "ica")]
mod ica;
#[cfg(feature = "matrix")]
mod matrix;
mod py;
use config::{BotConfig, IcaConfig};
use tracing::info;
mod client;
mod config;
mod ica;
mod matrix;
mod py;
#[allow(non_upper_case_globals)]
pub static mut ClientStatus_Global: client::BotStatus = client::BotStatus {
pub static mut ClientStatus_Global: ica::client::BotStatus = ica::client::BotStatus {
login: false,
current_loaded_messages_count: 0,
online_data: None,
@ -33,6 +35,7 @@ macro_rules! wrap_any_callback {
|event: Event, payload: Payload, client: Client| $f(event, payload, client).boxed()
};
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt().with_max_level(tracing::Level::DEBUG).init();
@ -40,13 +43,14 @@ async fn main() {
// 从命令行获取 host 和 key
// 从命令行获取配置文件路径
let bot_config = config::BotConfig::new_from_cli();
client::BotStatus::update_config(bot_config.clone());
let bot_config = BotConfig::new_from_cli();
ica::client::BotStatus::update_config(bot_config.clone());
py::init_py(&bot_config);
// 准备一个用于停止 socket 的变量
let (send, recv) = tokio::sync::oneshot::channel::<()>();
if bot_config.enable_ica && bot_config.ica.is_some() {
if bot_config.check_ica() {
info!("启动 ica");
let config = bot_config.ica();
tokio::spawn(async move {
@ -62,7 +66,7 @@ async fn main() {
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
// socket.disconnect().await.expect("Disconnect failed");
send.send(()).ok();
info!("Disconnected");
}

View File

@ -4,9 +4,9 @@ use pyo3::prelude::*;
use rust_socketio::asynchronous::Client;
use tracing::{debug, info, warn};
use crate::client::BotStatus;
use crate::ica::data_struct::messages::NewMessage;
use crate::ica::data_struct::MessageId;
use crate::data_struct::ica::messages::NewMessage;
use crate::data_struct::ica::MessageId;
use crate::ica::client::BotStatus;
use crate::py::{class, PyPlugin, PyStatus};
pub fn get_func<'py>(py_module: &'py PyAny, path: &PathBuf, name: &'py str) -> Option<&'py PyAny> {

View File

@ -4,11 +4,11 @@ use tokio::runtime::Runtime;
use toml::Value as TomlValue;
use tracing::{debug, info, warn};
use crate::client::{delete_message, send_message, BotStatus};
use crate::ica::data_struct::messages::{
use crate::data_struct::ica::messages::{
DeleteMessage, MessageTrait, NewMessage, ReplyMessage, SendMessage,
};
use crate::ica::data_struct::MessageId;
use crate::data_struct::ica::MessageId;
use crate::ica::client::{delete_message, send_message, BotStatus};
use crate::ClientStatus_Global;
#[pyclass]

View File

@ -8,8 +8,8 @@ use pyo3::prelude::*;
use pyo3::types::PyTuple;
use tracing::{debug, info, warn};
use crate::client::BotStatus;
use crate::config::{BotConfig, IcaConfig};
use crate::ica::client::BotStatus;
#[derive(Debug, Clone)]
pub struct PyStatus {

0
ica-rs/src/status.rs Normal file
View File