Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

Read reminders from CloudKit: 705 real ones instead of 2 placeholders

Yashwanth Kamireddic14f8970ca4e
diff --git a/Cargo.lock b/Cargo.lock
index 2b216a1..2ec272e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3182,6 +3182,8 @@ dependencies = [
name = "quince-icloud"
version = "0.1.0"
dependencies = [
+ "base64 0.22.1",
+ "flate2",
"jiff",
"quince-core",
"reqwest 0.12.28",
diff --git a/crates/quince-icloud/Cargo.toml b/crates/quince-icloud/Cargo.toml
index eb1c14d..9e0ee6e 100644
--- a/crates/quince-icloud/Cargo.toml
+++ b/crates/quince-icloud/Cargo.toml
@@ -12,6 +12,8 @@ serde.workspace = true
serde_json.workspace = true
thiserror = "2"
jiff = "0.2.37"
+base64 = "0.22"
+flate2 = "1"
[dev-dependencies]
tokio = { workspace = true }
diff --git a/crates/quince-icloud/src/ckdocument.rs b/crates/quince-icloud/src/ckdocument.rs
new file mode 100644
index 0000000..c7a094b
--- /dev/null
+++ b/crates/quince-icloud/src/ckdocument.rs
@@ -0,0 +1,249 @@
+//! Reading the text out of CloudKit "document" fields.
+//!
+//! Reminders and Notes keep their title and body in fields CloudKit types as
+//! `ENCRYPTED_BYTES`. On an account without Advanced Data Protection that name is a
+//! misnomer: the payload begins `1f 8b 08` and is simply gzip. Inside is a protobuf whose
+//! text sits at field path 2 → 3 → 2.
+//!
+//! Rather than model Apple's whole document schema — which is large, undocumented, and would
+//! break on any change — this walks to that one path. Everything here treats the input as
+//! hostile: a malformed payload yields `None`, never a panic and never an allocation sized
+//! from an attacker-controlled length.
+
+use base64::{engine::general_purpose::STANDARD as B64, Engine};
+use std::io::Read;
+
+/// Where the readable text lives inside the document protobuf.
+const TEXT_PATH: &[u64] = &[2, 3, 2];
+
+/// A payload larger than this is not a reminder title or a note body; refuse it rather than
+/// decompress something hostile.
+const MAX_DECOMPRESSED: u64 = 8 * 1024 * 1024;
+
+/// Wire types we can advance past. Anything else means the bytes are not what we expect.
+const WIRE_VARINT: u64 = 0;
+const WIRE_64BIT: u64 = 1;
+const WIRE_LEN: u64 = 2;
+const WIRE_32BIT: u64 = 5;
+
+struct Reader<'a> {
+ bytes: &'a [u8],
+ at: usize,
+}
+
+impl<'a> Reader<'a> {
+ fn new(bytes: &'a [u8]) -> Self {
+ Self { bytes, at: 0 }
+ }
+
+ fn done(&self) -> bool {
+ self.at >= self.bytes.len()
+ }
+
+ /// Protobuf base-128 varint. Bounded at ten bytes so a run of continuation bits cannot
+ /// spin forever.
+ fn varint(&mut self) -> Option<u64> {
+ let mut value: u64 = 0;
+ for shift in 0..10 {
+ let byte = *self.bytes.get(self.at)?;
+ self.at += 1;
+ value |= u64::from(byte & 0x7F) << (shift * 7);
+ if byte & 0x80 == 0 {
+ return Some(value);
+ }
+ }
+ None
+ }
+
+ fn take(&mut self, n: usize) -> Option<&'a [u8]> {
+ let end = self.at.checked_add(n)?;
+ let slice = self.bytes.get(self.at..end)?;
+ self.at = end;
+ Some(slice)
+ }
+
+ /// Skip a field whose contents we do not need.
+ fn skip(&mut self, wire: u64) -> Option<()> {
+ match wire {
+ WIRE_VARINT => self.varint().map(|_| ()),
+ WIRE_64BIT => self.take(8).map(|_| ()),
+ WIRE_LEN => {
+ let n = usize::try_from(self.varint()?).ok()?;
+ self.take(n).map(|_| ())
+ }
+ WIRE_32BIT => self.take(4).map(|_| ()),
+ _ => None,
+ }
+ }
+}
+
+/// Follow `path` through nested length-delimited fields and return the bytes at the end.
+fn field_at<'a>(bytes: &'a [u8], path: &[u64]) -> Option<&'a [u8]> {
+ let Some((&wanted, rest)) = path.split_first() else {
+ return Some(bytes);
+ };
+ let mut reader = Reader::new(bytes);
+ while !reader.done() {
+ let key = reader.varint()?;
+ let (field, wire) = (key >> 3, key & 7);
+ if field == wanted && wire == WIRE_LEN {
+ let n = usize::try_from(reader.varint()?).ok()?;
+ let chunk = reader.take(n)?;
+ // The first match that resolves wins; a later sibling with the same tag is a
+ // different run of the document, not the text.
+ if let Some(found) = field_at(chunk, rest) {
+ return Some(found);
+ }
+ } else {
+ reader.skip(wire)?;
+ }
+ }
+ None
+}
+
+/// Ungzip a CloudKit document payload, refusing anything implausibly large.
+fn inflate(compressed: &[u8]) -> Option<Vec<u8>> {
+ // Not gzip: some payloads are stored plainly.
+ if compressed.len() < 3 || compressed[0] != 0x1f || compressed[1] != 0x8b {
+ return Some(compressed.to_vec());
+ }
+ let mut out = Vec::new();
+ flate2::read::GzDecoder::new(compressed)
+ .take(MAX_DECOMPRESSED)
+ .read_to_end(&mut out)
+ .ok()?;
+ Some(out)
+}
+
+/// The readable text of a CloudKit document field, or `None` if there is none to find.
+///
+/// Takes the base64 string CloudKit puts in the field's `value`.
+#[must_use]
+pub fn text_from_base64(value: &str) -> Option<String> {
+ let raw = B64.decode(value.trim()).ok()?;
+ text_from_bytes(&raw)
+}
+
+#[must_use]
+pub fn text_from_bytes(raw: &[u8]) -> Option<String> {
+ let plain = inflate(raw)?;
+ let found = field_at(&plain, TEXT_PATH)?;
+ let text = String::from_utf8(found.to_vec()).ok()?;
+ let trimmed = text.trim();
+ (!trimmed.is_empty()).then(|| trimmed.to_string())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use std::io::Write;
+
+ /// Build a length-delimited protobuf field.
+ fn field(tag: u64, payload: &[u8]) -> Vec<u8> {
+ let mut out = Vec::new();
+ let mut key = (tag << 3) | WIRE_LEN;
+ loop {
+ let mut byte = (key & 0x7F) as u8;
+ key >>= 7;
+ if key != 0 {
+ byte |= 0x80;
+ }
+ out.push(byte);
+ if key == 0 {
+ break;
+ }
+ }
+ let mut len = payload.len() as u64;
+ loop {
+ let mut byte = (len & 0x7F) as u8;
+ len >>= 7;
+ if len != 0 {
+ byte |= 0x80;
+ }
+ out.push(byte);
+ if len == 0 {
+ break;
+ }
+ }
+ out.extend_from_slice(payload);
+ out
+ }
+
+ /// A document shaped the way Apple's is, carrying `text`.
+ fn document(text: &str) -> Vec<u8> {
+ field(2, &field(3, &field(2, text.as_bytes())))
+ }
+
+ fn gzipped(bytes: &[u8]) -> Vec<u8> {
+ let mut encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
+ encoder.write_all(bytes).unwrap();
+ encoder.finish().unwrap()
+ }
+
+ #[test]
+ fn reads_text_from_a_gzipped_document() {
+ let payload = gzipped(&document("Move more"));
+ assert_eq!(
+ text_from_bytes(&payload).as_deref(),
+ Some("Move more")
+ );
+ }
+
+ /// Real titles carry emoji and accents, so the decode must be UTF-8, not ASCII.
+ #[test]
+ fn keeps_emoji_and_accents_intact() {
+ for text in [
+ "🏃 Move more",
+ "Café — déjà vu",
+ "日本語のリマインダー",
+ ] {
+ let payload = gzipped(&document(text));
+ assert_eq!(text_from_bytes(&payload).as_deref(), Some(text));
+ }
+ }
+
+ #[test]
+ fn reads_an_uncompressed_document_too() {
+ assert_eq!(
+ text_from_bytes(&document("plain")).as_deref(),
+ Some("plain")
+ );
+ }
+
+ #[test]
+ fn accepts_the_base64_cloudkit_actually_sends() {
+ let encoded = B64.encode(gzipped(&document("Milk")));
+ assert_eq!(text_from_base64(&encoded).as_deref(), Some("Milk"));
+ }
+
+ /// Anything malformed must yield None. These bytes come off the network, so a panic here
+ /// would be a crash triggered by a remote value.
+ #[test]
+ fn malformed_input_is_none_not_a_panic() {
+ let cases: Vec<Vec<u8>> = vec![
+ vec![],
+ vec![0xff; 32],
+ vec![0x1f, 0x8b, 0x08], // gzip header, no body
+ vec![0x12, 0xff, 0xff, 0xff], // length far beyond the buffer
+ vec![0x80; 64], // varint continuation forever
+ field(2, &[0x12, 0x7f]), // nested length overruns
+ document("").to_vec(), // present but empty
+ gzipped(b"not a protobuf at all"),
+ ];
+ for bytes in cases {
+ let _ = text_from_bytes(&bytes);
+ }
+ assert!(text_from_bytes(&[0x80; 64]).is_none());
+ assert!(
+ text_from_bytes(&document("")).is_none(),
+ "empty text is not text"
+ );
+ }
+
+ #[test]
+ fn a_document_without_the_text_field_is_none() {
+ // Right nesting, wrong final tag.
+ let wrong = field(2, &field(3, &field(9, b"hidden")));
+ assert!(text_from_bytes(&wrong).is_none());
+ }
+}
diff --git a/crates/quince-icloud/src/cloudkit.rs b/crates/quince-icloud/src/cloudkit.rs
new file mode 100644
index 0000000..f7b9156
--- /dev/null
+++ b/crates/quince-icloud/src/cloudkit.rs
@@ -0,0 +1,310 @@
+//! Reading a CloudKit private-database zone.
+//!
+//! Three things about this API bite immediately, and all three fail quietly:
+//!
+//! * `records/query` is refused for types Apple has not marked indexable — `Reminder` is
+//! one — so `changes/zone` is the way in. It is also the incremental path.
+//! * `desiredKeys: []` asks for *no* fields rather than all of them, which returns records
+//! that look empty.
+//! * Responses page. Ignoring `moreComing` silently truncates someone's data.
+
+use crate::{Error, Result};
+use serde_json::{json, Value};
+
+/// Records fetched in one request. Apple's own client uses a few hundred.
+const PAGE_SIZE: u64 = 200;
+
+/// A ceiling on paging, so a server that always says `moreComing` cannot spin forever.
+const MAX_PAGES: usize = 200;
+
+pub struct ZoneClient<'a> {
+ http: &'a reqwest::Client,
+ base: String,
+ container: String,
+ params: Vec<(String, String)>,
+}
+
+/// One page of zone changes.
+pub struct Page {
+ pub records: Vec<Value>,
+ pub sync_token: Option<String>,
+ pub more_coming: bool,
+}
+
+impl<'a> ZoneClient<'a> {
+ pub fn new(
+ http: &'a reqwest::Client,
+ ckdatabasews_root: &str,
+ container: &str,
+ params: Vec<(String, String)>,
+ ) -> Self {
+ Self {
+ http,
+ base: ckdatabasews_root.trim_end_matches('/').to_string(),
+ container: container.to_string(),
+ params,
+ }
+ }
+
+ fn query(&self) -> Vec<(String, String)> {
+ let mut q = self.params.clone();
+ q.push(("ckjsBuildVersion".into(), "17DProjectDev77".into()));
+ q.push(("ckjsVersion".into(), "2.6.1".into()));
+ q
+ }
+
+ /// One page of changes for `zone`.
+ pub async fn changes(&self, zone: &str, sync_token: Option<&str>) -> Result<Page> {
+ let mut zone_body = json!({
+ "zoneID": { "zoneName": zone, "zoneType": "REGULAR_CUSTOM_ZONE" },
+ "maxChanges": PAGE_SIZE,
+ });
+ if let Some(token) = sync_token {
+ zone_body["syncToken"] = Value::String(token.to_string());
+ }
+ // Note: no `desiredKeys`. Sending an empty list asks Apple for no fields at all.
+
+ let url = format!(
+ "{}/database/1/{}/production/private/changes/zone",
+ self.base, self.container
+ );
+ let response = self
+ .http
+ .post(url)
+ .query(&self.query())
+ .json(&json!({ "zones": [zone_body] }))
+ .send()
+ .await?;
+
+ let status = response.status();
+ let text = response.text().await.unwrap_or_default();
+ if status.as_u16() == 450 {
+ return Err(Error::ReauthRequired);
+ }
+ if !status.is_success() {
+ return Err(Error::Protocol(format!(
+ "CloudKit zone {zone} returned {status}: {}",
+ text.chars().take(200).collect::<String>()
+ )));
+ }
+
+ let body: Value = serde_json::from_str(&text)
+ .map_err(|e| Error::Protocol(format!("CloudKit returned invalid JSON: {e}")))?;
+ let zone_result = body
+ .get("zones")
+ .and_then(Value::as_array)
+ .and_then(|zones| zones.first())
+ .ok_or_else(|| Error::Protocol("CloudKit returned no zone".into()))?;
+
+ if let Some(reason) = zone_result.get("reason").and_then(Value::as_str) {
+ return Err(Error::Protocol(format!(
+ "CloudKit refused zone {zone}: {reason}"
+ )));
+ }
+
+ Ok(Page {
+ records: zone_result
+ .get("records")
+ .and_then(Value::as_array)
+ .cloned()
+ .unwrap_or_default(),
+ sync_token: zone_result
+ .get("syncToken")
+ .and_then(Value::as_str)
+ .map(ToString::to_string),
+ more_coming: zone_result
+ .get("moreComing")
+ .and_then(Value::as_bool)
+ .unwrap_or(false),
+ })
+ }
+
+ /// Every record in a zone, following `moreComing`.
+ ///
+ /// Returns the records and the final sync token, which a later call can resume from so a
+ /// refresh fetches only what changed.
+ pub async fn all_records(
+ &self,
+ zone: &str,
+ since: Option<&str>,
+ ) -> Result<(Vec<Value>, Option<String>)> {
+ let mut records = Vec::new();
+ let mut token = since.map(ToString::to_string);
+
+ for _ in 0..MAX_PAGES {
+ let page = self.changes(zone, token.as_deref()).await?;
+ records.extend(page.records);
+ token = page.sync_token;
+ if !page.more_coming {
+ break;
+ }
+ // A page that says "more coming" but hands back no cursor would loop forever.
+ if token.is_none() {
+ break;
+ }
+ }
+ Ok((records, token))
+ }
+}
+
+/// Field helpers. CloudKit wraps every value as `{"type": …, "value": …}`.
+pub mod field {
+ use serde_json::Value;
+
+ #[must_use]
+ pub fn string(record: &Value, name: &str) -> Option<String> {
+ record
+ .pointer(&format!("/fields/{name}/value"))?
+ .as_str()
+ .map(ToString::to_string)
+ }
+
+ #[must_use]
+ pub fn int(record: &Value, name: &str) -> Option<i64> {
+ record.pointer(&format!("/fields/{name}/value"))?.as_i64()
+ }
+
+ #[must_use]
+ pub fn flag(record: &Value, name: &str) -> bool {
+ int(record, name).unwrap_or(0) != 0
+ }
+
+ /// CloudKit timestamps are milliseconds; the library stores seconds.
+ #[must_use]
+ pub fn timestamp_seconds(record: &Value, name: &str) -> Option<i64> {
+ int(record, name).map(|ms| ms / 1000)
+ }
+
+ /// The target of a REFERENCE field, e.g. `List/A233F124-…`.
+ #[must_use]
+ pub fn reference(record: &Value, name: &str) -> Option<String> {
+ record
+ .pointer(&format!("/fields/{name}/value/recordName"))?
+ .as_str()
+ .map(ToString::to_string)
+ }
+
+ /// Text out of a document field such as `TitleDocument`.
+ #[must_use]
+ pub fn document_text(record: &Value, name: &str) -> Option<String> {
+ crate::ckdocument::text_from_base64(&string(record, name)?)
+ }
+
+ #[must_use]
+ pub fn record_name(record: &Value) -> Option<String> {
+ record.get("recordName")?.as_str().map(ToString::to_string)
+ }
+
+ #[must_use]
+ pub fn record_type(record: &Value) -> Option<String> {
+ record.get("recordType")?.as_str().map(ToString::to_string)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use wiremock::matchers::{method, path_regex};
+ use wiremock::{Mock, MockServer, ResponseTemplate};
+
+ fn record(name: &str, kind: &str) -> Value {
+ json!({
+ "recordName": name,
+ "recordType": kind,
+ "fields": { "Completed": { "type": "NUMBER_INT64", "value": 0 } }
+ })
+ }
+
+ /// Ignoring moreComing silently truncates someone's data, so paging is the thing to test.
+ #[tokio::test]
+ async fn paging_follows_more_coming_to_the_end() {
+ let server = MockServer::start().await;
+ let pages = vec![
+ json!({ "zones": [{ "records": [record("a", "Reminder")],
+ "syncToken": "t1", "moreComing": true }] }),
+ json!({ "zones": [{ "records": [record("b", "Reminder")],
+ "syncToken": "t2", "moreComing": true }] }),
+ json!({ "zones": [{ "records": [record("c", "Reminder")],
+ "syncToken": "t3", "moreComing": false }] }),
+ ];
+ for page in pages {
+ Mock::given(method("POST"))
+ .and(path_regex(r".*/changes/zone$"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(page))
+ .up_to_n_times(1)
+ .mount(&server)
+ .await;
+ }
+
+ let http = reqwest::Client::new();
+ let client = ZoneClient::new(&http, &server.uri(), "com.apple.reminders", vec![]);
+ let (records, token) = client.all_records("Reminders", None).await.unwrap();
+
+ assert_eq!(records.len(), 3, "every page must be collected");
+ assert_eq!(
+ token.as_deref(),
+ Some("t3"),
+ "the final cursor is what resumes later"
+ );
+ }
+
+ /// A server that always says more-coming without a cursor must not spin.
+ #[tokio::test]
+ async fn paging_stops_when_there_is_no_cursor() {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path_regex(r".*/changes/zone$"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
+ "zones": [{ "records": [record("a", "Reminder")], "moreComing": true }]
+ })))
+ .mount(&server)
+ .await;
+
+ let http = reqwest::Client::new();
+ let client = ZoneClient::new(&http, &server.uri(), "com.apple.reminders", vec![]);
+ let (records, _) = client.all_records("Reminders", None).await.unwrap();
+ assert_eq!(records.len(), 1);
+ }
+
+ #[tokio::test]
+ async fn a_refused_zone_is_an_error_not_an_empty_result() {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path_regex(r".*/changes/zone$"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
+ "zones": [{ "reason": "Zone does not exist" }]
+ })))
+ .mount(&server)
+ .await;
+
+ let http = reqwest::Client::new();
+ let client = ZoneClient::new(&http, &server.uri(), "com.apple.reminders", vec![]);
+ assert!(client.all_records("Missing", None).await.is_err());
+ }
+
+ #[test]
+ fn field_helpers_read_cloudkits_wrapping() {
+ let rec = json!({
+ "recordName": "Reminder/abc",
+ "recordType": "Reminder",
+ "fields": {
+ "Completed": { "type": "NUMBER_INT64", "value": 1 },
+ "CreationDate": { "type": "TIMESTAMP", "value": 1_753_808_338_928_i64 },
+ "List": { "type": "REFERENCE", "value": { "recordName": "List/xyz" } }
+ }
+ });
+ assert_eq!(field::record_name(&rec).as_deref(), Some("Reminder/abc"));
+ assert_eq!(field::record_type(&rec).as_deref(), Some("Reminder"));
+ assert!(field::flag(&rec, "Completed"));
+ assert!(
+ !field::flag(&rec, "Missing"),
+ "an absent flag reads as false"
+ );
+ assert_eq!(field::reference(&rec, "List").as_deref(), Some("List/xyz"));
+ assert_eq!(
+ field::timestamp_seconds(&rec, "CreationDate"),
+ Some(1_753_808_338),
+ "milliseconds must become seconds"
+ );
+ }
+}
diff --git a/crates/quince-icloud/src/lib.rs b/crates/quince-icloud/src/lib.rs
index 1c8ca91..47e73de 100644
--- a/crates/quince-icloud/src/lib.rs
+++ b/crates/quince-icloud/src/lib.rs
@@ -4,6 +4,8 @@
//! untrusted: a missing field or a malformed date returns an error, never a panic.
pub mod calendar;
+pub mod ckdocument;
+pub mod cloudkit;
pub mod reminders;
pub mod sync;
diff --git a/crates/quince-icloud/src/reminders.rs b/crates/quince-icloud/src/reminders.rs
index 34da8fe..5199868 100644
--- a/crates/quince-icloud/src/reminders.rs
+++ b/crates/quince-icloud/src/reminders.rs
@@ -370,3 +370,210 @@ mod tests {
assert_eq!(parse_snapshot(&body).reminders.len(), 1);
}
}
+
+// ---------------------------------------------------------------------------------------
+// CloudKit
+// ---------------------------------------------------------------------------------------
+//
+// The legacy /rd endpoints answer with Apple's placeholder once an account's reminders have
+// been migrated — a list named "Reminders ⚠️" holding "Where are my reminders?". The real
+// records live in the CloudKit `Reminders` zone, so this is the path that actually works.
+
+use crate::cloudkit::{field, ZoneClient};
+
+const CONTAINER: &str = "com.apple.reminders";
+const ZONE: &str = "Reminders";
+
+/// Turn one CloudKit `Reminder` record into a reminder.
+///
+/// Returns `None` for records that are deleted or carry no identity, so tombstones — which
+/// are the bulk of the zone — fall away.
+fn reminder_from_record(record: &Value) -> Option<Reminder> {
+ if field::flag(record, "Deleted") {
+ return None;
+ }
+ let guid = field::record_name(record)?;
+ let list_guid = field::reference(record, "List")?;
+
+ let completed = field::flag(record, "Completed");
+ Some(Reminder {
+ guid,
+ list_guid,
+ // A reminder with no readable title is still a reminder; showing it beats hiding it.
+ title: field::document_text(record, "TitleDocument").unwrap_or_else(|| "Untitled".into()),
+ notes: field::document_text(record, "NotesDocument"),
+ due_at: field::timestamp_seconds(record, "DueDate"),
+ // CloudKit stores completion as a flag; the closest instant it also gives us is the
+ // last modification, which is when the tick happened.
+ completed_at: completed
+ .then(|| field::timestamp_seconds(record, "LastModifiedDate"))
+ .flatten()
+ .or(if completed { Some(0) } else { None }),
+ priority: field::int(record, "Priority").unwrap_or(0),
+ etag: record
+ .get("recordChangeTag")
+ .and_then(Value::as_str)
+ .map(ToString::to_string),
+ raw: serde_json::to_string(record).ok(),
+ })
+}
+
+/// Turn a CloudKit list record into a reminder list.
+fn list_from_record(record: &Value) -> Option<ReminderList> {
+ if field::flag(record, "Deleted") {
+ return None;
+ }
+ let guid = field::record_name(record)?;
+ let title = field::string(record, "Name")
+ .or_else(|| field::document_text(record, "NameDocument"))
+ .or_else(|| field::string(record, "DisplayName"))
+ .unwrap_or_else(|| "Reminders".into());
+ Some(ReminderList {
+ guid,
+ title,
+ colour: field::string(record, "Color"),
+ ctag: None,
+ sort_order: field::int(record, "SortOrder").unwrap_or(0),
+ enabled: true,
+ })
+}
+
+/// Read reminders from CloudKit into the local library.
+///
+/// Returns the number of lists and reminders stored, plus the sync token to resume from.
+pub async fn sync_cloudkit(
+ http: &reqwest::Client,
+ ckdatabasews_root: &str,
+ params: Vec<(String, String)>,
+ library: &mut quince_core::Library,
+ since: Option<&str>,
+) -> Result<(usize, usize, Option<String>)> {
+ let client = ZoneClient::new(http, ckdatabasews_root, CONTAINER, params);
+ let (records, token) = client.all_records(ZONE, since).await?;
+
+ let mut lists: Vec<ReminderList> = Vec::new();
+ let mut reminders: Vec<Reminder> = Vec::new();
+
+ for record in &records {
+ match field::record_type(record).as_deref() {
+ Some("Reminder") => reminders.extend(reminder_from_record(record)),
+ // Apple has used more than one name for the container of a list over the years.
+ Some("List" | "ListSection" | "SmartList") => lists.extend(list_from_record(record)),
+ _ => {}
+ }
+ }
+
+ // Every list a reminder points at must exist, or storing it fails the foreign key.
+ // Apple does not always return the list record in the same page as its reminders.
+ for reminder in &reminders {
+ if !lists.iter().any(|l| l.guid == reminder.list_guid) {
+ lists.push(ReminderList {
+ guid: reminder.list_guid.clone(),
+ title: "Reminders".into(),
+ colour: None,
+ ctag: None,
+ sort_order: 0,
+ enabled: true,
+ });
+ }
+ }
+
+ library.put_reminder_lists(&lists)?;
+ library.put_reminders(&reminders)?;
+ library.mark_synced("reminders", token.as_deref())?;
+ Ok((lists.len(), reminders.len(), token))
+}
+
+#[cfg(test)]
+mod cloudkit_tests {
+ use super::*;
+ use serde_json::json;
+
+ fn document(text: &str) -> String {
+ use base64::{engine::general_purpose::STANDARD as B64, Engine};
+ use std::io::Write;
+ // Apple's shape: text at field path 2 -> 3 -> 2.
+ fn field(tag: u8, payload: &[u8]) -> Vec<u8> {
+ let mut out = vec![(tag << 3) | 2];
+ let mut len = payload.len();
+ loop {
+ let mut byte = (len & 0x7F) as u8;
+ len >>= 7;
+ if len != 0 {
+ byte |= 0x80;
+ }
+ out.push(byte);
+ if len == 0 {
+ break;
+ }
+ }
+ out.extend_from_slice(payload);
+ out
+ }
+ let proto = field(2, &field(3, &field(2, text.as_bytes())));
+ let mut gz = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
+ gz.write_all(&proto).unwrap();
+ B64.encode(gz.finish().unwrap())
+ }
+
+ fn reminder_record(name: &str, title: &str, completed: bool, deleted: bool) -> Value {
+ json!({
+ "recordName": name,
+ "recordType": "Reminder",
+ "recordChangeTag": "tag1",
+ "fields": {
+ "TitleDocument": { "type": "ENCRYPTED_BYTES", "value": document(title) },
+ "Completed": { "type": "NUMBER_INT64", "value": i64::from(completed) },
+ "Deleted": { "type": "NUMBER_INT64", "value": i64::from(deleted) },
+ "LastModifiedDate": { "type": "TIMESTAMP", "value": 1_753_808_674_659_i64 },
+ "List": { "type": "REFERENCE", "value": { "recordName": "List/abc" } }
+ }
+ })
+ }
+
+ #[test]
+ fn a_record_becomes_a_reminder_with_its_real_title() {
+ let r = reminder_from_record(&reminder_record(
+ "Reminder/1",
+ "🏃 Move more",
+ false,
+ false,
+ ))
+ .unwrap();
+ assert_eq!(r.title, "🏃 Move more");
+ assert_eq!(r.list_guid, "List/abc");
+ assert!(!r.is_done());
+ assert!(r.raw.is_some(), "the record is kept for writes");
+ }
+
+ #[test]
+ fn completion_carries_an_instant() {
+ let r = reminder_from_record(&reminder_record("Reminder/1", "Done thing", true, false))
+ .unwrap();
+ assert!(r.is_done());
+ assert_eq!(r.completed_at, Some(1_753_808_674));
+ }
+
+ /// Tombstones are the bulk of the zone and must not appear as reminders.
+ #[test]
+ fn deleted_records_are_dropped() {
+ assert!(
+ reminder_from_record(&reminder_record("Reminder/1", "Gone", false, true)).is_none()
+ );
+ }
+
+ #[test]
+ fn a_record_with_no_list_is_skipped() {
+ let mut record = reminder_record("Reminder/1", "Orphan", false, false);
+ record["fields"].as_object_mut().unwrap().remove("List");
+ assert!(reminder_from_record(&record).is_none());
+ }
+
+ #[test]
+ fn an_unreadable_title_still_yields_a_reminder() {
+ let mut record = reminder_record("Reminder/1", "x", false, false);
+ record["fields"]["TitleDocument"]["value"] = json!("!!!not base64!!!");
+ let r = reminder_from_record(&record).unwrap();
+ assert_eq!(r.title, "Untitled", "showing it beats hiding it");
+ }
+}
diff --git a/probe/src/main.rs b/probe/src/main.rs
index 9dca9d7..7ac9403 100644
--- a/probe/src/main.rs
+++ b/probe/src/main.rs
@@ -582,48 +582,50 @@ async fn main() -> Result<()> {
println!(" Apple offered no calendar service for this account");
}
- // ---- Reminders, end to end --------------------------------------------------
- println!("\n\x1b[1m── Reminders sync (the real path the app uses) ──\x1b[0m");
- if let Some(root) = session.service_url("reminders") {
+ // ---- Reminders from CloudKit, end to end ------------------------------------
+ // The legacy /rd endpoints answer with Apple's placeholder on a migrated account, so
+ // this reads the CloudKit zone where the real records live.
+ println!("\n\x1b[1m── Reminders via CloudKit (the real records) ──\x1b[0m");
+ if let Some(ck) = session.service_url("ckdatabasews") {
let params = client.service_params();
let http = client.http().clone();
match quince_core::Library::open(&dir) {
Err(e) => println!(" \x1b[31mcouldn't open the local library:\x1b[0m {e}"),
Ok(mut library) => {
- match quince_icloud::reminders::sync(
+ match quince_icloud::reminders::sync_cloudkit(
&http,
- &root,
+ &ck,
params,
&mut library,
- "Asia/Kolkata",
+ None,
)
.await
{
Err(e) => println!(" \x1b[31mFAIL\x1b[0m {e}"),
- Ok((lists, count)) => {
- println!(" \x1b[32mPASS\x1b[0m {lists} list(s), {count} reminder(s)");
+ Ok((lists, count, token)) => {
+ println!(
+ " \x1b[32mPASS\x1b[0m {lists} list(s), {count} reminder(s), resume token {}",
+ if token.is_some() { "stored" } else { "none" }
+ );
for l in library.reminder_lists().unwrap_or_default() {
let items = library.reminders_in(&l.guid, true).unwrap_or_default();
- println!(
- " {:<24} {:<9} {} item(s)",
- l.title,
- l.colour.unwrap_or_else(|| "—".into()),
- items.len()
- );
- for r in items.iter().take(6) {
- println!(
- " [{}] {}",
- if r.is_done() { "x" } else { " " },
- r.title
- );
+ println!(" \x1b[1m{}\x1b[0m — {} item(s)", l.title, items.len());
+ for r in items.iter().take(15) {
+ let mark = if r.is_done() { "x" } else { " " };
+ let note = r
+ .notes
+ .as_deref()
+ .map(|n| {
+ format!(" — {}", n.chars().take(46).collect::<String>())
+ })
+ .unwrap_or_default();
+ println!(" [{mark}] {}{note}", r.title);
}
}
}
}
}
}
- } else {
- println!(" Apple offered no reminders service for this account");
}
// ---- CloudKit containers ----------------------------------------------------
@@ -645,7 +647,14 @@ async fn main() -> Result<()> {
let mut q = client.service_params();
q.push(("ckjsBuildVersion".into(), "17DProjectDev77".into()));
q.push(("ckjsVersion".into(), "2.6.1".into()));
- match client.http().post(&url).query(&q).json(&serde_json::json!({})).send().await {
+ match client
+ .http()
+ .post(&url)
+ .query(&q)
+ .json(&serde_json::json!({}))
+ .send()
+ .await
+ {
Err(e) => println!(" {container:<26} transport error: {e}"),
Ok(r) => {
let status = r.status();
@@ -712,9 +721,15 @@ async fn main() -> Result<()> {
let text = r.text().await.unwrap_or_default();
let v: serde_json::Value =
serde_json::from_str(&text).unwrap_or(serde_json::Value::Null);
- let Some(zone_result) = v.get("zones").and_then(|z| z.as_array()).and_then(|a| a.first())
+ let Some(zone_result) = v
+ .get("zones")
+ .and_then(|z| z.as_array())
+ .and_then(|a| a.first())
else {
- println!(" unexpected: {}", text.chars().take(200).collect::<String>());
+ println!(
+ " unexpected: {}",
+ text.chars().take(200).collect::<String>()
+ );
break;
};
@@ -724,7 +739,10 @@ async fn main() -> Result<()> {
.cloned()
.unwrap_or_default();
for rec in &records {
- let rt = rec.get("recordType").and_then(|t| t.as_str()).unwrap_or("?");
+ let rt = rec
+ .get("recordType")
+ .and_then(|t| t.as_str())
+ .unwrap_or("?");
*types.entry(rt.to_string()).or_default() += 1;
if samples.iter().all(|(t, _, _)| t != rt) {
let fields: Vec<String> = rec
@@ -758,7 +776,11 @@ async fn main() -> Result<()> {
.get("syncToken")
.and_then(|t| t.as_str())
.map(ToString::to_string);
- if !zone_result.get("moreComing").and_then(|m| m.as_bool()).unwrap_or(false) {
+ if !zone_result
+ .get("moreComing")
+ .and_then(|m| m.as_bool())
+ .unwrap_or(false)
+ {
break;
}
}