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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//! The one process that owns the engine, serving it to the GUI, CLI and MCP over a private Unix socket.
use std::collections::HashMap;
use std::fs::{self, File, TryLockError};
use std::io::{self, BufRead, BufReader, IsTerminal, Read, Write};
use std::net::Shutdown;
use std::os::unix::fs::PermissionsExt;
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::Path;
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};

use anyhow::{Context, anyhow, bail};

use crate::config::Config;
use crate::engine::{self, Connector, Engine};
use crate::message::clean;
use crate::paths::{self, FileWatch, Paths};
use crate::store::Store;
use crate::sync::{self, Activity, Command, Event, RequestId};
use wire::{AccountStatus, ClientMessage, DaemonMessage, Outcome, Payload, Status};

pub mod client;
pub mod service;
#[cfg(any(test, feature = "testing"))]
pub mod test_support;
#[cfg(test)]
mod tests;
pub mod wire;

pub use client::Client;

const FILE_POLL: Duration = Duration::from_secs(2);
/// How long exit waits for clients to take their last replies before closing their connections.
const FLUSH_GRACE: Duration = Duration::from_secs(1);
const LOG_LIMIT: u64 = 1_048_576;
const POLL: Duration = Duration::from_millis(200);

pub struct Options {
    /// Exit after this long without a connected client; None never idles out.
    pub idle_exit: Option<Duration>,
    pub connect: Connector,
    /// Sends desktop notifications; tests turn it off.
    pub notify: bool,
}

impl Options {
    pub fn foreground() -> Options {
        Options {
            idle_exit: None,
            connect: engine::imap_connector(),
            notify: true,
        }
    }

    pub fn auto_started(idle: Duration) -> Options {
        Options {
            idle_exit: Some(idle),
            ..Options::foreground()
        }
    }
}

/// Serves until `shutdown`, idle exit, or a fatal error; returns after the engine stopped and the socket file is gone.
pub fn run(paths: &Paths, options: Options) -> anyhow::Result<()> {
    // The private state directory guards the socket between `bind` and its chmod.
    paths::create_private_dir(&paths.state_dir)?;
    let _lock = take_daemon_lock(&paths.daemon_lock())?;
    truncate_large_log(&paths.daemon_log())?;
    let socket = paths.daemon_socket();
    let listener = bind_private(&socket)?;
    let served = serve(paths, options, listener);
    let _ = fs::remove_file(&socket);
    served
}

/// The `[account] …` line for an event, as `postvak run` and `sync` print it.
pub fn report(event: &Event) {
    match event {
        Event::NewMail {
            account,
            from,
            subject,
            ..
        } => println!(
            "[{account}] new mail from {}: {}",
            clean(from, false),
            clean(subject, false)
        ),
        Event::Synced {
            account,
            new_messages,
            actions,
            ..
        } => println!("[{account}] synced: {new_messages} new, {actions} rule actions"),
        Event::CommandFailed {
            account, message, ..
        }
        | Event::Error { account, message } => report_error(account, message),
        Event::Activity { account, activity } => log::debug!("[{account}] {activity:?}"),
        Event::ActionDone { .. }
        | Event::BodiesFetched { .. }
        | Event::BodyReady { .. }
        | Event::Restored { .. }
        | Event::RuleApplied { .. }
        | Event::ServerSearchDone { .. } => {}
    }
}

/// The `[account] error: …` line `report` prints for a failure.
pub fn report_error(account: &str, message: &str) {
    eprintln!("[{account}] error: {}", clean(message, false));
}

fn truncate_large_log(log: &Path) -> io::Result<()> {
    match fs::metadata(log) {
        Ok(meta) if meta.len() > LOG_LIMIT => File::options().write(true).open(log)?.set_len(0),
        _ => Ok(()),
    }
}

/// Locks `path` and writes our pid there; held until the returned file drops.
fn take_daemon_lock(path: &Path) -> anyhow::Result<File> {
    let mut file = File::options()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(path)?;
    match file.try_lock() {
        Ok(()) => {
            file.set_len(0)?;
            write!(file, "{}", std::process::id())?;
            Ok(file)
        }
        Err(TryLockError::WouldBlock) => {
            let mut pid = String::new();
            file.read_to_string(&mut pid)?;
            let pid: Option<u32> = pid.trim().parse().ok();
            bail!(
                "already running (pid {})",
                pid.map_or_else(|| "unknown".to_string(), |pid| pid.to_string())
            )
        }
        Err(TryLockError::Error(e)) => Err(e.into()),
    }
}

/// Replaces a leftover socket file and binds a fresh one only we can open.
fn bind_private(socket: &Path) -> anyhow::Result<UnixListener> {
    match fs::remove_file(socket) {
        Err(e) if e.kind() != io::ErrorKind::NotFound => {
            return Err(e).with_context(|| format!("removing {}", socket.display()));
        }
        _ => {}
    }
    let listener = UnixListener::bind(socket).map_err(|e| match e.kind() {
        io::ErrorKind::InvalidInput => anyhow!("socket path too long: {}", socket.display()),
        _ => anyhow::Error::new(e).context(format!("binding {}", socket.display())),
    })?;
    fs::set_permissions(socket, fs::Permissions::from_mode(0o600))?;
    listener.set_nonblocking(true)?;
    Ok(listener)
}

type ClientId = u64;

struct ClientConn {
    out: Sender<String>,
    subscribed: bool,
    writer: JoinHandle<()>,
}

struct Hub {
    activity: HashMap<String, Activity>,
    clients: HashMap<ClientId, ClientConn>,
    last_client_left: Instant,
    next_request: RequestId,
    /// The daemon's request id to the client that sent it and that client's own id.
    pending: HashMap<RequestId, (ClientId, u64)>,
    shutdown: bool,
    started: Instant,
}

impl Hub {
    fn new() -> Hub {
        let started = Instant::now();
        Hub {
            activity: HashMap::new(),
            clients: HashMap::new(),
            last_client_left: started,
            next_request: 0,
            pending: HashMap::new(),
            shutdown: false,
            started,
        }
    }

    fn send(&self, client: ClientId, message: &DaemonMessage) {
        let Some(conn) = self.clients.get(&client) else {
            return;
        };
        match wire::line(message) {
            Ok(line) => {
                let _ = conn.out.send(line);
            }
            Err(e) => log::error!("could not encode a message: {e}"),
        }
    }

    fn reply(&self, client: ClientId, id: u64, outcome: Outcome) {
        self.send(client, &DaemonMessage::Reply { id, outcome });
    }

    fn broadcast(&self, event: &Event) {
        let line = match wire::line(&DaemonMessage::Event(event.clone())) {
            Ok(line) => line,
            Err(e) => return log::error!("could not encode an event: {e}"),
        };
        for conn in self.clients.values().filter(|conn| conn.subscribed) {
            let _ = conn.out.send(line.clone());
        }
    }
}

/// Readers reach the engine through its own mutex, so a slow engine call never stalls the hub.
struct Shared {
    engine: Mutex<Option<Engine>>,
    hub: Mutex<Hub>,
}

/// A panic elsewhere must not take the daemon down with a poisoned lock.
fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
    mutex.lock().unwrap_or_else(PoisonError::into_inner)
}

fn serve(paths: &Paths, options: Options, listener: UnixListener) -> anyhow::Result<()> {
    let mut watch = Watch::new(paths);
    let config = Config::load(&paths.config_file())?;
    migrate_stores(&config, paths);
    let (engine, events) = Engine::start(&config, paths, options.connect.clone());
    let shared = Arc::new(Shared {
        engine: Mutex::new(Some(engine)),
        hub: Mutex::new(Hub::new()),
    });
    let notify: fn(&str, &str) = if options.notify {
        crate::notify::new_mail
    } else {
        |_, _| {}
    };
    let router = std::thread::Builder::new()
        .name("daemon-events".into())
        .spawn({
            let shared = shared.clone();
            move || route_events(&shared, events, io::stdout().is_terminal(), notify)
        })?;
    let connections = accept_until_done(&shared, &listener, &mut watch, options.idle_exit);
    // Connects fail at once from here on instead of waiting out the hello timeout; the socket file and lock remain.
    drop(listener);
    stop(&shared, router, connections);
    Ok(())
}

/// Clients open the stores as soon as the daemon answers, so every migration has to be done before it does; a store
/// that fails to open fails again in its account's thread, which reports it.
fn migrate_stores(config: &Config, paths: &Paths) {
    for account in &config.accounts {
        if let Err(e) = Store::open_account(paths, &account.name) {
            log::warn!("[{}] could not open the store: {e}", account.name);
        }
    }
}

/// config.toml and rules.toml, looked at every 2 s.
struct Watch {
    config: FileWatch,
    rules: FileWatch,
    checked: Instant,
}

impl Watch {
    fn new(paths: &Paths) -> Watch {
        Watch {
            config: FileWatch::new(paths.config_file()),
            rules: FileWatch::new(paths.rules_file()),
            checked: Instant::now(),
        }
    }

    /// The config, loaded again once it changed, and whether the rules file changed.
    fn changed(&mut self) -> (Option<Config>, bool) {
        if self.checked.elapsed() < FILE_POLL {
            return (None, false);
        }
        self.checked = Instant::now();
        let config = self.config.changed().then(|| {
            Config::load(&self.config.path)
                .inspect_err(|e| log::error!("{e}; keeping the running accounts"))
                .ok()
        });
        (config.flatten(), self.rules.changed())
    }
}

struct Connection {
    stream: UnixStream,
    reader: JoinHandle<()>,
}

fn accept_until_done(
    shared: &Arc<Shared>,
    listener: &UnixListener,
    watch: &mut Watch,
    idle_exit: Option<Duration>,
) -> Vec<Connection> {
    let mut connections: Vec<Connection> = Vec::new();
    let mut next_client: ClientId = 0;
    while !done(shared, idle_exit) {
        let (config, rules) = watch.changed();
        if let Some(engine) = lock(&shared.engine).as_mut() {
            if let Some(config) = &config {
                engine.apply_config(config);
            }
            engine.start_ready();
            if rules {
                // Rules act on mail synced after they change, so every running account syncs at once; request 0 has
                // nobody waiting for its reply.
                for account in engine.accounts() {
                    engine.send(&account, 0, Command::SyncNow);
                }
            }
        }
        match listener.accept() {
            Ok((stream, _)) => {
                next_client += 1;
                match open_connection(shared, stream, next_client) {
                    Ok(connection) => connections.push(connection),
                    Err(e) => log::warn!("could not serve a client: {e}"),
                }
            }
            Err(e) if e.kind() == io::ErrorKind::WouldBlock => std::thread::sleep(POLL),
            Err(e) => {
                log::warn!("accept failed: {e}");
                std::thread::sleep(POLL);
            }
        }
        connections.retain(|connection| !connection.reader.is_finished());
    }
    connections
}

fn done(shared: &Shared, idle_exit: Option<Duration>) -> bool {
    let hub = lock(&shared.hub);
    hub.shutdown
        || idle_exit
            .is_some_and(|idle| hub.clients.is_empty() && hub.last_client_left.elapsed() >= idle)
}

/// Stops the engine, delivers the replies its threads still send, then closes every connection.
fn stop(shared: &Shared, router: JoinHandle<()>, connections: Vec<Connection>) {
    lock(&shared.hub).shutdown = true;
    let engine = lock(&shared.engine).take();
    drop(engine);
    let _ = router.join();
    let clients: Vec<ClientConn> = lock(&shared.hub)
        .clients
        .drain()
        .map(|(_, conn)| conn)
        .collect();
    let writers: Vec<JoinHandle<()>> = clients.into_iter().map(|conn| conn.writer).collect();
    let deadline = Instant::now() + FLUSH_GRACE;
    while writers.iter().any(|writer| !writer.is_finished()) && Instant::now() < deadline {
        std::thread::sleep(Duration::from_millis(10));
    }
    for connection in &connections {
        let _ = connection.stream.shutdown(Shutdown::Both);
    }
    writers.into_iter().for_each(|writer| drop(writer.join()));
    for connection in connections {
        let _ = connection.reader.join();
    }
}

/// Senders and subjects only go to a terminal, never into daemon.log; errors go to both.
fn reported(event: &Event, terminal: bool) -> bool {
    terminal || matches!(event, Event::CommandFailed { .. } | Event::Error { .. })
}

fn route_events(
    shared: &Shared,
    events: Receiver<Event>,
    terminal: bool,
    notify: impl Fn(&str, &str),
) {
    for event in events {
        if reported(&event, terminal) {
            report(&event);
        }
        // The sync sends NewMail only for mail that should notify, rules included.
        if let Event::NewMail { from, subject, .. } = &event {
            notify(from, subject);
        }
        let mut hub = lock(&shared.hub);
        if let Event::Activity { account, activity } = &event {
            hub.activity.insert(account.clone(), activity.clone());
        }
        for request in event.request_ids() {
            if let Some((client, id)) = hub.pending.remove(&request) {
                hub.reply(client, id, outcome_of(&event));
            }
        }
        hub.broadcast(&event);
    }
}

fn outcome_of(event: &Event) -> Outcome {
    match event {
        Event::CommandFailed { message, .. } => Outcome::Error(message.clone()),
        _ => Outcome::Ok(Payload::Event(event.clone())),
    }
}

fn open_connection(
    shared: &Arc<Shared>,
    stream: UnixStream,
    client: ClientId,
) -> io::Result<Connection> {
    // macOS hands out accepted sockets with the listener's non-blocking flag.
    stream.set_nonblocking(false)?;
    let kept = stream.try_clone()?;
    let shared = shared.clone();
    let reader = std::thread::Builder::new()
        .name(format!("daemon-client-{client}"))
        .spawn(move || serve_client(&shared, client, stream))?;
    Ok(Connection {
        stream: kept,
        reader,
    })
}

fn serve_client(shared: &Shared, client: ClientId, stream: UnixStream) {
    if let Err(e) = converse(shared, client, &stream) {
        log::debug!("client {client}: {e}");
    }
    if let Some(conn) = unregister(shared, client) {
        drop(conn.out);
        let _ = conn.writer.join();
    }
    let _ = stream.shutdown(Shutdown::Both);
}

fn converse(shared: &Shared, client: ClientId, stream: &UnixStream) -> io::Result<()> {
    let mut lines = BufReader::new(stream).lines();
    let Some(first) = lines.next().transpose()? else {
        return Ok(());
    };
    if !matches!(
        serde_json::from_str(&first),
        Ok(ClientMessage::Hello { .. })
    ) {
        return Ok(());
    }
    if !register(shared, client, stream.try_clone()?)? {
        return Ok(());
    }
    for line in lines {
        if !handle_line(shared, client, &line?) {
            break;
        }
    }
    Ok(())
}

/// Adds the client with its writer thread and greets it; false once the daemon is stopping.
fn register(shared: &Shared, client: ClientId, stream: UnixStream) -> io::Result<bool> {
    let (out, lines) = mpsc::channel();
    let writer = std::thread::Builder::new()
        .name(format!("daemon-writer-{client}"))
        .spawn(move || write_lines(stream, lines))?;
    let mut hub = lock(&shared.hub);
    if hub.shutdown {
        return Ok(false);
    }
    hub.clients.insert(
        client,
        ClientConn {
            out,
            subscribed: false,
            writer,
        },
    );
    let hello = DaemonMessage::Hello {
        protocol: wire::PROTOCOL,
        version: wire::VERSION.into(),
        pid: std::process::id(),
    };
    hub.send(client, &hello);
    Ok(true)
}

fn write_lines(mut stream: UnixStream, lines: Receiver<String>) {
    for line in lines {
        if stream.write_all(line.as_bytes()).is_err() {
            return;
        }
    }
}

fn unregister(shared: &Shared, client: ClientId) -> Option<ClientConn> {
    let mut hub = lock(&shared.hub);
    let conn = hub.clients.remove(&client)?;
    hub.pending.retain(|_, (owner, _)| *owner != client);
    if hub.clients.is_empty() {
        hub.last_client_left = Instant::now();
    }
    Some(conn)
}

/// Answers one line; false when the connection should close.
fn handle_line(shared: &Shared, client: ClientId, line: &str) -> bool {
    let message = match serde_json::from_str::<ClientMessage>(line) {
        Ok(message) => message,
        Err(e) => {
            let Some(id) = id_in(line) else {
                return false;
            };
            lock(&shared.hub).reply(client, id, Outcome::Error(format!("bad message: {e}")));
            return true;
        }
    };
    match message {
        ClientMessage::Hello { .. } => {}
        ClientMessage::Subscribe { id } => {
            let mut hub = lock(&shared.hub);
            if let Some(conn) = hub.clients.get_mut(&client) {
                conn.subscribed = true;
            }
            hub.reply(client, id, Outcome::Ok(Payload::Done));
        }
        ClientMessage::Status { id } => {
            let status = status(shared);
            lock(&shared.hub).reply(client, id, Outcome::Ok(Payload::Status(status)));
        }
        ClientMessage::Shutdown { id } => {
            let mut hub = lock(&shared.hub);
            hub.reply(client, id, Outcome::Ok(Payload::Done));
            hub.shutdown = true;
        }
        ClientMessage::Command {
            id,
            account,
            command,
        } => submit(shared, client, id, &account, command),
    }
    true
}

/// The `id` of a message that did not parse, at its top level or inside its one variant.
fn id_in(line: &str) -> Option<u64> {
    let value: serde_json::Value = serde_json::from_str(line).ok()?;
    let variant = value.as_object()?.values().next();
    value.get("id").or_else(|| variant?.get("id"))?.as_u64()
}

fn status(shared: &Shared) -> Status {
    let accounts = lock(&shared.engine)
        .as_ref()
        .map(Engine::accounts)
        .unwrap_or_default();
    let hub = lock(&shared.hub);
    Status {
        pid: std::process::id(),
        version: wire::VERSION.into(),
        uptime_secs: hub.started.elapsed().as_secs(),
        clients: hub.clients.len(),
        accounts: accounts
            .into_iter()
            .map(|name| AccountStatus {
                activity: hub.activity.get(&name).cloned(),
                name,
            })
            .collect(),
    }
}

/// Refuses the command at once when its account cannot run it, and otherwise hands it to the engine.
fn submit(shared: &Shared, client: ClientId, id: u64, account: &str, command: Command) {
    // None once `stop` took the engine.
    let known = lock(&shared.engine).as_ref().map(|engine| {
        let running = engine.accounts().iter().any(|name| name == account);
        (running, engine.is_restarting(account))
    });
    let request = {
        let mut hub = lock(&shared.hub);
        let refused = match known {
            None => Some("the daemon is stopping".to_string()),
            Some((running, restarting)) => {
                refusal(account, running, restarting, hub.activity.get(account))
            }
        };
        if let Some(refusal) = refused {
            hub.reply(client, id, Outcome::Error(refusal));
            return;
        }
        hub.next_request += 1;
        let request = hub.next_request;
        hub.pending.insert(request, (client, id));
        request
    };
    let sent = lock(&shared.engine)
        .as_ref()
        .is_some_and(|engine| engine.send(account, request, command));
    if !sent {
        let mut hub = lock(&shared.hub);
        if let Some((client, id)) = hub.pending.remove(&request) {
            hub.reply(
                client,
                id,
                Outcome::Error(format!("{account} is not running")),
            );
        }
    }
}

/// Why a command for `account` cannot run now, if it cannot.
fn refusal(
    account: &str,
    running: bool,
    restarting: bool,
    activity: Option<&Activity>,
) -> Option<String> {
    match (running, activity) {
        (false, _) if restarting => Some(format!("{account} is restarting")),
        (false, _) => Some(format!("no account named '{account}'")),
        (true, Some(Activity::Offline { reason, retry_at })) => {
            Some(sync::offline_message(account, reason, *retry_at))
        }
        (true, _) => None,
    }
}