summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 8085ec6ef9298062e2461bf980d3d4f5c215e028 (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
use std::{io::{Stdin, IsTerminal, Write}, process::{exit, Command, Stdio}, fs::File};


struct Config {
    ssh_destination: String,
    xochitl_path: String,
    ssh_key_path: Option<String>,
    extensions: Extensions,
    tmpdir: String,
    uuid: String
}

#[derive(Default, Clone)]
struct Extensions {
    unavailable_response: bool,
    info: bool
}

fn get_line(stdin: &mut Stdin) -> Option<String> {
    let mut buf = String::new();
    match stdin.read_line(&mut buf) {
        Ok(0) => None,
        Err(err) => {
            eprintln!("read encountered error: {}", err);
            None
        }
        _ => Some(buf)
    }
}

fn get_value(stdin: &mut Stdin) -> Option<String> {
    if let Some(line) = get_line(stdin) {
        if let Some(value) = line.strip_prefix("VALUE ") {
            if value.split_ascii_whitespace().count() == 0 {
                return None
            } else {
                return Some(value.to_owned());
            }
        }
        if line == "VALUE" {
            return None;
        }
    }
    println!("ERROR did not receive value when expected");
    exit(1);
}

fn get_uuid(stdin: &mut Stdin) -> Option<String> {
    println!("GETUUID");
    get_value(stdin)
        .map(|s| s.strip_suffix("\n").unwrap().to_string())
}

fn get_config_value(stdin: &mut Stdin, key: &str) -> Option<String> {
    println!("GETCONFIG {}", key);
    get_value(stdin)
        .map(|s| s.strip_suffix("\n").unwrap().to_string())
}

fn get_config(stdin: &mut Stdin, extensions: &Extensions) -> Config {
    Config {
        ssh_destination: get_config_value(stdin, "ssh_destination").unwrap(),
        xochitl_path: get_config_value(stdin, "xochitl_directory")
            .unwrap_or_else(|| ".local/share/remarkable/xochitl".to_owned()),
        ssh_key_path: get_config_value(stdin, "ssh_key"),
        tmpdir: get_config_value(stdin, "tmpdir")
            .unwrap_or_else(|| "/tmp/xochitl".to_owned()),
        extensions: extensions.to_owned(),
        uuid: get_uuid(stdin).unwrap()
    }
}

fn startup(stdin: &mut Stdin) -> Config {
    let mut extensions = Extensions::default();
    while let Some(line) = get_line(stdin) {
        let mut words = line.split_ascii_whitespace();
        let verb = words.next().expect("missing command verb");
        match verb {
            "EXTENSIONS" => {
                let supported: Vec<_> = words.collect();
                extensions.unavailable_response = supported.contains(&"UNAVAILABLERESPONSE");
                extensions.info = supported.contains(&"INFO");
                println!(
                    "EXTENSIONS {} {}",
                    if extensions.unavailable_response { "UNAVAILABLERESPONSE" } else { "" },
                    if extensions.info { "INFO" } else { "" }
                )
            }
            "PREPARE" => {
                let config = get_config(stdin, &extensions);
                println!("PREPARE-SUCCESS");
                return config;
            },
            "LISTCONFIGS" => {
                println!("CONFIG ssh_destination ssh destination under which the reMarkable tablet can be reached.");
                println!("CONFIG ssh_key the path to an ssh key to use (optional).");
                println!("CONFIG xochitl_directory the directory on the remarkable device in which data is stored (default .local/share/remarkable/xochitl).");
                println!("CONFIGEND")
            },
            "INITREMOTE" => {
                let config = get_config(stdin, &extensions);
                let check = Command::new("ssh")
                    .arg(config.ssh_destination)
                    .arg("ls")
                    .arg(config.xochitl_path)
                    .stdout(Stdio::null())
                    .status();
                if let Ok(status) = check {
                    if status.success() {
                        println!("INITREMOTE-SUCCESS");
                    } else {
                        println!("INITREMOTE-FAILURE ssh check returned {}", status);
                    }
                } else {
                    println!("INITREMOTE-FAILURE failed to spawn ssh check command");
                }
            }
            _ => {
                println!("UNSUPPORTED-REQUEST");
                eprintln!("got unsupported verb while getting config: {}", line);
            }
        }
    }
    println!("DEBUG done early?");
    exit(0);
}


fn key_to_uuid(config: &Config, key: &str) -> String {
    let maybe = Command::new("uuidgen")
        .arg("-s")
        .arg("-n")
        .arg(&config.uuid)
        .arg("-N")
        .arg(key)
        .output();
    match maybe {
        Ok(uuid) => String::from_utf8_lossy(&uuid.stdout)
            .split_ascii_whitespace().next().unwrap().to_owned(),
        Err(e) => {
            println!("DEBUG conversion to uuid failed: {}", e);
            exit(1)
        }
    }
}

fn get_exifdata(filepath: &str, key: &str) -> Option<String> {
    let maybe = Command::new("exiftool")
        .arg(filepath)
        .arg("-b")
        .arg(format!("-{}", key))
        .output();
    match maybe {
        Ok(out) => {
            let value = String::from_utf8_lossy(&out.stdout);
            if value.split_ascii_whitespace().next().is_some() {
                Some(value.to_string())
            } else {
                println!("DEBUG could not get exif data for key {}: empty value returned", key);
                return None
            }
        },
        Err(e) => {
            println!("DEBUG could not get exif data for key {}: {}", key, e);
            return None
        }
    }
}

fn check_key_present<'a>(config: &Config, mut words: impl Iterator<Item=&'a str>) {
    let key = words.next().unwrap();
    assert_no_args_left(words);
    let uuid = key_to_uuid(config, key);
    let check = Command::new("ssh")
        .arg(&config.ssh_destination)
        .arg(format!("test -f {dir}/{uuid}.pdf && test ! -f {dir}/{uuid}.tombstone", dir=config.xochitl_path, uuid=uuid))
        .status();
    if let Ok(code) = check {
        if code.success() {
            println!("CHECKPRESENT-SUCCESS {}", key);
        } else {
            println!("CHECKPRESENT-FAILURE {}", key); // TODO: check if remote present or not
        }
    } else {
        println!("CHECKPRESENT-UNKNOWN {} ssh failed", key);
    }
}

// see https://www.ietf.org/rfc/rfc4627.txt
fn json_string_escape(str: &str) -> String {
    let mut buf = String::with_capacity(str.len());
    for c in str.chars() {
        match c {
            '"' => buf.push_str("\\\""),
            '\\' => buf.push_str("\\\\"),
            // technically (maybe?) overshoots? the rfc seems to think only 0x00
            // to 0x1f are control characters
            _ if c.is_control() => buf.push_str(&format!("\\u{:04x}", c as u32)),
            _ => buf.push(c)
        }
    }
    buf
}

#[test]
fn test_json() {
    assert_eq!(json_string_escape("abcd"), "abcd");
    assert_eq!(json_string_escape("ab\"d"), "ab\\\"d");
    assert_eq!(json_string_escape("ab\nd"), "ab\\u000ad");
}

fn setup_filestructure(config: &Config, path: &str, uuid: &str, file: &str) -> Option<()> {
    let title = match get_exifdata(file, "Title") {
        Some(title) => title,
        None => {
            println!("DEBUG could not get title from exif data, using uuid instead");
            uuid.to_owned()
            // TODO: this can't easily access the actual file name, since the file we
            // get is actually the version in git annex's store, which is content-addressed,
            // and special remotes aren't really supposed to know anything about the file
            // they store.
            //
            // As a workaround, maybe iterate through the repo's file listing until a
            // matching symlink is found, than use that one's file name?
        }
    };
    if config.extensions.info {
        println!("INFO using title {:?}", title);
    }
    Command::new("mkdir")
        .arg("-p")
        .arg(&path)
        .status().ok()?;
    Command::new("ln")
        .arg("-s")
        .arg("-r")
        .arg(file)
        .arg(format!("{}/{}.pdf", path, uuid))
        .status().ok()?;
    let mut metadata = File::create(format!("{}/{}.metadata", path, uuid)).ok()?;
    metadata.write_all(format!(r#"{{
      "createdTime": "{time}",
      "lastModified": "{time}",
      "lastOpened": "0",
      "lastOpenedPage": 0,
      "parent": "",
      "pinned": false,
      "type": "DocumentType",
      "visibleName": "{title}"
    }}"#, time=10, title=json_string_escape(&title)).as_bytes()).ok()?;
    let mut content = File::create(format!("{}/{}.content", path, uuid)).ok()?;
    content.write_all(format!(r#"{{
      "coverPageNumber": 0,
      "documentMetadata": {{}},
      "extraMetadata": {{}},
      "fileType": "pdf",
      "fontName": "",
      "pageTags": [],
      "tags": [],
      "textAlignment": "justify",
      "textScale": 1,
      "zoomMode": "bestFit"
    }}"#).as_bytes()).ok()?;
    Some(())
}


fn store_key<'a>(config: &Config, mut words: impl Iterator<Item=&'a str>) {
    let key = words.next().unwrap();
    let file = words.next().unwrap();
    assert_no_args_left(words);
    let uuid = key_to_uuid(config, key);
    let path = format!("{}/{}", config.tmpdir, uuid);

    if setup_filestructure(config, &path, &uuid, file).is_none() {
        println!("TRANSFER-FAILURE STORE {} could not set up xochitl file structure", key);
        return
    }

    // TODO: check if anything with that uuid is present already, otherwise destructive
    let check = Command::new("scp")
        .arg("-r")
        .arg(format!("{}/{}.pdf", path, uuid))
        .arg(format!("{}/{}.content", path, uuid))
        .arg(format!("{}/{}.metadata", path, uuid))
        .arg(format!("{}:{}", config.ssh_destination, config.xochitl_path))
        .status();
    if let Ok(code) = check {
        if code.success() {
            println!("TRANSFER-SUCCESS STORE {}", key);
        } else {
            println!("TRANSFER-FAILURE STORE {}", key); // TODO: check if remote present or not
        }
    } else {
        println!("TRANSFER-FAILURE STORE {} ssh failed", key);
    }

    let cleanup = Command::new("rm")
        .arg("-r")
        .arg(&path)
        .status();
    let maybe_info = if config.extensions.info { "INFO" } else { "DEBUG" };
    if let Ok(code) = cleanup {
        if !code.success() {
            println!("{} could not clean up tmp path {}: `rm' returned code {}", maybe_info, path, code);
        }
    } else {
        println!("{} could not clean up tmp path {}: could not run `rm'", maybe_info, path);
    }
}

fn retrieve_key<'a>(config: &Config, mut words: impl Iterator<Item=&'a str>) {
    let key = words.next().unwrap();
    let file = words.next().unwrap();
    assert_no_args_left(words);
    let uuid = key_to_uuid(config, key);
    let check = Command::new("scp")
        .arg(format!("{}:{}/{}.pdf", config.ssh_destination, config.xochitl_path, uuid))
        .arg(file)
        .status();
    if let Ok(code) = check {
        if code.success() {
            println!("TRANSFER-SUCCESS RETRIEVE {}", key);
        } else {
            println!("TRANSFER-FAILURE RETRIEVE {}", key); // TODO: check if remote present or not
        }
    } else {
        println!("TRANSFER-FAILURE RETRIEVE {} ssh failed", key);
    }
}

fn remove_key<'a>(config: &Config, mut words: impl Iterator<Item=&'a str>) {
    let key = words.next().unwrap();
    assert_no_args_left(words);
    let uuid = key_to_uuid(config, key);

    let cmd = Command::new("ssh")
        .arg(&config.ssh_destination)
        .arg(format!("rm -r {dir}/{uuid}*", dir=config.xochitl_path, uuid=uuid))
        .status();
    if let Ok(code) = cmd {
        if code.success() {
            println!("REMOVE-SUCCESS {}", key);
        } else {
            println!("REMOVE-FAILURE {} command failed", key); // TODO: check if remote present or not
        }
    } else {
        println!("REMOVE-FAILURE {} ssh failed", key);
    }
}

fn whereis<'a>(config: &Config, mut words: impl Iterator<Item=&'a str>) {
    let key = words.next().unwrap();
    assert_no_args_left(words);
    let uuid = key_to_uuid(config, key);
    println!("WHEREIS-SUCCESS {ssh}:{dir}/{uuid}.pdf", ssh=config.ssh_destination, dir=config.xochitl_path, uuid=uuid);
}

fn assert_no_args_left<'a>(mut words: impl Iterator<Item=&'a str>) {
    if words.next().is_some() {
        println!("UNSUPPORTED-REQUEST");
        println!("ERROR");
        exit(1);
    }
}

fn main() {
    let mut stdin = std::io::stdin();
    if stdin.is_terminal() {
        eprintln!("warning: this program is not meant to be invoked by hand.");
    }

    println!("VERSION 2");

    let config = startup(&mut stdin);

    while let Some(line) = get_line(&mut stdin) {
        let mut words = line.split_ascii_whitespace();
        let verb = words.next().expect("missing command verb");
        match verb {
            "CHECKPRESENT" => check_key_present(&config, words),
            "TRANSFER" => match words.next().expect("missing STORE/RETRIEVE") {
                "STORE" => store_key(&config, words),
                "RETRIEVE" => retrieve_key(&config, words),
                _ => println!("UNSUPPORTED-REQUEST")
            }
            "REMOVE" => remove_key(&config, words),
            "WHEREIS" => whereis(&config, words),
            "GETAVAILABILITY" => {
                println!("ERROR");
                unimplemented!()
                // check routing table if usb network device is present?
            }
            _ => println!("UNSUPPORTED-REQUEST")
        }
    }

}