1use serde_json::Value;
8
9#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
11pub enum Target {
12 #[default]
14 Active,
15 Local,
16 Tab(u32),
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
21pub enum WriteMode {
22 Create,
23 Truncate,
24 CreateNew,
25}
26
27#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
28pub enum Fs {
29 Read {
30 path: String,
31 offset: u64,
32 len: Option<u64>,
33 },
34 Write {
35 path: String,
36 #[serde(with = "serde_bytes")]
37 data: Vec<u8>,
38 mode: WriteMode,
39 perm: Option<u32>,
40 atomic: bool,
41 },
42 Append {
43 path: String,
44 #[serde(with = "serde_bytes")]
45 data: Vec<u8>,
46 },
47 Stat {
48 path: String,
49 follow: bool,
50 },
51 List {
52 path: String,
53 glob: Option<String>,
54 hidden: bool,
55 cursor: Option<String>,
56 },
57 Mkdir {
58 path: String,
59 parents: bool,
60 perm: Option<u32>,
61 },
62 Remove {
64 path: String,
65 recursive: bool,
66 to_trash: bool,
67 },
68 Rename {
69 from: String,
70 to: String,
71 overwrite: bool,
72 },
73 Chmod {
74 path: String,
75 mode: u32,
76 },
77 Watch {
78 path: String,
79 recursive: bool,
80 },
81 Unwatch {
82 id: u64,
83 },
84}
85
86impl Fs {
87 pub fn mutates(&self) -> bool {
89 !matches!(self, Fs::Read { .. } | Fs::Stat { .. } | Fs::List { .. } | Fs::Watch { .. } | Fs::Unwatch { .. })
90 }
91}
92
93#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
94pub enum Io {
95 Pipes,
96 Pty {
97 cols: u16,
98 rows: u16,
99 },
100 VisiblePty {
102 new: bool,
103 },
104}
105
106#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
107pub struct Spawn {
108 pub program: String,
109 pub args: Vec<String>,
110 pub env_inherit: bool,
111 pub env_set: Vec<(String, String)>,
112 pub cwd: Option<String>,
113 pub io: Io,
114 pub timeout_ms: Option<u64>,
115}
116
117pub const ESCALATORS: &[&str] = &["sudo", "doas", "pkexec", "su", "run0"];
119
120impl Spawn {
121 pub fn check_escalation(&self) -> Result<(), String> {
123 let base = self.program.rsplit(['/', '\\']).next().unwrap_or(&self.program);
124 let base = base.strip_suffix(".exe").unwrap_or(base);
125 if ESCALATORS.contains(&base) && !matches!(self.io, Io::VisiblePty { .. }) {
126 return Err(format!("{base} must run in the visible terminal (EscalationNeedsVisiblePty)"));
127 }
128 Ok(())
129 }
130}
131
132#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
133pub enum Signal {
134 Term,
135 Kill,
136 Int,
137}
138
139#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
141pub enum Call {
142 Fs {
143 target: Target,
144 op: Fs,
145 },
146 Spawn {
147 target: Target,
148 spec: Spawn,
149 },
150 Stdin {
151 proc: u64,
152 #[serde(with = "serde_bytes")]
153 data: Vec<u8>,
154 close: bool,
155 },
156 Kill {
157 proc: u64,
158 signal: Signal,
159 },
160 Resize {
161 proc: u64,
162 cols: u16,
163 rows: u16,
164 },
165 Escalate {
168 target: Target,
169 cmdline: String,
170 },
171 Service {
173 method: String,
174 params: Value,
175 },
176 Approve {
178 title: String,
179 detail: String,
180 options: Vec<String>,
181 },
182 TimerSet {
183 id: String,
184 every_ms: Option<u64>,
185 at_ms: Option<u64>,
186 },
187 TimerClear {
188 id: String,
189 },
190 ClipboardGet,
191 ClipboardSet(String),
192}
193
194pub const DENY_LIST: &[&str] =
196 &["$HOME/.ssh/**", "$HOME/.gnupg/**", "$MOUSER_HOME/secrets/**", "$MOUSER_HOME/config.toml", "$HOME/Library/Keychains/**"];