mirror of
https://codeberg.org/steam-vent/chat.git
synced 2026-11-30 04:30:49 +01:00
82 lines
2.6 KiB
Rust
82 lines
2.6 KiB
Rust
use clap::Parser;
|
|
use std::error::Error;
|
|
use std::io::stdin;
|
|
use std::str::FromStr;
|
|
use steam_vent::auth::{
|
|
AuthConfirmationHandler, ConsoleAuthConfirmationHandler, DeviceConfirmationHandler,
|
|
FileGuardDataStore, SharedSecretAuthConfirmationHandler,
|
|
};
|
|
use steam_vent::{Connection, ServerList};
|
|
use aurelia_steam_vent_chat::{ChatClient, ChatEvent};
|
|
use steamid_ng::SteamID;
|
|
use tokio::spawn;
|
|
use tokio_stream::StreamExt;
|
|
|
|
#[derive(Parser)]
|
|
pub struct Args {
|
|
/// Username to log in with
|
|
username: String,
|
|
/// Password to log in with
|
|
password: String,
|
|
/// User to chat with
|
|
target: String,
|
|
/// base64 encoded steam-guard secret
|
|
#[arg(long)]
|
|
guard_secret: Option<String>,
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn Error>> {
|
|
tracing_subscriber::fmt::init();
|
|
let args: Args = Args::parse();
|
|
|
|
let target_steam_id = SteamID::from_str(args.target.as_str()).expect("invalid steam id");
|
|
|
|
let server_list = ServerList::discover().await?;
|
|
let connection = match args.guard_secret {
|
|
Some(secret) => {
|
|
Connection::login(
|
|
&server_list,
|
|
&args.username,
|
|
&args.password,
|
|
FileGuardDataStore::user_cache(),
|
|
SharedSecretAuthConfirmationHandler::new(&secret),
|
|
)
|
|
.await?
|
|
}
|
|
None => {
|
|
Connection::login(
|
|
&server_list,
|
|
&args.username,
|
|
&args.password,
|
|
FileGuardDataStore::user_cache(),
|
|
ConsoleAuthConfirmationHandler::default().or(DeviceConfirmationHandler),
|
|
)
|
|
.await?
|
|
}
|
|
};
|
|
|
|
// listen() borrows; spawn needs 'static.
|
|
let chat: &'static ChatClient = Box::leak(Box::new(ChatClient::new(connection)));
|
|
|
|
let mut events = chat.listen();
|
|
spawn(async move {
|
|
while let Some(Ok(event)) = events.next().await {
|
|
match event {
|
|
ChatEvent::Typing(event) => println!("{} is tying...", event.source.steam64()),
|
|
ChatEvent::Message(event) => println!("{}: {}", event.source.steam64(), event.message_no_bbcode.unwrap_or(event.message)),
|
|
ChatEvent::EchoMessage(event) => println!("me: {}", event.message_no_bbcode.unwrap_or(event.message)),
|
|
}
|
|
}
|
|
});
|
|
|
|
let mut read_buff = String::with_capacity(32);
|
|
loop {
|
|
read_buff.clear();
|
|
stdin().read_line(&mut read_buff).expect("stdin error");
|
|
let input = read_buff.trim();
|
|
if !input.is_empty() {
|
|
chat.send_message(target_steam_id, input.into()).await?;
|
|
}
|
|
}
|
|
}
|