pim_schedule.rs
⎇
Raw
1//! Implicit scheduling (RFC 6638) between the principals of this server.
2//!
3//! `pimdav::itip` decides what a change sends to whom. This module finds the
4//! recipients and their objects, and turns every message into writes that
5//! commit together with the change itself. Nothing leaves the server: an
6//! address outside it gets a delivery failure in its SCHEDULE-STATUS.
7//!
8//! Rooms and resources answer at once, from their own bookings. The outbox
9//! answers free-busy requests from the recipients' calendars.
10
11use chrono::{DateTime, Utc};
12use percent_encoding::percent_decode_str;
13use pimdav::calcard::icalendar::{ICalendar, ICalendarComponentType};
14use pimdav::filter::TimeRange;
15use pimdav::freebusy::{self, Period};
16use pimdav::itip::{self, Message, Method, Role};
17use pimdav::principal::UserType;
18use pimdav::xml::{CALDAV, el, hrefs, with_children};
19use pimdav::zone::{self, Zone};
20use sha2::{Digest, Sha256};
21use tokio::sync::Mutex;
22use xmltree::Element;
23
24use super::pim::{
25 INBOX, MAIL_DOMAIN, OUTBOX, collection_href, etag_of, local_part, need_privilege,
26 principal_name, principal_uuid, seg,
27};
28use crate::api::common::blocking;
29use crate::db::{PimKind, PimObject, PimOp, PimPrincipal};
30use crate::error::{ApiError, AppState};
31
32/// Held from reading a calendar object to committing the change, so that a
33/// change and the writes it causes see a consistent store.
34// ponytail: one lock for every object write. Per-UID locks if write
35// throughput ever matters.
36pub(crate) static LOCK: Mutex<()> = Mutex::const_new(());
37
38/// The delivery status codes of RFC 6638, 3.2.9.
39const DELIVERED: &str = "1.2";
40/// An address in this server's domains that names no one.
41const INVALID_USER: &str = "3.7";
42/// An address outside this server: there is no iMIP to reach it.
43const NO_ROUTE: &str = "5.2";
44/// The recipient has no calendar for the component.
45const REFUSED: &str = "5.3";
46/// The recipient holds an object with this UID that is not its copy of the
47/// sender's meeting (RFC 6638: no scheduling privileges).
48const NO_AUTHORITY: &str = "3.8";
49
50/// Who writes into a calendar, as far as scheduling cares.
51pub(crate) struct Writer<'a> {
52 pub owner: &'a PimPrincipal,
53 /// May send messages as the owner (RFC 6638 `schedule-send`).
54 pub may_schedule: bool,
55 /// The writer's address when it is not the owner, for SENT-BY.
56 pub sent_by: Option<String>,
57}
58
59impl Writer<'_> {
60 /// The owner itself.
61 pub(crate) fn owner(owner: &PimPrincipal) -> Writer<'_> {
62 Writer {
63 owner,
64 may_schedule: true,
65 sent_by: None,
66 }
67 }
68
69 /// 403 `need-privileges` on the owner's outbox.
70 fn refused(&self, privilege: &str) -> Element {
71 let outbox = collection_href(&self.owner.name, PimKind::Calendar, OUTBOX, None);
72 need_privilege(&outbox, CALDAV, privilege)
73 }
74}
75
76/// Every principal, for mapping calendar user addresses.
77#[derive(Clone)]
78pub(crate) struct Directory(Vec<PimPrincipal>);
79
80enum Recipient<'a> {
81 Local(&'a PimPrincipal),
82 Unknown,
83 External,
84}
85
86fn found(p: Option<&PimPrincipal>) -> Recipient<'_> {
87 match p {
88 Some(p) => Recipient::Local(p),
89 None => Recipient::Unknown,
90 }
91}
92
93impl Directory {
94 pub(crate) async fn load(state: &AppState) -> Result<Self, ApiError> {
95 Ok(Directory(state.db.pim_principals().await?))
96 }
97
98 pub(crate) fn get(&self, id: i64) -> Option<&PimPrincipal> {
99 self.0.iter().find(|p| p.id == id)
100 }
101
102 /// The forms `calendar-user-address-set` lists: the mailto address, the
103 /// principal URL and the `urn:uuid:`. Compared without case.
104 fn resolve(&self, addr: &str) -> Recipient<'_> {
105 let addr = addr.trim();
106 let lower = addr.to_ascii_lowercase();
107 if let Some(rest) = lower.strip_prefix("mailto:") {
108 let Some((local, domain)) = rest.rsplit_once('@') else {
109 return Recipient::External;
110 };
111 let kind = match domain.strip_suffix(MAIL_DOMAIN) {
112 Some("") => UserType::Individual,
113 Some("rooms.") => UserType::Room,
114 Some("resources.") => UserType::Resource,
115 // The tombstone of a deleted principal.
116 Some("deleted.") => return Recipient::Unknown,
117 _ => return Recipient::External,
118 };
119 let name = percent_decode_str(local).decode_utf8_lossy();
120 return found(
121 self.0
122 .iter()
123 .find(|p| p.kind == kind && p.name.eq_ignore_ascii_case(&name)),
124 );
125 }
126 if let Some(uuid) = lower.strip_prefix("urn:uuid:") {
127 return found(self.0.iter().find(|p| principal_uuid(p.id) == uuid));
128 }
129 match principal_name(addr) {
130 Some(name) => found(self.0.iter().find(|p| p.name.eq_ignore_ascii_case(&name))),
131 None if lower.starts_with('/') || lower.starts_with("http") => Recipient::Unknown,
132 None => Recipient::External,
133 }
134 }
135
136 /// Whether an address names principal `id`.
137 pub(crate) fn is(&self, id: i64) -> impl Fn(&str) -> bool + '_ {
138 move |a: &str| matches!(self.resolve(a), Recipient::Local(p) if p.id == id)
139 }
140}
141
142/// The writes that make other principals' objects forget `gone` before it
143/// is deleted. Its addresses become a tombstone in `deleted.` of the mail
144/// domain, which names no one, so a later principal of the same name gets
145/// nothing meant for the old one. Commit them together with the delete,
146/// holding [`LOCK`].
147pub(crate) async fn forget(state: &AppState, gone: &PimPrincipal) -> Result<Vec<PimOp>, ApiError> {
148 let dir = Directory(vec![gone.clone()]);
149 let is_gone = dir.is(gone.id);
150 let encoded = local_part(&gone.name);
151 let tombstone = format!("mailto:{encoded}-{}@deleted.{MAIL_DOMAIN}", gone.id);
152 let uuid = principal_uuid(gone.id);
153 let needles = [gone.name.as_str(), encoded.as_str(), uuid.as_str()];
154 let mut ops = Vec::new();
155 for (collection_id, obj, data) in state.db.pim_objects_mentioning(gone.id, &needles).await? {
156 let Some(new) = itip::forget(&String::from_utf8_lossy(&data), &is_gone, &tombstone) else {
157 continue;
158 };
159 let data = new.into_bytes();
160 ops.push(PimOp::Put {
161 collection_id,
162 obj: PimObject {
163 etag: etag_of(&data),
164 ..obj
165 },
166 data,
167 });
168 }
169 Ok(ops)
170}
171
172/// What a PUT of a calendar object stores, and what else it writes.
173pub(crate) struct Stored {
174 pub data: Vec<u8>,
175 /// Whether `data` differs from the request body.
176 pub changed: bool,
177 pub schedule_tag: Option<String>,
178 pub ops: Vec<PimOp>,
179}
180
181/// A PUT of `body` over `old` into collection `at.0` under the name `at.1`.
182/// `Err` names a failed scheduling precondition.
183pub(crate) async fn put(
184 state: &AppState,
185 dir: &Directory,
186 w: &Writer<'_>,
187 at: (i64, &str),
188 old: Option<&[u8]>,
189 body: &[u8],
190) -> Result<Result<Stored, Element>, ApiError> {
191 let parse = |b: &[u8]| ICalendar::parse(String::from_utf8_lossy(b).as_ref()).ok();
192 let Some(sent) = parse(body) else {
193 return Ok(Ok(unchanged(body, None)));
194 };
195 let owner = w.owner;
196 let owns = dir.is(owner.id);
197 let role = match itip::role(&sent, &owns) {
198 Ok(r) => r,
199 Err(refused) => return Ok(Err(refused.condition())),
200 };
201 if role != Role::None
202 && let Some(holder) = elsewhere(state, owner, &sent, at).await?
203 {
204 return Ok(Err(holder));
205 }
206 let old = old.and_then(parse);
207 let old_role = old.as_ref().and_then(|o| itip::role(o, &owns).ok());
208 let now = Utc::now();
209 let mut ops = Vec::new();
210
211 let stored = match (role, old_role) {
212 (Role::Organizer, _) => {
213 let old = old.as_ref().filter(|_| old_role == Some(Role::Organizer));
214 let (mut store, force) = itip::prepare(old, &sent, &owns);
215 let mut messages = itip::messages(old, Some(&store), &owns, &force, now);
216 if !messages.is_empty() {
217 if !w.may_schedule {
218 return Ok(Err(w.refused("schedule-send-invite")));
219 }
220 itip::stamp_sender(&mut store, &owns, w.sent_by.as_deref());
221 messages = itip::messages(old, Some(&store), &owns, &force, now);
222 }
223 // Rooms answer first, so the others' copies carry their answers.
224 if answer_rooms(state, dir, owner, &mut store, &messages, &mut ops, now).await? {
225 messages = itip::messages(old, Some(&store), &owns, &force, now);
226 }
227 for m in &messages {
228 if let Some(status) = deliver(state, dir, owner, m, &mut ops).await? {
229 itip::set_attendee_status(&mut store, &m.to, status);
230 }
231 }
232 store
233 }
234 (Role::Attendee, Some(Role::Attendee)) => {
235 let old = old.as_ref().expect("an attendee role needs the old object");
236 let (mut store, reply) = match itip::attend(old, sent.clone(), &owns, now) {
237 Ok(v) => v,
238 Err(refused) => return Ok(Err(refused.condition())),
239 };
240 if let Some(mut reply) = reply {
241 if !w.may_schedule {
242 return Ok(Err(w.refused("schedule-send-reply")));
243 }
244 itip::stamp_sender(&mut store, &owns, w.sent_by.as_deref());
245 itip::stamp_sender(&mut reply.cal, &owns, w.sent_by.as_deref());
246 let status = reply_to(state, dir, owner, &reply, &mut ops).await?;
247 itip::set_organizer_status(&mut store, status);
248 }
249 store
250 }
251 // No longer a scheduling object, or a copy the attendee brings in
252 // itself (RFC 6638, 3.2.2.2): stored as sent.
253 (_, previous) => {
254 if let Some(old) = &old {
255 match removed(state, dir, w, old, previous, true).await? {
256 Ok(more) => ops.extend(more),
257 Err(refused) => return Ok(Err(refused)),
258 }
259 }
260 let tag = (role != Role::None).then(|| etag_of(body));
261 return Ok(Ok(Stored {
262 ops,
263 ..unchanged(body, tag)
264 }));
265 }
266 };
267 let changed = stored != sent;
268 let data = match changed {
269 true => stored.to_string().into_bytes(),
270 false => body.to_vec(),
271 };
272 Ok(Ok(Stored {
273 schedule_tag: Some(etag_of(&data)),
274 data,
275 changed,
276 ops,
277 }))
278}
279
280/// The Schedule-Tag an import stores with `body`, `None` for an object that
281/// schedules nothing. An import sends no messages: the object is stored as
282/// sent. `Err` names the precondition that refuses it.
283pub(crate) async fn import_tag(
284 state: &AppState,
285 dir: &Directory,
286 owner: &PimPrincipal,
287 at: (i64, &str),
288 body: &[u8],
289) -> Result<Result<Option<String>, Element>, ApiError> {
290 let Ok(cal) = ICalendar::parse(String::from_utf8_lossy(body).as_ref()) else {
291 return Ok(Ok(None));
292 };
293 match itip::role(&cal, &dir.is(owner.id)) {
294 Err(refused) => Ok(Err(refused.condition())),
295 Ok(Role::None) => Ok(Ok(None)),
296 Ok(_) => Ok(match elsewhere(state, owner, &cal, at).await? {
297 Some(holder) => Err(holder),
298 None => Ok(Some(etag_of(body))),
299 }),
300 }
301}
302
303/// The resource name the server picks for an object it creates.
304pub(crate) fn object_name(uid: &str, kind: PimKind) -> String {
305 let ext = match kind {
306 PimKind::Calendar => "ics",
307 PimKind::AddressBook => "vcf",
308 };
309 format!("{}.{ext}", &crate::hex(&Sha256::digest(uid))[..32])
310}
311
312fn unchanged(body: &[u8], schedule_tag: Option<String>) -> Stored {
313 Stored {
314 data: body.to_vec(),
315 changed: false,
316 schedule_tag,
317 ops: Vec::new(),
318 }
319}
320
321/// The writes a DELETE of `old` causes. `reply` is false for
322/// `Schedule-Reply: F` (RFC 6638, 8.1). `Err` names a lacking privilege.
323pub(crate) async fn delete(
324 state: &AppState,
325 dir: &Directory,
326 w: &Writer<'_>,
327 old: &[u8],
328 reply: bool,
329) -> Result<Result<Vec<PimOp>, Element>, ApiError> {
330 let Ok(old) = ICalendar::parse(String::from_utf8_lossy(old).as_ref()) else {
331 return Ok(Ok(Vec::new()));
332 };
333 let role = itip::role(&old, &dir.is(w.owner.id)).ok();
334 removed(state, dir, w, &old, role, reply).await
335}
336
337/// An organizer object going away cancels; an attendee copy declines.
338async fn removed(
339 state: &AppState,
340 dir: &Directory,
341 w: &Writer<'_>,
342 old: &ICalendar,
343 role: Option<Role>,
344 reply: bool,
345) -> Result<Result<Vec<PimOp>, Element>, ApiError> {
346 let owner = w.owner;
347 let owns = dir.is(owner.id);
348 let now = Utc::now();
349 let mut old = old.clone();
350 itip::stamp_sender(&mut old, &owns, w.sent_by.as_deref());
351 let mut ops = Vec::new();
352 match role {
353 Some(Role::Organizer) => {
354 let (_, messages) = itip::organize(Some(&old), None, &owns, now);
355 if !messages.is_empty() && !w.may_schedule {
356 return Ok(Err(w.refused("schedule-send-invite")));
357 }
358 for m in &messages {
359 deliver(state, dir, owner, m, &mut ops).await?;
360 }
361 }
362 Some(Role::Attendee) if reply => {
363 if let Some(m) = itip::decline(&old, &owns, now) {
364 if !w.may_schedule {
365 return Ok(Err(w.refused("schedule-send-reply")));
366 }
367 reply_to(state, dir, owner, &m, &mut ops).await?;
368 }
369 }
370 _ => {}
371 }
372 Ok(Ok(ops))
373}
374
375/// The resource of the owner that already schedules this UID elsewhere:
376/// RFC 6638 allows one per UID (3.2.4.1).
377async fn elsewhere(
378 state: &AppState,
379 owner: &PimPrincipal,
380 cal: &ICalendar,
381 (collection_id, name): (i64, &str),
382) -> Result<Option<Element>, ApiError> {
383 let Some((uid, _)) = identity(cal) else {
384 return Ok(None);
385 };
386 let Some((holder_id, holder, _)) = state.db.pim_find_uid(owner.id, &uid).await? else {
387 return Ok(None);
388 };
389 if holder_id == collection_id && holder.name == name {
390 return Ok(None);
391 }
392 let slug = match state.db.pim_collection_by_id(holder_id).await? {
393 Some((_, _, c)) => c.slug,
394 None => return Ok(None),
395 };
396 let href = collection_href(&owner.name, PimKind::Calendar, &slug, None) + &seg(&holder.name);
397 Ok(Some(with_children(
398 el(CALDAV, "unique-scheduling-object-resource"),
399 hrefs([href.as_str()]),
400 )))
401}
402
403/// Rooms and resources answer their invitations at once: accepted where
404/// free, declined where their bookings overlap. The answers go into the
405/// organizer's `store` and inbox. Returns whether any room answered.
406async fn answer_rooms(
407 state: &AppState,
408 dir: &Directory,
409 organizer: &PimPrincipal,
410 store: &mut ICalendar,
411 messages: &[Message],
412 ops: &mut Vec<PimOp>,
413 now: DateTime<Utc>,
414) -> Result<bool, ApiError> {
415 let mut answered = false;
416 for m in messages
417 .iter()
418 .filter(|m| m.method == Method::Request && !m.quiet)
419 {
420 let Recipient::Local(room) = dir.resolve(&m.to) else {
421 continue;
422 };
423 let Some((uid, component)) = identity(&m.cal) else {
424 continue;
425 };
426 if room.kind == UserType::Individual {
427 continue;
428 }
429 let Some(calendar) = state.db.pim_calendar_for(room.id, &component).await? else {
430 continue;
431 };
432 let Ok(copy) = copy_of(state, dir, room, organizer, &uid).await? else {
433 continue;
434 };
435 let Some(received) = itip::receive(copy.as_ref().map(|(_, _, c)| c), m) else {
436 continue;
437 };
438 let is_room = dir.is(room.id);
439 let window = now..now + itip::answer_horizon(&received);
440 let taken = busy_of(state, dir, room, &window, Some(&uid)).await?;
441 let floating = floating_of(calendar.timezone.as_deref());
442 let answer = itip::auto_answer(&received, &is_room, &taken, &window, &floating);
443 let Ok((_, Some(reply))) = itip::attend(&received, answer, &is_room, now) else {
444 continue;
445 };
446 answered |= itip::apply_reply(store, &reply.cal, &is_room);
447 ops.push(inbox(organizer, &reply, &component));
448 }
449 Ok(answered)
450}
451
452/// The busy time a principal shows to scheduling: its opaque calendars that
453/// take events, never the inbox. Objects with UID `skip` do not count.
454// ponytail: reads every object of those calendars per call. Keep busy
455// periods in a table if principals grow large calendars.
456pub(crate) async fn busy_of(
457 state: &AppState,
458 dir: &Directory,
459 p: &PimPrincipal,
460 range: &TimeRange,
461 skip: Option<&str>,
462) -> Result<Vec<Period>, ApiError> {
463 let mut calendars = Vec::new();
464 for c in state.db.pim_collections(p.id, PimKind::Calendar).await? {
465 if c.slug == INBOX || c.transparent || !c.components.split(',').any(|x| x == "VEVENT") {
466 continue;
467 }
468 let objects = state.db.pim_objects_with_data(c.id).await?;
469 calendars.push((floating_of(c.timezone.as_deref()), objects));
470 }
471 let (dir, id, range, skip) = (dir.clone(), p.id, range.clone(), skip.map(str::to_string));
472 blocking(move || -> Result<_, ApiError> {
473 let me = dir.is(id);
474 let mut busy = Vec::new();
475 for (floating, objects) in calendars {
476 for (o, data) in objects {
477 if skip.as_deref().is_some_and(|u| u == o.uid) {
478 continue;
479 }
480 if let Ok(cal) = ICalendar::parse(String::from_utf8_lossy(&data).as_ref()) {
481 busy.extend(freebusy::busy(&cal, &range, &floating, Some(&me)));
482 }
483 }
484 }
485 Ok(freebusy::merge(busy))
486 })
487 .await
488}
489
490fn floating_of(timezone: Option<&str>) -> Zone {
491 timezone.and_then(zone::from_vtimezone).unwrap_or(Zone::Utc)
492}
493
494/// The answers to a free-busy request to an outbox (RFC 6638, 5.2): per
495/// recipient its address, the REQUEST-STATUS and the VFREEBUSY reply.
496pub(crate) async fn free_busy(
497 state: &AppState,
498 dir: &Directory,
499 req: &freebusy::Request,
500) -> Result<Vec<(String, &'static str, Option<String>)>, ApiError> {
501 let now = Utc::now();
502 let mut out = Vec::new();
503 for to in &req.attendees {
504 let (status, data) = match dir.resolve(to) {
505 Recipient::Local(p) => {
506 let busy = busy_of(state, dir, p, &req.range, None).await?;
507 ("2.0;Success", Some(freebusy::reply(&busy, req, to, now)))
508 }
509 Recipient::Unknown => ("3.7;Invalid calendar user", None),
510 Recipient::External => ("5.2;Invalid calendar service", None),
511 };
512 out.push((to.clone(), status, data));
513 }
514 Ok(out)
515}
516
517/// A REQUEST or CANCEL from `sender` into the recipient's calendar and
518/// inbox. Returns the delivery status, `None` for the sender itself.
519async fn deliver(
520 state: &AppState,
521 dir: &Directory,
522 sender: &PimPrincipal,
523 m: &Message,
524 ops: &mut Vec<PimOp>,
525) -> Result<Option<&'static str>, ApiError> {
526 let p = match dir.resolve(&m.to) {
527 Recipient::Local(p) if p.id == sender.id => return Ok(None),
528 Recipient::Local(p) => p,
529 Recipient::Unknown => return Ok(Some(INVALID_USER)),
530 Recipient::External => return Ok(Some(NO_ROUTE)),
531 };
532 ensure(state, p).await?;
533 let Some((uid, component)) = identity(&m.cal) else {
534 return Ok(Some(REFUSED));
535 };
536 let Ok(copy) = copy_of(state, dir, p, sender, &uid).await? else {
537 return Ok(Some(NO_AUTHORITY));
538 };
539 if let Some(next) = itip::receive(copy.as_ref().map(|(_, _, c)| c), m) {
540 let data = next.to_string().into_bytes();
541 let etag = etag_of(&data);
542 let (collection_id, name, schedule_tag) = match copy {
543 // Only the others' answers changed: the attendee's pending edit
544 // may still go through (RFC 6638, 3.2.10).
545 Some((id, obj, _)) => (
546 id,
547 obj.name,
548 if m.quiet {
549 obj.schedule_tag
550 } else {
551 Some(etag.clone())
552 },
553 ),
554 None => match state.db.pim_calendar_for(p.id, &component).await? {
555 Some(c) => (
556 c.id,
557 object_name(&uid, PimKind::Calendar),
558 Some(etag.clone()),
559 ),
560 None => return Ok(Some(REFUSED)),
561 },
562 };
563 ops.push(PimOp::Put {
564 collection_id,
565 obj: PimObject {
566 name,
567 uid,
568 component: component.clone(),
569 etag,
570 schedule_tag,
571 ..Default::default()
572 },
573 data,
574 });
575 }
576 if !m.quiet {
577 ops.push(inbox(p, m, &component));
578 }
579 Ok(Some(DELIVERED))
580}
581
582/// An attendee's REPLY: applied to the organizer's object, passed on to the
583/// other attendees, and left in the organizer's inbox. Returns the delivery
584/// status for the attendee's copy.
585async fn reply_to(
586 state: &AppState,
587 dir: &Directory,
588 attendee: &PimPrincipal,
589 m: &Message,
590 ops: &mut Vec<PimOp>,
591) -> Result<&'static str, ApiError> {
592 let organizer = match dir.resolve(&m.to) {
593 Recipient::Local(p) => p,
594 Recipient::Unknown => return Ok(INVALID_USER),
595 Recipient::External => return Ok(NO_ROUTE),
596 };
597 let Some((uid, component)) = identity(&m.cal) else {
598 return Ok(REFUSED);
599 };
600 // RFC 6638, 4.2: a reply to an object the organizer no longer has is
601 // ignored.
602 let Some((collection_id, obj, data)) = state.db.pim_find_uid(organizer.id, &uid).await? else {
603 return Ok(NO_ROUTE);
604 };
605 let Ok(before) = ICalendar::parse(String::from_utf8_lossy(&data).as_ref()) else {
606 return Ok(NO_ROUTE);
607 };
608 // A UID alone proves nothing: only the organizer's own object takes it.
609 if !matches!(
610 itip::role(&before, &dir.is(organizer.id)),
611 Ok(Role::Organizer)
612 ) {
613 return Ok(NO_AUTHORITY);
614 }
615 let replier = dir.is(attendee.id);
616 if !matches!(itip::role(&before, &replier), Ok(Role::Attendee)) {
617 return Ok(NO_AUTHORITY);
618 }
619 let mut after = before.clone();
620 if itip::apply_reply(&mut after, &m.cal, &replier) {
621 let data = after.to_string().into_bytes();
622 ops.push(PimOp::Put {
623 collection_id,
624 obj: PimObject {
625 etag: etag_of(&data),
626 ..obj
627 },
628 data,
629 });
630 // The others learn the new answer without a new Schedule-Tag.
631 let organizes = dir.is(organizer.id);
632 for mut other in itip::messages(Some(&before), Some(&after), &organizes, &[], Utc::now()) {
633 if other.method == Method::Request && !replier(&other.to) {
634 other.quiet = true;
635 deliver(state, dir, organizer, &other, ops).await?;
636 }
637 }
638 }
639 ops.push(inbox(organizer, m, &component));
640 Ok(DELIVERED)
641}
642
643/// Whether `copy` is `p`'s attendee copy of a meeting `organizer` runs. A
644/// message may change only that: anyone can pick any UID.
645fn attends(dir: &Directory, copy: &ICalendar, p: &PimPrincipal, organizer: &PimPrincipal) -> bool {
646 matches!(itip::role(copy, &dir.is(p.id)), Ok(Role::Attendee))
647 && itip::organizer(copy).is_some_and(dir.is(organizer.id))
648}
649
650/// `p`'s copy of the meeting with `uid` and where it is stored. `Err` if
651/// `p` holds that UID in an object the message may not touch.
652async fn copy_of(
653 state: &AppState,
654 dir: &Directory,
655 p: &PimPrincipal,
656 organizer: &PimPrincipal,
657 uid: &str,
658) -> Result<Result<Option<(i64, PimObject, ICalendar)>, ()>, ApiError> {
659 let Some((id, obj, data)) = state.db.pim_find_uid(p.id, uid).await? else {
660 return Ok(Ok(None));
661 };
662 Ok(
663 match ICalendar::parse(String::from_utf8_lossy(&data).as_ref()) {
664 Ok(c) if attends(dir, &c, p, organizer) => Ok(Some((id, obj, c))),
665 _ => Err(()),
666 },
667 )
668}
669
670async fn ensure(state: &AppState, p: &PimPrincipal) -> Result<(), ApiError> {
671 match p.kind {
672 UserType::Individual => state.db.pim_ensure_defaults(p.id).await?,
673 _ => state.db.pim_ensure_inbox(p.id).await?,
674 }
675 Ok(())
676}
677
678fn inbox(p: &PimPrincipal, m: &Message, component: &str) -> PimOp {
679 let data = m.cal.to_string().into_bytes();
680 let stamp = Utc::now().timestamp_nanos_opt().unwrap_or_default();
681 let seed = format!(
682 "{}\n{}\n{stamp}\n{:?}",
683 m.to,
684 String::from_utf8_lossy(&data),
685 m.method
686 );
687 let name = format!("{}.ics", &crate::hex(&Sha256::digest(seed))[..32]);
688 PimOp::Inbox {
689 principal_id: p.id,
690 obj: PimObject {
691 // Inbox messages share UIDs, and the store keeps UIDs unique.
692 uid: name.clone(),
693 name,
694 component: component.to_string(),
695 etag: etag_of(&data),
696 ..Default::default()
697 },
698 data,
699 }
700}
701
702/// UID and component type of a scheduling message or object.
703fn identity(cal: &ICalendar) -> Option<(String, String)> {
704 let c = cal.components.iter().find(|c| {
705 matches!(
706 c.component_type,
707 ICalendarComponentType::VEvent
708 | ICalendarComponentType::VTodo
709 | ICalendarComponentType::VJournal
710 )
711 })?;
712 Some((c.uid()?.to_string(), c.component_type.as_str().to_string()))
713}
714