raffi 0.24.0

A simple fuzzel launcher based on YAML file
Documentation
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use std::io::Read;
use std::os::unix::process::CommandExt;
use std::process::{Command, Stdio};
use std::sync::mpsc;
use std::time::{Duration, Instant};

use super::placeholders::MAX_VALUE;

const CLIPBOARD_TIMEOUT: Duration = Duration::from_secs(2);
/// Typing a key per character is slow and each press is a command argument, so
/// keep caret movement for `{cursor}` within a sane bound.
const MAX_CURSOR_MOVES: usize = 1000;

fn find_tool(candidates: &[&'static str]) -> Option<&'static str> {
    std::env::var_os("PATH").and_then(|paths| {
        for dir in std::env::split_paths(&paths) {
            for candidate in candidates {
                if dir.join(candidate).is_file() {
                    return Some(*candidate);
                }
            }
        }
        None
    })
}

/// Shell command that types `$RAFFI_INSERT_VALUE` and then presses Left
/// `cursor_moves` times. wtype treats every argument after `--` as text, so
/// the key presses go through a second invocation.
fn insert_command(tool: &str, cursor_moves: usize) -> String {
    let mut command = if tool == "ydotool" {
        "ydotool type -- \"$RAFFI_INSERT_VALUE\"".to_string()
    } else {
        "wtype -- \"$RAFFI_INSERT_VALUE\"".to_string()
    };
    if cursor_moves > 0 {
        if tool == "ydotool" {
            command.push_str(" && ydotool key");
            // 105 is KEY_LEFT; each move is a press followed by a release.
            command.push_str(&" 105:1 105:0".repeat(cursor_moves));
        } else {
            command.push_str(" && wtype");
            command.push_str(&" -k Left".repeat(cursor_moves));
        }
    }
    format!("sleep 0.2 && {command}")
}

fn spawn_insert(value: &str, cursor_moves: usize) -> Result<(), String> {
    let tool =
        find_tool(&["wtype", "ydotool"]).ok_or("cannot type text: install wtype or ydotool")?;
    Command::new("sh")
        .arg("-c")
        .arg(insert_command(tool, cursor_moves))
        .env("RAFFI_INSERT_VALUE", value)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .map(|_| ())
        .map_err(|error| format!("cannot run {tool}: {error}"))
}

/// Copy a value to the clipboard using the first available tool.
/// Fallback chain: wl-copy -> xclip -> xsel.
pub(super) fn spawn_copy(value: &str) -> bool {
    try_copy(value).is_ok()
}

fn try_copy(value: &str) -> Result<(), String> {
    let tool = find_tool(&["wl-copy", "xclip", "xsel"])
        .ok_or("cannot copy: install wl-clipboard, xclip or xsel")?;

    let copy_cmd = match tool {
        "xclip" => "printf '%s' \"$RAFFI_COPY_VALUE\" | xclip -selection clipboard".to_string(),
        "xsel" => "printf '%s' \"$RAFFI_COPY_VALUE\" | xsel --clipboard --input".to_string(),
        _ => "wl-copy -- \"$RAFFI_COPY_VALUE\"".to_string(),
    };
    Command::new("sh")
        .arg("-c")
        .arg(&copy_cmd)
        .env("RAFFI_COPY_VALUE", value)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .map(|_| ())
        .map_err(|error| format!("cannot run {tool}: {error}"))
}

fn spawn_custom_action(action: &str, value: &str) -> Result<(), String> {
    let command = action.replace("{value}", value);
    crate::debug_log!("action: executing shell command={command:?}");
    Command::new("sh")
        .arg("-c")
        .arg(&command)
        .env("RAFFI_VALUE", value)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .map(|_| ())
        .map_err(|error| format!("cannot run action: {error}"))
}

/// Execute an action for a selected value.
/// Supported keywords:
/// - `insert`: type into the focused app via `wtype` or `ydotool`
/// - `copy`: copy to clipboard via the first available clipboard tool
/// - other: execute as `sh -c` after replacing `{value}` in the command string;
///   the value is also available as `$RAFFI_VALUE`
pub(super) fn execute_action(action: &str, value: &str) {
    crate::debug_log!("action: action={action:?} value={value:?}");
    let _ = dispatch(action, value, 0);
}

fn dispatch(action: &str, value: &str, cursor_moves: usize) -> Result<(), String> {
    match action {
        "insert" => spawn_insert(value, cursor_moves),
        "copy" => try_copy(value),
        "hypr_paste" => {
            Err("hypr_paste is only available for text snippets in the native UI".into())
        }
        _ => spawn_custom_action(action, value),
    }
}

/// Run a snippet action, reporting anything that would stop the value from
/// reaching its destination. `cursor_moves` only applies to `insert`.
pub(super) fn execute_snippet_action(
    action: &str,
    value: &str,
    cursor_moves: Option<usize>,
) -> Result<(), String> {
    crate::debug_log!("action: snippet action={action:?} cursor={cursor_moves:?}");
    validate_snippet(value)?;
    let cursor_moves = if action == "insert" {
        let moves = cursor_moves.unwrap_or(0);
        if moves > MAX_CURSOR_MOVES {
            return Err(format!(
                "{{cursor}} is {moves} characters from the end, the limit is {MAX_CURSOR_MOVES}"
            ));
        }
        moves
    } else {
        0
    };
    dispatch(action, value, cursor_moves)
}

pub(super) fn validate_snippet(value: &str) -> Result<(), String> {
    if value.len() > MAX_VALUE {
        return Err(format!(
            "snippet is larger than {} KiB and cannot be passed to the action",
            MAX_VALUE / 1024
        ));
    }
    if value.contains('\0') {
        return Err("snippet contains a NUL byte and cannot be passed to the action".into());
    }
    Ok(())
}

enum ReadOutcome {
    Done(Vec<u8>),
    Overflow,
    Failed(String),
}

pub(super) fn kill_process_group(pgid: u32) {
    let _ = Command::new("sh")
        .arg("-c")
        .arg(format!(
            "kill -s TERM -- -{pgid} 2>/dev/null; sleep 0.1; kill -s KILL -- -{pgid} 2>/dev/null"
        ))
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status();
}

/// Run `command` in its own process group and return its stdout.
///
/// The deadline covers both reading output and waiting for exit. On timeout
/// or when stdout exceeds `max_output` bytes, the whole process group is
/// killed so that background children cannot keep the pipe open.
pub(super) fn run_bounded(
    mut command: Command,
    timeout: Duration,
    max_output: usize,
) -> Result<Vec<u8>, String> {
    command
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .process_group(0);
    let mut child = command
        .spawn()
        .map_err(|error| format!("cannot start: {error}"))?;
    let pgid = child.id();
    let deadline = Instant::now() + timeout;

    let Some(mut stdout) = child.stdout.take() else {
        kill_process_group(pgid);
        let _ = child.wait();
        return Err("cannot read output".into());
    };
    let (sender, receiver) = mpsc::channel();
    std::thread::spawn(move || {
        let mut output = Vec::new();
        let mut chunk = [0u8; 8192];
        let outcome = loop {
            match stdout.read(&mut chunk) {
                Ok(0) => break ReadOutcome::Done(output),
                Ok(count) => {
                    output.extend_from_slice(&chunk[..count]);
                    if output.len() > max_output {
                        break ReadOutcome::Overflow;
                    }
                }
                Err(error) if error.kind() == std::io::ErrorKind::Interrupted => {}
                Err(error) => break ReadOutcome::Failed(error.to_string()),
            }
        };
        let _ = sender.send(outcome);
    });

    let fail = |child: &mut std::process::Child, message: String| {
        kill_process_group(pgid);
        let _ = child.wait();
        Err(message)
    };

    let output = match receiver.recv_timeout(deadline.saturating_duration_since(Instant::now())) {
        Ok(ReadOutcome::Done(output)) => output,
        Ok(ReadOutcome::Overflow) => {
            return fail(
                &mut child,
                format!("output is larger than {} KiB", max_output / 1024),
            )
        }
        Ok(ReadOutcome::Failed(error)) => {
            return fail(&mut child, format!("cannot read output: {error}"))
        }
        Err(_) => {
            return fail(
                &mut child,
                format!("timed out after {}s", timeout.as_secs_f32()),
            )
        }
    };

    let status = loop {
        match child.try_wait() {
            Ok(Some(status)) => break status,
            Ok(None) if Instant::now() >= deadline => {
                return fail(
                    &mut child,
                    format!("timed out after {}s", timeout.as_secs_f32()),
                );
            }
            Ok(None) => std::thread::sleep(Duration::from_millis(10)),
            Err(error) => return fail(&mut child, format!("cannot wait: {error}")),
        }
    };
    if !status.success() {
        return Err(format!("exited with {status}"));
    }
    Ok(output)
}

/// Read the clipboard as text, trying wl-paste, xclip and xsel in turn.
pub(super) fn read_clipboard() -> Result<String, String> {
    let backends: [(&str, &[&str]); 3] = [
        ("wl-paste", &["--no-newline", "--type", "text"]),
        ("xclip", &["-selection", "clipboard", "-o"]),
        ("xsel", &["--clipboard", "--output"]),
    ];
    let mut errors = Vec::new();
    for (tool, args) in backends {
        let mut command = Command::new(tool);
        command.args(args);
        match run_bounded(command, CLIPBOARD_TIMEOUT, MAX_VALUE) {
            Ok(bytes) => {
                return String::from_utf8(bytes).map_err(|_| "clipboard is not text".to_string())
            }
            // Tools that are not installed only add noise to the error.
            Err(error) if error.starts_with("cannot start:") => {}
            Err(error) => {
                if tool == "wl-paste" {
                    if let Some(reason) = wayland_clipboard_without_text() {
                        return Err(reason);
                    }
                }
                errors.push(format!("{tool}: {error}"));
            }
        }
    }
    if errors.is_empty() {
        return Err("no clipboard tool found: install wl-clipboard, xclip or xsel".into());
    }
    Err(format!(
        "clipboard is empty or unavailable ({})",
        errors.join("; ")
    ))
}

/// Explain a failed `wl-paste --type text` when the clipboard is empty or
/// holds only non-text data such as an image.
fn wayland_clipboard_without_text() -> Option<String> {
    let mut command = Command::new("wl-paste");
    command.arg("--list-types");
    match run_bounded(command, CLIPBOARD_TIMEOUT, 64 * 1024) {
        Ok(output) => describe_non_text(&String::from_utf8_lossy(&output)),
        Err(_) => None,
    }
}

fn describe_non_text(types: &str) -> Option<String> {
    let types: Vec<&str> = types
        .lines()
        .map(str::trim)
        .filter(|kind| !kind.is_empty())
        .collect();
    if types.is_empty() {
        return Some("clipboard is empty".into());
    }
    let is_text = |kind: &&str| {
        kind.starts_with("text/") || matches!(*kind, "TEXT" | "STRING" | "UTF8_STRING")
    };
    if types.iter().any(is_text) {
        return None;
    }
    let shown: Vec<&str> = types.iter().copied().take(3).collect();
    Some(format!(
        "clipboard has no text (it holds {})",
        shown.join(", ")
    ))
}

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

    fn sh(script: &str) -> Command {
        let mut command = Command::new("sh");
        command.arg("-c").arg(script);
        command
    }

    #[test]
    fn test_insert_command_without_cursor() {
        assert_eq!(
            insert_command("wtype", 0),
            "sleep 0.2 && wtype -- \"$RAFFI_INSERT_VALUE\""
        );
        assert_eq!(
            insert_command("ydotool", 0),
            "sleep 0.2 && ydotool type -- \"$RAFFI_INSERT_VALUE\""
        );
    }

    #[test]
    fn test_insert_command_moves_cursor_in_a_separate_invocation() {
        assert_eq!(
            insert_command("wtype", 2),
            "sleep 0.2 && wtype -- \"$RAFFI_INSERT_VALUE\" && wtype -k Left -k Left"
        );
        assert_eq!(
            insert_command("ydotool", 2),
            "sleep 0.2 && ydotool type -- \"$RAFFI_INSERT_VALUE\" && ydotool key 105:1 105:0 105:1 105:0"
        );
    }

    #[test]
    fn test_snippet_action_rejects_values_that_cannot_be_passed() {
        let big = "x".repeat(MAX_VALUE + 1);
        assert!(execute_snippet_action("copy", &big, None)
            .unwrap_err()
            .contains("larger than"));
        assert!(execute_snippet_action("copy", "a\0b", None)
            .unwrap_err()
            .contains("NUL"));
        assert!(
            execute_snippet_action("insert", "abc", Some(MAX_CURSOR_MOVES + 1))
                .unwrap_err()
                .contains("{cursor}")
        );
    }

    #[test]
    fn test_custom_action_receives_hostile_value_through_env() {
        let dir = std::env::temp_dir().join(format!("raffi-action-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let out = dir.join("out");
        let value = "it's \"quoted\"\n$(touch pwned) `touch pwned2` ; echo hi";
        // Run inside `dir` so an injected `touch pwned` would land there.
        let action = format!(
            "cd '{}' && printf '%s' \"$RAFFI_VALUE\" > '{}'",
            dir.display(),
            out.display()
        );
        execute_snippet_action(&action, value, None).unwrap();

        let deadline = Instant::now() + Duration::from_secs(5);
        while Instant::now() < deadline {
            if std::fs::read_to_string(&out).is_ok_and(|written| written == value) {
                break;
            }
            std::thread::sleep(Duration::from_millis(20));
        }
        assert_eq!(std::fs::read_to_string(&out).unwrap(), value);
        assert!(!dir.join("pwned").exists());
        assert!(!dir.join("pwned2").exists());
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn test_run_bounded_returns_output() {
        let output = run_bounded(sh("printf 'hi\\n'"), Duration::from_secs(5), 1024).unwrap();
        assert_eq!(output, b"hi\n");
    }

    #[test]
    fn test_run_bounded_reports_nonzero_exit() {
        let error = run_bounded(sh("echo out; exit 3"), Duration::from_secs(5), 1024).unwrap_err();
        assert!(error.contains("exited"), "{error}");
    }

    #[test]
    fn test_run_bounded_times_out() {
        let started = Instant::now();
        let error = run_bounded(sh("sleep 10"), Duration::from_millis(200), 1024).unwrap_err();
        assert!(error.contains("timed out"), "{error}");
        assert!(started.elapsed() < Duration::from_secs(5));
    }

    #[test]
    fn test_run_bounded_kills_processes_ignoring_term() {
        let started = Instant::now();
        let error = run_bounded(
            sh("trap '' TERM; sleep 3 & wait"),
            Duration::from_millis(200),
            1024,
        )
        .unwrap_err();
        assert!(error.contains("timed out"), "{error}");
        assert!(started.elapsed() < Duration::from_secs(2));
    }

    #[test]
    fn test_run_bounded_kills_on_output_overflow() {
        let started = Instant::now();
        let error = run_bounded(sh("yes"), Duration::from_secs(10), 4096).unwrap_err();
        assert!(error.contains("larger than"), "{error}");
        assert!(started.elapsed() < Duration::from_secs(5));
    }

    #[test]
    fn test_run_bounded_kills_descendants_holding_stdout() {
        let started = Instant::now();
        let error = run_bounded(
            sh("(sleep 10 &); echo parent done"),
            Duration::from_millis(300),
            1024,
        )
        .unwrap_err();
        assert!(error.contains("timed out"), "{error}");
        assert!(started.elapsed() < Duration::from_secs(5));
    }

    #[test]
    fn test_run_bounded_reports_missing_program() {
        let error = run_bounded(
            Command::new("raffi-no-such-program"),
            Duration::from_secs(1),
            1024,
        )
        .unwrap_err();
        assert!(error.contains("cannot start"), "{error}");
    }

    #[test]
    fn test_describe_non_text_clipboard() {
        assert_eq!(describe_non_text("").as_deref(), Some("clipboard is empty"));
        assert_eq!(
            describe_non_text("image/png\nimage/jpeg\n").as_deref(),
            Some("clipboard has no text (it holds image/png, image/jpeg)")
        );
        assert_eq!(describe_non_text("image/png\ntext/plain\n"), None);
        assert_eq!(describe_non_text("UTF8_STRING\n"), None);
    }
}