lariv-rs 0.1.0

Compile-time plugin web application framework built on Axum, SeaORM, Maud, and HTMX
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
//! RFC822 MIME parsing for inbound email automation.

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::io::Cursor;

use mailparse::{MailAddr, MailHeaderMap, ParsedMail, addrparse, parse_mail};

use super::config::{
    EMAIL_MAX_ATTACHMENT_BYTES, EMAIL_MAX_ATTACHMENTS, EMAIL_MAX_TOTAL_ATTACHMENT_BYTES,
};

const LOG_TARGET: &str = "llm_assistant::imap";

/// One attachment extracted during MIME parse (held in memory until filter passes).
#[derive(Debug, Clone)]
pub struct ParsedAttachment {
    pub filename: String,
    pub mime_type: String,
    pub bytes: Vec<u8>,
}

/// Structured inbound email after MIME parse.
#[derive(Debug, Clone)]
pub struct ParsedEmail {
    pub message_id: Option<String>,
    pub reply_to: String,
    pub references: Option<String>,
    pub subject: String,
    pub body_text: String,
    pub attachments: Vec<ParsedAttachment>,
    pub from_display: String,
    pub date: Option<String>,
}

impl ParsedEmail {
    /// Dedup key: `Message-ID` when present, else synthetic hash.
    pub fn dedup_key(&self, uid: u32, fallback_from: &str, fallback_subject: &str) -> String {
        if let Some(id) = self
            .message_id
            .as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty())
        {
            return normalize_message_id(id);
        }
        let mut h = DefaultHasher::new();
        uid.hash(&mut h);
        self.date.hash(&mut h);
        fallback_from.hash(&mut h);
        fallback_subject.hash(&mut h);
        self.from_display.hash(&mut h);
        format!("synthetic:{:x}", h.finish())
    }
}

fn normalize_message_id(raw: &str) -> String {
    let trimmed = raw.trim();
    if trimmed.starts_with('<') && trimmed.ends_with('>') {
        trimmed.to_string()
    } else {
        format!("<{trimmed}>")
    }
}

/// Parse raw RFC822 bytes into structured fields and in-memory attachments.
pub fn parse_rfc822(
    raw: &[u8],
    envelope_from: &str,
    envelope_subject: &str,
) -> anyhow::Result<ParsedEmail> {
    let mail = parse_mail(raw).map_err(|e| anyhow::anyhow!("MIME parse: {e}"))?;

    let message_id = header_value(&mail, "Message-ID").map(|id| normalize_message_id(&id));
    let references = header_value(&mail, "References");
    let subject = header_value(&mail, "Subject").unwrap_or_else(|| envelope_subject.to_string());
    let date = header_value(&mail, "Date");

    let from_display = header_value(&mail, "From").unwrap_or_else(|| envelope_from.to_string());
    let reply_to = parse_reply_address(&mail).unwrap_or_else(|| from_display.clone());

    let mut plain_body: Option<String> = None;
    let mut html_body: Option<String> = None;
    let mut attachments = Vec::new();
    let mut total_attachment_bytes = 0usize;

    walk_parts(
        &mail,
        &mut plain_body,
        &mut html_body,
        &mut attachments,
        &mut total_attachment_bytes,
    );

    let body_text = match plain_body {
        Some(text) if !text.trim().is_empty() => text,
        _ => html_body
            .map(|html| html_to_plain(&html))
            .unwrap_or_default(),
    };

    Ok(ParsedEmail {
        message_id,
        reply_to,
        references,
        subject,
        body_text,
        attachments,
        from_display,
        date,
    })
}

fn parse_reply_address(mail: &ParsedMail) -> Option<String> {
    let raw = header_value(mail, "Reply-To").or_else(|| header_value(mail, "From"))?;
    parse_single_address(&raw)
}

fn parse_single_address(raw: &str) -> Option<String> {
    let addrs = addrparse(raw).ok()?;
    for entry in addrs.iter() {
        match entry {
            MailAddr::Single(s) if !s.addr.trim().is_empty() => {
                let addr = s.addr.trim();
                return Some(
                    match s
                        .display_name
                        .as_deref()
                        .map(str::trim)
                        .filter(|n| !n.is_empty())
                    {
                        Some(name) => format!("{name} <{addr}>"),
                        None => addr.to_string(),
                    },
                );
            }
            MailAddr::Single(_) => {}
            MailAddr::Group(g) => {
                if let Some(s) = g.addrs.first() {
                    let addr = s.addr.trim();
                    if !addr.is_empty() {
                        return Some(
                            match s
                                .display_name
                                .as_deref()
                                .map(str::trim)
                                .filter(|n| !n.is_empty())
                            {
                                Some(name) => format!("{name} <{addr}>"),
                                None => addr.to_string(),
                            },
                        );
                    }
                }
            }
        }
    }
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        None
    } else {
        Some(trimmed.to_string())
    }
}

fn header_value(mail: &ParsedMail, name: &str) -> Option<String> {
    mail.headers
        .get_first_header(name)
        .map(|h| h.get_value().trim().to_string())
        .filter(|v| !v.is_empty())
}

fn walk_parts(
    mail: &ParsedMail,
    plain_body: &mut Option<String>,
    html_body: &mut Option<String>,
    attachments: &mut Vec<ParsedAttachment>,
    total_attachment_bytes: &mut usize,
) {
    if !mail.subparts.is_empty() {
        for part in &mail.subparts {
            walk_parts(
                part,
                plain_body,
                html_body,
                attachments,
                total_attachment_bytes,
            );
        }
        return;
    }

    let ctype = mail.ctype.mimetype.to_ascii_lowercase();
    if is_attachment_part(mail, &ctype) {
        try_push_attachment(mail, &ctype, attachments, total_attachment_bytes);
        return;
    }

    if ctype == "text/plain" && plain_body.is_none() {
        if let Ok(text) = mail.get_body() {
            if !text.trim().is_empty() {
                *plain_body = Some(text);
            }
        }
    } else if ctype == "text/html" && html_body.is_none() {
        if let Ok(html) = mail.get_body() {
            if !html.trim().is_empty() {
                *html_body = Some(html);
            }
        }
    }
}

fn is_attachment_part(mail: &ParsedMail, ctype: &str) -> bool {
    let disposition = header_value(mail, "Content-Disposition")
        .map(|d| d.to_ascii_lowercase())
        .unwrap_or_default();

    if disposition.contains("attachment") {
        return true;
    }
    if disposition.contains("inline") {
        return false;
    }
    if ctype.starts_with("text/") {
        return false;
    }
    part_filename(mail).is_some()
}

fn try_push_attachment(
    mail: &ParsedMail,
    ctype: &str,
    attachments: &mut Vec<ParsedAttachment>,
    total_attachment_bytes: &mut usize,
) {
    if attachments.len() >= EMAIL_MAX_ATTACHMENTS {
        tracing::warn!(
            target: LOG_TARGET,
            "skipping attachment — max count ({EMAIL_MAX_ATTACHMENTS}) reached"
        );
        return;
    }

    let bytes = match mail.get_body_raw() {
        Ok(b) => b,
        Err(e) => {
            tracing::warn!(target: LOG_TARGET, "skipping attachment — decode failed: {e}");
            return;
        }
    };

    if bytes.len() > EMAIL_MAX_ATTACHMENT_BYTES {
        tracing::warn!(
            target: LOG_TARGET,
            size = bytes.len(),
            limit = EMAIL_MAX_ATTACHMENT_BYTES,
            "skipping oversized attachment"
        );
        return;
    }

    if *total_attachment_bytes + bytes.len() > EMAIL_MAX_TOTAL_ATTACHMENT_BYTES {
        tracing::warn!(
            target: LOG_TARGET,
            size = bytes.len(),
            limit = EMAIL_MAX_TOTAL_ATTACHMENT_BYTES,
            "skipping attachment — total size limit exceeded"
        );
        return;
    }

    let filename = part_filename(mail).unwrap_or_else(|| "attachment".to_string());
    *total_attachment_bytes += bytes.len();
    attachments.push(ParsedAttachment {
        filename,
        mime_type: ctype.to_string(),
        bytes,
    });
}

fn part_filename(mail: &ParsedMail) -> Option<String> {
    if let Some(cd) = header_value(mail, "Content-Disposition") {
        if let Some(name) = parse_filename_param(&cd) {
            return Some(name);
        }
    }
    if let Some(name) = mail.ctype.params.get("name") {
        let trimmed = name.trim().trim_matches('"');
        if !trimmed.is_empty() {
            return Some(trimmed.to_string());
        }
    }
    None
}

fn parse_filename_param(header: &str) -> Option<String> {
    for segment in header.split(';') {
        let segment = segment.trim();
        let lower = segment.to_ascii_lowercase();
        if lower.starts_with("filename*=") {
            let value = segment.split_once('=')?.1.trim().trim_matches('"');
            if let Some((_charset, encoded)) = value.split_once("''") {
                return Some(encoded.to_string());
            }
        } else if lower.starts_with("filename=") {
            let value = segment.split_once('=')?.1.trim().trim_matches('"');
            if !value.is_empty() {
                return Some(value.to_string());
            }
        }
    }
    None
}

fn html_to_plain(html: &str) -> String {
    html2text::from_read(Cursor::new(html.as_bytes()), 80).unwrap_or_else(|_| html.to_string())
}

/// Attachment metadata for the filter LLM (no binary).
pub fn attachment_metadata_lines(attachments: &[ParsedAttachment]) -> String {
    if attachments.is_empty() {
        return String::new();
    }
    attachments
        .iter()
        .map(|a| {
            format!(
                "- {} ({}, {} bytes)",
                a.filename,
                a.mime_type,
                a.bytes.len()
            )
        })
        .collect::<Vec<_>>()
        .join("\n")
}

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

    #[test]
    fn simple_plain_text() {
        let raw = b"From: alice@example.com\r\nSubject: Hello\r\nMessage-ID: <abc@example.com>\r\n\r\nHello world";
        let parsed = parse_rfc822(raw, "alice@example.com", "Hello").unwrap();
        assert_eq!(parsed.body_text.trim(), "Hello world");
        assert_eq!(parsed.message_id.as_deref(), Some("<abc@example.com>"));
        assert!(parsed.attachments.is_empty());
    }

    #[test]
    fn reply_to_preferred() {
        let raw = b"From: alice@example.com\r\nReply-To: bot-replies@example.com\r\n\r\nBody";
        let parsed = parse_rfc822(raw, "alice@example.com", "Hi").unwrap();
        assert!(parsed.reply_to.contains("bot-replies@example.com"));
    }

    #[test]
    fn dedup_key_uses_message_id() {
        let parsed = ParsedEmail {
            message_id: Some("<id@test>".into()),
            reply_to: String::new(),
            references: None,
            subject: String::new(),
            body_text: String::new(),
            attachments: vec![],
            from_display: String::new(),
            date: None,
        };
        assert_eq!(parsed.dedup_key(1, "a", "b"), "<id@test>");
    }

    #[test]
    fn dedup_key_synthetic_when_missing_message_id() {
        let parsed = ParsedEmail {
            message_id: None,
            reply_to: String::new(),
            references: None,
            subject: "s".into(),
            body_text: String::new(),
            attachments: vec![],
            from_display: "a@b".into(),
            date: Some("Mon".into()),
        };
        let k1 = parsed.dedup_key(5, "a@b", "s");
        let k2 = parsed.dedup_key(5, "a@b", "s");
        assert!(k1.starts_with("synthetic:"));
        assert_eq!(k1, k2);
    }

    #[test]
    fn html_only_body() {
        let raw = concat!(
            "From: a@b.com\r\n",
            "Subject: HTML\r\n",
            "Content-Type: text/html; charset=utf-8\r\n",
            "\r\n",
            "<html><body><p>Hello <b>world</b></p></body></html>"
        );
        let parsed = parse_rfc822(raw.as_bytes(), "a@b.com", "HTML").unwrap();
        assert!(parsed.body_text.contains("Hello"));
        assert!(parsed.body_text.contains("world"));
    }

    #[test]
    fn base64_attachment_extracted() {
        let raw = concat!(
            "From: a@b.com\r\n",
            "Subject: file\r\n",
            "Content-Type: multipart/mixed; boundary=abc\r\n",
            "\r\n",
            "--abc\r\n",
            "Content-Type: text/plain\r\n",
            "\r\n",
            "See attached\r\n",
            "--abc\r\n",
            "Content-Type: application/octet-stream\r\n",
            "Content-Disposition: attachment; filename=\"data.bin\"\r\n",
            "Content-Transfer-Encoding: base64\r\n",
            "\r\n",
            "AQID\r\n",
            "--abc--\r\n"
        );
        let parsed = parse_rfc822(raw.as_bytes(), "a@b.com", "file").unwrap();
        assert_eq!(parsed.attachments.len(), 1);
        assert_eq!(parsed.attachments[0].filename, "data.bin");
        assert_eq!(parsed.attachments[0].bytes, vec![1, 2, 3]);
    }

    #[test]
    fn attachment_count_limit_skips_excess() {
        let mut parts = String::from(
            "From: a@b.com\r\nSubject: many\r\nContent-Type: multipart/mixed; boundary=b\r\n\r\n",
        );
        for i in 0..12 {
            parts.push_str("--b\r\n");
            parts.push_str("Content-Type: application/octet-stream\r\n");
            parts.push_str(&format!(
                "Content-Disposition: attachment; filename=\"f{i}.bin\"\r\n\r\n"
            ));
            parts.push('x');
            parts.push_str("\r\n");
        }
        parts.push_str("--b--\r\n");
        let parsed = parse_rfc822(parts.as_bytes(), "a@b.com", "many").unwrap();
        assert_eq!(
            parsed.attachments.len(),
            crate::plugins::llm_assistant::config::EMAIL_MAX_ATTACHMENTS
        );
    }
}