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
/*
* SPDX-FileCopyrightText: 2020 Stalwart Labs Ltd <hello@stalw.art>
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
*/
use sieve::{runtime::RuntimeError, Compiler, Event, Input, Runtime};
fn main() {
let text_script = br#"
require ["fileinto", "body", "imap4flags"];
if body :contains "tps" {
setflag "$tps_reports";
}
if header :matches "List-ID" "*<*@*" {
fileinto "INBOX.lists.${2}"; stop;
}
"#;
let raw_message = r#"From: Sales Mailing List <list-sales@example.org>
To: John Doe <jdoe@example.org>
List-ID: <sales@example.org>
Subject: TPS Reports
We're putting new coversheets on all the TPS reports before they go out now.
So if you could go ahead and try to remember to do that from now on, that'd be great. All right!
"#;
// Compile
let compiler = Compiler::new();
let script = compiler.compile(text_script).unwrap();
// Build runtime
let runtime = Runtime::new();
// Create filter instance
let mut instance = runtime.filter(raw_message.as_bytes());
let mut input = Input::script("my-script", script);
let mut messages: Vec<String> = Vec::new();
// Start event loop
while let Some(result) = instance.run(input) {
match result {
Ok(event) => match event {
Event::IncludeScript { name, optional } => {
// NOTE: Just for demonstration purposes, script name needs to be validated first.
if let Ok(bytes) = std::fs::read(name.as_str()) {
let script = compiler.compile(&bytes).unwrap();
input = Input::script(name, script);
} else if optional {
input = Input::False;
} else {
panic!("Script {name} not found.");
}
}
Event::MailboxExists { .. } => {
// Set to true if the mailbox exists
input = false.into();
}
Event::ListContains { .. } => {
// Set to true if the list(s) contains an entry
input = false.into();
}
Event::DuplicateId { .. } => {
// Set to true if the ID is duplicate
input = false.into();
}
Event::SetEnvelope { envelope, value } => {
println!("Set envelope {envelope:?} to {value:?}");
input = true.into();
}
Event::Keep { flags, message_id } => {
println!(
"Keep message '{}' with flags {:?}.",
if message_id > 0 {
messages[message_id - 1].as_str()
} else {
raw_message
},
flags
);
input = true.into();
}
Event::Discard => {
println!("Discard message.");
input = true.into();
}
Event::Reject { reason, .. } => {
println!("Reject message with reason {reason:?}.");
input = true.into();
}
Event::FileInto {
folder,
flags,
message_id,
..
} => {
println!(
"File message '{}' in folder {:?} with flags {:?}.",
if message_id > 0 {
messages[message_id - 1].as_str()
} else {
raw_message
},
folder,
flags
);
input = true.into();
}
Event::SendMessage {
recipient,
message_id,
..
} => {
println!(
"Send message '{}' to {:?}.",
if message_id > 0 {
messages[message_id - 1].as_str()
} else {
raw_message
},
recipient
);
input = true.into();
}
Event::Notify {
message, method, ..
} => {
println!("Notify URI {method:?} with message {message:?}");
input = true.into();
}
Event::CreatedMessage { message, .. } => {
messages.push(String::from_utf8(message).unwrap());
input = true.into();
}
Event::Function { id, arguments } => {
println!(
"Script executed external function {id} with parameters {arguments:?}"
);
// Return variable result back to interpreter
input = Input::result("hello world".into());
}
#[cfg(test)]
_ => unreachable!(),
},
Err(error) => {
match error {
RuntimeError::TooManyIncludes => {
eprintln!("Too many included scripts.");
}
RuntimeError::InvalidInstruction(instruction) => {
eprintln!(
"Invalid instruction {:?} found at {}:{}.",
instruction.name(),
instruction.line_num(),
instruction.line_pos()
);
}
RuntimeError::ScriptErrorMessage(message) => {
eprintln!("Script called the 'error' function with {message:?}");
}
RuntimeError::CapabilityNotAllowed(capability) => {
eprintln!(
"Capability {capability:?} has been disabled by the administrator.",
);
}
RuntimeError::CapabilityNotSupported(capability) => {
eprintln!("Capability {capability:?} not supported.");
}
RuntimeError::CPULimitReached => {
eprintln!("Script exceeded the configured CPU limit.");
}
}
input = true.into();
}
}
}
}