1
use anyhow::Context;
2
use clap::{CommandFactory, Parser};
3
use clap_complete::{Shell, generate};
4
use roowho2_lib::{proto::WhodUserEntry, server::varlink_api::VarlinkRwhodClientProxy};
5

            
6
/// Check who is logged in on local machines.
7
///
8
/// The `rwho` command produces output similar to `who`, but for all machines on the local network.
9
/// If no report has been received from a machine for 11 minutes then rwho assumes the machine is down,
10
/// and does not report users last known to be logged into that machine.
11
///
12
/// If a users hasn't typed to the system for a minute or more, then rwho reports this idle time.
13
/// If a user hasn't typed to the system for an hour or more,
14
/// then the user will be omitted from the output of `rwho` unless the `-a` flag is given.
15
#[derive(Debug, Parser)]
16
#[command(author = "Programvareverkstedet <projects@pvv.ntnu.no>", version)]
17
pub struct Args {
18
    /// Print all machines responding even if no one is currently logged in
19
    #[arg(long, short)]
20
    all: bool,
21

            
22
    /// Print the output with the old formatting
23
    #[arg(long, short)]
24
    old: bool,
25

            
26
    /// Output in JSON format
27
    #[arg(long, short)]
28
    json: bool,
29

            
30
    /// Generate shell completion scripts for the specified shell
31
    /// and print them to stdout.
32
    #[arg(long, value_enum, hide = true)]
33
    completions: Option<Shell>,
34
}
35

            
36
#[tokio::main]
37
async fn main() -> anyhow::Result<()> {
38
    let args = Args::parse();
39

            
40
    if let Some(shell) = args.completions {
41
        generate(shell, &mut Args::command(), "rwho", &mut std::io::stdout());
42
        return Ok(());
43
    }
44

            
45
    let mut conn = zlink::unix::connect("/run/roowho2/roowho2.varlink")
46
        .await
47
        .expect("Failed to connect to rwhod server");
48

            
49
    let mut reply = conn
50
        .rwho(args.all)
51
        .await
52
        .context("Failed to send rwho request")?
53
        .map_err(|e| anyhow::anyhow!("Server returned an error for rwho request: {:?}", e))?;
54

            
55
    if !args.all {
56
        reply.retain(|(_, user)| user.idle_time.num_minutes() <= 11);
57
    }
58

            
59
    reply.sort_by(|(host, user), (host2, user2)| {
60
        user.user_id
61
            .cmp(&user2.user_id)
62
            .then_with(|| host.cmp(host2))
63
            .then_with(|| user.tty.cmp(&user2.tty))
64
    });
65

            
66
    if args.json {
67
        println!("{}", serde_json::to_string_pretty(&reply).unwrap());
68
    } else if args.old {
69
        old_format_user_entries(&reply)
70
            .iter()
71
            .for_each(|line| println!("{}", line));
72
    } else {
73
        old_format_user_entries(&reply)
74
            .iter()
75
            .for_each(|line| println!("{}", line));
76
    }
77

            
78
    Ok(())
79
}
80

            
81
fn old_format_user_entries(entries: &[(String, WhodUserEntry)]) -> Vec<String> {
82
    let hostname_tty_width = entries
83
        .iter()
84
        .map(|(host, user)| host.len() + user.tty.len() + 1)
85
        .max()
86
        .unwrap_or(0);
87

            
88
    let idle_time_width = entries
89
        .iter()
90
        .map(|(_, user)| user.idle_time.num_hours())
91
        .max()
92
        .map(|hours| {
93
            if hours >= 10 {
94
                5
95
            } else if hours > 0 {
96
                4
97
            } else {
98
                3
99
            }
100
        })
101
        .unwrap_or(0);
102

            
103
    entries
104
        .iter()
105
        .map(|(hostname, user)| {
106
            old_format_user_entry(hostname, hostname_tty_width, idle_time_width, user)
107
        })
108
        .collect()
109
}
110

            
111
fn old_format_user_entry(
112
    hostname: &str,
113
    hostname_tty_width: usize,
114
    idle_time_width: usize,
115
    user: &WhodUserEntry,
116
) -> String {
117
    let idle_str = {
118
        let hours = user.idle_time.num_hours().min(99);
119
        let minutes = user.idle_time.num_minutes() % 60;
120
        format!(
121
            "{}:{:02}",
122
            if hours == 0 {
123
                "".to_string()
124
            } else {
125
                hours.to_string()
126
            },
127
            minutes
128
        )
129
    };
130

            
131
    format!(
132
        "{:<8.8} {:<hostname_tty_width$} {:.12} {:>idle_time_width$}",
133
        user.user_id,
134
        format!("{hostname}:{}", user.tty),
135
        user.login_time.format("%b %d %H:%M"),
136
        idle_str,
137
    )
138
}