bundle.rs
⎇
Raw
1//! Whole collections as one file, and one file split into objects: public
2//! feeds, export and import.
3//!
4//! Works on the text, not on parsed data, so a kept line reaches the output
5//! unchanged. Only line endings are rewritten, to CRLF.
6
7use std::collections::{HashMap, HashSet};
8
9use calcard::icalendar::{ICalendar, ICalendarComponentType, ICalendarProperty};
10use chrono::{DateTime, Utc};
11use sha2::{Digest, Sha256};
12
13use crate::expand::stamp;
14use crate::text::{
15 escape_text, fold, logical_lines, name, param, param_parts, unescape_text, unfold, value,
16 value_start,
17};
18use crate::zone::{Zone, Zones};
19
20const PRODID: &str = "PRODID:-//dovenest//pimdav//EN";
21
22/// The properties a busy-only event keeps.
23const BUSY_PROPS: [&str; 13] = [
24 "UID",
25 "DTSTAMP",
26 "DTSTART",
27 "DTEND",
28 "DURATION",
29 "RRULE",
30 "RDATE",
31 "EXDATE",
32 "EXRULE",
33 "RECURRENCE-ID",
34 "SEQUENCE",
35 "TRANSP",
36 "STATUS",
37];
38
39/// How much of a calendar a file shows.
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub enum Detail {
42 /// Everything, for the owner or a borrower.
43 All,
44 /// Everything, except that an object with a private or confidential
45 /// component shows as busy time only, as Google and Nextcloud do.
46 Public,
47 /// Busy time only.
48 Busy,
49}
50
51/// One VCALENDAR with the components of every object in `objects`, each
52/// VTIMEZONE once per TZID. Subscribers are asked to poll hourly.
53pub fn calendar(objects: &[&str], name: Option<&str>, detail: Detail) -> String {
54 let mut zones = String::new();
55 let mut body = String::new();
56 let mut seen = HashSet::new();
57 for object in objects {
58 for cal in top_blocks(object, "VCALENDAR") {
59 // A component cut off before its END would swallow the ones after.
60 let children: Vec<Block> = split_level(inner(&cal.lines))
61 .1
62 .into_iter()
63 .filter(Block::complete)
64 .collect();
65 for z in children.iter().filter(|c| c.name == "VTIMEZONE") {
66 if z.prop("TZID").is_some_and(|id| seen.insert(id)) {
67 push_lines(&mut zones, &z.lines);
68 }
69 }
70 let busy = match detail {
71 Detail::All => false,
72 Detail::Busy => true,
73 Detail::Public => children.iter().any(is_private),
74 };
75 if busy {
76 let events: Vec<&Block> = children.iter().filter(|c| c.name == "VEVENT").collect();
77 push_busy(&mut body, &events);
78 } else {
79 for c in children.iter().filter(|c| c.name != "VTIMEZONE") {
80 push_lines(&mut body, &c.lines);
81 }
82 }
83 }
84 }
85 // RFC 7986's REFRESH-INTERVAL, and Outlook's own name for it.
86 let mut out = format!(
87 "BEGIN:VCALENDAR\r\nVERSION:2.0\r\n{PRODID}\r\n\
88 REFRESH-INTERVAL;VALUE=DURATION:PT1H\r\nX-PUBLISHED-TTL:PT1H\r\n"
89 );
90 // RFC 7986's NAME, and the X-WR-CALNAME most clients read instead.
91 if let Some(name) = name {
92 let name = escape_text(name);
93 out.push_str(&fold(&format!("NAME:{name}"), "\r\n"));
94 out.push_str(&fold(&format!("X-WR-CALNAME:{name}"), "\r\n"));
95 }
96 out + &zones + &body + "END:VCALENDAR\r\n"
97}
98
99/// Every vCard of `objects`, one after the other.
100pub fn cards(objects: &[&str]) -> String {
101 let mut out = String::new();
102 for object in objects {
103 for card in top_blocks(object, "VCARD")
104 .into_iter()
105 .filter(Block::complete)
106 {
107 push_lines(&mut out, &card.lines);
108 }
109 }
110 out
111}
112
113/// The display name and color a calendar file names for itself: RFC 7986's
114/// NAME and COLOR, or the X-WR-CALNAME and X-APPLE-CALENDAR-COLOR most
115/// exports carry instead. The color is as written; the caller checks it.
116pub fn calendar_meta(text: &str) -> (Option<String>, Option<String>) {
117 let Some(cal) = top_blocks(text, "VCALENDAR").into_iter().next() else {
118 return (None, None);
119 };
120 let first = |props: &[&str]| {
121 props
122 .iter()
123 .find_map(|p| cal.prop(p))
124 .map(|v| v.trim().to_string())
125 .filter(|v| !v.is_empty())
126 };
127 (
128 first(&["NAME", "X-WR-CALNAME"]),
129 first(&["COLOR", "X-APPLE-CALENDAR-COLOR"]),
130 )
131}
132
133/// Splits an iCalendar file into calendar object resources: one per UID,
134/// overrides with their master, each with the VTIMEZONEs it names. Keeps
135/// VERSION, PRODID and CALSCALE of the file and drops the other calendar
136/// properties, METHOD among them. A component without UID gets
137/// `new_uid(its text)`, and so does a component type that reuses the UID of
138/// another type, or a second master of a UID: one object holds one type and
139/// one master. `None` when the objects
140/// would pass `max_bytes` together: each one repeats the zones it names.
141pub fn split_calendar(
142 text: &str,
143 new_uid: &mut dyn FnMut(&str) -> String,
144 max_bytes: usize,
145) -> Option<Vec<String>> {
146 let mut header: Option<Vec<String>> = None;
147 let mut zones: HashMap<String, String> = HashMap::new();
148 // (uid, components, TZIDs they name), in file order.
149 let mut groups: Vec<(String, String, HashSet<String>)> = Vec::new();
150 // By the UID in the file and the component type.
151 let mut by_uid: HashMap<(String, String), usize> = HashMap::new();
152 let mut file_uids: HashSet<String> = HashSet::new();
153 // The masters of each file UID and component type, as group indexes. A
154 // second master starts an object of its own.
155 let mut masters: HashMap<(String, String), Vec<usize>> = HashMap::new();
156 // Overrides wait until every master is known: each joins the master that
157 // has its instance.
158 let mut overrides: Vec<(Block, HashSet<String>)> = Vec::new();
159 // A file that holds the same component twice keeps one copy.
160 let mut seen: HashSet<String> = HashSet::new();
161 let mut add = |groups: &mut Vec<(String, String, HashSet<String>)>,
162 c: &Block,
163 tzids: HashSet<String>,
164 group: Option<usize>| {
165 let file_uid = c.prop("UID").filter(|u| !u.trim().is_empty());
166 // An override without its master joins the others of its UID.
167 let group = group.or_else(|| match (c.prop("RECURRENCE-ID"), &file_uid) {
168 (Some(_), Some(u)) => by_uid.get(&(u.clone(), c.name.clone())).copied(),
169 _ => None,
170 });
171 let uid = match (&file_uid, group) {
172 (_, Some(i)) => groups[i].0.clone(),
173 (Some(u), None) if !file_uids.contains(u) => u.clone(),
174 _ => new_uid(&lines_text(&c.lines)),
175 };
176 let text = match file_uid.as_ref() == Some(&uid) {
177 true => lines_text(&c.lines),
178 false => {
179 let rest = without_own(&c.lines, "UID");
180 let mut text = String::new();
181 push_lines(&mut text, &rest[..1]);
182 text.push_str(&format!("UID:{uid}\r\n"));
183 push_lines(&mut text, &rest[1..]);
184 text
185 }
186 };
187 match group {
188 Some(i) => {
189 groups[i].1.push_str(&text);
190 groups[i].2.extend(tzids);
191 i
192 }
193 None => {
194 let key = file_uid.unwrap_or_else(|| uid.clone());
195 file_uids.insert(key.clone());
196 by_uid.entry((key, c.name.clone())).or_insert(groups.len());
197 groups.push((uid, text, tzids));
198 groups.len() - 1
199 }
200 }
201 };
202 for cal in top_blocks(text, "VCALENDAR") {
203 let (props, children) = split_level(inner(&cal.lines));
204 header.get_or_insert_with(|| {
205 props
206 .iter()
207 .filter(|l| ["VERSION", "PRODID", "CALSCALE"].contains(&name(l).as_str()))
208 .map(|l| l.to_string())
209 .collect()
210 });
211 for c in children.into_iter().filter(Block::complete) {
212 if c.name == "VTIMEZONE" {
213 if let Some(id) = c.prop("TZID") {
214 zones.entry(id).or_insert_with(|| lines_text(&c.lines));
215 }
216 continue;
217 }
218 let tzids: HashSet<String> = c
219 .lines
220 .iter()
221 .filter_map(|l| param(&unfold(l), "TZID"))
222 .collect();
223 if !seen.insert(lines_text(&c.lines)) {
224 continue;
225 }
226 let file_uid = c.prop("UID").filter(|u| !u.trim().is_empty());
227 match (c.prop("RECURRENCE-ID").is_some(), file_uid) {
228 (true, Some(_)) => overrides.push((c, tzids)),
229 (_, file_uid) => {
230 let at = add(&mut groups, &c, tzids, None);
231 if let Some(u) = file_uid {
232 masters.entry((u, c.name.clone())).or_default().push(at);
233 }
234 }
235 }
236 }
237 }
238 let zones_of = |ids: &HashSet<String>| -> String {
239 ids.iter()
240 .filter_map(|id| zones.get(id))
241 .map(String::as_str)
242 .collect()
243 };
244 for (c, tzids) in overrides {
245 let key = (c.prop("UID").unwrap_or_default(), c.name.clone());
246 let group = match masters.get(&key).map(Vec::as_slice) {
247 Some([only]) => Some(*only),
248 Some(list) => {
249 let rid = instant(
250 &(zones_of(&tzids) + &lines_text(&c.lines)),
251 ICalendarProperty::RecurrenceId,
252 );
253 // ponytail: the master that started last before the instance.
254 // Interleaved series of one UID can pick wrong; expanding each
255 // candidate is exact but costs a full expansion per override.
256 list.iter()
257 .copied()
258 .filter_map(|i| {
259 let (_, text, ids) = &groups[i];
260 let start = instant(&(zones_of(ids) + text), ICalendarProperty::Dtstart)?;
261 Some((start, i)).filter(|(s, _)| rid.is_some_and(|r| *s <= r))
262 })
263 .max()
264 .map(|(_, i)| i)
265 .or(list.first().copied())
266 }
267 None => None,
268 };
269 add(&mut groups, &c, tzids, group);
270 }
271 let mut head = String::from("BEGIN:VCALENDAR\r\n");
272 let header = header.unwrap_or_default();
273 if !header.iter().any(|l| name(l) == "VERSION") {
274 head.push_str("VERSION:2.0\r\n");
275 }
276 if !header.iter().any(|l| name(l) == "PRODID") {
277 head.push_str(PRODID);
278 head.push_str("\r\n");
279 }
280 for l in &header {
281 push_lines(&mut head, &[l.as_str()]);
282 }
283 const END: &str = "END:VCALENDAR\r\n";
284 let size = |(_, components, tzids): &(String, String, HashSet<String>)| {
285 let zones: usize = tzids
286 .iter()
287 .filter_map(|id| zones.get(id))
288 .map(String::len)
289 .sum();
290 head.len() + zones + components.len() + END.len()
291 };
292 if groups.iter().map(size).sum::<usize>() > max_bytes {
293 return None;
294 }
295 let objects = groups
296 .into_iter()
297 .map(|(_, components, tzids)| {
298 let mut out = head.clone();
299 let mut tzids: Vec<_> = tzids.into_iter().collect();
300 tzids.sort();
301 for id in tzids {
302 if let Some(z) = zones.get(&id) {
303 out.push_str(z);
304 }
305 }
306 out + &components + END
307 })
308 .collect();
309 Some(objects)
310}
311
312/// Splits a vCard file into one text per card. A card without UID gets
313/// `new_uid(its text)`, inserted before its END line: vCard 4.0 wants
314/// VERSION right after BEGIN.
315pub fn split_cards(text: &str, new_uid: &mut dyn FnMut(&str) -> String) -> Vec<String> {
316 top_blocks(text, "VCARD")
317 .into_iter()
318 .filter(Block::complete)
319 .map(|card| {
320 let text = lines_text(&card.lines);
321 if card.prop("UID").is_some_and(|u| !u.trim().is_empty()) {
322 return text;
323 }
324 let lines = without_own(&card.lines, "UID");
325 let (body, end) = match lines.split_last() {
326 Some((last, body)) if name(last) == "END" => (body, Some(*last)),
327 _ => (&lines[..], None),
328 };
329 let mut out = String::new();
330 push_lines(&mut out, body);
331 out.push_str(&format!("UID:{}\r\n", new_uid(&text)));
332 if let Some(end) = end {
333 push_lines(&mut out, &[end]);
334 }
335 out
336 })
337 .collect()
338}
339
340/// A component as its logical lines, BEGIN and END included.
341struct Block<'a> {
342 /// Upper case.
343 name: String,
344 lines: Vec<&'a str>,
345}
346
347impl Block<'_> {
348 /// Not cut off by the end of the text.
349 fn complete(&self) -> bool {
350 let depth = self.lines.iter().fold(0i64, |d, l| match name(l).as_str() {
351 "BEGIN" => d + 1,
352 "END" => d - 1,
353 _ => d,
354 });
355 self.lines.len() > 1 && depth == 0
356 }
357
358 /// The value of the first own property called `prop`, not one of a
359 /// nested component, unfolded and unescaped as TEXT.
360 fn prop(&self, prop: &str) -> Option<String> {
361 split_level(inner(&self.lines))
362 .0
363 .into_iter()
364 .find(|l| name(l) == prop)
365 .map(|l| unescape_text(value(&unfold(l))))
366 }
367}
368
369/// A component's lines without its own `prop` lines. Nested components keep
370/// theirs.
371fn without_own<'a>(lines: &[&'a str], prop: &str) -> Vec<&'a str> {
372 let mut depth = 0usize;
373 let mut out = Vec::with_capacity(lines.len());
374 for &line in lines {
375 let n = name(line);
376 match n.as_str() {
377 "BEGIN" => depth += 1,
378 "END" => depth = depth.saturating_sub(1),
379 _ if depth == 1 && n == prop => continue,
380 _ => {}
381 }
382 out.push(line);
383 }
384 out
385}
386
387/// `lines` without their BEGIN and END line.
388fn inner<'a, 'b>(lines: &'b [&'a str]) -> &'b [&'a str] {
389 let start = usize::from(lines.first().is_some_and(|l| name(l) == "BEGIN"));
390 let end =
391 lines.len() - usize::from(lines.len() > start && name(lines[lines.len() - 1]) == "END");
392 &lines[start..end]
393}
394
395/// The top-level components called `outer`.
396fn top_blocks<'a>(text: &'a str, outer: &str) -> Vec<Block<'a>> {
397 split_level(&logical_lines(text))
398 .1
399 .into_iter()
400 .filter(|b| b.name == outer)
401 .collect()
402}
403
404/// The properties of this level and its components. A component cut off by
405/// the end of the text is kept, without END.
406fn split_level<'a>(lines: &[&'a str]) -> (Vec<&'a str>, Vec<Block<'a>>) {
407 let mut props = Vec::new();
408 let mut blocks: Vec<Block<'a>> = Vec::new();
409 let mut depth = 0usize;
410 for &line in lines {
411 let n = name(line);
412 if depth == 0 {
413 if n == "BEGIN" {
414 blocks.push(Block {
415 name: value(&unfold(line)).trim().to_ascii_uppercase(),
416 lines: vec![line],
417 });
418 depth = 1;
419 } else if !n.is_empty() {
420 props.push(line);
421 }
422 continue;
423 }
424 blocks.last_mut().expect("depth > 0").lines.push(line);
425 match n.as_str() {
426 "BEGIN" => depth += 1,
427 "END" => depth -= 1,
428 _ => {}
429 }
430 }
431 (props, blocks)
432}
433
434fn push_lines(out: &mut String, lines: &[&str]) {
435 for line in lines {
436 for physical in line.split_inclusive('\n') {
437 out.push_str(physical.trim_end_matches(['\r', '\n']));
438 out.push_str("\r\n");
439 }
440 }
441}
442
443fn lines_text(lines: &[&str]) -> String {
444 let mut out = String::new();
445 push_lines(&mut out, lines);
446 out
447}
448
449fn is_private(c: &Block) -> bool {
450 c.prop("CLASS").is_some_and(|v| {
451 v.eq_ignore_ascii_case("PRIVATE") || v.eq_ignore_ascii_case("CONFIDENTIAL")
452 })
453}
454
455/// Transparent and cancelled events block no time.
456fn blocks_time(e: &Block) -> bool {
457 !(e.prop("TRANSP")
458 .is_some_and(|v| v.eq_ignore_ascii_case("TRANSPARENT"))
459 || e.prop("STATUS")
460 .is_some_and(|v| v.eq_ignore_ascii_case("CANCELLED")))
461}
462
463/// The events of one object as busy time. Events that block no time are
464/// left out; a left-out override becomes an EXDATE of its master, so its
465/// instance stays free. A free THISANDFUTURE override stays, so it frees the
466/// later instances too. The UID becomes a hash of it, because UIDs often
467/// hold host names or mail addresses.
468fn push_busy(out: &mut String, events: &[&Block]) {
469 // A free master stays, as free, when a busy range override needs its rule.
470 let busy_range = events.iter().any(|e| ranged(e) && blocks_time(e));
471 let (kept, free): (Vec<&&Block>, Vec<&&Block>) = events.iter().partition(|e| {
472 blocks_time(e) || ranged(e) || (busy_range && e.prop("RECURRENCE-ID").is_none())
473 });
474 let mut exdates: Vec<String> = free
475 .iter()
476 .filter_map(|e| {
477 let (props, _) = split_level(inner(&e.lines));
478 props
479 .into_iter()
480 .find(|l| name(l) == "RECURRENCE-ID")
481 .map(exdate)
482 })
483 .collect();
484 // One master takes them. A second master of the same UID is invalid.
485 for e in kept {
486 out.push_str("BEGIN:VEVENT\r\nSUMMARY:Busy\r\n");
487 let (props, _) = split_level(inner(&e.lines));
488 let master = !props.iter().any(|l| name(l) == "RECURRENCE-ID");
489 for line in props {
490 match name(line).as_str() {
491 "UID" => {
492 let uid = unescape_text(value(&unfold(line)));
493 let hash = Sha256::digest(uid.as_bytes());
494 let hex: String = hash[..16].iter().map(|b| format!("{b:02x}")).collect();
495 out.push_str(&format!("UID:{hex}\r\n"));
496 }
497 n if BUSY_PROPS.contains(&n) => push_lines(out, &[line]),
498 _ => {}
499 }
500 }
501 if master {
502 for x in std::mem::take(&mut exdates) {
503 out.push_str(&x);
504 }
505 }
506 out.push_str("END:VEVENT\r\n");
507 }
508}
509
510/// Whether `e` overrides an instance and all later ones.
511fn ranged(e: &Block) -> bool {
512 split_level(inner(&e.lines)).0.into_iter().any(|l| {
513 name(l) == "RECURRENCE-ID"
514 && param(&unfold(l), "RANGE").is_some_and(|r| r.eq_ignore_ascii_case("THISANDFUTURE"))
515 })
516}
517
518/// A RECURRENCE-ID line as the EXDATE of that instance, without RANGE.
519fn exdate(rid: &str) -> String {
520 let line = unfold(rid);
521 let start = value_start(&line);
522 let params: String = param_parts(&line)
523 .into_iter()
524 .filter(|p| !p.trim().to_ascii_uppercase().starts_with("RANGE="))
525 .map(|p| format!(";{p}"))
526 .collect();
527 format!("EXDATE{params}:{}\r\n", &line[start..])
528}
529
530/// The first `prop` of the components in `body`, with the VTIMEZONEs it
531/// names, as an instant.
532fn instant(body: &str, prop: ICalendarProperty) -> Option<DateTime<Utc>> {
533 let cal = ICalendar::parse(format!(
534 "BEGIN:VCALENDAR\r\nVERSION:2.0\r\n{body}END:VCALENDAR\r\n"
535 ))
536 .ok()?;
537 let zones = Zones::new(&cal, Zone::Utc);
538 let e = cal
539 .components
540 .iter()
541 .filter(|c| {
542 !matches!(
543 c.component_type,
544 ICalendarComponentType::VTimezone
545 | ICalendarComponentType::Standard
546 | ICalendarComponentType::Daylight
547 )
548 })
549 .find_map(|c| c.property(&prop))?;
550 stamp(&zones, e.values.first()?.as_partial_date_time()?, e.tz_id()).map(|s| s.utc())
551}
552