summaryrefslogtreecommitdiff
path: root/crates/smtp/src/scripts/functions/mod.rs
blob: c09a923995d68a951b043be7c9676a65e679c241 (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
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
/*
 * 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.
*/

mod array;
mod email;
mod header;
pub mod html;
mod image;
mod misc;
mod text;
mod unicode;
mod url;

use sieve::{runtime::Variable, FunctionMap};

use crate::config::scripts::SieveContext;

use self::{
    array::*, email::*, header::*, html::*, image::*, misc::*, text::*, unicode::*, url::*,
};

pub fn register_functions() -> FunctionMap<SieveContext> {
    FunctionMap::new()
        .with_function("trim", fn_trim)
        .with_function("trim_start", fn_trim_start)
        .with_function("trim_end", fn_trim_end)
        .with_function("len", fn_len)
        .with_function("count", fn_count)
        .with_function("is_empty", fn_is_empty)
        .with_function("is_ascii", fn_is_ascii)
        .with_function("to_lowercase", fn_to_lowercase)
        .with_function("to_uppercase", fn_to_uppercase)
        .with_function("detect_language", fn_detect_language)
        .with_function("is_email", fn_is_email)
        .with_function("thread_name", fn_thread_name)
        .with_function("html_to_text", fn_html_to_text)
        .with_function("is_uppercase", fn_is_uppercase)
        .with_function("is_lowercase", fn_is_lowercase)
        .with_function("has_digits", fn_has_digits)
        .with_function("count_spaces", fn_count_spaces)
        .with_function("count_uppercase", fn_count_uppercase)
        .with_function("count_lowercase", fn_count_lowercase)
        .with_function("count_chars", fn_count_chars)
        .with_function("dedup", fn_dedup)
        .with_function("lines", fn_lines)
        .with_function("is_header_utf8_valid", fn_is_header_utf8_valid)
        .with_function("img_metadata", fn_img_metadata)
        .with_function("is_ip_addr", fn_is_ip_addr)
        .with_function("is_ipv4_addr", fn_is_ipv4_addr)
        .with_function("is_ipv6_addr", fn_is_ipv6_addr)
        .with_function("ip_reverse_name", fn_ip_reverse_name)
        .with_function("winnow", fn_winnow)
        .with_function("has_zwsp", fn_has_zwsp)
        .with_function("has_obscured", fn_has_obscured)
        .with_function("is_single_script", fn_is_single_script)
        .with_function("puny_decode", fn_puny_decode)
        .with_function("unicode_skeleton", fn_unicode_skeleton)
        .with_function("cure_text", fn_cure_text)
        .with_function("detect_file_type", fn_detect_file_type)
        .with_function_args("sort", fn_sort, 2)
        .with_function_args("tokenize", fn_tokenize, 2)
        .with_function_args("email_part", fn_email_part, 2)
        .with_function_args("domain_part", fn_domain_part, 2)
        .with_function_args("eq_ignore_case", fn_eq_ignore_case, 2)
        .with_function_args("contains", fn_contains, 2)
        .with_function_args("contains_ignore_case", fn_contains_ignore_case, 2)
        .with_function_args("starts_with", fn_starts_with, 2)
        .with_function_args("ends_with", fn_ends_with, 2)
        .with_function_args("received_part", fn_received_part, 2)
        .with_function_args("cosine_similarity", fn_cosine_similarity, 2)
        .with_function_args("jaccard_similarity", fn_jaccard_similarity, 2)
        .with_function_args("levenshtein_distance", fn_levenshtein_distance, 2)
        .with_function_args("html_has_tag", fn_html_has_tag, 2)
        .with_function_args("html_attr", fn_html_attr, 2)
        .with_function_args("html_attrs", fn_html_attrs, 3)
        .with_function_args("html_attr_size", fn_html_attr_size, 3)
        .with_function_args("uri_part", fn_uri_part, 2)
        .with_function_args("substring", fn_substring, 3)
        .with_function_args("split", fn_split, 2)
        .with_function_args("rsplit", fn_rsplit, 2)
        .with_function_args("split_once", fn_split_once, 2)
        .with_function_args("rsplit_once", fn_rsplit_once, 2)
        .with_function_args("strip_prefix", fn_strip_prefix, 2)
        .with_function_args("strip_suffix", fn_strip_suffix, 2)
        .with_function_args("is_intersect", fn_is_intersect, 2)
        .with_function_args("hash", fn_hash, 2)
        .with_function_no_args("is_encoding_problem", fn_is_encoding_problem)
        .with_function_no_args("is_attachment", fn_is_attachment)
        .with_function_no_args("is_body", fn_is_body)
        .with_function_no_args("var_names", fn_is_var_names)
        .with_function_no_args("attachment_name", fn_attachment_name)
}

pub trait ApplyString<'x> {
    fn transform(&self, f: impl Fn(&'_ str) -> Variable) -> Variable;
}

impl<'x> ApplyString<'x> for Variable {
    fn transform(&self, f: impl Fn(&'_ str) -> Variable) -> Variable {
        match self {
            Variable::String(s) => f(s),
            Variable::Array(list) => list
                .iter()
                .map(|v| match v {
                    Variable::String(s) => f(s),
                    v => f(v.to_string().as_ref()),
                })
                .collect::<Vec<_>>()
                .into(),
            v => f(v.to_string().as_ref()),
        }
    }
}