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
|
use crate::parser;
use aero_dav::caltypes as cal;
pub fn is_component_match(
parent: &icalendar::parser::Component,
components: &[icalendar::parser::Component],
filter: &cal::CompFilter,
) -> bool {
// Find the component among the list
let maybe_comps = components
.iter()
.filter(|candidate| candidate.name.as_str() == filter.name.as_str())
.collect::<Vec<_>>();
// Filter according to rules
match (&maybe_comps[..], &filter.additional_rules) {
([_, ..], None) => true,
([], Some(cal::CompFilterRules::IsNotDefined)) => true,
([], None) => false,
([_, ..], Some(cal::CompFilterRules::IsNotDefined)) => false,
(comps, Some(cal::CompFilterRules::Matches(matcher))) => comps.iter().any(|component| {
// check time range
if let Some(time_range) = &matcher.time_range {
if !is_in_time_range(
&filter.name,
parent,
component.properties.as_ref(),
time_range,
) {
return false;
}
}
// check properties
if !is_properties_match(component.properties.as_ref(), matcher.prop_filter.as_ref()) {
return false;
}
// check inner components
matcher.comp_filter.iter().all(|inner_filter| {
is_component_match(component, component.components.as_ref(), &inner_filter)
})
}),
}
}
fn prop_date(
properties: &[icalendar::parser::Property],
name: &str,
) -> Option<chrono::DateTime<chrono::Utc>> {
properties
.iter()
.find(|candidate| candidate.name.as_str() == name)
.map(|p| p.val.as_str())
.map(parser::date_time)
.flatten()
}
fn prop_parse<T: std::str::FromStr>(
properties: &[icalendar::parser::Property],
name: &str,
) -> Option<T> {
properties
.iter()
.find(|candidate| candidate.name.as_str() == name)
.map(|p| p.val.as_str().parse::<T>().ok())
.flatten()
}
fn is_properties_match(props: &[icalendar::parser::Property], filters: &[cal::PropFilter]) -> bool {
filters.iter().all(|single_filter| {
// Find the property
let candidate_props = props
.iter()
.filter(|candidate| candidate.name.as_str() == single_filter.name.0.as_str())
.collect::<Vec<_>>();
match (&single_filter.additional_rules, &candidate_props[..]) {
(None, [_, ..]) | (Some(cal::PropFilterRules::IsNotDefined), []) => true,
(None, []) | (Some(cal::PropFilterRules::IsNotDefined), [_, ..]) => false,
(Some(cal::PropFilterRules::Match(pattern)), multi_props) => {
multi_props.iter().any(|prop| {
// check value
match &pattern.time_or_text {
Some(cal::TimeOrText::Time(time_range)) => {
let maybe_parsed_date = parser::date_time(prop.val.as_str());
let parsed_date = match maybe_parsed_date {
None => return false,
Some(v) => v,
};
// see if entry is in range
let is_in_range = match time_range {
cal::TimeRange::OnlyStart(after) => &parsed_date >= after,
cal::TimeRange::OnlyEnd(before) => &parsed_date <= before,
cal::TimeRange::FullRange(after, before) => {
&parsed_date >= after && &parsed_date <= before
}
};
if !is_in_range {
return false;
}
// if you are here, this subcondition is valid
}
Some(cal::TimeOrText::Text(txt_match)) => {
//@FIXME ignoring collation
let is_match = match txt_match.negate_condition {
None | Some(false) => {
prop.val.as_str().contains(txt_match.text.as_str())
}
Some(true) => !prop.val.as_str().contains(txt_match.text.as_str()),
};
if !is_match {
return false;
}
}
None => (), // if not filter on value is set, continue
};
// check parameters
pattern.param_filter.iter().all(|single_param_filter| {
let multi_param = prop
.params
.iter()
.filter(|candidate| {
candidate.key.as_str() == single_param_filter.name.as_str()
})
.collect::<Vec<_>>();
match (&multi_param[..], &single_param_filter.additional_rules) {
([.., _], None) => true,
([], None) => false,
([.., _], Some(cal::ParamFilterMatch::IsNotDefined)) => false,
([], Some(cal::ParamFilterMatch::IsNotDefined)) => true,
(many_params, Some(cal::ParamFilterMatch::Match(txt_match))) => {
many_params.iter().any(|param| {
let param_val = match ¶m.val {
Some(v) => v,
None => return false,
};
match txt_match.negate_condition {
None | Some(false) => {
param_val.as_str().contains(txt_match.text.as_str())
}
Some(true) => {
!param_val.as_str().contains(txt_match.text.as_str())
}
}
})
}
}
})
})
}
}
})
}
fn resolve_trigger(
parent: &icalendar::parser::Component,
properties: &[icalendar::parser::Property],
) -> Option<chrono::DateTime<chrono::Utc>> {
// A. Do we have a TRIGGER property? If not, returns early
let maybe_trigger_prop = properties
.iter()
.find(|candidate| candidate.name.as_str() == "TRIGGER");
let trigger_prop = match maybe_trigger_prop {
None => return None,
Some(v) => v,
};
// B.1 Is it an absolute datetime? If so, returns early
let maybe_absolute = trigger_prop
.params
.iter()
.find(|param| param.key.as_str() == "VALUE")
.map(|param| param.val.as_ref())
.flatten()
.map(|v| v.as_str() == "DATE-TIME");
if maybe_absolute.is_some() {
let final_date = prop_date(properties, "TRIGGER");
tracing::trace!(trigger=?final_date, "resolved absolute trigger");
return final_date;
}
// B.2 Otherwise it's a timedelta relative to a parent field.
// C.1 Parse the timedelta value, returns early if invalid
let (_, time_delta) = parser::dur_value(trigger_prop.val.as_str()).ok()?;
// C.2 Get the parent reference absolute datetime, returns early if invalid
let maybe_bound = trigger_prop
.params
.iter()
.find(|param| param.key.as_str() == "RELATED")
.map(|param| param.val.as_ref())
.flatten();
// If the trigger is set relative to START, then the "DTSTART" property MUST be present in the associated
// "VEVENT" or "VTODO" calendar component.
//
// If an alarm is specified for an event with the trigger set relative to the END,
// then the "DTEND" property or the "DTSTART" and "DURATION " properties MUST be present
// in the associated "VEVENT" calendar component.
//
// If the alarm is specified for a to-do with a trigger set relative to the END,
// then either the "DUE" property or the "DTSTART" and "DURATION " properties
// MUST be present in the associated "VTODO" calendar component.
let related_field = match maybe_bound.as_ref().map(|v| v.as_str()) {
Some("START") => "DTSTART",
Some("END") => "DTEND", //@FIXME must add support for DUE, DTSTART, and DURATION
_ => "DTSTART", // by default use DTSTART
};
let parent_date = match prop_date(parent.properties.as_ref(), related_field) {
Some(v) => v,
_ => return None,
};
// C.3 Compute the final date from the base date + timedelta
let final_date = parent_date + time_delta;
tracing::trace!(trigger=?final_date, "resolved relative trigger");
Some(final_date)
}
fn is_in_time_range(
component: &cal::Component,
parent: &icalendar::parser::Component,
properties: &[icalendar::parser::Property],
time_range: &cal::TimeRange,
) -> bool {
//@FIXME timezones are not properly handled currently (everything is UTC)
//@FIXME does not support repeat
//ref: https://datatracker.ietf.org/doc/html/rfc4791#section-9.9
let (start, end) = match time_range {
cal::TimeRange::OnlyStart(start) => (start, &chrono::DateTime::<chrono::Utc>::MAX_UTC),
cal::TimeRange::OnlyEnd(end) => (&chrono::DateTime::<chrono::Utc>::MIN_UTC, end),
cal::TimeRange::FullRange(start, end) => (start, end),
};
match component {
cal::Component::VEvent => {
let dtstart = match prop_date(properties, "DTSTART") {
Some(v) => v,
_ => return false,
};
let maybe_dtend = prop_date(properties, "DTEND");
let maybe_duration = prop_parse::<i64>(properties, "DURATION")
.map(|d| chrono::TimeDelta::new(std::cmp::max(d, 0), 0))
.flatten();
//@FIXME missing "date" management (only support "datetime")
match (&maybe_dtend, &maybe_duration) {
// | Y | N | N | * | (start < DTEND AND end > DTSTART) |
(Some(dtend), _) => start < dtend && end > &dtstart,
// | N | Y | Y | * | (start < DTSTART+DURATION AND end > DTSTART) |
(_, Some(duration)) => *start <= dtstart + *duration && end > &dtstart,
// | N | N | N | Y | (start <= DTSTART AND end > DTSTART) |
_ => start <= &dtstart && end > &dtstart,
}
}
cal::Component::VTodo => {
let maybe_dtstart = prop_date(properties, "DTSTART");
let maybe_due = prop_date(properties, "DUE");
let maybe_completed = prop_date(properties, "COMPLETED");
let maybe_created = prop_date(properties, "CREATED");
let maybe_duration = prop_parse::<i64>(properties, "DURATION")
.map(|d| chrono::TimeDelta::new(d, 0))
.flatten();
match (
maybe_dtstart,
maybe_duration,
maybe_due,
maybe_completed,
maybe_created,
) {
// | Y | Y | N | * | * | (start <= DTSTART+DURATION) AND |
// | | | | | | ((end > DTSTART) OR |
// | | | | | | (end >= DTSTART+DURATION)) |
(Some(dtstart), Some(duration), None, _, _) => {
*start <= dtstart + duration && (*end > dtstart || *end >= dtstart + duration)
}
// | Y | N | Y | * | * | ((start < DUE) OR (start <= DTSTART)) |
// | | | | | | AND |
// | | | | | | ((end > DTSTART) OR (end >= DUE)) |
(Some(dtstart), None, Some(due), _, _) => {
(*start < due || *start <= dtstart) && (*end > dtstart || *end >= due)
}
// | Y | N | N | * | * | (start <= DTSTART) AND (end > DTSTART) |
(Some(dtstart), None, None, _, _) => *start <= dtstart && *end > dtstart,
// | N | N | Y | * | * | (start < DUE) AND (end >= DUE) |
(None, None, Some(due), _, _) => *start < due && *end >= due,
// | N | N | N | Y | Y | ((start <= CREATED) OR (start <= COMPLETED))|
// | | | | | | AND |
// | | | | | | ((end >= CREATED) OR (end >= COMPLETED))|
(None, None, None, Some(completed), Some(created)) => {
(*start <= created || *start <= completed)
&& (*end >= created || *end >= completed)
}
// | N | N | N | Y | N | (start <= COMPLETED) AND (end >= COMPLETED) |
(None, None, None, Some(completed), None) => {
*start <= completed && *end >= completed
}
// | N | N | N | N | Y | (end > CREATED) |
(None, None, None, None, Some(created)) => *end > created,
// | N | N | N | N | N | TRUE |
_ => true,
}
}
cal::Component::VJournal => {
let maybe_dtstart = prop_date(properties, "DTSTART");
match maybe_dtstart {
// | Y | Y | (start <= DTSTART) AND (end > DTSTART) |
Some(dtstart) => *start <= dtstart && *end > dtstart,
// | N | * | FALSE |
None => false,
}
}
cal::Component::VFreeBusy => {
//@FIXME freebusy is not supported yet
false
}
cal::Component::VAlarm => {
//@FIXME does not support REPEAT
let maybe_trigger = resolve_trigger(parent, properties);
match maybe_trigger {
// (start <= trigger-time) AND (end > trigger-time)
Some(trigger_time) => *start <= trigger_time && *end > trigger_time,
_ => false,
}
}
_ => false,
}
}
|