//! Busy time of calendar objects, for free-busy-query (RFC 4791, 7.10). use calcard::icalendar::{ ICalendar, ICalendarComponentType, ICalendarEntry, ICalendarFreeBusyType, ICalendarParameterName, ICalendarParameterValue, ICalendarPeriod, ICalendarProperty, ICalendarStatus, ICalendarTransparency, ICalendarValue, }; use chrono::{DateTime, Utc}; use crate::expand::{expand, stamp}; use crate::filter::TimeRange; use crate::zone::{Zone, Zones, add}; /// In the order a client ranks them. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub enum Busy { Busy, Tentative, Unavailable, } impl Busy { fn fbtype(self) -> &'static str { match self { Busy::Busy => "BUSY", Busy::Tentative => "BUSY-TENTATIVE", Busy::Unavailable => "BUSY-UNAVAILABLE", } } } #[derive(Debug, Clone, PartialEq, Eq)] pub struct Period { pub kind: Busy, pub start: DateTime, pub end: DateTime, } /// The busy periods of one calendar object, clipped to `range`. Transparent /// and cancelled events are free; VFREEBUSY components count as stored. pub fn busy(cal: &ICalendar, range: &TimeRange, floating: &Zone) -> Vec { let zones = Zones::new(cal, floating.clone()); let mut out = Vec::new(); let mut push = |kind, start: DateTime, end: DateTime| { let (start, end) = (start.max(range.start), end.min(range.end)); if start < end { out.push(Period { kind, start, end }); } }; for x in expand(cal, range.clone(), floating.clone()).instances { let c = &cal.components[x.component]; if c.component_type != ICalendarComponentType::VEvent || c.transparency() == Some(&ICalendarTransparency::Transparent) { continue; } let kind = match c.status() { Some(ICalendarStatus::Cancelled) => continue, Some(ICalendarStatus::Tentative) => Busy::Tentative, _ => Busy::Busy, }; push(kind, x.start, x.end); } for c in cal .components .iter() .filter(|c| c.component_type == ICalendarComponentType::VFreebusy) { for e in c.properties(&ICalendarProperty::Freebusy) { let kind = match e.parameter(&ICalendarParameterName::Fbtype) { Some(ICalendarParameterValue::Fbtype(ICalendarFreeBusyType::Free)) => continue, Some(ICalendarParameterValue::Fbtype(ICalendarFreeBusyType::BusyTentative)) => { Busy::Tentative } Some(ICalendarParameterValue::Fbtype(ICalendarFreeBusyType::BusyUnavailable)) => { Busy::Unavailable } _ => Busy::Busy, }; for (s, e) in periods(&zones, e) { push(kind, s, e); } } } out } /// Sorts the periods and joins those of one kind that overlap or touch. pub fn merge(mut periods: Vec) -> Vec { periods.sort_by_key(|p| (p.kind, p.start)); let mut out: Vec = Vec::with_capacity(periods.len()); for p in periods { match out.last_mut() { Some(last) if last.kind == p.kind && p.start <= last.end => { last.end = last.end.max(p.end); } _ => out.push(p), } } out } /// The VCALENDAR a free-busy-query answers with. pub fn vfreebusy(periods: &[Period], range: &TimeRange, now: DateTime) -> String { let t = |d: DateTime| d.format("%Y%m%dT%H%M%SZ").to_string(); let mut out = format!( "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//filebrowser-ng//pimdav//EN\r\n\ BEGIN:VFREEBUSY\r\nDTSTAMP:{}\r\nDTSTART:{}\r\nDTEND:{}\r\n", t(now), t(range.start), t(range.end) ); for p in periods { out.push_str(&format!( "FREEBUSY;FBTYPE={}:{}/{}\r\n", p.kind.fbtype(), t(p.start), t(p.end) )); } out.push_str("END:VFREEBUSY\r\nEND:VCALENDAR\r\n"); out } /// The PERIOD values of a FREEBUSY property. pub(crate) fn periods(zones: &Zones, e: &ICalendarEntry) -> Vec<(DateTime, DateTime)> { e.values .iter() .filter_map(|v| period(zones, e.tz_id(), v)) .collect() } pub(crate) fn period( zones: &Zones, tzid: Option<&str>, v: &ICalendarValue, ) -> Option<(DateTime, DateTime)> { match v { ICalendarValue::Period(ICalendarPeriod::Range { start, end }) => Some(( stamp(zones, start, tzid)?.utc(), stamp(zones, end, tzid)?.utc(), )), ICalendarValue::Period(ICalendarPeriod::Duration { start, duration }) => { let s = stamp(zones, start, tzid)?.utc(); Some((s, add(s, duration.to_time_delta()?))) } _ => None, } }