From 8717f31a1b7d7a55cf802b065f7684f2a2644adf Mon Sep 17 00:00:00 2001 From: 0xf8 <0xf8.dev@proton.me> Date: Wed, 19 Apr 2023 19:16:47 -0400 Subject: [PATCH] Move prompt to prompt.rs; add rooms command --- src/main.rs | 113 ++++---------------------------------------------- src/prompt.rs | 106 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 105 deletions(-) create mode 100644 src/prompt.rs diff --git a/src/main.rs b/src/main.rs index 212a163..f1d92ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,14 @@ use matrix_sdk::{ room::Room, ruma::{ - events::room::{ - message::{ - MessageType, OriginalSyncRoomMessageEvent, Relation, RoomMessageEvent, - }, + events::room::message::{ + MessageType, OriginalSyncRoomMessageEvent, Relation, RoomMessageEvent, }, - OwnedRoomId, - RoomId + OwnedRoomId }, - Error, LoopCtrl, Client, + Error, LoopCtrl, }; -use std::{path::PathBuf, io::{self,BufRead}, sync::Mutex}; +use std::{path::PathBuf, sync::Mutex}; use once_cell::sync::Lazy; pub mod config; @@ -19,6 +16,7 @@ pub mod judge; pub mod keywords; pub mod matrix; pub mod debug; +pub mod prompt; static DATA_DIR: Lazy = Lazy::new(|| dirs::data_dir().expect("No data_dir found").join("scam_police")); @@ -159,101 +157,6 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) -> any } static mut SHOULD_STOP: Lazy> = Lazy::new(|| Mutex::new(false)); -async fn prompt(client: Client) -> anyhow::Result<()> { - loop { - use std::io::Write; - let mut stream = match std::fs::OpenOptions::new().write(true).open("/dev/tty") { - Ok(s) => s, - Err(e) => { eprintln!("prompt: {e}"); break; } - }; - stream - .write_all("> ".as_bytes()) - .and_then(|_| stream.flush())?; - - if let Ok(should_stop) = unsafe { SHOULD_STOP.try_lock() } { - if should_stop.to_owned() { - break; - } - } - - let mut buffer = String::new(); - io::stdin().lock().read_line(&mut buffer)?; - - if buffer.is_empty() { - continue; - } - - let args: Vec<&str> = buffer.split_whitespace().collect(); - if let Some(cmd) = args.get(0) { - match cmd.as_ref() { - "help" => { - println!("help; stop; join room_id; leave room_id"); - }, - "stop" => { - let should_stop = unsafe { SHOULD_STOP.get_mut() }; - if let Ok(should_stop) = should_stop { - *should_stop = true; - eprintln!("Stopping..."); - } - }, - "join" => { - let room = args.get(1); - if room.is_none() { - eprintln!("join: No room_id given.\nSyntax: join room_id"); - continue; - } - - let id = RoomId::parse(room.unwrap().to_string()); - if let Err(e) = id { - eprintln!("join: {e}"); - continue; - } - - if let Err(e) = client.join_room_by_id(&id.unwrap()).await { - if e.to_string().contains("No known servers") { - eprintln!("join: Not a public room and not invited"); - } else { - eprintln!("join: {e}"); - } - } else { - println!("join: Joined room!"); - } - }, - "leave" => { - let room = args.get(1); - if room.is_none() { - eprintln!("leave: no room_id given.\nSyntax: leave room_id"); - continue; - } - - let id = RoomId::parse(room.unwrap().to_string()); - if let Err(e) = id { - eprintln!("leave: {e}"); - continue; - } - - let room = client.get_joined_room(&id.unwrap()); - if room.is_none() { - eprintln!("leave: Not in room"); - continue; - } - - let room = room.unwrap(); - if let Err(e) = room.leave().await { - eprintln!("leave: {e}"); - } else { - println!("leave: Left room!"); - } - } - _ => { eprintln!("Unknown command \"{cmd}\""); } - } - } - } - - Ok(()) -} - - #[tokio::main] async fn main() -> anyhow::Result<()> { let args: Vec = std::env::args().collect(); @@ -279,8 +182,8 @@ async fn main() -> anyhow::Result<()> { anyhow::bail!("{e}"); } }; - - let _stdin_thread = tokio::spawn(prompt(client.to_owned())); + + let _stdin_thread = tokio::spawn(prompt::prompt(client.to_owned())); client.add_event_handler(on_room_message); client .sync_with_result_callback(sync_settings, |sync_result| async move { diff --git a/src/prompt.rs b/src/prompt.rs new file mode 100644 index 0000000..1db632a --- /dev/null +++ b/src/prompt.rs @@ -0,0 +1,106 @@ + +use matrix_sdk::{Client, ruma::RoomId, DisplayName}; +use std::io::{self,BufRead}; + + +pub async fn prompt(client: Client) -> anyhow::Result<()> { + loop { + use std::io::Write; + let mut stream = match std::fs::OpenOptions::new().write(true).open("/dev/tty") { + Ok(s) => s, + Err(e) => { eprintln!("prompt: {e}"); break; } + }; + stream + .write_all("> ".as_bytes()) + .and_then(|_| stream.flush())?; + + if let Ok(should_stop) = unsafe { crate::SHOULD_STOP.try_lock() } { + if should_stop.to_owned() { + break; + } + } + + let mut buffer = String::new(); + io::stdin().lock().read_line(&mut buffer)?; + + if buffer.is_empty() { + continue; + } + + let args: Vec<&str> = buffer.split_whitespace().collect(); + if let Some(cmd) = args.get(0) { + match cmd.as_ref() { + "help" => { + println!("help; stop; rooms; join room_id; leave room_id"); + }, + "stop" => { + let should_stop = unsafe { crate::SHOULD_STOP.get_mut() }; + if let Ok(should_stop) = should_stop { + *should_stop = true; + eprintln!("Stopping..."); + } + }, + "rooms" => { + let rooms = client.rooms(); + + for room in rooms { + println!("{:?}\t{} ({})", room.room_type(), room.room_id(), room.display_name().await.unwrap_or(DisplayName::Empty).to_string()); + } + }, + "join" => { + let room = args.get(1); + if room.is_none() { + eprintln!("join: No room_id given.\nSyntax: join room_id"); + continue; + } + + let id = RoomId::parse(room.unwrap().to_string()); + if let Err(e) = id { + eprintln!("join: {e}"); + continue; + } + + if let Err(e) = client.join_room_by_id(&id.unwrap()).await { + if e.to_string().contains("No known servers") { + eprintln!("join: Not a public room and not invited"); + } else { + eprintln!("join: {e}"); + } + } else { + println!("join: Joined room!"); + } + }, + "leave" => { + let room = args.get(1); + if room.is_none() { + eprintln!("leave: no room_id given.\nSyntax: leave room_id"); + continue; + } + + let id = RoomId::parse(room.unwrap().to_string()); + if let Err(e) = id { + eprintln!("leave: {e}"); + continue; + } + + let room = client.get_joined_room(&id.unwrap()); + if room.is_none() { + eprintln!("leave: Not in room"); + continue; + } + + let room = room.unwrap(); + if let Err(e) = room.leave().await { + eprintln!("leave: {e}"); + } else { + println!("leave: Left room!"); + } + } + _ => { eprintln!("Unknown command \"{cmd}\""); } + } + } + } + + Ok(()) +} +