diff --git a/caldav.js b/caldav.js index a7cfc98..dfc1878 100644 --- a/caldav.js +++ b/caldav.js @@ -65,10 +65,15 @@ async function connect(appleId, password) { if (!homeHref) throw new Error('Could not find the calendar home for this account'); const home = abs(homeHref, b.url); const c = await dav('PROPFIND', home, { ...o, depth: 1, body: PROPFIND_CALS }); - const calendars = []; + if (c.status >= 400) throw new Error(`iCloud answered ${c.status} when listing the calendars`); + const calendars = [], seen = []; for (const r of responses(c.text)) { const rt = tag(r, 'resourcetype') || ''; - if (!/<(?:\w+:)?calendar\s*\/?>/i.test(rt)) continue; + // Apple writes (with attributes, and often no prefix); the lookahead keeps + // calendar-proxy-read, calendar-home-set etc. from matching. + const isCal = /<(?:[\w-]+:)?calendar(?=[\s/>])/i.test(rt); + seen.push(isCal ? 'calendar' : (rt.replace(/<(?:[\w-]+:)?([\w-]+)[^>]*>/g, '$1 ').trim() || 'other').slice(0, 30)); + if (!isCal) continue; const comps = tag(r, 'supported-calendar-component-set'); const events = !comps || /name="VEVENT"/i.test(comps), todos = !!comps && /name="VTODO"/i.test(comps); if (!events && !todos) continue; @@ -76,7 +81,7 @@ async function connect(appleId, password) { const color = (tag(r, 'calendar-color') || '').trim().slice(0, 7); calendars.push({ id: calId(href), href, name: decode(tag(r, 'displayname') || 'Calendar').trim() || 'Calendar', color: /^#[0-9a-f]{6}$/i.test(color) ? color : '#d9453a', enabled: true, events, todos }); } - if (!calendars.length) throw new Error('No calendars found on this account'); + if (!calendars.length) throw new Error(`Signed in, but no calendars were found (iCloud listed ${seen.length} item${seen.length === 1 ? '' : 's'}: ${seen.slice(0, 6).join(', ') || 'none'}). Make sure Calendar is turned on for this Apple ID in iCloud settings.`); save({ user: o.user, pass: o.pass, calendars }); return status(); }