mirror of
http://shenjack.top:5100/shenjack/icalingua-python-bot.git
synced 2025-04-20 09:19:53 +08:00
Compare commits
2 Commits
4656655017
...
48d4c8fd5d
Author | SHA1 | Date | |
---|---|---|---|
48d4c8fd5d | |||
51cc24e347 |
45
.github/workflows/publish.yml
vendored
Normal file
45
.github/workflows/publish.yml
vendored
Normal file
|
@ -0,0 +1,45 @@
|
|||
name: publish
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
|
||||
jobs:
|
||||
publish: # 全都要!
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest]
|
||||
# python-version: ["3.8", "3.9", "3.10", "3.11",]
|
||||
|
||||
runs-on: ${{ matrix.os }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: 获取版本号
|
||||
id: get_version
|
||||
uses: sravinet/toml-select@v1.0.1
|
||||
with:
|
||||
file: ./ica-rs/Cargo.toml
|
||||
field: "package.version"
|
||||
|
||||
- name: Setup Python
|
||||
uses: actions/setup-python@v5.0.0
|
||||
with:
|
||||
# Version range or exact version of Python or PyPy to use, using SemVer's version range syntax. Reads from .python-version if unset.
|
||||
python-version: 3.8
|
||||
|
||||
- name: Run tests
|
||||
run: cargo test --verbose
|
||||
|
||||
- name: Build
|
||||
run: cargo build --release
|
||||
|
||||
- name: 上传
|
||||
uses: actions/upload-artifact@v2
|
||||
with:
|
||||
name: ica-rs-b${{ github.run_number }}-${{ steps.get_version.outputs.value }}-py38-win-x64
|
||||
path: ./target/release/ica-rs.exe
|
93
Cargo.lock
generated
93
Cargo.lock
generated
|
@ -57,7 +57,7 @@ checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -68,7 +68,7 @@ checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -256,7 +256,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -385,21 +385,6 @@ dependencies = [
|
|||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures"
|
||||
version = "0.3.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-executor",
|
||||
"futures-io",
|
||||
"futures-sink",
|
||||
"futures-task",
|
||||
"futures-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-channel"
|
||||
version = "0.3.30"
|
||||
|
@ -407,7 +392,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-sink",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -416,17 +400,6 @@ version = "0.3.30"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
|
||||
|
||||
[[package]]
|
||||
name = "futures-executor"
|
||||
version = "0.3.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-task",
|
||||
"futures-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-io"
|
||||
version = "0.3.30"
|
||||
|
@ -441,7 +414,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -462,7 +435,6 @@ version = "0.3.30"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-io",
|
||||
"futures-macro",
|
||||
|
@ -640,7 +612,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "ica-rs"
|
||||
version = "0.4.12"
|
||||
version = "0.5.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"colored",
|
||||
|
@ -649,7 +621,6 @@ dependencies = [
|
|||
"futures-util",
|
||||
"hex",
|
||||
"pyo3",
|
||||
"pyo3-asyncio",
|
||||
"rust_socketio",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
@ -881,7 +852,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1016,31 +987,6 @@ dependencies = [
|
|||
"unindent",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyo3-asyncio"
|
||||
version = "0.20.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6ea6b68e93db3622f3bb3bf363246cf948ed5375afe7abff98ccbdd50b184995"
|
||||
dependencies = [
|
||||
"futures",
|
||||
"once_cell",
|
||||
"pin-project-lite",
|
||||
"pyo3",
|
||||
"pyo3-asyncio-macros",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyo3-asyncio-macros"
|
||||
version = "0.20.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "56c467178e1da6252c95c29ecf898b133f742e9181dca5def15dc24e19d45a39"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyo3-build-config"
|
||||
version = "0.20.3"
|
||||
|
@ -1070,7 +1016,7 @@ dependencies = [
|
|||
"proc-macro2",
|
||||
"pyo3-macros-backend",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1083,7 +1029,7 @@ dependencies = [
|
|||
"proc-macro2",
|
||||
"pyo3-build-config",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1325,7 +1271,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1450,17 +1396,6 @@ version = "2.5.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.109"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.50"
|
||||
|
@ -1534,7 +1469,7 @@ checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1620,7 +1555,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1720,7 +1655,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1889,7 +1824,7 @@ dependencies = [
|
|||
"once_cell",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
"syn",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
|
@ -1923,7 +1858,7 @@ checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.50",
|
||||
"syn",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
|
|
@ -1,4 +1,12 @@
|
|||
|
||||
# python 插件路径
|
||||
py_plugin_path = "/path/to/your/plugin"
|
||||
py_config_path = "/path/to/your/config"
|
||||
|
||||
enable_ica = true # 是否启用 ica
|
||||
|
||||
[ica]
|
||||
|
||||
private_key = "" # 与 icalingua 客户端使用的 private_key 一致
|
||||
host = "" # docker 版 icalingua 服务的地址
|
||||
self_id = 0 # 机器人的 qq 号
|
||||
|
@ -12,7 +20,3 @@ notice_start = true # 是否在启动 bot 后通知
|
|||
admin_list = [0] # 机器人的管理员
|
||||
# 过滤的人
|
||||
filter_list = [0]
|
||||
|
||||
# python 插件路径
|
||||
py_plugin_path = "/path/to/your/plugin"
|
||||
py_config_path = "/path/to/your/config"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "ica-rs"
|
||||
version = "0.4.12"
|
||||
version = "0.5.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
@ -21,7 +21,7 @@ tokio = { version = "1.0", features = ["full"] }
|
|||
futures-util = "0.3.30"
|
||||
pyo3 = "0.20.3"
|
||||
# pyo3-async = "0.3.2"
|
||||
pyo3-asyncio = { version = "0.20.0", features = ["attributes", "tokio-runtime"] }
|
||||
# pyo3-asyncio = { version = "0.20.0", features = ["attributes", "tokio-runtime"] }
|
||||
|
||||
tracing = "0.1.40"
|
||||
tracing-subscriber = { version = "0.3.18", features = ["time"] }
|
||||
|
|
|
@ -146,6 +146,9 @@ on_delete_message = Callable[[MessageId, IcaClient], None]
|
|||
# def on_delete_message(msg_id: MessageId, client: IcaClient) -> None:
|
||||
# ...
|
||||
|
||||
# TODO: Matrix adapter
|
||||
# on_matrix_room_message = Callable[[RoomId, NewMessage, IcaClient], None]
|
||||
|
||||
on_config = Callable[[None], Tuple[str, str]]
|
||||
|
||||
CONFIG_DATA: ConfigData = ConfigData()
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use crate::config::IcaConfig;
|
||||
use crate::data_struct::all_rooms::Room;
|
||||
use crate::data_struct::messages::{DeleteMessage, SendMessage};
|
||||
use crate::data_struct::online_data::OnlineData;
|
||||
use crate::ClientStatus;
|
||||
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::ClientStatus_Global;
|
||||
|
||||
use colored::Colorize;
|
||||
use ed25519_dalek::{Signature, Signer, SigningKey};
|
||||
|
@ -47,73 +47,85 @@ pub async fn delete_message(client: &Client, message: &DeleteMessage) -> bool {
|
|||
// pub async fn fetch_history(client: &Client, roomd_id: RoomId) -> bool { false }
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IcalinguaStatus {
|
||||
pub struct BotStatus {
|
||||
pub login: bool,
|
||||
/// currentLoadedMessagesCount
|
||||
pub current_loaded_messages_count: u64,
|
||||
pub online_data: Option<OnlineData>,
|
||||
pub rooms: Option<Vec<Room>>,
|
||||
pub config: Option<IcaConfig>,
|
||||
pub config: Option<BotConfig>,
|
||||
}
|
||||
|
||||
impl IcalinguaStatus {
|
||||
impl BotStatus {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
login: false,
|
||||
current_loaded_messages_count: 0,
|
||||
online_data: None,
|
||||
rooms: None,
|
||||
config: Some(IcaConfig::new_from_cli()),
|
||||
config: Some(BotConfig::new_from_cli()),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn update_online_data(online_data: OnlineData) {
|
||||
unsafe {
|
||||
ClientStatus.online_data = Some(online_data);
|
||||
ClientStatus_Global.online_data = Some(online_data);
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn update_rooms(rooms: Vec<Room>) {
|
||||
unsafe {
|
||||
ClientStatus.rooms = Some(rooms);
|
||||
ClientStatus_Global.rooms = Some(rooms);
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn update_login_status(login: bool) {
|
||||
unsafe {
|
||||
ClientStatus.login = login;
|
||||
ClientStatus_Global.login = login;
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn update_config(config: IcaConfig) {
|
||||
pub fn update_config(config: BotConfig) {
|
||||
unsafe {
|
||||
ClientStatus.config = Some(config);
|
||||
ClientStatus_Global.config = Some(config);
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn update_loaded_messages_count(count: u64) {
|
||||
unsafe {
|
||||
ClientStatus.current_loaded_messages_count = count;
|
||||
ClientStatus_Global.current_loaded_messages_count = count;
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn get_login_status() -> bool { unsafe { ClientStatus.login } }
|
||||
pub fn get_login_status() -> bool { unsafe { ClientStatus_Global.login } }
|
||||
#[inline]
|
||||
pub fn get_rooms() -> &'static Vec<Room> {
|
||||
unsafe { ClientStatus.rooms.as_ref().expect("rooms should be set") }
|
||||
unsafe { ClientStatus_Global.rooms.as_ref().expect("rooms should be set") }
|
||||
}
|
||||
#[inline]
|
||||
pub fn get_loaded_messages_count() -> u64 {
|
||||
unsafe { ClientStatus.current_loaded_messages_count }
|
||||
unsafe { ClientStatus_Global.current_loaded_messages_count }
|
||||
}
|
||||
#[inline]
|
||||
pub fn get_online_data() -> &'static OnlineData {
|
||||
unsafe { ClientStatus.online_data.as_ref().expect("online_data should be set") }
|
||||
unsafe { ClientStatus_Global.online_data.as_ref().expect("online_data should be set") }
|
||||
}
|
||||
#[inline]
|
||||
pub fn get_config() -> &'static IcaConfig {
|
||||
unsafe { ClientStatus.config.as_ref().expect("config should be set") }
|
||||
pub fn get_config() -> &'static BotConfig {
|
||||
unsafe { ClientStatus_Global.config.as_ref().expect("config should be set") }
|
||||
}
|
||||
#[inline]
|
||||
pub fn get_ica_config() -> &'static IcaConfig {
|
||||
unsafe {
|
||||
ClientStatus_Global
|
||||
.config
|
||||
.as_ref()
|
||||
.expect("config should be set")
|
||||
.ica
|
||||
.as_ref()
|
||||
.expect("ica should be set")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -134,7 +146,7 @@ pub async fn sign_callback(payload: Payload, client: Client) {
|
|||
.expect("auth_key should be string");
|
||||
let salt = hex::decode(auth_key).expect("Got an invalid salt from the server");
|
||||
// 签名
|
||||
let private_key = IcalinguaStatus::get_config().private_key.clone();
|
||||
let private_key = BotStatus::get_config().ica().private_key.clone();
|
||||
let array_key: [u8; 32] = hex::decode(private_key)
|
||||
.expect("Not a vaild pub key")
|
||||
.try_into()
|
||||
|
|
|
@ -21,13 +21,24 @@ pub struct IcaConfig {
|
|||
pub admin_list: Vec<i64>,
|
||||
/// 过滤列表
|
||||
pub filter_list: Vec<i64>,
|
||||
}
|
||||
|
||||
/// 主配置
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct BotConfig {
|
||||
/// 是否启用 icalingua
|
||||
pub enable_ica: bool,
|
||||
/// Ica 配置
|
||||
pub ica: Option<IcaConfig>,
|
||||
/// Matrix 配置
|
||||
// TODO: MatrixConfig
|
||||
/// Python 插件路径
|
||||
pub py_plugin_path: Option<String>,
|
||||
/// Python 配置文件路径
|
||||
pub py_config_path: Option<String>,
|
||||
}
|
||||
|
||||
impl IcaConfig {
|
||||
impl BotConfig {
|
||||
pub fn new_from_path(config_file_path: String) -> Self {
|
||||
// try read config from file
|
||||
let config = fs::read_to_string(&config_file_path).expect("Failed to read config file");
|
||||
|
@ -39,4 +50,6 @@ impl IcaConfig {
|
|||
let config_file_path = env::args().nth(1).expect("No config path given");
|
||||
Self::new_from_path(config_file_path)
|
||||
}
|
||||
|
||||
pub fn ica(&self) -> IcaConfig { self.ica.clone().expect("No ica config found") }
|
||||
}
|
||||
|
|
52
ica-rs/src/ica.rs
Normal file
52
ica-rs/src/ica.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
pub mod data_struct;
|
||||
pub mod events;
|
||||
|
||||
use futures_util::FutureExt;
|
||||
use rust_socketio::asynchronous::{Client, ClientBuilder};
|
||||
use rust_socketio::{Event, Payload, TransportType};
|
||||
use tracing::info;
|
||||
|
||||
use crate::config::IcaConfig;
|
||||
use crate::{wrap_any_callback, wrap_callback};
|
||||
|
||||
pub async fn start_ica(config: &IcaConfig, stop_reciver: tokio::sync::oneshot::Receiver<()>) {
|
||||
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("message", wrap_callback!(events::connect_callback))
|
||||
.on("authSucceed", wrap_callback!(events::connect_callback))
|
||||
.on("authFailed", wrap_callback!(events::connect_callback))
|
||||
.on("messageSuccess", wrap_callback!(events::succes_message))
|
||||
.on("messageFailed", wrap_callback!(events::failed_message))
|
||||
.on("onlineData", wrap_callback!(events::get_online_data))
|
||||
.on("setAllRooms", wrap_callback!(events::update_all_room))
|
||||
.on("setMessages", wrap_callback!(events::set_messages))
|
||||
.on("addMessage", wrap_callback!(events::add_message))
|
||||
.on("deleteMessage", wrap_callback!(events::delete_message))
|
||||
.connect()
|
||||
.await
|
||||
.expect("Connection failed");
|
||||
|
||||
info!("Connected");
|
||||
|
||||
if config.notice_start {
|
||||
for room in config.notice_room.iter() {
|
||||
let startup_msg = data_struct::messages::SendMessage::new(
|
||||
format!("ica-async-rs bot v{}", crate::VERSION),
|
||||
room.clone(),
|
||||
None,
|
||||
);
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
info!("发送启动消息到房间: {}", room);
|
||||
if let Err(e) =
|
||||
socket.emit("sendMessage", serde_json::to_value(startup_msg).unwrap()).await
|
||||
{
|
||||
info!("启动信息发送失败 房间:{}|e:{}", room, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 等待停止信号
|
||||
stop_reciver.await.ok();
|
||||
socket.disconnect().await.expect("Disconnect failed");
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
use crate::data_struct::messages::{At, LastMessage};
|
||||
use crate::data_struct::RoomId;
|
||||
use crate::ica::data_struct::messages::{At, LastMessage};
|
||||
use crate::ica::data_struct::RoomId;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value as JsonValue;
|
|
@ -1,5 +1,5 @@
|
|||
use crate::data_struct::files::MessageFile;
|
||||
use crate::data_struct::{MessageId, RoomId, UserId};
|
||||
use crate::ica::data_struct::files::MessageFile;
|
||||
use crate::ica::data_struct::{MessageId, RoomId, UserId};
|
||||
|
||||
use chrono::NaiveDateTime;
|
||||
use serde::{Deserialize, Serialize};
|
|
@ -4,9 +4,9 @@ use chrono::NaiveDateTime;
|
|||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value as JsonValue;
|
||||
|
||||
use crate::client::IcalinguaStatus;
|
||||
use crate::data_struct::messages::{At, Message, NewMessage};
|
||||
use crate::data_struct::{MessageId, UserId};
|
||||
use crate::client::BotStatus;
|
||||
use crate::ica::data_struct::messages::{At, Message, NewMessage};
|
||||
use crate::ica::data_struct::{MessageId, UserId};
|
||||
|
||||
impl Serialize for At {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
|
@ -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 = IcalinguaStatus::get_online_data().qqid;
|
||||
let qq_id = BotStatus::get_online_data().qqid;
|
||||
self.sender_id() == qq_id
|
||||
}
|
||||
fn msg_id(&self) -> &MessageId;
|
|
@ -3,10 +3,10 @@ use rust_socketio::asynchronous::Client;
|
|||
use rust_socketio::{Event, Payload};
|
||||
use tracing::{info, warn};
|
||||
|
||||
use crate::client::{send_message, IcalinguaStatus};
|
||||
use crate::data_struct::all_rooms::Room;
|
||||
use crate::data_struct::messages::{Message, MessageTrait, NewMessage};
|
||||
use crate::data_struct::online_data::OnlineData;
|
||||
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::{py, VERSION};
|
||||
|
||||
/// 获取在线数据
|
||||
|
@ -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());
|
||||
IcalinguaStatus::update_online_data(online_data);
|
||||
BotStatus::update_online_data(online_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ 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 IcalinguaStatus::get_config().filter_list.contains(&message.msg.sender_id) {
|
||||
if BotStatus::get_ica_config().filter_list.contains(&message.msg.sender_id) {
|
||||
return;
|
||||
}
|
||||
info!("add_message {}", message.to_string().cyan());
|
||||
|
@ -76,7 +76,7 @@ pub async fn update_all_room(payload: Payload, _client: Client) {
|
|||
if let Some(raw_rooms) = value.as_array() {
|
||||
let rooms: Vec<Room> =
|
||||
raw_rooms.iter().map(|room| Room::new_from_json(room)).collect();
|
||||
IcalinguaStatus::update_rooms(rooms.clone());
|
||||
BotStatus::update_rooms(rooms.clone());
|
||||
info!("update_all_room {}", rooms.len());
|
||||
}
|
||||
}
|
|
@ -1,18 +1,16 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use futures_util::FutureExt;
|
||||
use rust_socketio::asynchronous::{Client, ClientBuilder};
|
||||
use rust_socketio::{Event, Payload, TransportType};
|
||||
use config::{BotConfig, IcaConfig};
|
||||
use tracing::info;
|
||||
|
||||
mod client;
|
||||
mod config;
|
||||
mod data_struct;
|
||||
mod events;
|
||||
mod ica;
|
||||
mod matrix;
|
||||
mod py;
|
||||
|
||||
#[allow(non_upper_case_globals)]
|
||||
pub static mut ClientStatus: client::IcalinguaStatus = client::IcalinguaStatus {
|
||||
pub static mut ClientStatus_Global: client::BotStatus = client::BotStatus {
|
||||
login: false,
|
||||
current_loaded_messages_count: 0,
|
||||
online_data: None,
|
||||
|
@ -22,18 +20,19 @@ pub static mut ClientStatus: client::IcalinguaStatus = client::IcalinguaStatus {
|
|||
|
||||
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! wrap_callback {
|
||||
($f:expr) => {
|
||||
|payload: Payload, client: Client| $f(payload, client).boxed()
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! wrap_any_callback {
|
||||
($f:expr) => {
|
||||
|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();
|
||||
|
@ -41,45 +40,20 @@ async fn main() {
|
|||
|
||||
// 从命令行获取 host 和 key
|
||||
// 从命令行获取配置文件路径
|
||||
let ica_config = config::IcaConfig::new_from_cli();
|
||||
client::IcalinguaStatus::update_config(ica_config.clone());
|
||||
py::init_py(&ica_config);
|
||||
let bot_config = config::BotConfig::new_from_cli();
|
||||
client::BotStatus::update_config(bot_config.clone());
|
||||
py::init_py(&bot_config);
|
||||
|
||||
let socket = ClientBuilder::new(ica_config.host.clone())
|
||||
.transport_type(TransportType::Websocket)
|
||||
.on_any(wrap_any_callback!(events::any_event))
|
||||
.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))
|
||||
.on("messageSuccess", wrap_callback!(events::succes_message))
|
||||
.on("messageFailed", wrap_callback!(events::failed_message))
|
||||
.on("onlineData", wrap_callback!(events::get_online_data))
|
||||
.on("setAllRooms", wrap_callback!(events::update_all_room))
|
||||
.on("setMessages", wrap_callback!(events::set_messages))
|
||||
.on("addMessage", wrap_callback!(events::add_message))
|
||||
.on("deleteMessage", wrap_callback!(events::delete_message))
|
||||
.connect()
|
||||
.await
|
||||
.expect("Connection failed");
|
||||
|
||||
info!("Connected");
|
||||
|
||||
if ica_config.notice_start {
|
||||
for room in ica_config.notice_room.iter() {
|
||||
let startup_msg = crate::data_struct::messages::SendMessage::new(
|
||||
format!("ica-async-rs bot v{}", VERSION),
|
||||
room.clone(),
|
||||
None,
|
||||
);
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
info!("发送启动消息到房间: {}", room);
|
||||
if let Err(e) =
|
||||
socket.emit("sendMessage", serde_json::to_value(startup_msg).unwrap()).await
|
||||
{
|
||||
info!("启动信息发送失败 房间:{}|e:{}", room, e);
|
||||
}
|
||||
}
|
||||
// 准备一个用于停止 socket 的变量
|
||||
let (send, recv) = tokio::sync::oneshot::channel::<()>();
|
||||
if bot_config.enable_ica && bot_config.ica.is_some() {
|
||||
info!("启动 ica");
|
||||
let config = bot_config.ica();
|
||||
tokio::spawn(async move {
|
||||
ica::start_ica(&config, recv).await;
|
||||
});
|
||||
} else {
|
||||
info!("未启用 ica");
|
||||
}
|
||||
|
||||
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||
|
@ -88,6 +62,7 @@ async fn main() {
|
|||
let mut input = String::new();
|
||||
std::io::stdin().read_line(&mut input).unwrap();
|
||||
|
||||
socket.disconnect().await.expect("Disconnect failed");
|
||||
// socket.disconnect().await.expect("Disconnect failed");
|
||||
send.send(()).ok();
|
||||
info!("Disconnected");
|
||||
}
|
||||
|
|
1
ica-rs/src/matrix.rs
Normal file
1
ica-rs/src/matrix.rs
Normal file
|
@ -0,0 +1 @@
|
|||
|
|
@ -4,9 +4,9 @@ use pyo3::prelude::*;
|
|||
use rust_socketio::asynchronous::Client;
|
||||
use tracing::{debug, info, warn};
|
||||
|
||||
use crate::client::IcalinguaStatus;
|
||||
use crate::data_struct::messages::NewMessage;
|
||||
use crate::data_struct::MessageId;
|
||||
use crate::client::BotStatus;
|
||||
use crate::ica::data_struct::messages::NewMessage;
|
||||
use crate::ica::data_struct::MessageId;
|
||||
use crate::py::{class, PyPlugin, PyStatus};
|
||||
|
||||
pub fn get_func<'py>(py_module: &'py PyAny, path: &PathBuf, name: &'py str) -> Option<&'py PyAny> {
|
||||
|
@ -45,7 +45,7 @@ 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<PathBuf> = Vec::new();
|
||||
let plugin_path = IcalinguaStatus::get_config().py_plugin_path.as_ref();
|
||||
let plugin_path = BotStatus::get_config().py_plugin_path.as_ref();
|
||||
if let None = plugin_path {
|
||||
warn!("未配置 Python 插件路径");
|
||||
return;
|
||||
|
|
|
@ -4,12 +4,12 @@ use tokio::runtime::Runtime;
|
|||
use toml::Value as TomlValue;
|
||||
use tracing::{debug, info, warn};
|
||||
|
||||
use crate::client::{delete_message, send_message, IcalinguaStatus};
|
||||
use crate::data_struct::messages::{
|
||||
use crate::client::{delete_message, send_message, BotStatus};
|
||||
use crate::ica::data_struct::messages::{
|
||||
DeleteMessage, MessageTrait, NewMessage, ReplyMessage, SendMessage,
|
||||
};
|
||||
use crate::data_struct::MessageId;
|
||||
use crate::ClientStatus;
|
||||
use crate::ica::data_struct::MessageId;
|
||||
use crate::ClientStatus_Global;
|
||||
|
||||
#[pyclass]
|
||||
#[pyo3(name = "IcaStatus")]
|
||||
|
@ -20,39 +20,37 @@ impl IcaStatusPy {
|
|||
#[new]
|
||||
pub fn py_new() -> Self { Self {} }
|
||||
#[getter]
|
||||
pub fn get_login(&self) -> bool { unsafe { ClientStatus.login } }
|
||||
pub fn get_login(&self) -> bool { unsafe { ClientStatus_Global.login } }
|
||||
#[getter]
|
||||
pub fn get_online(&self) -> bool { IcalinguaStatus::get_online_data().online }
|
||||
pub fn get_online(&self) -> bool { BotStatus::get_online_data().online }
|
||||
#[getter]
|
||||
pub fn get_self_id(&self) -> i64 { IcalinguaStatus::get_online_data().qqid }
|
||||
pub fn get_self_id(&self) -> i64 { BotStatus::get_online_data().qqid }
|
||||
#[getter]
|
||||
pub fn get_nick_name(&self) -> String { IcalinguaStatus::get_online_data().nick.clone() }
|
||||
pub fn get_nick_name(&self) -> String { BotStatus::get_online_data().nick.clone() }
|
||||
#[getter]
|
||||
pub fn get_loaded_messages_count(&self) -> u64 { IcalinguaStatus::get_loaded_messages_count() }
|
||||
pub fn get_loaded_messages_count(&self) -> u64 { BotStatus::get_loaded_messages_count() }
|
||||
#[getter]
|
||||
pub fn get_ica_version(&self) -> String {
|
||||
IcalinguaStatus::get_online_data().icalingua_info.ica_version.clone()
|
||||
BotStatus::get_online_data().icalingua_info.ica_version.clone()
|
||||
}
|
||||
|
||||
#[getter]
|
||||
pub fn get_os_info(&self) -> String {
|
||||
IcalinguaStatus::get_online_data().icalingua_info.os_info.clone()
|
||||
BotStatus::get_online_data().icalingua_info.os_info.clone()
|
||||
}
|
||||
|
||||
#[getter]
|
||||
pub fn get_resident_set_size(&self) -> String {
|
||||
IcalinguaStatus::get_online_data().icalingua_info.resident_set_size.clone()
|
||||
BotStatus::get_online_data().icalingua_info.resident_set_size.clone()
|
||||
}
|
||||
|
||||
#[getter]
|
||||
pub fn get_heap_used(&self) -> String {
|
||||
IcalinguaStatus::get_online_data().icalingua_info.heap_used.clone()
|
||||
BotStatus::get_online_data().icalingua_info.heap_used.clone()
|
||||
}
|
||||
|
||||
#[getter]
|
||||
pub fn get_load(&self) -> String {
|
||||
IcalinguaStatus::get_online_data().icalingua_info.load.clone()
|
||||
}
|
||||
pub fn get_load(&self) -> String { BotStatus::get_online_data().icalingua_info.load.clone() }
|
||||
}
|
||||
|
||||
impl IcaStatusPy {
|
||||
|
@ -180,16 +178,16 @@ impl IcaClientPy {
|
|||
/// 仅作占位
|
||||
/// (因为目前来说, rust调用 Python端没法启动一个异步运行时
|
||||
/// 所以只能 tokio::task::block_in_place 转换成同步调用)
|
||||
#[staticmethod]
|
||||
pub fn send_message_a(
|
||||
py: Python,
|
||||
client: IcaClientPy,
|
||||
message: SendMessagePy,
|
||||
) -> PyResult<&PyAny> {
|
||||
pyo3_asyncio::tokio::future_into_py(py, async move {
|
||||
Ok(send_message(&client.client, &message.msg).await)
|
||||
})
|
||||
}
|
||||
// #[staticmethod]
|
||||
// pub fn send_message_a(
|
||||
// py: Python,
|
||||
// client: IcaClientPy,
|
||||
// message: SendMessagePy,
|
||||
// ) -> PyResult<&PyAny> {
|
||||
// pyo3_asyncio::tokio::future_into_py(py, async move {
|
||||
// Ok(send_message(&client.client, &message.msg).await)
|
||||
// })
|
||||
// }
|
||||
|
||||
#[getter]
|
||||
pub fn get_status(&self) -> IcaStatusPy { IcaStatusPy::new() }
|
||||
|
|
|
@ -8,8 +8,8 @@ use pyo3::prelude::*;
|
|||
use pyo3::types::PyTuple;
|
||||
use tracing::{debug, info, warn};
|
||||
|
||||
use crate::client::IcalinguaStatus;
|
||||
use crate::config::IcaConfig;
|
||||
use crate::client::BotStatus;
|
||||
use crate::config::{BotConfig, IcaConfig};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PyStatus {
|
||||
|
@ -75,7 +75,7 @@ impl TryFrom<RawPyPlugin> for PyPlugin {
|
|||
if config.is_instance_of::<PyTuple>() {
|
||||
let (config, default) = config.extract::<(String, String)>().unwrap();
|
||||
let base_path =
|
||||
IcalinguaStatus::get_config().py_config_path.as_ref().unwrap();
|
||||
BotStatus::get_config().py_config_path.as_ref().unwrap();
|
||||
|
||||
let mut base_path: PathBuf = PathBuf::from(base_path);
|
||||
|
||||
|
@ -244,7 +244,7 @@ pub fn load_py_file(path: &PathBuf) -> std::io::Result<RawPyPlugin> {
|
|||
Ok((path.clone(), changed_time, content))
|
||||
}
|
||||
|
||||
pub fn init_py(config: &IcaConfig) {
|
||||
pub fn init_py(config: &BotConfig) {
|
||||
debug!("initing python threads");
|
||||
pyo3::prepare_freethreaded_python();
|
||||
if let Some(plugin_path) = &config.py_plugin_path {
|
||||
|
|
Loading…
Reference in New Issue
Block a user