aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/rbw/actions.rs
blob: cca2cf151f420b5f42426ba96e48f19727942652 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
pub fn login() {
    simple_action(rbw::agent::Action::Login, "login");
}

pub fn unlock() {
    simple_action(rbw::agent::Action::Unlock, "unlock");
}

pub fn sync() {
    simple_action(rbw::agent::Action::Sync, "sync");
}

pub fn lock() {
    simple_action(rbw::agent::Action::Lock, "lock");
}

pub fn quit() {
    let mut sock = crate::sock::Sock::connect();
    sock.send(&rbw::agent::Request {
        tty: std::env::var("TTY").ok(),
        action: rbw::agent::Action::Quit,
    });
}

pub fn decrypt(cipherstring: &str) -> String {
    let mut sock = crate::sock::Sock::connect();
    sock.send(&rbw::agent::Request {
        tty: std::env::var("TTY").ok(),
        action: rbw::agent::Action::Decrypt {
            cipherstring: cipherstring.to_string(),
        },
    });

    let res = sock.recv();
    match res {
        rbw::agent::Response::Decrypt { plaintext } => plaintext,
        rbw::agent::Response::Error { error } => {
            panic!("failed to decrypt: {}", error)
        }
        _ => panic!("unexpected message: {:?}", res),
    }
}

fn simple_action(action: rbw::agent::Action, desc: &str) {
    let mut sock = crate::sock::Sock::connect();

    sock.send(&rbw::agent::Request {
        tty: std::env::var("TTY").ok(),
        action,
    });

    let res = sock.recv();
    match res {
        rbw::agent::Response::Ack => (),
        rbw::agent::Response::Error { error } => {
            panic!("failed to {}: {}", desc, error)
        }
        _ => panic!("unexpected message: {:?}", res),
    }
}