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
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
pub mod chat;
use futures::channel::mpsc;
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::{fmt, io};

pub mod keybase_cmd {
    use super::{ApiError, KBError};
    use futures::{channel::mpsc, stream::Stream};
    use serde::{de::DeserializeOwned, Deserialize, Serialize};
    use serde_json;
    use std::io::{self, BufRead, BufReader, Write};
    use std::process::{Command, Stdio};
    use std::thread;

    thread_local! {
        pub static KEYBASE: String = which_keybase();
    }

    #[derive(Deserialize, Serialize)]
    pub struct APIResult<T> {
        pub result: T,
        pub error: Option<KBError>,
    }

    pub fn which_keybase() -> String {
        let path = String::from_utf8(
            Command::new("which")
                .arg("keybase")
                .output()
                .expect("which is not installed")
                .stdout,
        )
        .expect("Output not in UTF-8");
        String::from(path.trim())
    }

    pub fn call_chat_api<T>(input: &[u8]) -> Result<T, ApiError>
    where
        T: DeserializeOwned,
    {
        let mut child = KEYBASE.with(|kb| {
            Command::new(kb)
                .arg("chat")
                .arg("api")
                .stdin(Stdio::piped())
                .stdout(Stdio::piped())
                .spawn()
                .expect("Couldn't run `keybase`")
        });
        if let Some(stdin) = child.stdin.as_mut() {
            stdin.write_all(input)?;
            let output = child.wait_with_output()?;
            if !output.status.success() {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "Keybase did not return successful exit code",
                )
                .into());
            }

            let output = String::from_utf8(output.stdout)?;
            let res: APIResult<T> = serde_json::from_str(&output)?;
            if let Some(error) = res.error {
                Err(ApiError::KBErr(error))
            } else {
                Ok(res.result)
            }
        } else {
            Err(io::Error::new(io::ErrorKind::BrokenPipe, "Couldn't get stdin").into())
        }
    }

    pub fn listen_chat_api<T>() -> Result<
        (
            impl Stream<Item = T>,
            thread::JoinHandle<Result<(), ApiError>>,
        ),
        ApiError,
    >
    where
        T: DeserializeOwned + Send + 'static,
    {
        let mut child = KEYBASE.with(|kb| {
            Command::new(kb)
                .arg("chat")
                .arg("api-listen")
                .stdin(Stdio::piped())
                .stdout(Stdio::piped())
                .spawn()
                .expect("Couldn't run `keybase`")
        });
        if let Some(stdout) = child.stdout.take() {
            let (mut sender, receiver) = mpsc::channel::<T>(128);
            let handler: thread::JoinHandle<Result<(), ApiError>> = thread::spawn(move || {
                let mut reader = BufReader::new(stdout);
                loop {
                    let mut line = String::new();
                    let _bytes_written = reader.read_line(&mut line)?;
                    println!("got notif: {:?}", line);
                    let res: T = serde_json::from_str(&line)?;
                    sender.start_send(res)?;
                    // if let Some(error) = res.error {
                    //     let err = ApiError::KBErr(error);
                    //     println!("Error in listening: {:?}", &err);
                    //     return Err(err);
                    // } else {
                    //     println!("got notif: {:?}", line);
                    //     sender.start_send(res)?;
                    // }
                }
            });
            Ok((receiver, handler))
        } else {
            Err(io::Error::new(io::ErrorKind::BrokenPipe, "Couldn't get stdout").into())
        }
    }
}

#[derive(Debug)]
pub enum ApiError {
    Parsing(serde_json::error::Error),
    IOErr(io::Error),
    KBErr(KBError),
    UTF8Err(std::string::FromUtf8Error),
    ChannelErr(mpsc::SendError),
}

#[derive(Deserialize, Serialize, Debug)]
pub struct KBError {
    pub code: i32,
    pub message: String,
}

impl fmt::Display for ApiError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl Error for ApiError {}

impl From<mpsc::SendError> for ApiError {
    fn from(error: mpsc::SendError) -> Self {
        ApiError::ChannelErr(error)
    }
}

impl From<std::string::FromUtf8Error> for ApiError {
    fn from(error: std::string::FromUtf8Error) -> Self {
        ApiError::UTF8Err(error)
    }
}

impl From<serde_json::error::Error> for ApiError {
    fn from(error: serde_json::error::Error) -> Self {
        ApiError::Parsing(error)
    }
}

impl From<std::io::Error> for ApiError {
    fn from(error: std::io::Error) -> Self {
        ApiError::IOErr(error)
    }
}

#[cfg(test)]
mod tests {
    use super::chat::*;
    use super::keybase_cmd::*;
    use super::*;

    #[test]
    fn can_find_keybase() {
        println!("Keybase is at: {}", which_keybase());
        assert!(!which_keybase().is_empty());
    }

    #[test]
    fn ls_inbox() {
        list().unwrap();
    }
}