postvak-core 0.3.1

Postvak's sync, store, rules and daemon, shared by its command line, window and MCP server
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
//! Direct actions on chosen messages: mark, move, archive and delete, through the same `apply` as rules, which the
//! daemon's sync loop runs; plus the dry-run and rule preview helpers the CLI and the MCP server share.
use crate::config::{AccountConfig, Identity};
use crate::mail_ops::{MailError, MailOps};
use crate::message::clean;
use crate::rules::apply::{ApplyError, apply, ensure_raw, trash_destination};
use crate::rules::engine::{Context, Mode, Plan, PlannedAction, evaluate};
use crate::rules::{Action, CompiledRule};
use crate::store::{Message, Store, StoreError};
use crate::trash::Trash;

/// The rule name the CLI logs direct actions under.
pub const RULE_NAME: &str = "cli";

#[derive(Debug, thiserror::Error)]
pub enum ActionError {
    #[error(transparent)]
    Apply(#[from] ApplyError),
    #[error("{0} changed on the server since the last sync; run `postvak sync` first")]
    FolderChanged(String),
    #[error(transparent)]
    Mail(#[from] MailError),
    #[error("no message {folder}/{uid} in the local store; run `postvak sync` first")]
    NotFound { folder: String, uid: u32 },
    #[error(transparent)]
    Store(#[from] StoreError),
    #[error("{0} has not been synced yet; run `postvak sync` first")]
    UnknownFolder(String),
}

/// The outcome for one uid: how many actions ran, or why none did.
pub type UidResult = (u32, Result<usize, ActionError>);

/// Selects `folder`, refusing when the stored uids belong to another UIDVALIDITY and would hit unrelated messages.
pub fn select_synced(
    ops: &mut dyn MailOps,
    store: &Store,
    folder: &str,
) -> Result<(), ActionError> {
    let stored = store
        .folder(folder)?
        .ok_or_else(|| ActionError::UnknownFolder(folder.to_string()))?;
    if ops.select(folder)?.uidvalidity != stored.uidvalidity {
        return Err(ActionError::FolderChanged(folder.to_string()));
    }
    Ok(())
}

/// Runs `action` on each uid in `folder`, logged under `rule_name`. One result per uid, so a missing message does not
/// stop the others.
#[allow(clippy::too_many_arguments)]
pub fn run(
    ops: &mut dyn MailOps,
    store: &Store,
    trash: &Trash,
    folder: &str,
    uids: &[u32],
    action: &Action,
    rule_name: &str,
    now: i64,
) -> Result<Vec<UidResult>, ActionError> {
    select_synced(ops, store, folder)?;
    let plan = Plan {
        actions: vec![PlannedAction {
            rule: rule_name.to_string(),
            action: action.clone(),
        }],
        notify: false,
    };
    let mut results = Vec::new();
    for &uid in uids {
        let result = match store.message(folder, uid) {
            Ok(Some(msg)) => apply(&plan, &msg, ops, store, trash, now).map_err(ActionError::from),
            Ok(None) => Err(ActionError::NotFound {
                folder: folder.to_string(),
                uid,
            }),
            Err(e) => Err(e.into()),
        };
        results.push((uid, result));
    }
    Ok(results)
}

/// Downloads and indexes every body `folder` lacks. Returns how many arrived; a message that cannot be fetched is
/// logged and skipped.
pub fn fetch_bodies(
    ops: &mut dyn MailOps,
    store: &Store,
    folder: &str,
) -> Result<usize, ActionError> {
    let missing = store.messages_without_body(folder)?;
    if missing.is_empty() {
        return Ok(0);
    }
    select_synced(ops, store, folder)?;
    let mut fetched = 0;
    for msg in &missing {
        match ensure_raw(msg, ops, store) {
            Ok(_) => fetched += 1,
            Err(e) => log::warn!("{}/{}: body fetch failed: {e}", msg.folder, msg.uid),
        }
    }
    Ok(fetched)
}

/// Downloads and indexes one message body, refusing when the folder changed on the server since the last sync.
pub fn fetch_body(
    ops: &mut dyn MailOps,
    store: &Store,
    folder: &str,
    uid: u32,
) -> Result<(), ActionError> {
    select_synced(ops, store, folder)?;
    let msg = store
        .message(folder, uid)?
        .ok_or_else(|| ActionError::NotFound {
            folder: folder.to_string(),
            uid,
        })?;
    ensure_raw(&msg, ops, store)?;
    Ok(())
}

/// The most hits one folder adds to a server search, the highest uids first.
pub const SERVER_HITS_PER_FOLDER: usize = 500;

/// The words of a search query that a server search needs all of; control characters cannot go into IMAP.
pub fn search_words(query: &str) -> Vec<String> {
    query
        .split_whitespace()
        .map(|word| clean(word, false))
        .filter(|word| !word.is_empty())
        .collect()
}

/// Searches `folder` on the server for messages whose headers or body contain every word. Returns the uids the store
/// knows; mail not synced yet waits for the next sync.
pub fn search_server(
    ops: &mut dyn MailOps,
    store: &Store,
    folder: &str,
    words: &[String],
) -> Result<Vec<u32>, ActionError> {
    if words.is_empty() {
        return Ok(Vec::new());
    }
    select_synced(ops, store, folder)?;
    let mut hits = Vec::new();
    for uid in ops
        .search_text(words)?
        .into_iter()
        .rev()
        .take(SERVER_HITS_PER_FOLDER)
    {
        if store.message(folder, uid)?.is_some() {
            hits.push(uid);
        }
    }
    Ok(hits)
}

/// Local search results plus the server's hits the store knows, each once, newest first, at most `limit`.
pub fn with_server_hits(
    store: &Store,
    mut found: Vec<Message>,
    hits: &[(String, u32)],
    limit: u32,
) -> Result<Vec<Message>, StoreError> {
    for (folder, uid) in hits {
        if found.iter().any(|m| m.folder == *folder && m.uid == *uid) {
            continue;
        }
        if let Some(message) = store.message(folder, *uid)? {
            found.push(message);
        }
    }
    found.sort_by_key(|m| std::cmp::Reverse((m.internaldate, m.uid)));
    found.truncate(limit as usize);
    Ok(found)
}

/// What a dry run would do to `msg`; a user delete names its outcome, because an expunge cannot be undone on the server.
pub fn planned_effect(store: &Store, msg: &Message, action: &Action) -> Result<String, StoreError> {
    if *action != Action::Trash {
        return Ok(format!("would {}", clean(&action.label(), false)));
    }
    let effect = match trash_destination(store, msg)? {
        Some(trash) => format!("would move to {}", clean(&trash, false)),
        None => "would delete (expunge, .eml backup kept)".to_string(),
    };
    Ok(effect)
}

/// One action a rule would take on a cached message.
#[derive(Debug, Clone)]
pub struct Planned {
    pub rule: String,
    pub message: Message,
    pub action: Action,
}

/// What `rules` would do to every message cached in `store`, evaluated as `rules apply-existing` would.
pub fn planned(
    rules: &[CompiledRule],
    store: &Store,
    account: &AccountConfig,
    identity: &Identity,
    now: i64,
) -> Result<Vec<Planned>, StoreError> {
    let ctx = Context {
        account: &account.name,
        identity,
        now,
        mode: Mode::ApplyExisting,
        notify_default: false,
    };
    let mut planned = Vec::new();
    for folder in store.folders()? {
        for message in store.messages_in_folder(&folder.name)? {
            for PlannedAction { rule, action } in evaluate(rules, &message, &ctx).actions {
                planned.push(Planned {
                    rule,
                    message: message.clone(),
                    action,
                });
            }
        }
    }
    Ok(planned)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mail_ops::RecordingOps;
    use crate::store::{Folder, Message};

    fn setup() -> (RecordingOps, Store, Trash, tempfile::TempDir) {
        let dir = tempfile::tempdir().unwrap();
        let mut ops = RecordingOps::default().with_folder("INBOX", None);
        ops.add_mail(
            "INBOX",
            5,
            100,
            "Subject: hi\r\n\r\n",
            Some("Subject: hi\r\n\r\nbody"),
        );
        let store = Store::open_in_memory().unwrap();
        store
            .upsert_folder(&Folder {
                name: "INBOX".into(),
                uidvalidity: 1,
                last_uid: 5,
                special_use: None,
                delimiter: None,
            })
            .unwrap();
        store
            .insert_message(&Message {
                folder: "INBOX".into(),
                uid: 5,
                thread_id: "t".into(),
                subject: Some("hi".into()),
                internaldate: 100,
                headers: b"Subject: hi\r\n\r\n".to_vec(),
                ..Default::default()
            })
            .unwrap();
        (ops, store, Trash::new(dir.path().join("trash")), dir)
    }

    #[test]
    fn planned_effect_names_a_delete_without_trash() {
        let (_ops, store, _trash, _dir) = setup();
        let msg = store.message("INBOX", 5).unwrap().unwrap();
        assert_eq!(
            planned_effect(&store, &msg, &Action::Trash).unwrap(),
            "would delete (expunge, .eml backup kept)"
        );
        assert_eq!(
            planned_effect(&store, &msg, &Action::Archive).unwrap(),
            "would archive"
        );
    }

    #[test]
    fn run_applies_to_each_uid_and_reports_missing_ones() {
        let (mut ops, store, trash, _dir) = setup();
        let results = run(
            &mut ops,
            &store,
            &trash,
            "INBOX",
            &[5, 99],
            &Action::Flag,
            "mcp:test-host",
            500,
        )
        .unwrap();
        assert!(matches!(results[0], (5, Ok(1))));
        assert!(matches!(
            results[1],
            (99, Err(ActionError::NotFound { .. }))
        ));
        assert_eq!(ops.mail["INBOX"][0].flags, ["\\Flagged"]);
        assert_eq!(store.log(10).unwrap()[0].rule_name, "mcp:test-host");
    }

    #[test]
    fn run_refuses_a_folder_whose_uidvalidity_changed() {
        let (mut ops, store, trash, _dir) = setup();
        ops.uidvalidity.insert("INBOX".into(), 2);
        let err = run(
            &mut ops,
            &store,
            &trash,
            "INBOX",
            &[5],
            &Action::Trash,
            RULE_NAME,
            500,
        )
        .unwrap_err();
        assert!(matches!(err, ActionError::FolderChanged(_)));
        assert_eq!(ops.mail["INBOX"].len(), 1);
        assert_eq!(ops.calls, ["select INBOX"]);
    }

    #[test]
    fn run_refuses_a_folder_that_was_never_synced() {
        let (mut ops, store, trash, _dir) = setup();
        let err = run(
            &mut ops,
            &store,
            &trash,
            "Receipts",
            &[5],
            &Action::Flag,
            RULE_NAME,
            500,
        )
        .unwrap_err();
        assert!(matches!(err, ActionError::UnknownFolder(_)));
        assert!(ops.calls.is_empty());
    }

    #[test]
    fn fetch_bodies_indexes_only_missing_bodies() {
        let (mut ops, store, _trash, _dir) = setup();
        assert_eq!(fetch_bodies(&mut ops, &store, "INBOX").unwrap(), 1);
        assert_eq!(store.search("body", None, 10).unwrap().len(), 1);
        let calls = ops.calls.len();
        assert_eq!(fetch_bodies(&mut ops, &store, "INBOX").unwrap(), 0);
        assert_eq!(ops.calls.len(), calls, "nothing left to fetch");
    }

    #[test]
    fn search_server_returns_the_hits_the_store_knows() {
        let (mut ops, store, _trash, _dir) = setup();
        let mut ops = {
            ops.add_mail(
                "INBOX",
                9,
                300,
                "Subject: unsynced\r\n\r\n",
                Some("Subject: unsynced\r\n\r\nbody"),
            );
            ops.with_folder("Archive", None)
        };
        ops.add_mail(
            "Archive",
            7,
            200,
            "Subject: old\r\n\r\n",
            Some("Subject: old\r\n\r\nsome BODY text"),
        );
        ops.add_mail(
            "Archive",
            8,
            250,
            "Subject: other\r\n\r\n",
            Some("Subject: other\r\n\r\nnothing"),
        );
        store
            .upsert_folder(&Folder {
                name: "Archive".into(),
                uidvalidity: 1,
                last_uid: 8,
                special_use: None,
                delimiter: None,
            })
            .unwrap();
        for uid in [7, 8] {
            store
                .insert_message(&Message {
                    folder: "Archive".into(),
                    uid,
                    thread_id: format!("t{uid}"),
                    ..Default::default()
                })
                .unwrap();
        }
        let words = search_words("body \u{7}");
        assert_eq!(words, ["body"]);
        assert_eq!(
            search_server(&mut ops, &store, "Archive", &words).unwrap(),
            [7]
        );
        assert_eq!(
            search_server(&mut ops, &store, "INBOX", &words).unwrap(),
            [5],
            "uid 9 is not synced yet"
        );
        ops.calls.clear();
        assert!(
            search_server(&mut ops, &store, "INBOX", &search_words("  "))
                .unwrap()
                .is_empty()
        );
        assert!(ops.calls.is_empty());
    }

    #[test]
    fn with_server_hits_adds_the_known_ones_newest_first_once() {
        let (_ops, store, _trash, _dir) = setup();
        let older = Message {
            folder: "INBOX".into(),
            uid: 2,
            thread_id: "t2".into(),
            internaldate: 50,
            ..Default::default()
        };
        store.insert_message(&older).unwrap();
        let local = store.message("INBOX", 5).unwrap().into_iter().collect();
        let hits = [
            ("INBOX".to_string(), 2),
            ("INBOX".to_string(), 5),
            ("INBOX".to_string(), 77),
        ];
        let merged = with_server_hits(&store, local, &hits, 10).unwrap();
        let uids: Vec<u32> = merged.iter().map(|m| m.uid).collect();
        assert_eq!(uids, [5, 2]);
        assert_eq!(
            with_server_hits(&store, Vec::new(), &hits, 1)
                .unwrap()
                .len(),
            1
        );
    }
}