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
use log::{info, warn};
use crate::inbox::{Folder, Inbox, Message};
#[cfg(test)]
pub mod tests;
#[derive(Debug)]
pub enum Action {
Delete,
Move(Folder),
}
#[derive(Debug)]
pub enum Matcher {
And(Vec<Matcher>),
Or(Vec<Matcher>),
Not(Box<Matcher>),
Subject(StringMatcher),
From(StringMatcher),
To(StringMatcher),
Body(StringMatcher),
}
/// Matches a string value
#[derive(Debug)]
pub enum StringMatcher {
/// Matches if the value contains the string. Case-insensitive.
Contains(String),
/// Matches if the value starts with the string. Case-insensitive
StartsWith(String),
/// Matches if the value equals the string. Case-insensitive.
Equals(String),
/// Matches if the value matches the regex.
Regex(regex::Regex),
}
#[derive(Debug)]
pub struct Rule {
pub name: String,
matcher: Matcher,
pub action: Action,
}
impl Rule {
/// Checks whether the message matches the [Matcher] and then executes the [Action] if the
/// matcher returned true.
///
/// An [Inbox] with which the action will be executed is also passed to the function.
pub fn match_and_execute(
&self,
inbox: &mut impl Inbox,
message: &mut Message,
) -> anyhow::Result<()> {
if self.matcher.matches(message) {
info!(
"Rule '{}' matched the message with subject '{}'",
self.name,
message
.subject
.clone()
.unwrap_or("Unknown subject".to_string())
);
self.action.execute(inbox, message)?;
}
Ok(())
}
/// Checks whether the message matches the [Matcher] and then logs the [Action] if the
/// matcher returned true.
pub fn match_and_log(&self, message: &mut Message) -> bool {
if self.matcher.matches(message) {
info!(
"Rule '{}' matched the message with subject '{}'",
self.name,
&message
.subject
.clone()
.unwrap_or("Unknown subject".to_string())
);
true
} else {
false
}
}
/// Constructs new rule.
pub fn new(name: String, matcher: Matcher, action: Action) -> Self {
Self {
name,
matcher,
action,
}
}
}
impl Matcher {
/// Returns true if the messages matches the matcher.
fn matches(&self, message: &Message) -> bool {
// TODO: think about whether we want to error out or silently fail when subject
// doesn't exist
match self {
Matcher::Subject(string_matcher) => {
if let Some(string) = &message.subject {
string_matcher.matches(string)
} else {
warn!("Failed to get subject.");
false
}
}
Matcher::From(string_matcher) => {
if let Some(string) = &message.from {
string_matcher.matches(string)
} else {
warn!("Failed to get from.");
false
}
}
Matcher::To(string_matcher) => {
if let Some(string) = &message.to {
string_matcher.matches(string)
} else {
warn!("Failed to get to.");
false
}
}
Matcher::Body(string_matcher) => string_matcher.matches(&message.body),
Matcher::And(matchers) => matchers.iter().all(|m| m.matches(message)),
Matcher::Or(matchers) => matchers.iter().any(|m| m.matches(message)),
Matcher::Not(matcher) => !matcher.matches(message),
}
}
}
impl StringMatcher {
fn matches(&self, input: &str) -> bool {
// NOTE: We are using to_lowercase here because we assume that it doesn't have a big
// performance hit when creating new strings. If it turns out to be too slow, we will
// handle that later but this is currently the easiest way to do case-insensitive
// comparisons.
match self {
StringMatcher::Contains(pattern) => {
input.to_lowercase().contains(&pattern.to_lowercase())
}
StringMatcher::StartsWith(pattern) => {
input.to_lowercase().starts_with(&pattern.to_lowercase())
}
StringMatcher::Equals(pattern) => input.to_lowercase() == pattern.to_lowercase(),
StringMatcher::Regex(regex) => regex.is_match(input),
}
}
}
impl Action {
/// Executes the defined action on [inbox](Inbox).
fn execute(&self, inbox: &mut impl Inbox, message: &mut Message) -> anyhow::Result<()> {
if !message.valid {
warn!(
"Message with UID {:?} is invalid. That usually means that an action was already performed on it.",
message.uid()
);
return Ok(());
}
match self {
Action::Delete => inbox.delete_message(message)?,
Action::Move(folder) => inbox.move_message_to_folder(message, folder)?,
};
Ok(())
}
}