2024-02-18 21:25:42 +08:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
use rust_socketio::ClientBuilder;
|
2024-02-20 14:47:53 +08:00
|
|
|
use tracing::info;
|
2024-02-18 21:25:42 +08:00
|
|
|
|
2023-12-11 01:17:25 +08:00
|
|
|
mod client;
|
2024-01-24 18:14:50 +08:00
|
|
|
mod config;
|
2024-02-18 21:25:42 +08:00
|
|
|
mod data_struct;
|
|
|
|
mod events;
|
2024-01-24 18:14:50 +08:00
|
|
|
mod py;
|
2023-12-11 01:17:25 +08:00
|
|
|
|
2024-02-20 14:47:53 +08:00
|
|
|
#[allow(non_upper_case_globals)]
|
|
|
|
pub static mut ClientStatus: client::IcalinguaStatus = client::IcalinguaStatus {
|
|
|
|
login: false,
|
|
|
|
online_data: None,
|
|
|
|
rooms: None,
|
2024-02-20 17:47:45 +08:00
|
|
|
config: None,
|
2024-02-20 14:47:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
tracing_subscriber::fmt()
|
|
|
|
.with_max_level(tracing::Level::DEBUG)
|
|
|
|
.init();
|
|
|
|
|
2023-12-11 18:17:55 +08:00
|
|
|
// 从命令行获取 host 和 key
|
2023-12-24 17:27:15 +08:00
|
|
|
// 从命令行获取配置文件路径
|
2024-01-24 18:14:50 +08:00
|
|
|
let ica_config = config::IcaConfig::new_from_cli();
|
2024-02-20 17:47:45 +08:00
|
|
|
unsafe {
|
|
|
|
ClientStatus.update_config(ica_config.clone());
|
|
|
|
}
|
2024-02-20 20:11:25 +08:00
|
|
|
py::init_py(&ica_config);
|
2024-02-20 18:22:59 +08:00
|
|
|
let ica_singer = client::IcalinguaSinger::new_from_config(&ica_config);
|
2023-12-11 00:00:01 +08:00
|
|
|
|
2023-12-24 17:27:15 +08:00
|
|
|
let socket = ClientBuilder::new(ica_singer.host.clone())
|
2024-02-10 12:18:09 +08:00
|
|
|
.transport_type(rust_socketio::TransportType::Websocket)
|
2024-02-18 21:25:42 +08:00
|
|
|
.on_any(events::any_event)
|
2024-01-24 18:14:50 +08:00
|
|
|
.on("requireAuth", move |a, b| ica_singer.sign_callback(a, b))
|
2024-02-20 17:15:14 +08:00
|
|
|
.on("message", events::connect_callback)
|
2024-02-18 21:25:42 +08:00
|
|
|
.on("authSucceed", events::connect_callback)
|
|
|
|
.on("authFailed", events::connect_callback)
|
2024-02-18 22:43:54 +08:00
|
|
|
.on("onlineData", events::get_online_data)
|
2024-02-20 15:47:59 +08:00
|
|
|
.on("setAllRooms", events::update_all_room)
|
2024-02-18 22:43:54 +08:00
|
|
|
.on("addMessage", events::add_message)
|
2024-02-20 15:01:47 +08:00
|
|
|
.on("deleteMessage", events::delete_message)
|
2023-12-11 00:00:01 +08:00
|
|
|
.connect()
|
|
|
|
.expect("Connection failed");
|
|
|
|
|
2024-02-18 23:17:43 +08:00
|
|
|
info!("Connected");
|
2024-02-20 18:22:59 +08:00
|
|
|
|
|
|
|
if ica_config.notice_start {
|
|
|
|
for room in ica_config.notice_room.iter() {
|
|
|
|
let startup_msg = crate::data_struct::messages::SendMessage::new(
|
|
|
|
format!("ica-rs bot v{}", env!("CARGO_PKG_VERSION")),
|
|
|
|
room.clone(),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
std::thread::sleep(Duration::from_secs(1));
|
|
|
|
info!("发送启动消息到房间: {}", room);
|
|
|
|
if let Err(e) = socket.emit("sendMessage", serde_json::to_value(startup_msg).unwrap()) {
|
|
|
|
info!("启动信息发送失败 房间:{}|e:{}", room, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-18 23:17:43 +08:00
|
|
|
std::thread::sleep(Duration::from_secs(3));
|
|
|
|
// 等待一个输入
|
|
|
|
info!("Press any key to exit");
|
|
|
|
let mut input = String::new();
|
|
|
|
std::io::stdin().read_line(&mut input).unwrap();
|
|
|
|
socket.disconnect().expect("Disconnect failed");
|
|
|
|
info!("Disconnected");
|
2023-12-11 00:00:01 +08:00
|
|
|
}
|