Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
Contact photos cropped as Apple measures them; Delete All sent as icloud.com sends it
Yashwanth Kamireddidb76a15a8f9c
diff --git a/crates/quince-icloud/src/drive.rs b/crates/quince-icloud/src/drive.rsindex dc55f62..5c06ecd 100644--- a/crates/quince-icloud/src/drive.rs+++ b/crates/quince-icloud/src/drive.rs@@ -746,48 +746,154 @@ pub async fn trash_items(.collect())}-/// Put an item back where it was deleted from (`putBackItemsFromTrash`), or delete it for-/// good (`deleteItems`), as icloud.com's Recover and Delete Now do. Either way the trash is-/// re-read afterwards and the item must have left it: a 200 alone has been seen to do nothing.-async fn out_of_trash(+/// Put items back where they were deleted from (`putBackItemsFromTrash`), or delete them for+/// good (`deleteItems`), as icloud.com's Recover and Delete do — all of them in one request,+/// as its Recover All and Delete All send them. One at a time and all at once, thirty items+/// were thirty writes and sixty reads of the trash against Apple together; it turned them+/// away as busy, and what was not deleted came back on the next read.+///+/// Apple's answer is per item, and only `"OK"` is done: a 200 can carry refusals inside it.+/// An etag gone stale is tried once more with the one Apple sent back, as icloud.com does.+/// The trash is read again afterwards and each item must have left it. Returns what could not+/// be done, each with the reason.+pub async fn out_of_trash_many(http: &reqwest::Client,root: &str,params: Vec<(String, String)>,- item: &DriveItem,+ items: &[DriveItem],endpoint: &str,what: &str,-) -> Result<()> {+) -> Result<Vec<(String, String)>> {let current = folder_listing(http, root, ¶ms, TRASH).await?;- let Some(etag) = current- .iter()- .find(|i| i.drivewsid == item.drivewsid)- .map(|i| i.etag.clone().or_else(|| item.etag.clone()))- else {- return Err(Error::Refused(format!(- "That item is no longer in Recently Deleted, so it could not be {what}."- )));- };+ let mut failed: Vec<(String, String)> = Vec::new();+ // Each with the etag Apple has for it now.+ let mut todo: Vec<(DriveItem, Option<String>)> = Vec::new();+ for item in items {+ match current.iter().find(|i| i.drivewsid == item.drivewsid) {+ Some(found) => todo.push((+ item.clone(),+ found.etag.clone().or_else(|| item.etag.clone()),+ )),+ None => failed.push((+ item.drivewsid.clone(),+ format!("That item is no longer in Recently Deleted, so it could not be {what}."),+ )),+ }+ }let url = format!("{}/{endpoint}", root.trim_end_matches('/'));- let body = json!({ "items": [{ "drivewsid": item.drivewsid, "etag": etag }] });- let response = http.post(url).query(¶ms).json(&body).send().await?;- let status = response.status();- if status.as_u16() == 450 {- return Err(Error::ReauthRequired);+ for attempt in 0..2 {+ if todo.is_empty() {+ break;+ }+ // `deleteItems` takes each item's own id as `clientId` too (main.js, module 2231).+ let entries: Vec<Value> = todo+ .iter()+ .map(|(item, etag)| {+ if endpoint == "deleteItems" {+ json!({ "drivewsid": item.drivewsid, "etag": etag, "clientId": item.drivewsid })+ } else {+ json!({ "drivewsid": item.drivewsid, "etag": etag })+ }+ })+ .collect();+ let reply = post_json(http, &url, ¶ms, &json!({ "items": entries })).await?;+ let answers: Vec<Value> = reply+ .get("items")+ .and_then(Value::as_array)+ .cloned()+ .unwrap_or_default();+ let mut again = Vec::new();+ for (item, etag) in todo {+ let answer = answers.iter().find(|a| {+ a.get("drivewsid").and_then(Value::as_str) == Some(item.drivewsid.as_str())+ });+ match answer.and_then(|a| a.get("status")).and_then(Value::as_str) {+ Some("OK") | None => {}+ Some("ETAG_CONFLICT") if attempt == 0 => {+ let fresh = answer+ .and_then(|a| a.get("etag"))+ .and_then(Value::as_str)+ .map(str::to_string);+ again.push((item, fresh.or(etag)));+ }+ Some(other) => failed.push((+ item.drivewsid.clone(),+ format!("iCloud Drive did not let that be {what} ({other})."),+ )),+ }+ }+ todo = again;}- if !status.is_success() {- let text = response.text().await.unwrap_or_default();- return Err(Error::Protocol(format!(- "Drive refused that ({status}): {}",- text.chars().take(200).collect::<String>()- )));+ for (item, _) in todo {+ failed.push((+ item.drivewsid,+ format!("That item changed in iCloud Drive, so it was not {what}."),+ ));}let after = folder_listing(http, root, ¶ms, TRASH).await?;- if after.iter().any(|i| i.drivewsid == item.drivewsid) {- return Err(Error::Refused(format!(- "iCloud answered, but the item is still in Recently Deleted, so it was not {what}."- )));+ for item in items {+ if after.iter().any(|i| i.drivewsid == item.drivewsid)+ && !failed.iter().any(|(id, _)| id == &item.drivewsid)+ {+ failed.push((+ item.drivewsid.clone(),+ format!("iCloud answered, but the item is still in Recently Deleted, so it was not {what}."),+ ));+ }}- Ok(())+ Ok(failed)+}++/// [`out_of_trash_many`] for one item, its refusal as the error.+async fn out_of_trash(+ http: &reqwest::Client,+ root: &str,+ params: Vec<(String, String)>,+ item: &DriveItem,+ endpoint: &str,+ what: &str,+) -> Result<()> {+ let failed = out_of_trash_many(+ http,+ root,+ params,+ std::slice::from_ref(item),+ endpoint,+ what,+ )+ .await?;+ match failed.into_iter().next() {+ None => Ok(()),+ Some((_, why)) => Err(Error::Refused(why)),+ }+}++/// Recover all of these, in one request. What could not be, with why.+pub async fn put_back_many(+ http: &reqwest::Client,+ root: &str,+ params: Vec<(String, String)>,+ items: &[DriveItem],+) -> Result<Vec<(String, String)>> {+ out_of_trash_many(+ http,+ root,+ params,+ items,+ "putBackItemsFromTrash",+ "recovered",+ )+ .await+}++/// Delete all of these for good, in one request. What could not be, with why.+pub async fn delete_forever_many(+ http: &reqwest::Client,+ root: &str,+ params: Vec<(String, String)>,+ items: &[DriveItem],+) -> Result<Vec<(String, String)>> {+ out_of_trash_many(http, root, params, items, "deleteItems", "deleted").await}/// Recover: back to the folder it was deleted from.@@ -1455,6 +1561,88 @@ mod write_tests {assert_eq!(body["items"][0]["etag"], "fresh");}+ /// Delete All is one request with every item, each carrying its own id as `clientId`, as+ /// icloud.com sends it; a stale etag is tried again with Apple's; a refusal inside the 200+ /// is reported for its item alone.+ #[tokio::test]+ async fn delete_all_is_one_request_and_reads_each_answer() {+ let server = MockServer::start().await;+ let item = |id: &str| DriveItem {+ drivewsid: format!("FILE::com.apple.CloudDocs::{id}"),+ ..file()+ };+ let (a, b, c) = (item("A"), item("B"), item("C"));+ let trash = serde_json::json!([{ "drivewsid": TRASH, "status": "OK", "items": [+ { "drivewsid": a.drivewsid, "name": "a", "type": "FILE", "etag": "ea" },+ { "drivewsid": b.drivewsid, "name": "b", "type": "FILE", "etag": "eb" },+ { "drivewsid": c.drivewsid, "name": "c", "type": "FILE", "etag": "ec" },+ ]}]);+ // Read before: all three. Read after: only the refused one is left.+ Mock::given(method("POST"))+ .and(path("/retrieveItemDetailsInFolders"))+ .respond_with(ResponseTemplate::new(200).set_body_json(trash))+ .up_to_n_times(1)+ .mount(&server)+ .await;+ Mock::given(method("POST"))+ .and(path("/retrieveItemDetailsInFolders"))+ .respond_with(ResponseTemplate::new(200).set_body_json(+ serde_json::json!([{ "drivewsid": TRASH, "status": "OK", "items": [+ { "drivewsid": c.drivewsid, "name": "c", "type": "FILE", "etag": "ec" },+ ]}]),+ ))+ .mount(&server)+ .await;+ // First: A done, B's etag stale, C refused. Then B again with the fresh etag: done.+ Mock::given(method("POST"))+ .and(path("/deleteItems"))+ .respond_with(+ ResponseTemplate::new(200).set_body_json(serde_json::json!({ "items": [+ { "drivewsid": a.drivewsid, "status": "OK" },+ { "drivewsid": b.drivewsid, "status": "ETAG_CONFLICT", "etag": "eb2" },+ { "drivewsid": c.drivewsid, "status": "UNKNOWN_ERROR" },+ ]})),+ )+ .up_to_n_times(1)+ .mount(&server)+ .await;+ Mock::given(method("POST"))+ .and(path("/deleteItems"))+ .respond_with(+ ResponseTemplate::new(200).set_body_json(serde_json::json!({ "items": [+ { "drivewsid": b.drivewsid, "status": "OK" },+ ]})),+ )+ .mount(&server)+ .await;+ let failed = delete_forever_many(+ &reqwest::Client::new(),+ &server.uri(),+ vec![],+ &[a.clone(), b.clone(), c.clone()],+ )+ .await+ .unwrap();+ let deletes: Vec<serde_json::Value> = server+ .received_requests()+ .await+ .unwrap()+ .iter()+ .filter(|r| r.url.path().ends_with("/deleteItems"))+ .map(|r| serde_json::from_slice(&r.body).unwrap())+ .collect();+ assert_eq!(+ deletes.len(),+ 2,+ "one request for all, one more for the stale etag"+ );+ assert_eq!(deletes[0]["items"].as_array().unwrap().len(), 3);+ assert_eq!(deletes[0]["items"][0]["clientId"], a.drivewsid);+ assert_eq!(deletes[1]["items"][0]["etag"], "eb2");+ assert_eq!(failed.len(), 1, "{failed:?}");+ assert_eq!(failed[0].0, c.drivewsid);+ }+/// Delete Now that left the item in Recently Deleted is a failure, said so.#[tokio::test]async fn a_delete_now_that_did_nothing_is_reported() {diff --git a/crates/quince-media/src/lib.rs b/crates/quince-media/src/lib.rsindex 9275581..09284d8 100644--- a/crates/quince-media/src/lib.rs+++ b/crates/quince-media/src/lib.rs@@ -79,7 +79,12 @@ fn cut(image: image::DynamicImage, crop: Option<Crop>) -> image::DynamicImage {if cw == 0 || ch == 0 || x.saturating_add(cw) > w || y.saturating_add(ch) > h {return image;}- image.crop_imm(x, y, cw, ch)+ // Apple's crop is in the Mac's coordinates: measured from the bottom-left corner, `y`+ // rising up the picture. Read from the top, it drew the wrong band — measured on a real+ // card, 1696×2528 with a 1279 square at y = 1249: from the top that is the bottom half,+ // the body; from the bottom it is the top square, the face, as the phone shows it.+ let top = h - y - ch;+ image.crop_imm(x, top, cw, ch)}/// PNG where the picture needs transparency, JPEG where it does not.@@ -107,10 +112,10 @@ mod tests {use super::*;use image::{DynamicImage, GenericImageView, Rgba, RgbaImage};- /// A tall picture with a red square in the lower half — where a face would be in a portrait.+ /// A tall picture with a red square at its top right — where a face sits in a portrait.fn portrait() -> Vec<u8> {let mut img = RgbaImage::from_pixel(100, 200, Rgba([0, 0, 255, 255]));- for y in 150..200 {+ for y in 0..50 {for x in 50..100 {img.put_pixel(x, y, Rgba([255, 0, 0, 255]));}@@ -129,6 +134,8 @@ mod tests {}/// The whole point: a portrait photo cropped where its owner said, not through the middle.+ /// `y` is from the bottom, as Apple measures it: 150 up a 200-tall picture, a 50 square+ /// is the top one — the face — and read from the top it would have been the bottom.#[test]fn the_chosen_square_is_what_gets_drawn() {let source = portrait();diff --git a/probe/Cargo.toml b/probe/Cargo.tomlindex 0afbaff..40c83e9 100644--- a/probe/Cargo.toml+++ b/probe/Cargo.toml@@ -6,6 +6,7 @@ license.workspace = truepublish = false[dependencies]+image = { version = "0.25", default-features = false, features = ["png", "jpeg"] }sha1 = "0.10"sha2 = "0.10"anyhow.workspace = truediff --git a/probe/examples/contact_photos.rs b/probe/examples/contact_photos.rsnew file mode 100644index 0000000..0163d83--- /dev/null+++ b/probe/examples/contact_photos.rs@@ -0,0 +1,37 @@+//! Read-only: how many contacts carry a photo, and whether fetching one works as the app+//! fetches it (session cookies and the service query). Prints counts and statuses only.+use anyhow::{anyhow, Result};+#[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, p) = (client.http().clone(), client.service_params());+ let people = quince_icloud::contacts::fetch(&http, &root, p.clone())+ .await?+ .contacts;+ let with: Vec<_> = people.iter().filter(|c| c.photo_url.is_some()).collect();+ println!("{} contacts, {} with a photo", people.len(), with.len());+ for c in with.iter().take(3) {+ let url = c.photo_url.as_deref().unwrap();+ println!(" url host: {:?}", url.split('/').nth(2));+ let r = http.get(url).query(&p).send().await?;+ let st = r.status();+ let ct = r.headers().get("content-type").cloned();+ let n = r.bytes().await.map(|b| b.len()).unwrap_or(0);+ println!(" with query: {st} {ct:?} {n} bytes");+ let r = http.get(url).send().await?;+ println!(+ " bare: {} {} bytes",+ r.status(),+ r.bytes().await.map(|b| b.len()).unwrap_or(0)+ );+ }+ Ok(())+}diff --git a/probe/examples/crop_check.rs b/probe/examples/crop_check.rsnew file mode 100644index 0000000..16c0feb--- /dev/null+++ b/probe/examples/crop_check.rs@@ -0,0 +1,8 @@+//! The app's own avatar cut, on a saved photo and crop: `crop_check <in.jpg> <x,y,w,h> <out>`.+fn main() -> anyhow::Result<()> {+ let a: Vec<String> = std::env::args().collect();+ let bytes = std::fs::read(&a[1])?;+ let out = quince_media::avatar(&bytes, quince_media::Crop::parse(&a[2]), 256)?;+ std::fs::write(&a[3], out)?;+ Ok(())+}diff --git a/probe/examples/drive_purge_live.rs b/probe/examples/drive_purge_live.rsnew file mode 100644index 0000000..ee4e812--- /dev/null+++ b/probe/examples/drive_purge_live.rs@@ -0,0 +1,83 @@+//! Live, throwaway file only: "Quince purge test.txt" uploaded, moved to Recently Deleted,+//! then deleted for good — first asked the old way (no clientId) to see Apple's answer, then+//! the app's way — and the trash read again twenty seconds later.+use anyhow::{anyhow, Result};+use quince_icloud::drive;+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 docws = client+ .session()+ .service_url("docws")+ .ok_or_else(|| anyhow!("no docws"))?;+ let dws = client+ .session()+ .service_url("drivews")+ .ok_or_else(|| anyhow!("no drivews"))?;+ let (http, p) = (client.http().clone(), client.service_params());+ let tmp = std::env::temp_dir().join("quince-purge");+ std::fs::create_dir_all(&tmp)?;+ let f = tmp.join("Quince purge test.txt");+ std::fs::write(&f, b"safe to delete")?;+ let up = drive::upload(+ &http,+ &docws,+ &dws,+ p.clone(),+ ROOT,+ &f,+ std::sync::Arc::new(|_, _| {}),+ )+ .await?;+ drive::delete(&http, &dws, p.clone(), &up).await?;+ let in_trash = drive::trash(&http, &dws, p.clone()).await?;+ let t = in_trash+ .iter()+ .find(|i| i.drivewsid == up.drivewsid)+ .cloned()+ .ok_or_else(|| anyhow!("not in trash"))?;+ println!("in Recently Deleted: {}", t.name);+ // The old request, as Quince sent it.+ let r = http+ .post(format!("{}/deleteItems", dws.trim_end_matches('/')))+ .query(&p)+ .json(&serde_json::json!({ "items": [{ "drivewsid": t.drivewsid, "etag": t.etag }] }))+ .send()+ .await?;+ println!(+ "old way: {} {}",+ r.status(),+ r.text()+ .await+ .unwrap_or_default()+ .chars()+ .take(300)+ .collect::<String>()+ );+ let still = drive::trash(&http, &dws, p.clone())+ .await?+ .iter()+ .any(|i| i.drivewsid == up.drivewsid);+ println!("still in trash after the old way: {still}");+ if still {+ let t = drive::trash(&http, &dws, p.clone())+ .await?+ .into_iter()+ .find(|i| i.drivewsid == up.drivewsid)+ .unwrap();+ drive::delete_forever(&http, &dws, p.clone(), &t).await?;+ println!("fixed way: OK");+ }+ tokio::time::sleep(std::time::Duration::from_secs(20)).await;+ let later = drive::trash(&http, &dws, p.clone())+ .await?+ .iter()+ .any(|i| i.drivewsid == up.drivewsid);+ println!("in trash 20s later: {later}");+ Ok(())+}diff --git a/probe/examples/drive_purge_many.rs b/probe/examples/drive_purge_many.rsnew file mode 100644index 0000000..1cbdfed--- /dev/null+++ b/probe/examples/drive_purge_many.rs@@ -0,0 +1,58 @@+//! Live, throwaway files only: three "Quince purge test N.txt" uploaded, moved to Recently+//! Deleted, then deleted for good in one request as Delete All sends it; the trash read again+//! twenty seconds later, so a listing that merely lagged cannot pass for a delete.+use anyhow::{anyhow, Result};+use quince_icloud::drive;+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 docws = client+ .session()+ .service_url("docws")+ .ok_or_else(|| anyhow!("no docws"))?;+ let dws = client+ .session()+ .service_url("drivews")+ .ok_or_else(|| anyhow!("no drivews"))?;+ let (http, p) = (client.http().clone(), client.service_params());+ let tmp = std::env::temp_dir().join("quince-purge-many");+ std::fs::create_dir_all(&tmp)?;+ let mut ids = Vec::new();+ for n in 1..=3 {+ let f = tmp.join(format!("Quince purge test {n}.txt"));+ std::fs::write(&f, format!("safe to delete {n}"))?;+ let up = drive::upload(+ &http,+ &docws,+ &dws,+ p.clone(),+ ROOT,+ &f,+ std::sync::Arc::new(|_, _| {}),+ )+ .await?;+ drive::delete(&http, &dws, p.clone(), &up).await?;+ ids.push(up.drivewsid);+ }+ let trash = drive::trash(&http, &dws, p.clone()).await?;+ let ours: Vec<_> = trash+ .into_iter()+ .filter(|i| ids.contains(&i.drivewsid))+ .collect();+ println!("{} in Recently Deleted", ours.len());+ let failed = drive::delete_forever_many(&http, &dws, p.clone(), &ours).await?;+ println!("refused: {failed:?}");+ tokio::time::sleep(std::time::Duration::from_secs(20)).await;+ let left = drive::trash(&http, &dws, p.clone())+ .await?+ .into_iter()+ .filter(|i| ids.contains(&i.drivewsid))+ .count();+ println!("still in Recently Deleted 20s later: {left}");+ Ok(())+}diff --git a/probe/examples/me_photo.rs b/probe/examples/me_photo.rsnew file mode 100644index 0000000..47bb965--- /dev/null+++ b/probe/examples/me_photo.rs@@ -0,0 +1,55 @@+//! Read-only: the person's own card's photo — its pixel size, the crop Apple keeps for it,+//! and its EXIF orientation — to place the crop as Apple does.+use anyhow::{anyhow, Result};+#[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, p) = (client.http().clone(), client.service_params());+ let book = quince_icloud::contacts::fetch(&http, &root, p.clone()).await?;+ let me = book.me.clone().ok_or_else(|| anyhow!("no me card"))?;+ let card = book+ .contacts+ .iter()+ .find(|c| c.guid == me)+ .ok_or_else(|| anyhow!("me card not found"))?;+ println!("crop: {:?}", card.photo_crop);+ let url = card.photo_url.clone().ok_or_else(|| anyhow!("no photo"))?;+ let bytes = http.get(&url).send().await?.bytes().await?;+ let img = image::load_from_memory(&bytes)?;+ println!("decoded: {}x{}", img.width(), img.height());+ // EXIF orientation tag (0x0112), found by hand in the APP1 segment.+ let pos = bytes.windows(4).position(|w| w == b"Exif");+ println!("has exif: {}", pos.is_some());+ if let Some(i) = pos {+ let t = &bytes[i + 6..];+ let le = &t[0..2] == b"II";+ let rd16 = |o: usize| {+ if le {+ u16::from_le_bytes([t[o], t[o + 1]])+ } else {+ u16::from_be_bytes([t[o], t[o + 1]])+ }+ };+ let rd32 = |o: usize| if le { u32::from_le_bytes([t[o], t[o+1], t[o+2], t[o+3]]) } else { u32::from_be_bytes([t[o], t[o+1], t[o+2], t[o+3]]) } as usize;+ let ifd = rd32(4);+ let n = rd16(ifd) as usize;+ for k in 0..n {+ let e = ifd + 2 + k * 12;+ if rd16(e) == 0x0112 {+ println!("orientation: {}", rd16(e + 8));+ }+ }+ }+ let img2 = image::load_from_memory(&bytes)?;+ std::fs::write(std::env::temp_dir().join("me-photo-raw.jpg"), &bytes)?;+ let _ = img2;+ Ok(())+}