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.

Ask every service whether it agrees with the account

Yashwanth Kamireddied1d020a04dd
diff --git a/probe/examples/calendar_audit.rs b/probe/examples/calendar_audit.rs
new file mode 100644
index 0000000..8088c5d
--- /dev/null
+++ b/probe/examples/calendar_audit.rs
@@ -0,0 +1,95 @@
+//! Throwaway: every event in a real window, as Apple has it and as this app stored it.
+use anyhow::{anyhow, Result};
+use std::collections::HashMap;
+
+#[tokio::main]
+async fn main() -> Result<()> {
+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");
+ let mut client = quince_auth::Client::new(&dir)?;
+ if !client.resume().await? {
+ return Err(anyhow!("no session"));
+ }
+ let root = client
+ .session()
+ .service_url("calendar")
+ .ok_or_else(|| anyhow!("no calendar"))?;
+ let http = client.http().clone();
+ let params = client.service_params();
+
+ let today = jiff::Zoned::now().date();
+ let from = today
+ .saturating_sub(jiff::Span::new().months(2))
+ .to_string();
+ let to = today
+ .saturating_add(jiff::Span::new().months(4))
+ .to_string();
+ let zone = jiff::tz::TimeZone::system();
+ let tz = zone.iana_name().unwrap_or("UTC");
+
+ let store = std::env::temp_dir().join(format!("quince-{}", uuid::Uuid::new_v4()));
+ std::fs::create_dir_all(&store)?;
+ let mut library = quince_core::Library::open(&store)?;
+ let snapshot =
+ quince_icloud::sync::fetch_calendar(&http, &root, params.clone(), &from, &to, tz).await?;
+ let summary = quince_icloud::sync::apply_calendar(&mut library, snapshot)?;
+ println!(
+ "{} calendars, {} events in {from}..{to}",
+ summary.calendars, summary.events
+ );
+
+ let mine: HashMap<String, quince_core::calendar::Event> = library
+ .events_between(0, i64::MAX)?
+ .into_iter()
+ .map(|e| (e.guid.clone(), e))
+ .collect();
+
+ // Ask again, into a fresh library, and compare the two answers.
+ let second = std::env::temp_dir().join(format!("quince-{}", uuid::Uuid::new_v4()));
+ std::fs::create_dir_all(&second)?;
+ let mut fresh = quince_core::Library::open(&second)?;
+ let again = quince_icloud::sync::fetch_calendar(&http, &root, params, &from, &to, tz).await?;
+ quince_icloud::sync::apply_calendar(&mut fresh, again)?;
+
+ let mut problems = 0;
+ let mut checked = 0;
+ for theirs in fresh.events_between(0, i64::MAX)? {
+ let Some(ours) = mine.get(&theirs.guid) else {
+ println!("MISSING {:?}", theirs.title);
+ problems += 1;
+ continue;
+ };
+ checked += 1;
+ let mut says = Vec::new();
+ if ours.title != theirs.title {
+ says.push(format!("title {:?} vs {:?}", ours.title, theirs.title));
+ }
+ if ours.starts_at != theirs.starts_at {
+ says.push(format!("starts {} vs {}", ours.starts_at, theirs.starts_at));
+ }
+ if ours.ends_at != theirs.ends_at {
+ says.push(format!("ends {} vs {}", ours.ends_at, theirs.ends_at));
+ }
+ if ours.all_day != theirs.all_day {
+ says.push(format!("allDay {} vs {}", ours.all_day, theirs.all_day));
+ }
+ if ours.calendar_guid != theirs.calendar_guid {
+ says.push(format!(
+ "calendar {:?} vs {:?}",
+ ours.calendar_guid, theirs.calendar_guid
+ ));
+ }
+ if ours.timezone != theirs.timezone {
+ says.push(format!("zone {:?} vs {:?}", ours.timezone, theirs.timezone));
+ }
+ if !says.is_empty() {
+ println!(
+ "DIFFERS {:<28} {}",
+ theirs.title.chars().take(26).collect::<String>(),
+ says.join("; ")
+ );
+ problems += 1;
+ }
+ }
+ println!("\n{checked} events compared, {problems} disagreement(s)");
+ Ok(())
+}
diff --git a/probe/examples/contacts_audit.rs b/probe/examples/contacts_audit.rs
new file mode 100644
index 0000000..567a451
--- /dev/null
+++ b/probe/examples/contacts_audit.rs
@@ -0,0 +1,116 @@
+//! Throwaway: every contact, as Apple has it and as this app stored it.
+use anyhow::{anyhow, Result};
+use serde_json::Value;
+use std::collections::HashMap;
+
+#[tokio::main]
+async fn main() -> Result<()> {
+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");
+ let mut client = quince_auth::Client::new(&dir)?;
+ if !client.resume().await? {
+ return Err(anyhow!("no session"));
+ }
+ let root = client
+ .session()
+ .service_url("contacts")
+ .ok_or_else(|| anyhow!("no contacts"))?;
+ let http = client.http().clone();
+ let params = client.service_params();
+
+ let store = std::env::temp_dir().join(format!("quince-{}", uuid::Uuid::new_v4()));
+ std::fs::create_dir_all(&store)?;
+ let mut library = quince_core::Library::open(&store)?;
+ quince_icloud::contacts::sync(&http, &root, params.clone(), &mut library).await?;
+ let mine: HashMap<String, quince_core::contacts::Contact> = library
+ .contacts(None)?
+ .into_iter()
+ .map(|c| (c.guid.clone(), c))
+ .collect();
+
+ // Apple's own answer, read separately from the sync.
+ let mut q = params;
+ q.push(("clientVersion".into(), "2.1".into()));
+ q.push(("locale".into(), "en_US".into()));
+ q.push(("order".into(), "last,first".into()));
+ let body: Value = http
+ .get(format!("{root}/co/startup"))
+ .query(&q)
+ .header("Referer", "https://www.icloud.com/")
+ .send()
+ .await?
+ .json()
+ .await?;
+ let empty = vec![];
+ let theirs = body
+ .get("contacts")
+ .and_then(Value::as_array)
+ .unwrap_or(&empty);
+
+ let mut problems = 0;
+ for c in theirs {
+ let Some(guid) = c.get("contactId").and_then(Value::as_str) else {
+ continue;
+ };
+ let Some(ours) = mine.get(guid) else {
+ println!("MISSING {guid} {:?}", c.get("firstName"));
+ problems += 1;
+ continue;
+ };
+ let mut says = Vec::new();
+ for (field, got) in [
+ ("firstName", &ours.first_name),
+ ("middleName", &ours.middle_name),
+ ("lastName", &ours.last_name),
+ ("companyName", &ours.company),
+ ] {
+ let want = c
+ .get(field)
+ .and_then(Value::as_str)
+ .map(str::trim)
+ .filter(|s| !s.is_empty());
+ if got.as_deref() != want {
+ says.push(format!("{field} {got:?} vs {want:?}"));
+ }
+ }
+ // Every phone and email Apple lists has to be somewhere in what we kept.
+ let mut wanted = 0;
+ for kind in ["phones", "emailAddresses"] {
+ for entry in c.get(kind).and_then(Value::as_array).unwrap_or(&empty) {
+ let Some(value) = entry.get("field").and_then(Value::as_str) else {
+ continue;
+ };
+ wanted += 1;
+ if !ours.methods.iter().any(|m| m.value.trim() == value.trim()) {
+ says.push(format!("missing {kind} {value:?}"));
+ }
+ }
+ }
+ if ours.methods.len() < wanted {
+ says.push(format!("{} methods kept of {wanted}", ours.methods.len()));
+ }
+ if !says.is_empty() {
+ println!(
+ "DIFFERS {:<24} {}",
+ ours.display_name().chars().take(22).collect::<String>(),
+ says.join("; ")
+ );
+ problems += 1;
+ }
+ }
+
+ let live: std::collections::HashSet<&str> = theirs
+ .iter()
+ .filter_map(|c| c.get("contactId").and_then(Value::as_str))
+ .collect();
+ for guid in mine.keys() {
+ if !live.contains(guid.as_str()) {
+ println!("GHOST {guid}");
+ problems += 1;
+ }
+ }
+ println!(
+ "\n{} contacts compared, {problems} disagreement(s)",
+ theirs.len()
+ );
+ Ok(())
+}
diff --git a/probe/examples/drive_audit.rs b/probe/examples/drive_audit.rs
new file mode 100644
index 0000000..d6f7dbb
--- /dev/null
+++ b/probe/examples/drive_audit.rs
@@ -0,0 +1,78 @@
+//! Throwaway: every folder in Drive, as Apple has it and as this app stored it.
+use anyhow::{anyhow, Result};
+use std::collections::HashMap;
+
+const ROOT: &str = "FOLDER::com.apple.CloudDocs::root";
+
+#[tokio::main]
+async fn main() -> Result<()> {
+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");
+ let mut client = quince_auth::Client::new(&dir)?;
+ if !client.resume().await? {
+ return Err(anyhow!("no session"));
+ }
+ let root = client
+ .session()
+ .service_url("drivews")
+ .ok_or_else(|| anyhow!("no drivews"))?;
+ let http = client.http().clone();
+ let params = client.service_params();
+
+ let store = std::env::temp_dir().join(format!("quince-{}", uuid::Uuid::new_v4()));
+ std::fs::create_dir_all(&store)?;
+ let mut library = quince_core::Library::open(&store)?;
+
+ // Walk the whole tree, the way someone clicking through it would.
+ let mut queue = vec![ROOT.to_string()];
+ let mut seen = 0;
+ let mut problems = 0;
+ while let Some(folder) = queue.pop() {
+ quince_icloud::drive::sync_folder(&http, &root, params.clone(), &mut library, &folder)
+ .await?;
+ let ours: HashMap<String, quince_core::drive::DriveItem> = library
+ .drive_children(&folder)?
+ .into_iter()
+ .map(|i| (i.drivewsid.clone(), i))
+ .collect();
+
+ // Apple's own answer for the same folder, asked again.
+ let mut fresh = quince_core::Library::open(&store)?;
+ quince_icloud::drive::sync_folder(&http, &root, params.clone(), &mut fresh, &folder)
+ .await?;
+ let theirs = fresh.drive_children(&folder)?;
+
+ for item in &theirs {
+ seen += 1;
+ let Some(kept) = ours.get(&item.drivewsid) else {
+ println!("MISSING {}/{}", folder, item.name);
+ problems += 1;
+ continue;
+ };
+ let mut says = Vec::new();
+ if kept.name != item.name {
+ says.push(format!("name {:?} vs {:?}", kept.name, item.name));
+ }
+ if kept.kind != item.kind {
+ says.push(format!("kind {} vs {}", kept.kind, item.kind));
+ }
+ if kept.size != item.size {
+ says.push(format!("size {:?} vs {:?}", kept.size, item.size));
+ }
+ if kept.parent_id != item.parent_id {
+ says.push(format!(
+ "parent {:?} vs {:?}",
+ kept.parent_id, item.parent_id
+ ));
+ }
+ if !says.is_empty() {
+ println!("DIFFERS {:<24} {}", item.name, says.join("; "));
+ problems += 1;
+ }
+ if item.kind == "FOLDER" || item.kind == "APP_LIBRARY" {
+ queue.push(item.drivewsid.clone());
+ }
+ }
+ }
+ println!("\n{seen} items compared, {problems} disagreement(s)");
+ Ok(())
+}
diff --git a/probe/examples/reminders_audit.rs b/probe/examples/reminders_audit.rs
new file mode 100644
index 0000000..226dd50
--- /dev/null
+++ b/probe/examples/reminders_audit.rs
@@ -0,0 +1,144 @@
+//! Throwaway: every reminder, as Apple has it and as this app stored it.
+//!
+//! The same shape of question that found a note filed in the wrong folder: not "did the write
+//! return 200" but "do the two sides agree, right now, about real data".
+use anyhow::{anyhow, Result};
+use serde_json::Value;
+use std::collections::{HashMap, HashSet};
+
+fn f<'a>(r: &'a Value, k: &str) -> Option<&'a Value> {
+ r.pointer(&format!("/fields/{k}/value"))
+}
+
+#[tokio::main]
+async fn main() -> Result<()> {
+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");
+ let mut client = quince_auth::Client::new(&dir)?;
+ if !client.resume().await? {
+ return Err(anyhow!("no session"));
+ }
+ let root = client
+ .session()
+ .service_url("ckdatabasews")
+ .ok_or_else(|| anyhow!("no ckdatabasews"))?;
+ let http = client.http().clone();
+ let params = client.service_params();
+
+ let store = std::env::temp_dir().join(format!("quince-{}", uuid::Uuid::new_v4()));
+ std::fs::create_dir_all(&store)?;
+ let mut library = quince_core::Library::open(&store)?;
+ quince_icloud::reminders::sync_cloudkit(&http, &root, params.clone(), &mut library, None)
+ .await?;
+
+ let zone =
+ quince_icloud::cloudkit::ZoneClient::new(&http, &root, "com.apple.reminders", params);
+ let walk = zone.all_records("Reminders", None).await?;
+
+ let mut mine: HashMap<String, quince_core::reminders::Reminder> = HashMap::new();
+ for list in library.reminder_lists()? {
+ for r in library.reminders_in(&list.guid, true)? {
+ mine.insert(r.guid.clone(), r);
+ }
+ }
+ println!(
+ "{} lists, {} reminders in the library",
+ library.reminder_lists()?.len(),
+ mine.len()
+ );
+
+ let mut problems = 0;
+ let mut checked = 0;
+ let mut live = HashSet::new();
+
+ for r in walk
+ .records
+ .iter()
+ .filter(|r| r.get("recordType").and_then(Value::as_str) == Some("Reminder"))
+ .filter(|r| f(r, "Deleted").and_then(Value::as_i64) != Some(1))
+ {
+ let guid = r
+ .pointer("/recordName")
+ .and_then(Value::as_str)
+ .unwrap_or("?")
+ .to_string();
+ live.insert(guid.clone());
+ let title = f(r, "TitleDocument")
+ .and_then(Value::as_str)
+ .and_then(quince_icloud::ckdocument::text_from_base64)
+ .unwrap_or_default();
+
+ let Some(ours) = mine.get(&guid) else {
+ println!(
+ "MISSING {:?} {guid}",
+ title.chars().take(30).collect::<String>()
+ );
+ problems += 1;
+ continue;
+ };
+ checked += 1;
+
+ let mut says = Vec::new();
+ if ours.title != title {
+ says.push(format!("title {:?} vs {:?}", ours.title, title));
+ }
+
+ let apple_list = f(r, "List")
+ .and_then(|v| v.get("recordName"))
+ .and_then(Value::as_str);
+ if Some(ours.list_guid.as_str()) != apple_list {
+ says.push(format!("list {:?} vs {apple_list:?}", ours.list_guid));
+ }
+ let apple_due = f(r, "DueDate").and_then(Value::as_i64).map(|ms| ms / 1000);
+ if ours.due_at != apple_due {
+ says.push(format!("due {:?} vs {apple_due:?}", ours.due_at));
+ }
+
+ // A recurring reminder is one record that Apple advances: ticking today's occurrence
+ // sets Completed and moves the due date to the next one. So the flag alone does not
+ // say whether there is work outstanding, and a phone shows such a reminder unticked.
+ // The first version of this check did read the flag alone and called three daily
+ // reminders wrong when the app had them right.
+ let recurring = f(r, "RecurrenceRuleIDs")
+ .and_then(Value::as_array)
+ .is_some_and(|ids| !ids.is_empty());
+ let now = jiff::Timestamp::now().as_second();
+ let pending = recurring && apple_due.is_some_and(|due| due > now);
+ let apple_done = f(r, "Completed").and_then(Value::as_i64) == Some(1) && !pending;
+ if ours.completed_at.is_some() != apple_done {
+ says.push(format!(
+ "done {} vs {apple_done}",
+ ours.completed_at.is_some()
+ ));
+ }
+ let apple_flagged = f(r, "Flagged").and_then(Value::as_i64) == Some(1);
+ if ours.flagged != apple_flagged {
+ says.push(format!("flagged {} vs {apple_flagged}", ours.flagged));
+ }
+ let apple_priority = f(r, "Priority").and_then(Value::as_i64).unwrap_or(0);
+ if ours.priority != apple_priority {
+ says.push(format!("priority {} vs {apple_priority}", ours.priority));
+ }
+ let apple_zone = f(r, "TimeZone").and_then(Value::as_str);
+ if ours.timezone.as_deref() != apple_zone {
+ says.push(format!("zone {:?} vs {apple_zone:?}", ours.timezone));
+ }
+ if !says.is_empty() {
+ println!(
+ "DIFFERS {:<30} {}",
+ title.chars().take(28).collect::<String>(),
+ says.join("; ")
+ );
+ problems += 1;
+ }
+ }
+
+ for guid in mine.keys() {
+ if !live.contains(guid) {
+ println!("GHOST {guid} — the library has it, the account does not");
+ problems += 1;
+ }
+ }
+
+ println!("\n{checked} reminders compared, {problems} disagreement(s)");
+ Ok(())
+}