summaryrefslogtreecommitdiff
path: root/crates/smtp/src/scripts/mod.rs
blob: c53b686d0680ffd8a082db0aa31c891bb6f3aace (plain)
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
/*
 * Copyright (c) 2023 Stalwart Labs Ltd.
 *
 * This file is part of Stalwart Mail Server.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 * in the LICENSE file at the top-level directory of this distribution.
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You can be released from the requirements of the AGPLv3 license by
 * purchasing a commercial license. Please contact licensing@stalw.art
 * for more details.
*/

use std::{borrow::Cow, sync::Arc};

use ahash::AHashMap;
use sieve::{runtime::Variable, Envelope};

pub mod envelope;
pub mod event_loop;
pub mod exec;
pub mod functions;
pub mod plugins;

#[derive(Debug)]
pub enum ScriptResult {
    Accept {
        modifications: Vec<(Envelope, String)>,
    },
    Replace {
        message: Vec<u8>,
        modifications: Vec<(Envelope, String)>,
    },
    Reject(String),
    Discard,
}

pub struct ScriptParameters {
    message: Option<Arc<Vec<u8>>>,
    variables: AHashMap<Cow<'static, str>, Variable>,
    envelope: Vec<(Envelope, Variable)>,
    #[cfg(feature = "test_mode")]
    expected_variables: Option<AHashMap<String, Variable>>,
}

impl ScriptParameters {
    pub fn new() -> Self {
        ScriptParameters {
            variables: AHashMap::with_capacity(10),
            envelope: Vec::with_capacity(6),
            message: None,
            #[cfg(feature = "test_mode")]
            expected_variables: None,
        }
    }

    pub fn with_message(self, message: Arc<Vec<u8>>) -> Self {
        Self {
            message: message.into(),
            ..self
        }
    }

    pub fn set_variable(
        mut self,
        name: impl Into<Cow<'static, str>>,
        value: impl Into<Variable>,
    ) -> Self {
        self.variables.insert(name.into(), value.into());
        self
    }

    #[cfg(feature = "test_mode")]
    pub fn with_expected_variables(
        mut self,
        expected_variables: AHashMap<String, Variable>,
    ) -> Self {
        self.expected_variables = expected_variables.into();
        self
    }
}

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