ecr-server 0.1.1

The ecr mail server: REST, SSE, bearer auth and a maildir watcher over ecr-store
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
642
643
644
645
use crate::error::{ApiError, ApiResult};
use crate::events::{ServerEvent, SyncProgress};
use crate::state::AppState;
use axum::extract::{Path, Query as AxumQuery, State};
use axum::http::{header, HeaderMap, StatusCode};
use axum::response::sse::{Event, KeepAlive, Sse};
use axum::response::{IntoResponse, Response};
use axum::Json;
use ecr_core::account::{Account, AccountId};
use ecr_core::doctor::Doctor;
use ecr_core::message::{
    Body, BodyFormat, Message, MessageId, PartId, Query, SyncReport, TagOp, Thread, ThreadId,
};
use ecr_core::revision::Revision;
use ecr_store::store::{BodyOptions, MailStore};
use futures::stream::Stream;
use serde::{Deserialize, Serialize};

#[derive(Serialize)]
pub struct Page<T> {
    pub revision: Revision,
    pub total: usize,
    pub items: Vec<T>,
}

#[derive(Debug, Deserialize)]
pub struct ThreadQuery {
    #[serde(default)]
    pub q: String,
    pub limit: Option<usize>,
    pub offset: Option<usize>,
}

impl ThreadQuery {
    fn to_query(&self) -> Query {
        Query::new(self.q.clone())
            .limit(self.limit.unwrap_or(Query::DEFAULT_LIMIT).clamp(1, 500))
            .offset(self.offset.unwrap_or(0))
    }
}

pub async fn health(State(state): State<AppState>) -> Json<Doctor> {
    Json(state.store.doctor().await)
}

pub async fn revision(State(state): State<AppState>) -> ApiResult<Json<Revision>> {
    Ok(Json(state.store.revision().await?))
}

pub async fn accounts(State(state): State<AppState>) -> ApiResult<Json<Vec<Account>>> {
    Ok(Json(state.store.accounts().await?))
}

#[derive(Serialize)]
pub struct AddressEntry {
    pub name: Option<String>,
    pub email: String,
    pub source: &'static str,
    pub count: usize,
}

/// The address book, for recipient completion in the composer.
pub async fn addresses(State(state): State<AppState>) -> ApiResult<Json<Vec<AddressEntry>>> {
    let book = state.store.notmuch().address_book(0).await?;

    Ok(Json(
        book.ranked()
            .into_iter()
            .map(|entry| AddressEntry {
                name: entry.address.name,
                email: entry.address.email,
                source: match entry.source {
                    ecr_store::address::Source::Recipient => "recipient",
                    ecr_store::address::Source::Sender => "sender",
                },
                count: entry.count,
            })
            .collect(),
    ))
}

/// Every tag in the database, for query completion.
pub async fn tags(State(state): State<AppState>) -> ApiResult<Json<Vec<String>>> {
    Ok(Json(state.store.notmuch().tags().await?))
}

/// How many recent messages the list scan reads headers from.
const LIST_SCAN: usize = 2000;

#[derive(Serialize)]
pub struct Lists {
    pub lists: Vec<ecr_store::notmuch::MailingList>,
    /// False when `index.header.List` is unset, which makes `List:` unsearchable
    /// and every row below useless. The client says so rather than showing rows
    /// that match nothing.
    pub searchable: bool,
}

pub async fn lists(State(state): State<AppState>) -> ApiResult<Json<Lists>> {
    let notmuch = state.store.notmuch();
    Ok(Json(Lists {
        lists: notmuch.mailing_lists(LIST_SCAN).await?,
        searchable: notmuch.indexes_list_id().await,
    }))
}

/// How many queries one request may ask about.
///
/// The sidebar sends a query per visible row, which is tens. A cap keeps a
/// single request from turning into an unbounded amount of Xapian work.
const MAX_COUNT_QUERIES: usize = 200;

#[derive(Deserialize)]
pub struct CountsRequest {
    pub queries: Vec<String>,
}

#[derive(Serialize)]
pub struct CountsResponse {
    /// Positional, matching the request: duplicate queries stay cheap and the
    /// client zips by index rather than re-deriving the key it sent.
    pub counts: Vec<u64>,
}

pub async fn counts(
    State(state): State<AppState>,
    Json(request): Json<CountsRequest>,
) -> ApiResult<Json<CountsResponse>> {
    if request.queries.len() > MAX_COUNT_QUERIES {
        return Err(ApiError::BadRequest(format!(
            "at most {MAX_COUNT_QUERIES} queries per request, got {}",
            request.queries.len()
        )));
    }

    let counts = state.store.count_batch(&request.queries).await?;
    Ok(Json(CountsResponse { counts }))
}

pub async fn threads(
    State(state): State<AppState>,
    headers: HeaderMap,
    AxumQuery(params): AxumQuery<ThreadQuery>,
) -> ApiResult<Response> {
    let revision = state.store.revision().await?;

    if let Some(etag) = headers
        .get(header::IF_NONE_MATCH)
        .and_then(|v| v.to_str().ok())
    {
        if etag == revision.etag() {
            return Ok(StatusCode::NOT_MODIFIED.into_response());
        }
    }

    let query = params.to_query();
    let items = state.store.search_threads(&query).await?;
    let total = state.store.count(&query).await?;

    let etag = revision.etag();
    let page = Page {
        revision,
        total,
        items,
    };

    Ok(([(header::ETAG, etag)], Json(page)).into_response())
}

pub async fn thread(
    State(state): State<AppState>,
    Path(id): Path<String>,
) -> ApiResult<Json<Thread>> {
    let thread = state.store.thread(&ThreadId(id)).await?;
    if thread.messages.is_empty() {
        return Err(ApiError::NotFound("no such thread".to_string()));
    }
    Ok(Json(thread))
}

pub async fn message(
    State(state): State<AppState>,
    Path(id): Path<String>,
) -> ApiResult<Json<Message>> {
    Ok(Json(state.store.message(&MessageId(id)).await?))
}

#[derive(Debug, Deserialize)]
pub struct BodyQuery {
    #[serde(default)]
    pub html: Option<bool>,
    #[serde(default)]
    pub remote: Option<bool>,
}

pub async fn body(
    State(state): State<AppState>,
    Path(id): Path<String>,
    AxumQuery(params): AxumQuery<BodyQuery>,
) -> ApiResult<Json<Body>> {
    let options = BodyOptions {
        format: if params.html.unwrap_or(true) {
            BodyFormat::Html
        } else {
            BodyFormat::Text
        },
        allow_remote_resources: params.remote.unwrap_or(false),
    };

    Ok(Json(state.store.body(&MessageId(id), options).await?))
}

pub async fn part(
    State(state): State<AppState>,
    Path((id, part)): Path<(String, u32)>,
) -> ApiResult<Response> {
    let part = state.store.part(&MessageId(id), &PartId(part)).await?;

    let disposition = match &part.meta.filename {
        Some(name) => format!("attachment; filename=\"{}\"", sanitize_filename(name)),
        None => "inline".to_string(),
    };

    Ok((
        [
            (header::CONTENT_TYPE, part.meta.content_type.clone()),
            (header::CONTENT_DISPOSITION, disposition),
            (
                header::CACHE_CONTROL,
                "private, max-age=31536000, immutable".to_string(),
            ),
        ],
        part.bytes,
    )
        .into_response())
}

fn sanitize_filename(name: &str) -> String {
    name.chars()
        .filter(|c| !matches!(c, '"' | '\\' | '\r' | '\n'))
        .collect()
}

#[derive(Debug, Deserialize)]
pub struct TagRequest {
    pub ops: Vec<TagOp>,
}

pub async fn tag(
    State(state): State<AppState>,
    Json(request): Json<TagRequest>,
) -> ApiResult<Json<Revision>> {
    reject_if_read_only(&state)?;

    let ids: Vec<String> = request.ops.iter().map(|o| o.id.to_string()).collect();
    let revision = state.store.tag(&request.ops).await?;
    state.note_own_write(&revision).await;

    state.events.publish(ServerEvent::TagsChanged {
        revision: revision.clone(),
        ids,
    });

    Ok(Json(revision))
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct SyncRequest {
    pub accounts: Vec<String>,
}

pub async fn sync(
    State(state): State<AppState>,
    body: Option<Json<SyncRequest>>,
) -> ApiResult<Json<SyncReport>> {
    reject_if_read_only(&state)?;

    let accounts: Vec<AccountId> = body
        .map(|Json(r)| r.accounts)
        .unwrap_or_default()
        .into_iter()
        .map(AccountId)
        .collect();

    state.events.publish(ServerEvent::SyncStarted {
        accounts: accounts.iter().map(|a| a.to_string()).collect(),
    });

    let progress = SyncProgress::new(state.events.clone());
    let report = match state.store.sync(&accounts, &progress).await {
        Ok(report) => report,
        Err(err) => {
            state.events.publish(ServerEvent::Error {
                detail: err.to_string(),
            });
            return Err(err.into());
        }
    };

    let revision = state.store.revision().await?;
    state.events.publish(ServerEvent::SyncFinished {
        new_messages: report.new_messages,
        revision,
    });

    Ok(Json(report))
}

#[derive(Debug, Deserialize)]
pub struct SendRequest {
    pub account: String,
    #[serde(flatten)]
    pub draft: ecr_core::compose::Draft,
}

pub async fn send(
    State(state): State<AppState>,
    Json(request): Json<SendRequest>,
) -> ApiResult<Json<SendResponse>> {
    reject_if_read_only(&state)?;

    let accounts = state.store.accounts().await?;
    let account = accounts
        .iter()
        .find(|a| a.id.as_str() == request.account)
        .ok_or_else(|| ApiError::BadRequest(format!("no account named {}", request.account)))?;

    let raw = ecr_store::compose::build(account, &request.draft)
        .map_err(|e| ApiError::BadRequest(e.to_string()))?;

    state.store.send(&account.id, &raw).await?;

    Ok(Json(SendResponse {
        bytes: raw.len(),
        account: account.id.to_string(),
    }))
}

#[derive(Serialize)]
pub struct SendResponse {
    pub bytes: usize,
    pub account: String,
}

pub async fn events(
    State(state): State<AppState>,
) -> Sse<impl Stream<Item = Result<Event, std::convert::Infallible>>> {
    use tokio_stream::wrappers::BroadcastStream;
    use tokio_stream::StreamExt;

    let stream = BroadcastStream::new(state.events.subscribe()).filter_map(|event| {
        let event = event.ok()?;
        Some(Ok(Event::default()
            .event(event.name())
            .json_data(&event)
            .unwrap_or_else(|_| {
                Event::default().data("serialization failed")
            })))
    });

    Sse::new(stream).keep_alive(KeepAlive::default())
}

fn reject_if_read_only(state: &AppState) -> ApiResult<()> {
    if state.read_only {
        return Err(ApiError::BadRequest(
            "the server is running in --read-only mode".to_string(),
        ));
    }
    Ok(())
}

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

    #[test]
    fn a_thread_query_clamps_an_absurd_limit() {
        let params = ThreadQuery {
            q: "tag:inbox".to_string(),
            limit: Some(100_000),
            offset: None,
        };
        assert_eq!(params.to_query().limit, 500);
    }

    #[test]
    fn a_zero_limit_becomes_one_rather_than_returning_nothing() {
        let params = ThreadQuery {
            q: String::new(),
            limit: Some(0),
            offset: None,
        };
        assert_eq!(params.to_query().limit, 1);
    }

    #[test]
    fn an_absent_limit_uses_the_default() {
        let params = ThreadQuery {
            q: String::new(),
            limit: None,
            offset: None,
        };
        assert_eq!(params.to_query().limit, Query::DEFAULT_LIMIT);
    }

    #[test]
    fn a_filename_cannot_break_out_of_the_content_disposition_header() {
        assert_eq!(
            sanitize_filename("evil\";\r\nX-Injected: yes\".pdf"),
            "evil;X-Injected: yes.pdf"
        );
    }

    #[test]
    fn an_ordinary_filename_survives() {
        assert_eq!(sanitize_filename("report 2026.pdf"), "report 2026.pdf");
    }
}

#[derive(Serialize)]
pub struct ConfigFile {
    pub path: String,
    pub raw: String,
}

/// The settings file every client shares. Absent is not an error — a client
/// that finds it empty writes the commented default into it.
pub async fn config(State(state): State<AppState>) -> ApiResult<Json<ConfigFile>> {
    let path = state.store.paths().settings_file();
    let raw = match std::fs::read_to_string(&path) {
        Ok(text) => text,
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => String::new(),
        Err(err) => return Err(ApiError::Internal(err.to_string())),
    };

    Ok(Json(ConfigFile {
        path: path.display().to_string(),
        raw,
    }))
}

#[derive(Deserialize)]
pub struct ConfigUpdate {
    pub raw: String,
}

#[derive(Serialize)]
pub struct ConfigRejected {
    pub error: &'static str,
    pub detail: String,
    pub line: usize,
    pub column: usize,
}

/// Written only if it parses, so a client that sends nonsense cannot leave the
/// user with a settings file that no client can read back.
pub async fn save_config(
    State(state): State<AppState>,
    Json(update): Json<ConfigUpdate>,
) -> Response {
    if let Err(err) = update.raw.parse::<toml::Table>() {
        let (line, column) = err
            .span()
            .map(|span| position(&update.raw, span.start))
            .unwrap_or((1, 1));

        return (
            StatusCode::UNPROCESSABLE_ENTITY,
            Json(ConfigRejected {
                error: "invalid_toml",
                detail: err.message().to_string(),
                line,
                column,
            }),
        )
            .into_response();
    }

    let path = state.store.paths().settings_file();
    if let Some(parent) = path.parent() {
        if let Err(err) = std::fs::create_dir_all(parent) {
            return ApiError::Internal(err.to_string()).into_response();
        }
    }

    match write_atomically(&path, &update.raw) {
        Ok(()) => (
            StatusCode::OK,
            Json(serde_json::json!({ "path": path.display().to_string() })),
        )
            .into_response(),
        Err(err) => ApiError::Internal(err.to_string()).into_response(),
    }
}

#[derive(Serialize)]
pub struct ThemeListing {
    pub dir: String,
    pub presets: Vec<ThemeEntry>,
}

#[derive(Serialize)]
pub struct ThemeEntry {
    /// The value that goes in settings.toml, relative to the config dir.
    pub path: String,
    /// The display name from the file, falling back to the stem.
    pub name: String,
    pub builtin: bool,
}

/// Every theme on disk, seeded with the shipped presets on first call so a fresh
/// install has something to pick from.
pub async fn themes(State(state): State<AppState>) -> ApiResult<Json<ThemeListing>> {
    let dir = state.store.paths().themes_dir();
    ecr_store::themes::seed(&dir).map_err(|e| ApiError::Internal(e.to_string()))?;

    let builtin: std::collections::HashSet<&str> =
        ecr_store::themes::PRESETS.iter().map(|(n, _)| *n).collect();

    let mut presets = Vec::new();
    let entries = std::fs::read_dir(&dir).map_err(|e| ApiError::Internal(e.to_string()))?;

    for entry in entries.flatten() {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("toml") {
            continue;
        }
        let Some(stem) = path.file_stem().and_then(|s| s.to_str()) else {
            continue;
        };

        let name = std::fs::read_to_string(&path)
            .ok()
            .and_then(|raw| raw.parse::<toml::Table>().ok())
            .and_then(|doc| doc.get("name")?.as_str().map(str::to_string))
            .unwrap_or_else(|| stem.to_string());

        presets.push(ThemeEntry {
            path: format!("themes/{stem}.toml"),
            name,
            builtin: builtin.contains(stem),
        });
    }

    presets.sort_by(|a, b| a.path.cmp(&b.path));

    Ok(Json(ThemeListing {
        dir: dir.display().to_string(),
        presets,
    }))
}

#[derive(Deserialize)]
pub struct ThemeQuery {
    pub path: String,
}

/// Unlike the settings file, a theme that is not there is a real error: the file
/// was named by settings.toml, so its absence is a broken link the user should
/// see rather than an empty palette that silently does nothing.
pub async fn theme(
    State(state): State<AppState>,
    AxumQuery(query): AxumQuery<ThemeQuery>,
) -> ApiResult<Json<ConfigFile>> {
    let path = state
        .store
        .paths()
        .resolve_relative(&query.path)
        .map_err(|e| ApiError::BadRequest(e.to_string()))?;

    let raw = std::fs::read_to_string(&path).map_err(|err| match err.kind() {
        std::io::ErrorKind::NotFound => ApiError::NotFound(format!("no theme at {}", query.path)),
        _ => ApiError::Internal(err.to_string()),
    })?;

    Ok(Json(ConfigFile {
        path: path.display().to_string(),
        raw,
    }))
}

#[derive(Deserialize)]
pub struct ThemeUpdate {
    pub path: String,
    pub raw: String,
}

pub async fn save_theme(
    State(state): State<AppState>,
    Json(update): Json<ThemeUpdate>,
) -> Response {
    let path = match state.store.paths().resolve_relative(&update.path) {
        Ok(path) => path,
        Err(err) => return ApiError::BadRequest(err.to_string()).into_response(),
    };

    if let Err(err) = update.raw.parse::<toml::Table>() {
        let (line, column) = err
            .span()
            .map(|span| position(&update.raw, span.start))
            .unwrap_or((1, 1));

        return (
            StatusCode::UNPROCESSABLE_ENTITY,
            Json(ConfigRejected {
                error: "invalid_toml",
                detail: err.message().to_string(),
                line,
                column,
            }),
        )
            .into_response();
    }

    if let Some(parent) = path.parent() {
        if let Err(err) = std::fs::create_dir_all(parent) {
            return ApiError::Internal(err.to_string()).into_response();
        }
    }

    match write_atomically(&path, &update.raw) {
        Ok(()) => (
            StatusCode::OK,
            Json(serde_json::json!({ "path": path.display().to_string() })),
        )
            .into_response(),
        Err(err) => ApiError::Internal(err.to_string()).into_response(),
    }
}

/// A settings file half-written by a crash is worse than one not written.
fn write_atomically(path: &std::path::Path, contents: &str) -> std::io::Result<()> {
    let temp = path.with_extension("toml.new");
    std::fs::write(&temp, contents)?;
    std::fs::rename(&temp, path)
}

fn position(text: &str, offset: usize) -> (usize, usize) {
    let head = &text[..offset.min(text.len())];
    let line = head.matches('\n').count() + 1;
    let column = head.rsplit('\n').next().map(str::len).unwrap_or(0) + 1;
    (line, column)
}