aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/rbw/actions.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2020-04-11 18:53:45 -0400
committerJesse Luehrs <doy@tozt.net>2020-04-11 18:53:45 -0400
commitf65b65bb24960a75cf1f900819c4005e7729e834 (patch)
tree568998776208a3d9ba1c4adf738cef4a4fa28ea4 /src/bin/rbw/actions.rs
parent34053ffef22233c32b731acbf03d79f061e6c63b (diff)
downloadrbw-f65b65bb24960a75cf1f900819c4005e7729e834.tar.gz
rbw-f65b65bb24960a75cf1f900819c4005e7729e834.zip
refactor client code
Diffstat (limited to 'src/bin/rbw/actions.rs')
-rw-r--r--src/bin/rbw/actions.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/bin/rbw/actions.rs b/src/bin/rbw/actions.rs
new file mode 100644
index 0000000..cca2cf1
--- /dev/null
+++ b/src/bin/rbw/actions.rs
@@ -0,0 +1,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),
+ }
+}