Move prompt to prompt.rs; add rooms command

This commit is contained in:
0xf8 2023-04-19 19:16:47 -04:00
parent 2b325960ba
commit 8717f31a1b
Signed by: 0xf8
GPG Key ID: 446580D758689584
2 changed files with 114 additions and 105 deletions

View File

@ -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<PathBuf> = 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<Mutex<bool>> = 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<String> = std::env::args().collect();
@ -280,7 +183,7 @@ async fn main() -> anyhow::Result<()> {
}
};
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 {

106
src/prompt.rs Normal file
View File

@ -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(())
}