rusmes-core 0.1.2

Mailet processing engine for RusMES — composable mail processing pipeline with matchers, mailets, DKIM/SPF/DMARC, spam filtering, and AI integration
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
//! Greylisting mailet - temporarily reject first delivery attempts

use crate::mailet::{Mailet, MailetAction, MailetConfig};
use async_trait::async_trait;
use rusmes_proto::Mail;
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

/// Greylisting tuple (sender IP, mail from, rcpt to)
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct GreylistTuple {
    sender_ip: String,
    mail_from: String,
    rcpt_to: String,
}

impl GreylistTuple {
    fn new(sender_ip: String, mail_from: String, rcpt_to: String) -> Self {
        Self {
            sender_ip,
            mail_from,
            rcpt_to,
        }
    }
}

/// Greylist entry
#[derive(Debug, Clone)]
struct GreylistEntry {
    /// First attempt timestamp
    first_seen: u64,
    /// Last attempt timestamp
    last_seen: u64,
    /// Number of attempts
    attempts: u32,
    /// Whether passed greylisting
    passed: bool,
}

impl GreylistEntry {
    fn new() -> Self {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        Self {
            first_seen: now,
            last_seen: now,
            attempts: 1,
            passed: false,
        }
    }

    fn update(&mut self) {
        self.last_seen = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        self.attempts += 1;
    }
}

/// Greylisting storage
#[derive(Clone)]
struct GreylistStore {
    entries: Arc<Mutex<HashMap<GreylistTuple, GreylistEntry>>>,
}

impl GreylistStore {
    fn new() -> Self {
        Self {
            entries: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    fn check_and_update(
        &self,
        tuple: GreylistTuple,
        greylist_period: u64,
    ) -> (bool, GreylistEntry) {
        let mut entries = match self.entries.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };

        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let is_new = !entries.contains_key(&tuple);
        let entry = entries.entry(tuple).or_insert_with(GreylistEntry::new);

        // Only update if not a new entry
        if !is_new {
            entry.update();
        } else {
            // For new entries, just update last_seen to current time
            entry.last_seen = now;
        }

        // Check if greylist period has passed
        let time_since_first = now.saturating_sub(entry.first_seen);
        let should_pass = time_since_first >= greylist_period;

        if should_pass {
            entry.passed = true;
        }

        (entry.passed, entry.clone())
    }

    fn is_whitelisted(&self, tuple: &GreylistTuple, whitelist: &HashSet<String>) -> bool {
        // Check if IP is whitelisted
        if whitelist.contains(&tuple.sender_ip) {
            return true;
        }

        // Check if sender is whitelisted
        if whitelist.contains(&tuple.mail_from) {
            return true;
        }

        // Check if domain is whitelisted
        if let Some(domain) = tuple.mail_from.split('@').nth(1) {
            if whitelist.contains(domain) {
                return true;
            }
        }

        false
    }

    fn cleanup_old_entries(&self, max_age: u64) {
        let mut entries = match self.entries.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        entries.retain(|_, entry| now.saturating_sub(entry.last_seen) < max_age);
    }
}

/// Greylisting mailet
pub struct GreylistMailet {
    name: String,
    /// Greylist storage
    store: GreylistStore,
    /// Greylist period in seconds (default: 5 minutes)
    greylist_period: u64,
    /// Maximum age for entries (default: 24 hours)
    max_entry_age: u64,
    /// Whitelist (IPs, addresses, domains)
    whitelist: HashSet<String>,
    /// Whether greylisting is enabled
    enabled: bool,
}

impl GreylistMailet {
    /// Create a new greylist mailet
    pub fn new() -> Self {
        Self {
            name: "Greylist".to_string(),
            store: GreylistStore::new(),
            greylist_period: 300, // 5 minutes
            max_entry_age: 86400, // 24 hours
            whitelist: HashSet::new(),
            enabled: true,
        }
    }

    /// Add to whitelist
    pub fn add_to_whitelist(&mut self, entry: String) {
        self.whitelist.insert(entry);
    }

    /// Extract sender IP from mail
    fn extract_sender_ip(&self, mail: &Mail) -> Option<String> {
        mail.get_attribute("smtp.client_ip")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string())
    }

    /// Create greylist tuple from mail
    fn create_tuple(&self, mail: &Mail) -> Option<GreylistTuple> {
        let sender_ip = self.extract_sender_ip(mail)?;

        let mail_from = mail.sender()?.to_string();

        // For simplicity, use first recipient
        let rcpt_to = mail.recipients().first()?.to_string();

        Some(GreylistTuple::new(sender_ip, mail_from, rcpt_to))
    }
}

impl Default for GreylistMailet {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Mailet for GreylistMailet {
    async fn init(&mut self, config: MailetConfig) -> anyhow::Result<()> {
        if let Some(period) = config.get_param("greylist_period") {
            self.greylist_period = period.parse().unwrap_or(300);
        }

        if let Some(max_age) = config.get_param("max_entry_age") {
            self.max_entry_age = max_age.parse().unwrap_or(86400);
        }

        if let Some(enabled) = config.get_param("enabled") {
            self.enabled = enabled.parse().unwrap_or(true);
        }

        if let Some(whitelist_str) = config.get_param("whitelist") {
            for entry in whitelist_str.split(',') {
                self.whitelist.insert(entry.trim().to_string());
            }
        }

        tracing::info!(
            "Initialized GreylistMailet (period: {}s, max_age: {}s, whitelist: {})",
            self.greylist_period,
            self.max_entry_age,
            self.whitelist.len()
        );
        Ok(())
    }

    async fn service(&self, mail: &mut Mail) -> anyhow::Result<MailetAction> {
        if !self.enabled {
            return Ok(MailetAction::Continue);
        }

        let tuple = match self.create_tuple(mail) {
            Some(t) => t,
            None => {
                tracing::debug!("Cannot create greylist tuple for mail {}", mail.id());
                return Ok(MailetAction::Continue);
            }
        };

        // Check whitelist
        if self.store.is_whitelisted(&tuple, &self.whitelist) {
            tracing::debug!("Mail {} is whitelisted, skipping greylist", mail.id());
            mail.set_attribute("greylist.whitelisted", true);
            return Ok(MailetAction::Continue);
        }

        // Check and update greylist
        let (passed, entry) = self.store.check_and_update(tuple, self.greylist_period);

        mail.set_attribute("greylist.attempts", entry.attempts as i64);
        mail.set_attribute("greylist.first_seen", entry.first_seen as i64);

        if passed {
            tracing::debug!("Mail {} passed greylist check", mail.id());
            mail.set_attribute("greylist.passed", true);
            Ok(MailetAction::Continue)
        } else {
            tracing::info!(
                "Mail {} greylisted (attempt {}), deferring",
                mail.id(),
                entry.attempts
            );
            mail.set_attribute("greylist.passed", false);
            mail.set_attribute("greylist.deferred", true);

            // Defer for greylist period
            Ok(MailetAction::Defer(Duration::from_secs(
                self.greylist_period,
            )))
        }
    }

    async fn destroy(&mut self) -> anyhow::Result<()> {
        // Cleanup old entries
        self.store.cleanup_old_entries(self.max_entry_age);
        Ok(())
    }

    fn name(&self) -> &str {
        &self.name
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bytes::Bytes;
    use rusmes_proto::{HeaderMap, MailAddress, MessageBody, MimeMessage};
    use std::str::FromStr;

    #[tokio::test]
    async fn test_greylist_mailet_init() {
        let mut mailet = GreylistMailet::new();
        let config = MailetConfig::new("Greylist");

        mailet.init(config).await.unwrap();
        assert_eq!(mailet.name(), "Greylist");
        assert_eq!(mailet.greylist_period, 300);
    }

    #[tokio::test]
    async fn test_greylist_first_attempt_deferred() {
        let mailet = GreylistMailet::new();
        let mut mail = Mail::new(
            Some(MailAddress::from_str("sender@test.com").unwrap()),
            vec![MailAddress::from_str("rcpt@example.com").unwrap()],
            MimeMessage::new(HeaderMap::new(), MessageBody::Small(Bytes::from("Test"))),
            None,
            None,
        );
        mail.set_attribute("smtp.client_ip", "192.168.1.1");

        let action = mailet.service(&mut mail).await.unwrap();

        // First attempt should be deferred
        assert!(matches!(action, MailetAction::Defer(_)));
        assert_eq!(
            mail.get_attribute("greylist.passed")
                .and_then(|v| v.as_bool()),
            Some(false)
        );
        assert_eq!(
            mail.get_attribute("greylist.attempts")
                .and_then(|v| v.as_i64()),
            Some(1)
        );
    }

    #[tokio::test]
    async fn test_greylist_whitelist() {
        let mut mailet = GreylistMailet::new();
        mailet.add_to_whitelist("192.168.1.100".to_string());

        let mut mail = Mail::new(
            Some(MailAddress::from_str("sender@test.com").unwrap()),
            vec![MailAddress::from_str("rcpt@example.com").unwrap()],
            MimeMessage::new(HeaderMap::new(), MessageBody::Small(Bytes::from("Test"))),
            None,
            None,
        );
        mail.set_attribute("smtp.client_ip", "192.168.1.100");

        let action = mailet.service(&mut mail).await.unwrap();

        // Whitelisted, should continue
        assert_eq!(action, MailetAction::Continue);
        assert_eq!(
            mail.get_attribute("greylist.whitelisted")
                .and_then(|v| v.as_bool()),
            Some(true)
        );
    }

    #[tokio::test]
    async fn test_greylist_domain_whitelist() {
        let mut mailet = GreylistMailet::new();
        mailet.add_to_whitelist("trusted.com".to_string());

        let mut mail = Mail::new(
            Some(MailAddress::from_str("sender@trusted.com").unwrap()),
            vec![MailAddress::from_str("rcpt@example.com").unwrap()],
            MimeMessage::new(HeaderMap::new(), MessageBody::Small(Bytes::from("Test"))),
            None,
            None,
        );
        mail.set_attribute("smtp.client_ip", "192.168.1.1");

        let action = mailet.service(&mut mail).await.unwrap();

        assert_eq!(action, MailetAction::Continue);
        assert_eq!(
            mail.get_attribute("greylist.whitelisted")
                .and_then(|v| v.as_bool()),
            Some(true)
        );
    }

    #[tokio::test]
    async fn test_greylist_config_period() {
        let mut mailet = GreylistMailet::new();
        let config = MailetConfig::new("Greylist").with_param("greylist_period", "600");

        mailet.init(config).await.unwrap();
        assert_eq!(mailet.greylist_period, 600);
    }

    #[tokio::test]
    async fn test_greylist_config_max_age() {
        let mut mailet = GreylistMailet::new();
        let config = MailetConfig::new("Greylist").with_param("max_entry_age", "172800");

        mailet.init(config).await.unwrap();
        assert_eq!(mailet.max_entry_age, 172800);
    }

    #[tokio::test]
    async fn test_greylist_config_whitelist() {
        let mut mailet = GreylistMailet::new();
        let config = MailetConfig::new("Greylist")
            .with_param("whitelist", "192.168.1.1,192.168.1.2,trusted.com");

        mailet.init(config).await.unwrap();
        assert_eq!(mailet.whitelist.len(), 3);
        assert!(mailet.whitelist.contains("192.168.1.1"));
        assert!(mailet.whitelist.contains("trusted.com"));
    }

    #[tokio::test]
    async fn test_greylist_disabled() {
        let mut mailet = GreylistMailet::new();
        let config = MailetConfig::new("Greylist").with_param("enabled", "false");

        mailet.init(config).await.unwrap();

        let mut mail = Mail::new(
            Some(MailAddress::from_str("sender@test.com").unwrap()),
            vec![MailAddress::from_str("rcpt@example.com").unwrap()],
            MimeMessage::new(HeaderMap::new(), MessageBody::Small(Bytes::from("Test"))),
            None,
            None,
        );
        mail.set_attribute("smtp.client_ip", "192.168.1.1");

        let action = mailet.service(&mut mail).await.unwrap();
        assert_eq!(action, MailetAction::Continue);
    }

    #[tokio::test]
    async fn test_greylist_tuple_creation() {
        let mailet = GreylistMailet::new();
        let mut mail = Mail::new(
            Some(MailAddress::from_str("sender@test.com").unwrap()),
            vec![MailAddress::from_str("rcpt@example.com").unwrap()],
            MimeMessage::new(HeaderMap::new(), MessageBody::Small(Bytes::from("Test"))),
            None,
            None,
        );
        mail.set_attribute("smtp.client_ip", "192.168.1.1");

        let tuple = mailet.create_tuple(&mail).unwrap();
        assert_eq!(tuple.sender_ip, "192.168.1.1");
        assert_eq!(tuple.mail_from, "sender@test.com");
        assert_eq!(tuple.rcpt_to, "rcpt@example.com");
    }

    #[tokio::test]
    async fn test_greylist_multiple_attempts() {
        let mailet = GreylistMailet::new();
        let mut mail = Mail::new(
            Some(MailAddress::from_str("sender@test.com").unwrap()),
            vec![MailAddress::from_str("rcpt@example.com").unwrap()],
            MimeMessage::new(HeaderMap::new(), MessageBody::Small(Bytes::from("Test"))),
            None,
            None,
        );
        mail.set_attribute("smtp.client_ip", "192.168.1.1");

        // First attempt
        mailet.service(&mut mail).await.unwrap();
        let attempts1 = mail
            .get_attribute("greylist.attempts")
            .and_then(|v| v.as_i64());

        // Second attempt
        mailet.service(&mut mail).await.unwrap();
        let attempts2 = mail
            .get_attribute("greylist.attempts")
            .and_then(|v| v.as_i64());

        assert_eq!(attempts1, Some(1));
        assert_eq!(attempts2, Some(2));
    }

    #[test]
    fn test_greylist_entry_creation() {
        let entry = GreylistEntry::new();
        assert_eq!(entry.attempts, 1);
        assert!(!entry.passed);
    }

    #[test]
    fn test_greylist_entry_update() {
        let mut entry = GreylistEntry::new();
        let first_seen = entry.first_seen;

        std::thread::sleep(std::time::Duration::from_millis(10));
        entry.update();

        assert_eq!(entry.attempts, 2);
        assert_eq!(entry.first_seen, first_seen); // Should not change
        assert!(entry.last_seen >= first_seen);
    }

    #[test]
    fn test_greylist_store_check() {
        let store = GreylistStore::new();
        let tuple = GreylistTuple::new(
            "192.168.1.1".to_string(),
            "sender@test.com".to_string(),
            "rcpt@example.com".to_string(),
        );

        let (passed1, entry1) = store.check_and_update(tuple.clone(), 5);
        assert!(!passed1);
        assert_eq!(entry1.attempts, 1);

        let (passed2, entry2) = store.check_and_update(tuple.clone(), 5);
        assert!(!passed2);
        assert_eq!(entry2.attempts, 2);
    }

    #[test]
    fn test_greylist_store_whitelist_ip() {
        let store = GreylistStore::new();
        let tuple = GreylistTuple::new(
            "192.168.1.1".to_string(),
            "sender@test.com".to_string(),
            "rcpt@example.com".to_string(),
        );

        let mut whitelist = HashSet::new();
        whitelist.insert("192.168.1.1".to_string());

        assert!(store.is_whitelisted(&tuple, &whitelist));
    }

    #[test]
    fn test_greylist_store_whitelist_sender() {
        let store = GreylistStore::new();
        let tuple = GreylistTuple::new(
            "192.168.1.1".to_string(),
            "sender@test.com".to_string(),
            "rcpt@example.com".to_string(),
        );

        let mut whitelist = HashSet::new();
        whitelist.insert("sender@test.com".to_string());

        assert!(store.is_whitelisted(&tuple, &whitelist));
    }

    #[test]
    fn test_greylist_store_whitelist_domain() {
        let store = GreylistStore::new();
        let tuple = GreylistTuple::new(
            "192.168.1.1".to_string(),
            "sender@test.com".to_string(),
            "rcpt@example.com".to_string(),
        );

        let mut whitelist = HashSet::new();
        whitelist.insert("test.com".to_string());

        assert!(store.is_whitelisted(&tuple, &whitelist));
    }

    #[test]
    fn test_greylist_store_cleanup() {
        let store = GreylistStore::new();
        let tuple = GreylistTuple::new(
            "192.168.1.1".to_string(),
            "sender@test.com".to_string(),
            "rcpt@example.com".to_string(),
        );

        store.check_and_update(tuple.clone(), 0);

        // Entries count before cleanup
        let count_before = store
            .entries
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .len();
        assert_eq!(count_before, 1);

        // Cleanup with 0 max age (should remove all)
        store.cleanup_old_entries(0);

        let count_after = store
            .entries
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .len();
        assert_eq!(count_after, 0);
    }

    #[tokio::test]
    async fn test_greylist_no_sender_ip() {
        let mailet = GreylistMailet::new();
        let mut mail = Mail::new(
            Some(MailAddress::from_str("sender@test.com").unwrap()),
            vec![MailAddress::from_str("rcpt@example.com").unwrap()],
            MimeMessage::new(HeaderMap::new(), MessageBody::Small(Bytes::from("Test"))),
            None,
            None,
        );
        // No sender IP set

        let action = mailet.service(&mut mail).await.unwrap();
        // Should continue without greylisting
        assert_eq!(action, MailetAction::Continue);
    }

    #[tokio::test]
    async fn test_greylist_destroy_cleanup() {
        let mut mailet = GreylistMailet::new();
        let mut mail = Mail::new(
            Some(MailAddress::from_str("sender@test.com").unwrap()),
            vec![MailAddress::from_str("rcpt@example.com").unwrap()],
            MimeMessage::new(HeaderMap::new(), MessageBody::Small(Bytes::from("Test"))),
            None,
            None,
        );
        mail.set_attribute("smtp.client_ip", "192.168.1.1");

        mailet.service(&mut mail).await.unwrap();

        // Destroy should cleanup
        mailet.destroy().await.unwrap();
    }
}