filter.rs
⎇
Raw
1//! Query filters: `calendar-query` (RFC 4791, 9.7) and `addressbook-query`
2//! (RFC 6352, 10.5). Parsing and evaluation.
3
4use std::borrow::Cow;
5use std::cell::RefCell;
6use std::collections::HashMap;
7use std::ops::Range;
8use std::rc::Rc;
9
10use calcard::icalendar::{
11 ICalendar, ICalendarComponent, ICalendarComponentType, ICalendarEntry, ICalendarParameterName,
12 ICalendarParameterValue, ICalendarProperty, ICalendarRelated, ICalendarValue,
13};
14use calcard::vcard::{VCard, VCardVersion};
15use chrono::{DateTime, NaiveDateTime, TimeDelta, Utc};
16use unicode_normalization::UnicodeNormalization;
17use xmltree::Element;
18
19use crate::expand::{Expansion, expand, stamp};
20use crate::freebusy::periods;
21use crate::itip::endless;
22use crate::report::Refused;
23use crate::xml::{CALDAV, CARDDAV, Name, child, elements, text};
24use crate::zone::{Zone, Zones, add};
25
26pub type TimeRange = Range<DateTime<Utc>>;
27
28#[derive(Debug, Clone, PartialEq)]
29pub struct CompFilter {
30 /// Upper case.
31 pub name: String,
32 pub not_defined: bool,
33 pub time_range: Option<TimeRange>,
34 pub props: Vec<PropFilter>,
35 pub comps: Vec<CompFilter>,
36}
37
38#[derive(Debug, Clone, PartialEq)]
39pub struct PropFilter {
40 pub name: String,
41 pub not_defined: bool,
42 pub time_range: Option<TimeRange>,
43 pub text: Option<TextMatch>,
44 pub params: Vec<ParamFilter>,
45}
46
47#[derive(Debug, Clone, PartialEq)]
48pub struct ParamFilter {
49 pub name: String,
50 pub not_defined: bool,
51 pub text: Option<TextMatch>,
52}
53
54#[derive(Debug, Clone, PartialEq)]
55pub struct CardFilter {
56 /// `test="allof"`. The default is `anyof`.
57 pub all: bool,
58 pub props: Vec<CardPropFilter>,
59}
60
61#[derive(Debug, Clone, PartialEq)]
62pub struct CardPropFilter {
63 pub name: String,
64 pub all: bool,
65 pub not_defined: bool,
66 pub texts: Vec<TextMatch>,
67 pub params: Vec<ParamFilter>,
68}
69
70#[derive(Debug, Clone, PartialEq)]
71pub struct TextMatch {
72 pub text: String,
73 pub collation: Collation,
74 pub match_type: MatchType,
75 pub negate: bool,
76}
77
78#[derive(Debug, Clone, Copy, PartialEq, Eq)]
79pub enum Collation {
80 Octet,
81 AsciiCasemap,
82 UnicodeCasemap,
83}
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq)]
86pub enum MatchType {
87 Equals,
88 Contains,
89 StartsWith,
90 EndsWith,
91}
92
93impl Collation {
94 fn fold<'a>(self, s: &'a str) -> Cow<'a, str> {
95 match self {
96 Collation::Octet => Cow::Borrowed(s),
97 Collation::AsciiCasemap => Cow::Owned(s.to_ascii_lowercase()),
98 // Lowercase after NFKD, so compatibility forms such as U+FB01
99 // (the "fi" ligature) fold too.
100 Collation::UnicodeCasemap => {
101 Cow::Owned(s.nfkd().flat_map(char::to_lowercase).collect())
102 }
103 }
104 }
105}
106
107impl TextMatch {
108 /// `ns` picks the defaults: CalDAV folds ASCII only and CardDAV Unicode.
109 fn parse(e: &Element, ns: &str) -> Result<Self, Refused> {
110 let collation = match e.attributes.get("collation").map(String::as_str) {
111 None if ns == CARDDAV => Collation::UnicodeCasemap,
112 None => Collation::AsciiCasemap,
113 Some("i;octet") => Collation::Octet,
114 Some("i;ascii-casemap") => Collation::AsciiCasemap,
115 Some("i;unicode-casemap") => Collation::UnicodeCasemap,
116 Some(_) => return Err(Refused::Condition(Name::new(ns, "supported-collation"))),
117 };
118 let match_type = match e.attributes.get("match-type").map(String::as_str) {
119 None | Some("contains") => MatchType::Contains,
120 Some("equals") => MatchType::Equals,
121 Some("starts-with") => MatchType::StartsWith,
122 Some("ends-with") => MatchType::EndsWith,
123 Some(_) => return Err(Refused::Invalid),
124 };
125 Ok(TextMatch {
126 text: text(e),
127 collation,
128 match_type,
129 negate: e.attributes.get("negate-condition").map(String::as_str) == Some("yes"),
130 })
131 }
132
133 pub fn matches(&self, value: &str) -> bool {
134 let (v, t) = (self.collation.fold(value), self.collation.fold(&self.text));
135 let hit = match self.match_type {
136 MatchType::Equals => v == t,
137 MatchType::Contains => v.contains(&*t),
138 MatchType::StartsWith => v.starts_with(&*t),
139 MatchType::EndsWith => v.ends_with(&*t),
140 };
141 hit != self.negate
142 }
143}
144
145// ---------------------------------------------------------------------------
146// Parsing
147// ---------------------------------------------------------------------------
148
149/// The `<C:filter>` of a calendar-query.
150pub fn calendar_filter(e: &Element) -> Result<CompFilter, Refused> {
151 let root = comp_filter(child(e, CALDAV, "comp-filter").ok_or(Refused::Invalid)?)?;
152 if root.name != "VCALENDAR" {
153 return Err(Refused::Condition(Name::new(CALDAV, "valid-filter")));
154 }
155 Ok(root)
156}
157
158fn comp_filter(e: &Element) -> Result<CompFilter, Refused> {
159 let mut f = CompFilter {
160 name: name_attr(e)?.to_ascii_uppercase(),
161 not_defined: false,
162 time_range: None,
163 props: Vec::new(),
164 comps: Vec::new(),
165 };
166 for c in elements(e).filter(|c| Name::of(c).ns == CALDAV) {
167 match c.name.as_str() {
168 "is-not-defined" => f.not_defined = true,
169 "time-range" => f.time_range = Some(time_range(c)?),
170 "prop-filter" => f.props.push(prop_filter(c)?),
171 "comp-filter" => f.comps.push(comp_filter(c)?),
172 _ => {}
173 }
174 }
175 Ok(f)
176}
177
178fn prop_filter(e: &Element) -> Result<PropFilter, Refused> {
179 let mut f = PropFilter {
180 name: name_attr(e)?.to_string(),
181 not_defined: false,
182 time_range: None,
183 text: None,
184 params: Vec::new(),
185 };
186 for c in elements(e).filter(|c| Name::of(c).ns == CALDAV) {
187 match c.name.as_str() {
188 "is-not-defined" => f.not_defined = true,
189 "time-range" => f.time_range = Some(time_range(c)?),
190 "text-match" => f.text = Some(TextMatch::parse(c, CALDAV)?),
191 "param-filter" => f.params.push(param_filter(c, CALDAV)?),
192 _ => {}
193 }
194 }
195 Ok(f)
196}
197
198fn param_filter(e: &Element, ns: &str) -> Result<ParamFilter, Refused> {
199 let mut f = ParamFilter {
200 name: name_attr(e)?.to_string(),
201 not_defined: false,
202 text: None,
203 };
204 for c in elements(e).filter(|c| Name::of(c).ns == ns) {
205 match c.name.as_str() {
206 "is-not-defined" => f.not_defined = true,
207 "text-match" => f.text = Some(TextMatch::parse(c, ns)?),
208 _ => {}
209 }
210 }
211 Ok(f)
212}
213
214/// The `<CR:filter>` of an addressbook-query.
215pub fn card_filter(e: &Element) -> Result<CardFilter, Refused> {
216 let props = elements(e)
217 .filter(|c| Name::of(c).is(CARDDAV, "prop-filter"))
218 .map(card_prop_filter)
219 .collect::<Result<_, _>>()?;
220 Ok(CardFilter {
221 all: all_of(e)?,
222 props,
223 })
224}
225
226fn card_prop_filter(e: &Element) -> Result<CardPropFilter, Refused> {
227 let mut f = CardPropFilter {
228 name: name_attr(e)?.to_string(),
229 all: all_of(e)?,
230 not_defined: false,
231 texts: Vec::new(),
232 params: Vec::new(),
233 };
234 for c in elements(e).filter(|c| Name::of(c).ns == CARDDAV) {
235 match c.name.as_str() {
236 "is-not-defined" => f.not_defined = true,
237 "text-match" => f.texts.push(TextMatch::parse(c, CARDDAV)?),
238 "param-filter" => f.params.push(param_filter(c, CARDDAV)?),
239 _ => {}
240 }
241 }
242 Ok(f)
243}
244
245fn all_of(e: &Element) -> Result<bool, Refused> {
246 match e.attributes.get("test").map(String::as_str) {
247 None | Some("anyof") => Ok(false),
248 Some("allof") => Ok(true),
249 Some(_) => Err(Refused::Invalid),
250 }
251}
252
253fn name_attr(e: &Element) -> Result<&str, Refused> {
254 e.attributes
255 .get("name")
256 .map(String::as_str)
257 .ok_or(Refused::Invalid)
258}
259
260/// A `time-range` or `expand` element. A missing bound is open.
261pub fn time_range(e: &Element) -> Result<TimeRange, Refused> {
262 let at = |k: &str| {
263 e.attributes
264 .get(k)
265 .map(|v| {
266 NaiveDateTime::parse_from_str(v, "%Y%m%dT%H%M%SZ")
267 .map(|t| t.and_utc())
268 .map_err(|_| Refused::Invalid)
269 })
270 .transpose()
271 };
272 let start = at("start")?.unwrap_or(DateTime::<Utc>::MIN_UTC);
273 let end = at("end")?.unwrap_or(DateTime::<Utc>::MAX_UTC);
274 if start >= end {
275 return Err(Refused::Invalid);
276 }
277 Ok(start..end)
278}
279
280// ---------------------------------------------------------------------------
281// Calendar evaluation
282// ---------------------------------------------------------------------------
283
284/// Whether a calendar object matches `filter`. `floating` interprets values
285/// without a zone.
286pub fn matches_calendar(cal: &ICalendar, filter: &CompFilter, floating: &Zone) -> bool {
287 let alarm_reach = cal
288 .components
289 .iter()
290 .filter(|c| c.component_type == ICalendarComponentType::VAlarm)
291 .filter_map(relative_reach)
292 .max()
293 .unwrap_or_default();
294 let ctx = Ctx {
295 cal,
296 zones: Zones::new(cal, floating.clone()),
297 floating,
298 alarm_reach,
299 expanded: RefCell::default(),
300 };
301 ctx.comp(None, filter)
302}
303
304struct Ctx<'a> {
305 cal: &'a ICalendar,
306 zones: Zones,
307 floating: &'a Zone,
308 /// The widest reach of any relative alarm, so all alarms share one
309 /// expansion.
310 alarm_reach: TimeDelta,
311 /// One expansion per window, not one per component.
312 expanded: RefCell<HashMap<TimeRange, Rc<Expansion>>>,
313}
314
315/// The repetitions of alarm `a` and the time between them.
316fn repetitions(a: &ICalendarComponent) -> (i32, TimeDelta) {
317 let repeat = a
318 .property(&ICalendarProperty::Repeat)
319 .and_then(|e| e.values.first()?.as_integer())
320 .unwrap_or(0)
321 .clamp(0, 1000) as i32;
322 let every = match a
323 .property(&ICalendarProperty::Duration)
324 .and_then(|e| e.values.first())
325 {
326 Some(ICalendarValue::Duration(d)) => d.to_time_delta().unwrap_or_default(),
327 _ => TimeDelta::zero(),
328 };
329 (repeat, every)
330}
331
332/// How far from its instance a relative alarm, repetitions included, can
333/// fire, plus one second. `None` for an absolute trigger.
334fn relative_reach(a: &ICalendarComponent) -> Option<TimeDelta> {
335 let Some(ICalendarValue::Duration(d)) = a.property(&ICalendarProperty::Trigger)?.values.first()
336 else {
337 return None;
338 };
339 let (repeat, every) = repetitions(a);
340 Some(
341 d.to_time_delta()?
342 .abs()
343 .checked_add(&every.checked_mul(repeat).unwrap_or(TimeDelta::MAX))
344 .unwrap_or(TimeDelta::MAX)
345 .checked_add(&TimeDelta::seconds(1))
346 .unwrap_or(TimeDelta::MAX),
347 )
348}
349
350impl Ctx<'_> {
351 fn expand(&self, window: TimeRange) -> Rc<Expansion> {
352 self.expanded
353 .borrow_mut()
354 .entry(window.clone())
355 .or_insert_with(|| Rc::new(expand(self.cal, window, self.floating.clone())))
356 .clone()
357 }
358
359 fn comp(&self, parent: Option<usize>, f: &CompFilter) -> bool {
360 let children: Vec<usize> = match parent {
361 None => (!self.cal.components.is_empty())
362 .then_some(0)
363 .into_iter()
364 .collect(),
365 Some(p) => self.cal.components[p]
366 .component_ids
367 .iter()
368 .map(|&i| i as usize)
369 .collect(),
370 };
371 let mut found = children.into_iter().filter(|&i| {
372 self.cal.components[i]
373 .component_type
374 .as_str()
375 .eq_ignore_ascii_case(&f.name)
376 });
377 if f.not_defined {
378 return found.next().is_none();
379 }
380 found.any(|i| {
381 f.time_range.as_ref().is_none_or(|r| self.overlaps(i, r))
382 && f.props.iter().all(|p| self.prop(i, p))
383 && f.comps.iter().all(|c| self.comp(Some(i), c))
384 })
385 }
386
387 /// The time-range rules of RFC 4791, 9.9.
388 fn overlaps(&self, i: usize, r: &TimeRange) -> bool {
389 let c = &self.cal.components[i];
390 let has = |p: ICalendarProperty| c.has_property(&p);
391 let time = |p: ICalendarProperty| {
392 let e = c.property(&p)?;
393 stamp(
394 &self.zones,
395 e.values.first()?.as_partial_date_time()?,
396 e.tz_id(),
397 )
398 .map(|s| s.utc())
399 };
400 match c.component_type {
401 ICalendarComponentType::VEvent
402 | ICalendarComponentType::VTodo
403 | ICalendarComponentType::VJournal
404 if has(ICalendarProperty::Dtstart) =>
405 {
406 if r.end == DateTime::<Utc>::MAX_UTC && endless(&self.cal.components[i]) {
407 return true;
408 }
409 let todo = c.component_type == ICalendarComponentType::VTodo;
410 let (due, duration) = (
411 has(ICalendarProperty::Due),
412 has(ICalendarProperty::Duration),
413 );
414 // One second wider, so that the exact rules below decide the
415 // instances that only touch the range.
416 let second = TimeDelta::seconds(1);
417 let exp = self.expand(add(r.start, -second)..add(r.end, second));
418 // Unknown instances may overlap, so the object stays in.
419 if exp.truncated {
420 return true;
421 }
422 exp.instances.iter().filter(|x| x.component == i).any(|x| {
423 let (s, e) = (x.start, x.end);
424 match (todo, due, duration) {
425 (true, _, true) => r.start <= e && (r.end > s || r.end >= e),
426 (true, true, _) => {
427 (r.start < e || r.start <= s) && (r.end > s || r.end >= e)
428 }
429 (true, ..) => r.start <= s && r.end > s,
430 _ if s == e => r.start <= s && r.end > s,
431 _ => r.start < e && r.end > s,
432 }
433 })
434 }
435 ICalendarComponentType::VTodo => match (
436 time(ICalendarProperty::Due),
437 time(ICalendarProperty::Completed),
438 time(ICalendarProperty::Created),
439 ) {
440 (Some(due), _, _) => r.start < due && r.end >= due,
441 (None, Some(done), Some(made)) => {
442 (r.start <= made || r.start <= done) && (r.end >= made || r.end >= done)
443 }
444 (None, Some(done), None) => r.start <= done && r.end >= done,
445 (None, None, Some(made)) => r.end > made,
446 (None, None, None) => true,
447 },
448 ICalendarComponentType::VFreebusy => {
449 let busy: Vec<_> = c
450 .properties(&ICalendarProperty::Freebusy)
451 .flat_map(|e| periods(&self.zones, e))
452 .collect();
453 if !busy.is_empty() {
454 return busy.iter().any(|(s, e)| r.start < *e && r.end > *s);
455 }
456 match (
457 time(ICalendarProperty::Dtstart),
458 time(ICalendarProperty::Dtend),
459 ) {
460 (Some(s), Some(e)) => r.start <= e && r.end > s,
461 _ => false,
462 }
463 }
464 ICalendarComponentType::VAlarm => self.alarm_overlaps(i, r),
465 _ => false,
466 }
467 }
468
469 /// Whether a trigger of the alarm, repetitions included, falls into `r`.
470 /// A relative trigger fires once per instance of the parent component.
471 fn alarm_overlaps(&self, alarm: usize, r: &TimeRange) -> bool {
472 let a = &self.cal.components[alarm];
473 let Some(trigger) = a.property(&ICalendarProperty::Trigger) else {
474 return false;
475 };
476 let (repeat, every) = repetitions(a);
477 let hit = |base: DateTime<Utc>| {
478 (0..=repeat).any(|k| {
479 let t = add(base, every.checked_mul(k).unwrap_or(TimeDelta::MAX));
480 r.start <= t && r.end > t
481 })
482 };
483 match trigger.values.first() {
484 Some(ICalendarValue::PartialDateTime(p)) => {
485 stamp(&self.zones, p, trigger.tz_id()).is_some_and(|s| hit(s.utc()))
486 }
487 Some(ICalendarValue::Duration(d)) => {
488 let Some(offset) = d.to_time_delta() else {
489 return false;
490 };
491 let Some(parent) = self
492 .cal
493 .components
494 .iter()
495 .position(|c| c.component_ids.contains(&(alarm as u32)))
496 else {
497 return false;
498 };
499 if r.end == DateTime::<Utc>::MAX_UTC && endless(&self.cal.components[parent]) {
500 return true;
501 }
502 let owner = &self.cal.components[parent];
503 // A task without DTSTART has no instances. Its alarm can only
504 // relate to DUE (RFC 4791, 9.9).
505 if !owner.has_property(&ICalendarProperty::Dtstart) {
506 return owner
507 .property(&ICalendarProperty::Due)
508 .and_then(|e| {
509 stamp(
510 &self.zones,
511 e.values.first()?.as_partial_date_time()?,
512 e.tz_id(),
513 )
514 })
515 .is_some_and(|due| hit(add(due.utc(), offset)));
516 }
517 let from_end = trigger.parameter(&ICalendarParameterName::Related)
518 == Some(&ICalendarParameterValue::Related(ICalendarRelated::End));
519 let exp =
520 self.expand(add(r.start, -self.alarm_reach)..add(r.end, self.alarm_reach));
521 if exp.truncated {
522 return true;
523 }
524 exp.instances
525 .iter()
526 .filter(|x| x.component == parent)
527 .any(|x| hit(add(if from_end { x.end } else { x.start }, offset)))
528 }
529 _ => false,
530 }
531 }
532
533 fn prop(&self, i: usize, f: &PropFilter) -> bool {
534 let mut found = self.cal.components[i]
535 .entries
536 .iter()
537 .filter(|e| e.name.as_str().eq_ignore_ascii_case(&f.name));
538 if f.not_defined {
539 return found.next().is_none();
540 }
541 found.any(|e| {
542 let l = ical_line(e);
543 f.time_range.as_ref().is_none_or(|r| {
544 e.values
545 .iter()
546 .filter_map(|v| stamp(&self.zones, v.as_partial_date_time()?, e.tz_id()))
547 .any(|s| r.start <= s.utc() && r.end > s.utc())
548 }) && f.text.as_ref().is_none_or(|t| t.matches(&l.value))
549 && f.params.iter().all(|p| param_matches(&l, p))
550 })
551 }
552}
553
554// ---------------------------------------------------------------------------
555// Card evaluation
556// ---------------------------------------------------------------------------
557
558pub fn matches_card(card: &VCard, f: &CardFilter) -> bool {
559 if f.props.is_empty() {
560 return true;
561 }
562 let v4 = card.version() == Some(VCardVersion::V4_0);
563 let lines: Vec<Line> = card
564 .entries
565 .iter()
566 .map(|e| {
567 let mut s = String::new();
568 let _ = e.write_to(&mut s, v4);
569 line(&s)
570 })
571 .collect();
572 let hit = |p: &CardPropFilter| card_prop(&lines, p);
573 if f.all {
574 f.props.iter().all(hit)
575 } else {
576 f.props.iter().any(hit)
577 }
578}
579
580/// Each text-match and param-filter is tested against every instance of the
581/// property; `test` combines their results.
582fn card_prop(lines: &[Line], f: &CardPropFilter) -> bool {
583 let found: Vec<&Line> = lines
584 .iter()
585 .filter(|l| l.name.eq_ignore_ascii_case(&f.name))
586 .collect();
587 if f.not_defined {
588 return found.is_empty();
589 }
590 if found.is_empty() {
591 return false;
592 }
593 let mut tests = f
594 .texts
595 .iter()
596 .map(|t| found.iter().any(|l| t.matches(&l.value)))
597 .chain(
598 f.params
599 .iter()
600 .map(|p| found.iter().any(|l| param_matches(l, p))),
601 )
602 .peekable();
603 if tests.peek().is_none() {
604 return true;
605 }
606 if f.all {
607 tests.all(|b| b)
608 } else {
609 tests.any(|b| b)
610 }
611}
612
613fn param_matches(l: &Line, f: &ParamFilter) -> bool {
614 match l
615 .params
616 .iter()
617 .find(|(n, _)| n.eq_ignore_ascii_case(&f.name))
618 {
619 None => f.not_defined,
620 Some(_) if f.not_defined => false,
621 Some((_, values)) => f
622 .text
623 .as_ref()
624 .is_none_or(|t| values.iter().any(|v| t.matches(v))),
625 }
626}
627
628// ---------------------------------------------------------------------------
629// Content lines
630// ---------------------------------------------------------------------------
631
632/// A property as text, the way text-match sees it: the group dropped, the
633/// parameter values unquoted, the value unescaped.
634struct Line {
635 name: String,
636 params: Vec<(String, Vec<String>)>,
637 value: String,
638}
639
640fn ical_line(e: &ICalendarEntry) -> Line {
641 let mut s = String::new();
642 let _ = e.write_to(&mut s);
643 line(&s)
644}
645
646/// Parses one content line as calcard's writer produces it.
647fn line(written: &str) -> Line {
648 let unfolded = written.replace("\r\n ", "").replace("\r\n\t", "");
649 let unfolded = unfolded.trim_end_matches(['\r', '\n']);
650 let head = split_unquoted(unfolded, ':')[0];
651 let value = unfolded.get(head.len() + 1..).unwrap_or_default();
652 let mut head = split_unquoted(head, ';').into_iter();
653 let name = head.next().unwrap_or_default();
654 let name = name.rsplit('.').next().unwrap_or(name).to_ascii_uppercase();
655 let params = head
656 .filter_map(|p| p.split_once('='))
657 .map(|(k, v)| {
658 let values = split_unquoted(v, ',')
659 .into_iter()
660 .map(|v| v.trim_matches('"').to_string())
661 .collect();
662 (k.to_ascii_uppercase(), values)
663 })
664 .collect();
665 Line {
666 name,
667 params,
668 value: unescape(value),
669 }
670}
671
672fn split_unquoted(s: &str, sep: char) -> Vec<&str> {
673 let mut out = Vec::new();
674 let (mut quoted, mut from) = (false, 0);
675 for (i, c) in s.char_indices() {
676 if c == '"' {
677 quoted = !quoted;
678 } else if c == sep && !quoted {
679 out.push(&s[from..i]);
680 from = i + 1;
681 }
682 }
683 out.push(&s[from..]);
684 out
685}
686
687fn unescape(s: &str) -> String {
688 let mut out = String::with_capacity(s.len());
689 let mut chars = s.chars();
690 while let Some(c) = chars.next() {
691 if c != '\\' {
692 out.push(c);
693 continue;
694 }
695 match chars.next() {
696 Some('n' | 'N') => out.push('\n'),
697 Some(c) => out.push(c),
698 None => out.push('\\'),
699 }
700 }
701 out
702}
703