|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -// Best-effort decode of TMTask.rt1_recurrenceRule (a binary plist that |
4 | | -// archives an EKRecurrenceRule). We don't ship a full plist parser; instead |
5 | | -// we sniff the canonical bytes that always appear in the buffer. |
| 3 | +// Decode TMTask.rt1_recurrenceRule. In current Things 3 builds this is an |
| 4 | +// XML *property list* (not a binary plist) archiving the repeat rule. It uses |
| 5 | +// short keys: |
6 | 6 | // |
7 | | -// Returns { freq, interval, weekdays, raw } where freq ∈ |
8 | | -// 'DAILY'|'WEEKLY'|'MONTHLY'|'YEARLY'|'UNKNOWN'. |
| 7 | +// fu frequency unit — an NSCalendarUnit bit value: |
| 8 | +// 4=year, 8=month, 16=day, 256=weekday(week), |
| 9 | +// 512=weekday-ordinal (nth weekday of month) |
| 10 | +// fa frequency amount — the interval ("every N units") |
| 11 | +// of array of "on" specifiers, each a dict with either |
| 12 | +// wd=<1..7> (weekday, 1=Sun) or dy=<1..31> (day of month) |
| 13 | +// tp schedule type, ed/ia/sr anchor + end dates (not needed for cadence) |
| 14 | +// |
| 15 | +// Returns { freq, interval, weekday?, dayOfMonth? } where freq ∈ |
| 16 | +// 'DAILY'|'WEEKLY'|'MONTHLY'|'YEARLY'|'UNKNOWN'. Anything we can't parse |
| 17 | +// degrades to { freq: 'UNKNOWN', interval: 1 }. |
| 18 | + |
| 19 | +function _toXml(buf) { |
| 20 | + if (buf == null) return null; |
| 21 | + if (Buffer.isBuffer(buf)) return buf.length ? buf.toString('utf8') : null; |
| 22 | + const s = String(buf); |
| 23 | + return s.length ? s : null; |
| 24 | +} |
| 25 | + |
| 26 | +function _int(xml, key) { |
| 27 | + const m = xml.match(new RegExp(`<key>${key}</key>\\s*<integer>(-?\\d+)</integer>`)); |
| 28 | + return m ? parseInt(m[1], 10) : null; |
| 29 | +} |
9 | 30 |
|
10 | | -const FREQ = { 0: 'DAILY', 1: 'WEEKLY', 2: 'MONTHLY', 3: 'YEARLY' }; |
| 31 | +// First weekday/day-of-month inside the <key>of</key> array, if any. |
| 32 | +function _ofInt(xml, subkey) { |
| 33 | + const idx = xml.indexOf('<key>of</key>'); |
| 34 | + if (idx < 0) return null; |
| 35 | + const m = xml.slice(idx).match(new RegExp(`<key>${subkey}</key>\\s*<integer>(-?\\d+)</integer>`)); |
| 36 | + return m ? parseInt(m[1], 10) : null; |
| 37 | +} |
| 38 | + |
| 39 | +function _freqFromUnit(fu) { |
| 40 | + if (fu == null) return 'UNKNOWN'; |
| 41 | + if (fu & 4) return 'YEARLY'; // NSCalendarUnitYear |
| 42 | + if (fu & 8) return 'MONTHLY'; // NSCalendarUnitMonth |
| 43 | + if (fu & 512) return 'MONTHLY'; // NSCalendarUnitWeekdayOrdinal (nth weekday) |
| 44 | + if (fu & 256) return 'WEEKLY'; // NSCalendarUnitWeekday |
| 45 | + if (fu & 16) return 'DAILY'; // NSCalendarUnitDay |
| 46 | + return 'UNKNOWN'; |
| 47 | +} |
11 | 48 |
|
12 | 49 | function decodeRecurrenceRule(buf) { |
13 | | - if (!buf || !Buffer.isBuffer(buf)) return { freq: 'UNKNOWN', interval: 1 }; |
14 | | - |
15 | | - let freq = 'UNKNOWN'; |
16 | | - let interval = 1; |
17 | | - |
18 | | - // Look for "frequency" / "interval" UTF-8 keys, then read the byte after the |
19 | | - // associated kCFNumberSInt32Type marker. This is heuristic but stable across |
20 | | - // recent Things versions. |
21 | | - const text = buf.toString('binary'); |
22 | | - |
23 | | - const fIdx = text.indexOf('frequency'); |
24 | | - if (fIdx >= 0) { |
25 | | - // After 'frequency' (9 bytes) comes a few bytes of plist metadata, then |
26 | | - // the integer value as a single byte for small numbers. |
27 | | - for (let off = fIdx + 9; off < Math.min(fIdx + 24, buf.length); off++) { |
28 | | - const b = buf[off]; |
29 | | - if (b >= 0 && b <= 3) { freq = FREQ[b]; break; } |
30 | | - } |
31 | | - } |
32 | | - |
33 | | - const iIdx = text.indexOf('interval'); |
34 | | - if (iIdx >= 0) { |
35 | | - for (let off = iIdx + 8; off < Math.min(iIdx + 24, buf.length); off++) { |
36 | | - const b = buf[off]; |
37 | | - if (b >= 1 && b <= 60) { interval = b; break; } |
38 | | - } |
39 | | - } |
40 | | - |
41 | | - return { freq, interval }; |
| 50 | + const xml = _toXml(buf); |
| 51 | + if (!xml) return { freq: 'UNKNOWN', interval: 1 }; |
| 52 | + |
| 53 | + const freq = _freqFromUnit(_int(xml, 'fu')); |
| 54 | + const fa = _int(xml, 'fa'); |
| 55 | + const interval = fa && fa > 0 ? fa : 1; |
| 56 | + |
| 57 | + const rule = { freq, interval }; |
| 58 | + const wd = _ofInt(xml, 'wd'); |
| 59 | + if (wd != null) rule.weekday = wd; // 1=Sun … 7=Sat |
| 60 | + const dy = _ofInt(xml, 'dy'); |
| 61 | + if (dy != null) rule.dayOfMonth = dy; |
| 62 | + return rule; |
42 | 63 | } |
43 | 64 |
|
44 | 65 | function describe(rule) { |
|
0 commit comments