use anyhow::Context; use clap::Parser; use roowho2_lib::{proto::WhodUserEntry, server::rwhod::RwhodClientProxy}; /// Check who is logged in on local machines. /// /// The `rwho` command produces output similar to `who`, but for all machines on the local network. /// If no report has been received from a machine for 11 minutes then rwho assumes the machine is down, /// and does not report users last known to be logged into that machine. /// /// If a users hasn't typed to the system for a minute or more, then rwho reports this idle time. /// If a user hasn't typed to the system for an hour or more, /// then the user will be omitted from the output of `rwho` unless the `-a` flag is given. #[derive(Debug, Parser)] pub struct Args { /// Print all machines responding even if no one is currently logged in #[arg(long, short)] all: bool, /// Print the output with the old formatting #[arg(long, short)] old: bool, /// Output in JSON format #[arg(long, short)] json: bool, } #[tokio::main] async fn main() -> anyhow::Result<()> { let args = Args::parse(); let mut conn = zlink::unix::connect("/run/roowho2/roowho2.varlink") .await .expect("Failed to connect to rwhod server"); let reply = conn .rwho(args.all) .await .context("Failed to send rwho request")? .map_err(|e| anyhow::anyhow!("Server returned an error for rwho request: {:?}", e))?; if args.json { println!("{}", serde_json::to_string_pretty(&reply).unwrap()); } else if args.old { for (hostname, user) in &reply { let entry_str = old_format_user_entry(hostname, user); println!("{}", entry_str); } } else { for (hostname, user) in &reply { let entry_str = old_format_user_entry(hostname, user); println!("{}", entry_str); } } Ok(()) } fn old_format_user_entry(hostname: &str, user: &WhodUserEntry) -> String { let idle_str = { let hours = user.idle_time.num_hours().min(99); let minutes = user.idle_time.num_minutes() % 60; format!( "{:>2}:{:02}", if hours < 0 { "".to_string() } else { hours.to_string() }, minutes ) }; format!( "{:<8} {:<8}:{:<8} {} {}", user.user_id, hostname, user.tty, user.login_time.format("%b %d %H:%M"), idle_str, ) } // larseie hildring:pts/10 Jan 5 14:53 :01