@dawnlit/outlook-bridge 2.0.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/README.md +16 -9
  2. package/dist/backend.d.ts +2 -2
  3. package/dist/backend.js.map +1 -1
  4. package/dist/bridge.js +3 -3
  5. package/dist/bridge.js.map +1 -1
  6. package/dist/errors.d.ts +17 -8
  7. package/dist/errors.js +17 -13
  8. package/dist/errors.js.map +1 -1
  9. package/dist/index.d.ts +3 -3
  10. package/dist/index.js +3 -2
  11. package/dist/index.js.map +1 -1
  12. package/dist/mac/accounts.d.ts +4 -0
  13. package/dist/mac/accounts.js +10 -0
  14. package/dist/mac/accounts.js.map +1 -1
  15. package/dist/mac/attachments.js +1 -1
  16. package/dist/mac/attachments.js.map +1 -1
  17. package/dist/mac/bounces.js +18 -31
  18. package/dist/mac/bounces.js.map +1 -1
  19. package/dist/mac/cleanup.d.ts +11 -3
  20. package/dist/mac/cleanup.js +30 -58
  21. package/dist/mac/cleanup.js.map +1 -1
  22. package/dist/mac/drafts.d.ts +3 -1
  23. package/dist/mac/drafts.js +37 -49
  24. package/dist/mac/drafts.js.map +1 -1
  25. package/dist/mac/folders.d.ts +10 -3
  26. package/dist/mac/folders.js +31 -23
  27. package/dist/mac/folders.js.map +1 -1
  28. package/dist/mac/profile.d.ts +12 -0
  29. package/dist/mac/profile.js +75 -1
  30. package/dist/mac/profile.js.map +1 -1
  31. package/dist/mac/read.d.ts +4 -0
  32. package/dist/mac/read.js +68 -42
  33. package/dist/mac/read.js.map +1 -1
  34. package/dist/mac/run.d.ts +19 -5
  35. package/dist/mac/run.js +49 -9
  36. package/dist/mac/run.js.map +1 -1
  37. package/dist/mac/scripts.d.ts +67 -14
  38. package/dist/mac/scripts.js +150 -31
  39. package/dist/mac/scripts.js.map +1 -1
  40. package/dist/mac/send.d.ts +5 -4
  41. package/dist/mac/send.js +52 -14
  42. package/dist/mac/send.js.map +1 -1
  43. package/dist/mac/templates.js +16 -22
  44. package/dist/mac/templates.js.map +1 -1
  45. package/dist/mail.d.ts +11 -3
  46. package/dist/mail.js +25 -6
  47. package/dist/mail.js.map +1 -1
  48. package/dist/runtime.js.map +1 -1
  49. package/dist/shared/args.js.map +1 -1
  50. package/dist/shared/attachmentMatch.d.ts +0 -6
  51. package/dist/shared/attachmentMatch.js +0 -1
  52. package/dist/shared/attachmentMatch.js.map +1 -1
  53. package/dist/shared/exec.js.map +1 -1
  54. package/dist/shared/locate.d.ts +0 -8
  55. package/dist/shared/locate.js +0 -1
  56. package/dist/shared/locate.js.map +1 -1
  57. package/dist/shared/replyBody.d.ts +0 -15
  58. package/dist/shared/replyBody.js +0 -2
  59. package/dist/shared/replyBody.js.map +1 -1
  60. package/dist/shared/templateLimits.d.ts +14 -0
  61. package/dist/shared/templateLimits.js +21 -0
  62. package/dist/shared/templateLimits.js.map +1 -0
  63. package/dist/templateBody.d.ts +6 -0
  64. package/dist/templateBody.js +8 -1
  65. package/dist/templateBody.js.map +1 -1
  66. package/dist/types.d.ts +43 -7
  67. package/dist/types.js.map +1 -1
  68. package/dist/windows/accounts.js +1 -1
  69. package/dist/windows/accounts.js.map +1 -1
  70. package/dist/windows/attachments.js +2 -2
  71. package/dist/windows/attachments.js.map +1 -1
  72. package/dist/windows/bounces.js +7 -7
  73. package/dist/windows/bounces.js.map +1 -1
  74. package/dist/windows/cleanup.d.ts +5 -1
  75. package/dist/windows/cleanup.js +18 -15
  76. package/dist/windows/cleanup.js.map +1 -1
  77. package/dist/windows/drafts.d.ts +4 -4
  78. package/dist/windows/drafts.js +22 -33
  79. package/dist/windows/drafts.js.map +1 -1
  80. package/dist/windows/folders.js +2 -2
  81. package/dist/windows/folders.js.map +1 -1
  82. package/dist/{shared → windows}/json.d.ts +11 -6
  83. package/dist/{shared → windows}/json.js +12 -7
  84. package/dist/windows/json.js.map +1 -0
  85. package/dist/windows/read.js +23 -16
  86. package/dist/windows/read.js.map +1 -1
  87. package/dist/windows/run.d.ts +0 -2
  88. package/dist/windows/run.js +1 -2
  89. package/dist/windows/run.js.map +1 -1
  90. package/dist/windows/scripts.d.ts +10 -4
  91. package/dist/windows/scripts.js +21 -12
  92. package/dist/windows/scripts.js.map +1 -1
  93. package/dist/windows/send.d.ts +13 -4
  94. package/dist/windows/send.js +24 -6
  95. package/dist/windows/send.js.map +1 -1
  96. package/dist/windows/templates.js +11 -17
  97. package/dist/windows/templates.js.map +1 -1
  98. package/package.json +2 -2
  99. package/dist/shared/json.js.map +0 -1
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.profileRefusal = profileRefusal;
6
7
  exports.readProfileAccounts = readProfileAccounts;
7
8
  // Outlook's own profile database, read for the mailboxes the dictionary omits.
8
9
  //
@@ -24,7 +25,9 @@ exports.readProfileAccounts = readProfileAccounts;
24
25
  // folder id wherever the account probe comes up empty.
25
26
  //
26
27
  // Strictly read-only, and never fatal: every failure here leaves the caller with
27
- // exactly what AppleScript could see on its own.
28
+ // exactly what AppleScript could see on its own. When macOS privacy control is
29
+ // what failed, `profileRefusal` says so, for the callers that would otherwise
30
+ // report a mailbox in plain sight as missing.
28
31
  const child_process_1 = require("child_process");
29
32
  const fs_1 = __importDefault(require("fs"));
30
33
  const os_1 = __importDefault(require("os"));
@@ -111,6 +114,77 @@ function query(database, sql) {
111
114
  (0, child_process_1.execFile)('sqlite3', ['-readonly', '-list', '-noheader', '-separator', SEP, database, sql], { timeout: 5000, maxBuffer: 1024 * 1024 }, (error, stdout) => resolve(error ? '' : stdout));
112
115
  });
113
116
  }
117
+ /** The app macOS holds this process's file access against, once asked. */
118
+ let responsible = null;
119
+ /**
120
+ * The app macOS privacy control asks about this process's file access, by name:
121
+ * the outermost app bundle among its ancestors, the one launchd started. A
122
+ * process started from a terminal or an editor is that app's to grant, not its
123
+ * own, so naming this process would send the person to the wrong switch. '' when
124
+ * it can't be told.
125
+ */
126
+ function responsibleApp() {
127
+ responsible ??= new Promise(resolve => {
128
+ (0, child_process_1.execFile)('ps', ['-A', '-o', 'pid=,ppid=,comm='], {
129
+ timeout: 5000,
130
+ maxBuffer: 4 * 1024 * 1024
131
+ }, (error, stdout) => {
132
+ if (error)
133
+ return resolve('');
134
+ const processes = new Map();
135
+ for (const line of stdout.split('\n')) {
136
+ const row = /^\s*(\d+)\s+(\d+)\s+(.*)$/.exec(line);
137
+ if (row)
138
+ processes.set(Number(row[1]), { ppid: Number(row[2]), command: row[3] ?? '' });
139
+ }
140
+ let app = '';
141
+ const climbed = new Set();
142
+ for (let pid = process.pid; pid > 1 && !climbed.has(pid);) {
143
+ climbed.add(pid);
144
+ const entry = processes.get(pid);
145
+ if (!entry)
146
+ break;
147
+ // The first bundle in a path is the outer one: a helper app sits
148
+ // inside its parent's. Each one higher up replaces the last.
149
+ const bundle = /\/([^/]+)\.app(?:\/|$)/.exec(entry.command)?.[1];
150
+ if (bundle)
151
+ app = bundle;
152
+ pid = entry.ppid;
153
+ }
154
+ resolve(app);
155
+ });
156
+ });
157
+ return responsible;
158
+ }
159
+ /**
160
+ * Why Outlook's profile can't be read, when macOS privacy control is the
161
+ * reason: the sentence to act on, naming the app to grant access to. null when
162
+ * the profile can be read, or simply isn't there — New Outlook, or no Outlook
163
+ * profile on this Mac.
164
+ *
165
+ * Everything else in this module stays silent on purpose, which is right for an
166
+ * enrichment pass and wrong when the pass was the only way to see a mailbox:
167
+ * the caller then hears "no accounts" or "not found" about a mailbox in plain
168
+ * sight in Outlook. The callers that would say that ask here first.
169
+ */
170
+ async function profileRefusal() {
171
+ try {
172
+ fs_1.default.readdirSync(PROFILES_DIR);
173
+ return null;
174
+ }
175
+ catch (error) {
176
+ const code = error.code;
177
+ if (code !== 'EPERM' && code !== 'EACCES')
178
+ return null;
179
+ }
180
+ const app = await responsibleApp();
181
+ const who = app || 'the app this runs in (the terminal or editor it was started from, if it was)';
182
+ const it = app || 'that app';
183
+ // Plain ASCII: the sentence also travels inside a generated script's string literal.
184
+ return `macOS is not letting ${who} read Outlook's profile, the only place Outlook for Mac lists `
185
+ + "a Microsoft 365 mailbox it syncs through Microsoft's cloud. "
186
+ + `Turn on ${it} under System Settings > Privacy & Security > Full Disk Access, then quit ${it} and open it again.`;
187
+ }
114
188
  /**
115
189
  * Every mail account the profile knows, whether or not AppleScript can see it.
116
190
  *
@@ -1 +1 @@
1
- {"version":3,"file":"profile.js","sourceRoot":"","sources":["../../src/mac/profile.ts"],"names":[],"mappings":";;;;;AAoIA,kDASC;AA7ID,+EAA+E;AAC/E,EAAE;AACF,+EAA+E;AAC/E,2EAA2E;AAC3E,6EAA6E;AAC7E,+EAA+E;AAC/E,oEAAoE;AACpE,6EAA6E;AAC7E,yEAAyE;AACzE,6EAA6E;AAC7E,4BAA4B;AAC5B,EAAE;AACF,4EAA4E;AAC5E,+EAA+E;AAC/E,yEAAyE;AACzE,+EAA+E;AAC/E,gFAAgF;AAChF,uDAAuD;AACvD,EAAE;AACF,iFAAiF;AACjF,iDAAiD;AACjD,iDAAyC;AACzC,4CAAoB;AACpB,4CAAoB;AACpB,gDAAwB;AAExB,qFAAqF;AACrF,MAAM,YAAY,GAAG,cAAI,CAAC,IAAI,CAC1B,YAAE,CAAC,OAAO,EAAE,EACZ,SAAS,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,SAAS,EAAE,qBAAqB,CACvF,CAAC;AACF,MAAM,eAAe,GAAG,cAAc,CAAC;AAEvC;;;;;;;;;;GAUG;AACH,MAAM,kBAAkB,GAAqC;IACzD,OAAO,EAAE,CAAC;IACV,YAAY,EAAE,CAAC;IACf,eAAe,EAAE,CAAC;IAClB,QAAQ,EAAE,EAAE;IACZ,WAAW,EAAE,EAAE;IACf,aAAa,EAAE,EAAE;CACpB,CAAC;AASF,sFAAsF;AACtF,MAAM,GAAG,GAAG,MAAM,CAAC;AAEnB;;;;;;;GA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C,OAAO,EAAE,WAAW,EAAE,QAAQ,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACzD,MAAM,IAAI,GAAG,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC;QAC7C,IAAI,CAAC,OAAO,IAAI,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,IAAI,EAAE,CAAC;YAAE,SAAS;QACjE,MAAM,OAAO,GAAG,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;eAC7C,EAAC,YAAY,EAAE,OAAO,EAAE,SAAS,EAAE,EAAE,EAAC,CAAC;QAC9C,OAAO,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC3C,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,OAAO,CAAC,CAAC;IAClD,CAAC;IACD,OAAO,CAAC,GAAG,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC;AACnC,CAAC","sourcesContent":["// Outlook's own profile database, read for the mailboxes the dictionary omits.\n//\n// `getOutlookAccounts` can only enumerate `exchange accounts`, `imap accounts`\n// and `pop accounts`, and a Microsoft 365 mailbox added by the modern sync\n// engine is none of those: Outlook files it in `AccountsMail` under a 'SHDO'\n// server type, publishes no account object for it, and answers `every account`\n// with -1728 rather than listing it. Even `account of <folder>` and\n// `account of <message>` come back `missing value` inside that mailbox's own\n// tree, so there is nothing to match an address against — the mailbox is\n// invisible to every function in this directory while its mail sits in plain\n// sight in the folder list.\n//\n// The folders themselves are reachable, because `mail folder id N` resolves\n// against the application rather than through an account, and N is exactly the\n// `Record_RecordID` Outlook's profile database gives that folder. So the\n// addresses AppleScript can't report, and the ids of their well-known folders,\n// are read from that database instead, and the generated scripts fall back to a\n// folder id wherever the account probe comes up empty.\n//\n// Strictly read-only, and never fatal: every failure here leaves the caller with\n// exactly what AppleScript could see on its own.\nimport { execFile } from 'child_process';\nimport fs from 'fs';\nimport os from 'os';\nimport path from 'path';\n\n/** Where Outlook keeps its profiles, and the profile used when there are several. */\nconst PROFILES_DIR = path.join(\n os.homedir(),\n 'Library', 'Group Containers', 'UBF8T346G9.Office', 'Outlook', 'Outlook 15 Profiles',\n);\nconst DEFAULT_PROFILE = 'Main Profile';\n\n/**\n * `Folder_SpecialFolderType` for each root the generated scripts ask for, keyed\n * by the dictionary term `MAC_ROOT_TERMS` resolves.\n *\n * These are Outlook's own numbering, confirmed against a profile's local \"On My\n * Computer\" store, where every one of them appears exactly once: 1 Inbox,\n * 2 Outbox, 8 Sent Items, 9 Deleted Items, 10 Drafts, 12 Junk, 99 the account\n * root. Outbox is left out: a caller can still name it (`MAC_ROOT_TERMS` maps it),\n * but nothing here files mail into one, so it is not worth an id — that request\n * falls back to the account probe like any root the profile has no row for.\n */\nconst ROOT_SPECIAL_TYPES: Readonly<Record<string, number>> = {\n 'inbox': 1,\n 'sent items': 8,\n 'deleted items': 9,\n 'drafts': 10,\n 'junk mail': 12,\n 'root folder': 99,\n};\n\n/** One mail account as the profile database records it. */\nexport interface ProfileAccount {\n emailAccount: string;\n /** AppleScript folder id per root term — the keys of `ROOT_SPECIAL_TYPES`. */\n folderIds: Record<string, number>;\n}\n\n/** Column separator for the query output - a unit separator cannot occur in a row. */\nconst SEP = '\\x1f';\n\n/**\n * Address and well-known folder ids for every mail account in the profile.\n *\n * `Record_AccountUID` is a tagged id — the account's `Record_RecordID` in its low\n * 32 bits, a table tag above — so the join masks rather than assuming the tag,\n * and drops UID 0, which is the local \"On My Computer\" store rather than an\n * account.\n */\nconst ACCOUNTS_SQL = `\n SELECT a.Account_EmailAddress, f.Folder_SpecialFolderType, f.Record_RecordID\n FROM AccountsMail a\n JOIN Folders f ON (f.Record_AccountUID & 4294967295) = a.Record_RecordID\n WHERE f.Record_AccountUID <> 0\n AND a.Account_EmailAddress IS NOT NULL\n AND a.Account_EmailAddress <> ''\n AND f.Folder_SpecialFolderType IN (${Object.values(ROOT_SPECIAL_TYPES).join(', ')})\n ORDER BY a.Record_RecordID, f.Folder_SpecialFolderType`;\n\n/** The profile whose database to read, or '' when there is none to read. */\nfunction profileDatabase(): string {\n let names: string[];\n try {\n names = fs.readdirSync(PROFILES_DIR);\n } catch {\n return '';\n }\n // Outlook records no \"default profile\" anywhere reachable — not in its\n // preferences, not beside the profiles — so with more than one the newest\n // wins, after the name Outlook itself creates and all but everyone keeps.\n const dbOf = (name: string) => path.join(PROFILES_DIR, name, 'Data', 'Outlook.sqlite');\n // Outlook rewrites these files while it runs, and the whole directory sits\n // behind macOS privacy control, so a database that existed a moment ago need\n // not stat. An unreadable timestamp sorts last rather than throwing: this\n // runs on nearly every macOS operation (resolveMacAccount), and a mailbox\n // AppleScript could have named on its own must not be lost to it.\n const mtimeOf = (db: string) => {\n try {\n return fs.statSync(db).mtimeMs;\n } catch {\n return -Infinity;\n }\n };\n const candidates = names\n .map(dbOf)\n .filter(db => fs.existsSync(db))\n .sort((a, b) => mtimeOf(b) - mtimeOf(a));\n const preferred = dbOf(DEFAULT_PROFILE);\n if (candidates.includes(preferred)) return preferred;\n return candidates[0] ?? '';\n}\n\n/** Run one read-only query, or return '' if anything at all gets in the way. */\nfunction query(database: string, sql: string): Promise<string> {\n return new Promise(resolve => {\n execFile(\n 'sqlite3',\n ['-readonly', '-list', '-noheader', '-separator', SEP, database, sql],\n {timeout: 5000, maxBuffer: 1024 * 1024},\n (error, stdout) => resolve(error ? '' : stdout),\n );\n });\n}\n\n/**\n * Every mail account the profile knows, whether or not AppleScript can see it.\n *\n * An account whose row is missing a given root simply has no id for it, and the\n * caller keeps whatever the account probe would have done.\n */\nexport async function readProfileAccounts(): Promise<ProfileAccount[]> {\n try {\n return await readAccounts();\n } catch {\n // The module's contract, enforced at its one exit: this is an enrichment\n // pass over what AppleScript already found, so nothing that happens in it\n // is worth failing an operation that would otherwise have worked.\n return [];\n }\n}\n\nasync function readAccounts(): Promise<ProfileAccount[]> {\n const database = profileDatabase();\n if (!database) return [];\n const termOf = new Map(Object.entries(ROOT_SPECIAL_TYPES).map(([term, type]) => [type, term]));\n const byAddress = new Map<string, ProfileAccount>();\n for (const line of (await query(database, ACCOUNTS_SQL)).split('\\n')) {\n const [address, specialType, folderId] = line.split(SEP);\n const term = termOf.get(Number(specialType));\n if (!address || !term || !/^\\d+$/.test(folderId ?? '')) continue;\n const account = byAddress.get(address.toLowerCase())\n ?? {emailAccount: address, folderIds: {}};\n account.folderIds[term] = Number(folderId);\n byAddress.set(address.toLowerCase(), account);\n }\n return [...byAddress.values()];\n}\n"]}
1
+ {"version":3,"file":"profile.js","sourceRoot":"","sources":["../../src/mac/profile.ts"],"names":[],"mappings":";;;;;AAmLA,wCAeC;AAQD,kDASC;AAnND,+EAA+E;AAC/E,EAAE;AACF,+EAA+E;AAC/E,2EAA2E;AAC3E,6EAA6E;AAC7E,+EAA+E;AAC/E,oEAAoE;AACpE,6EAA6E;AAC7E,yEAAyE;AACzE,6EAA6E;AAC7E,4BAA4B;AAC5B,EAAE;AACF,4EAA4E;AAC5E,+EAA+E;AAC/E,yEAAyE;AACzE,+EAA+E;AAC/E,gFAAgF;AAChF,uDAAuD;AACvD,EAAE;AACF,iFAAiF;AACjF,+EAA+E;AAC/E,8EAA8E;AAC9E,8CAA8C;AAC9C,iDAAyC;AACzC,4CAAoB;AACpB,4CAAoB;AACpB,gDAAwB;AAExB,qFAAqF;AACrF,MAAM,YAAY,GAAG,cAAI,CAAC,IAAI,CAC1B,YAAE,CAAC,OAAO,EAAE,EACZ,SAAS,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,SAAS,EAAE,qBAAqB,CACvF,CAAC;AACF,MAAM,eAAe,GAAG,cAAc,CAAC;AAEvC;;;;;;;;;;GAUG;AACH,MAAM,kBAAkB,GAAqC;IACzD,OAAO,EAAE,CAAC;IACV,YAAY,EAAE,CAAC;IACf,eAAe,EAAE,CAAC;IAClB,QAAQ,EAAE,EAAE;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AAC,CAAC;QAC7C,IAAI,CAAC,OAAO,IAAI,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,IAAI,EAAE,CAAC;YAAE,SAAS;QACjE,MAAM,OAAO,GAAG,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;eAC7C,EAAE,YAAY,EAAE,OAAO,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC;QAChD,OAAO,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC3C,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,OAAO,CAAC,CAAC;IAClD,CAAC;IACD,OAAO,CAAC,GAAG,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC;AACnC,CAAC","sourcesContent":["// Outlook's own profile database, read for the mailboxes the dictionary omits.\n//\n// `getOutlookAccounts` can only enumerate `exchange accounts`, `imap accounts`\n// and `pop accounts`, and a Microsoft 365 mailbox added by the modern sync\n// engine is none of those: Outlook files it in `AccountsMail` under a 'SHDO'\n// server type, publishes no account object for it, and answers `every account`\n// with -1728 rather than listing it. Even `account of <folder>` and\n// `account of <message>` come back `missing value` inside that mailbox's own\n// tree, so there is nothing to match an address against — the mailbox is\n// invisible to every function in this directory while its mail sits in plain\n// sight in the folder list.\n//\n// The folders themselves are reachable, because `mail folder id N` resolves\n// against the application rather than through an account, and N is exactly the\n// `Record_RecordID` Outlook's profile database gives that folder. So the\n// addresses AppleScript can't report, and the ids of their well-known folders,\n// are read from that database instead, and the generated scripts fall back to a\n// folder id wherever the account probe comes up empty.\n//\n// Strictly read-only, and never fatal: every failure here leaves the caller with\n// exactly what AppleScript could see on its own. When macOS privacy control is\n// what failed, `profileRefusal` says so, for the callers that would otherwise\n// report a mailbox in plain sight as missing.\nimport { execFile } from 'child_process';\nimport fs from 'fs';\nimport os from 'os';\nimport path from 'path';\n\n/** Where Outlook keeps its profiles, and the profile used when there are several. */\nconst PROFILES_DIR = path.join(\n os.homedir(),\n 'Library', 'Group Containers', 'UBF8T346G9.Office', 'Outlook', 'Outlook 15 Profiles',\n);\nconst DEFAULT_PROFILE = 'Main Profile';\n\n/**\n * `Folder_SpecialFolderType` for each root the generated scripts ask for, keyed\n * by the dictionary term `MAC_ROOT_TERMS` resolves.\n *\n * These are Outlook's own numbering, confirmed against a profile's local \"On My\n * Computer\" store, where every one of them appears exactly once: 1 Inbox,\n * 2 Outbox, 8 Sent Items, 9 Deleted Items, 10 Drafts, 12 Junk, 99 the account\n * root. Outbox is left out: a caller can still name it (`MAC_ROOT_TERMS` maps it),\n * but nothing here files mail into one, so it is not worth an id — that request\n * falls back to the account probe like any root the profile has no row for.\n */\nconst ROOT_SPECIAL_TYPES: Readonly<Record<string, number>> = {\n 'inbox': 1,\n 'sent items': 8,\n 'deleted items': 9,\n 'drafts': 10,\n 'junk mail': 12,\n 'root folder': 99,\n};\n\n/** One mail account as the profile database records it. */\nexport interface ProfileAccount {\n emailAccount: string;\n /** AppleScript folder id per root term — the keys of `ROOT_SPECIAL_TYPES`. */\n folderIds: Record<string, number>;\n}\n\n/** Column separator for the query output - a unit separator cannot occur in a row. */\nconst SEP = '\\x1f';\n\n/**\n * Address and well-known folder ids for every mail account in the profile.\n *\n * `Record_AccountUID` is a tagged id — the account's `Record_RecordID` in its low\n * 32 bits, a table tag above — so the join masks rather than assuming the tag,\n * and drops UID 0, which is the local \"On My Computer\" store rather than an\n * account.\n */\nconst ACCOUNTS_SQL = `\n SELECT a.Account_EmailAddress, f.Folder_SpecialFolderType, f.Record_RecordID\n FROM AccountsMail a\n JOIN Folders f ON (f.Record_AccountUID & 4294967295) = a.Record_RecordID\n WHERE f.Record_AccountUID <> 0\n AND a.Account_EmailAddress IS NOT NULL\n AND a.Account_EmailAddress <> ''\n AND f.Folder_SpecialFolderType IN (${Object.values(ROOT_SPECIAL_TYPES).join(', ')})\n ORDER BY a.Record_RecordID, f.Folder_SpecialFolderType`;\n\n/** The profile whose database to read, or '' when there is none to read. */\nfunction profileDatabase(): string {\n let names: string[];\n try {\n names = fs.readdirSync(PROFILES_DIR);\n } catch {\n return '';\n }\n // Outlook records no \"default profile\" anywhere reachable — not in its\n // preferences, not beside the profiles — so with more than one the newest\n // wins, after the name Outlook itself creates and all but everyone keeps.\n const dbOf = (name: string) => path.join(PROFILES_DIR, name, 'Data', 'Outlook.sqlite');\n // Outlook rewrites these files while it runs, and the whole directory sits\n // behind macOS privacy control, so a database that existed a moment ago need\n // not stat. An unreadable timestamp sorts last rather than throwing: this\n // runs on nearly every macOS operation (resolveMacAccount), and a mailbox\n // AppleScript could have named on its own must not be lost to it.\n const mtimeOf = (db: string) => {\n try {\n return fs.statSync(db).mtimeMs;\n } catch {\n return -Infinity;\n }\n };\n const candidates = names\n .map(dbOf)\n .filter(db => fs.existsSync(db))\n .sort((a, b) => mtimeOf(b) - mtimeOf(a));\n const preferred = dbOf(DEFAULT_PROFILE);\n if (candidates.includes(preferred)) return preferred;\n return candidates[0] ?? '';\n}\n\n/** Run one read-only query, or return '' if anything at all gets in the way. */\nfunction query(database: string, sql: string): Promise<string> {\n return new Promise(resolve => {\n execFile(\n 'sqlite3',\n ['-readonly', '-list', '-noheader', '-separator', SEP, database, sql],\n { timeout: 5000, maxBuffer: 1024 * 1024 },\n (error, stdout) => resolve(error ? '' : stdout),\n );\n });\n}\n\n/** The app macOS holds this process's file access against, once asked. */\nlet responsible: Promise<string> | null = null;\n\n/**\n * The app macOS privacy control asks about this process's file access, by name:\n * the outermost app bundle among its ancestors, the one launchd started. A\n * process started from a terminal or an editor is that app's to grant, not its\n * own, so naming this process would send the person to the wrong switch. '' when\n * it can't be told.\n */\nfunction responsibleApp(): Promise<string> {\n responsible ??= new Promise(resolve => {\n execFile('ps', ['-A', '-o', 'pid=,ppid=,comm='], {\n timeout: 5000,\n maxBuffer: 4 * 1024 * 1024\n }, (error, stdout) => {\n if (error) return resolve('');\n const processes = new Map<number, { ppid: number; command: string }>();\n for (const line of stdout.split('\\n')) {\n const row = /^\\s*(\\d+)\\s+(\\d+)\\s+(.*)$/.exec(line);\n if (row) processes.set(Number(row[1]), { ppid: Number(row[2]), command: row[3] ?? '' });\n }\n let app = '';\n const climbed = new Set<number>();\n for (let pid = process.pid; pid > 1 && !climbed.has(pid);) {\n climbed.add(pid);\n const entry = processes.get(pid);\n if (!entry) break;\n // The first bundle in a path is the outer one: a helper app sits\n // inside its parent's. Each one higher up replaces the last.\n const bundle = /\\/([^/]+)\\.app(?:\\/|$)/.exec(entry.command)?.[1];\n if (bundle) app = bundle;\n pid = entry.ppid;\n }\n resolve(app);\n });\n });\n return responsible;\n}\n\n/**\n * Why Outlook's profile can't be read, when macOS privacy control is the\n * reason: the sentence to act on, naming the app to grant access to. null when\n * the profile can be read, or simply isn't there — New Outlook, or no Outlook\n * profile on this Mac.\n *\n * Everything else in this module stays silent on purpose, which is right for an\n * enrichment pass and wrong when the pass was the only way to see a mailbox:\n * the caller then hears \"no accounts\" or \"not found\" about a mailbox in plain\n * sight in Outlook. The callers that would say that ask here first.\n */\nexport async function profileRefusal(): Promise<string | null> {\n try {\n fs.readdirSync(PROFILES_DIR);\n return null;\n } catch (error) {\n const code = (error as NodeJS.ErrnoException).code;\n if (code !== 'EPERM' && code !== 'EACCES') return null;\n }\n const app = await responsibleApp();\n const who = app || 'the app this runs in (the terminal or editor it was started from, if it was)';\n const it = app || 'that app';\n // Plain ASCII: the sentence also travels inside a generated script's string literal.\n return `macOS is not letting ${who} read Outlook's profile, the only place Outlook for Mac lists `\n + \"a Microsoft 365 mailbox it syncs through Microsoft's cloud. \"\n + `Turn on ${it} under System Settings > Privacy & Security > Full Disk Access, then quit ${it} and open it again.`;\n}\n\n/**\n * Every mail account the profile knows, whether or not AppleScript can see it.\n *\n * An account whose row is missing a given root simply has no id for it, and the\n * caller keeps whatever the account probe would have done.\n */\nexport async function readProfileAccounts(): Promise<ProfileAccount[]> {\n try {\n return await readAccounts();\n } catch {\n // The module's contract, enforced at its one exit: this is an enrichment\n // pass over what AppleScript already found, so nothing that happens in it\n // is worth failing an operation that would otherwise have worked.\n return [];\n }\n}\n\nasync function readAccounts(): Promise<ProfileAccount[]> {\n const database = profileDatabase();\n if (!database) return [];\n const termOf = new Map(Object.entries(ROOT_SPECIAL_TYPES).map(([term, type]) => [type, term]));\n const byAddress = new Map<string, ProfileAccount>();\n for (const line of (await query(database, ACCOUNTS_SQL)).split('\\n')) {\n const [address, specialType, folderId] = line.split(SEP);\n const term = termOf.get(Number(specialType));\n if (!address || !term || !/^\\d+$/.test(folderId ?? '')) continue;\n const account = byAddress.get(address.toLowerCase())\n ?? { emailAccount: address, folderIds: {} };\n account.folderIds[term] = Number(folderId);\n byAddress.set(address.toLowerCase(), account);\n }\n return [...byAddress.values()];\n}\n"]}
@@ -22,6 +22,10 @@ export declare function searchInboxByFilter(request: SearchRequest): Promise<Inb
22
22
  /**
23
23
  * The email currently selected — or open — in Outlook. `current messages`
24
24
  * covers both cases the Windows reader handles separately.
25
+ *
26
+ * Where it sits comes back as a second record, so the detail row keeps its body
27
+ * last. A mailbox with no account object answers `account of` with missing
28
+ * value; its root folder's id is then looked up in the profile database.
25
29
  */
26
30
  export declare function readSelectedEmail(): Promise<SelectedEmail>;
27
31
  /** Open an email in Outlook and bring Outlook forward. */
package/dist/mac/read.js CHANGED
@@ -14,6 +14,7 @@ exports.openOutlookEmail = openOutlookEmail;
14
14
  // per-message in a loop costs an event per property and is unusably slow on a
15
15
  // real mailbox, which is the whole reason for the shape.
16
16
  const run_1 = require("./run");
17
+ const profile_1 = require("./profile");
17
18
  const scripts_1 = require("./scripts");
18
19
  const errors_1 = require("../errors");
19
20
  const mail_1 = require("../mail");
@@ -62,32 +63,22 @@ end tell`, 'standard'))
62
63
  if (chosen.length === 0)
63
64
  return [];
64
65
  // Pass 2 — the expensive reads, only for the messages kept.
65
- const preview = request.previewChars > 0
66
- ? ` set bodyText to ""
67
- try
68
- set bodyText to (plain text content of theMsg) as string
69
- end try
70
- if (length of bodyText) > ${(0, run_1.asInt)(request.previewChars)} then set bodyText to text 1 thru ${(0, run_1.asInt)(request.previewChars)} of bodyText`
71
- : ' set bodyText to ""';
72
66
  const raw = await (0, run_1.runOsaScript)(`tell application "Microsoft Outlook"
73
67
  set out to ""
74
68
  repeat with theId in ${(0, run_1.asIdList)(chosen.map(c => c.id))}
75
- set theMsg to missing value
76
- try
77
- set theMsg to message id theId
78
- end try
69
+ ${(0, scripts_1.messageByIdSnippet)(' ')}
79
70
  if theMsg is not missing value then
80
71
  set subj to ""
81
72
  try
82
73
  set subj to (subject of theMsg) as string
83
74
  end try
84
- ${outgoing ? (0, scripts_1.firstRecipientSnippet)('theMsg', ' ') : (0, scripts_1.senderSnippet)('theMsg', ' ')}
85
- ${preview}
75
+ ${outgoing ? (0, scripts_1.firstRecipientSnippet)(' ') : (0, scripts_1.senderSnippet)(' ')}
76
+ ${(0, scripts_1.bodyTextSnippet)(' ', request.previewChars)}
86
77
  set attNames to {}
87
78
  try
88
79
  set attNames to name of every attachment of theMsg
89
80
  end try
90
- set out to out & ${(0, run_1.asRow)(['(id of theMsg as string)', 'subj', 'sndName', 'sndAddr', 'bodyText', 'my sanitizeList(attNames)'])}
81
+ set out to out & ${(0, run_1.asRow)(['(id of theMsg as string)', 'subj', 'sndName', 'sndAddr', 'bodyText', 'my sanitizePositionalList(attNames)'])}
91
82
  end if
92
83
  end repeat
93
84
  return out
@@ -96,7 +87,7 @@ end tell`, 'standard');
96
87
  return (0, run_1.splitRecords)(raw).map(record => {
97
88
  const parts = (0, run_1.splitFields)(record);
98
89
  const id = (0, run_1.field)(parts, 0);
99
- const attachmentNames = (0, run_1.splitList)((0, run_1.field)(parts, 5));
90
+ const attachmentNames = (0, run_1.splitPositionalList)((0, run_1.field)(parts, 5));
100
91
  return {
101
92
  entryId: id,
102
93
  storeId: '',
@@ -129,7 +120,7 @@ end tell`, 'quick'));
129
120
  senderEmail: (0, run_1.field)(parts, scripts_1.MessageDetail.senderEmail),
130
121
  receivedTime: (0, run_1.field)(parts, scripts_1.MessageDetail.receivedTime),
131
122
  body: (0, run_1.field)(parts, scripts_1.MessageDetail.body),
132
- attachmentNames: (0, run_1.splitList)((0, run_1.field)(parts, scripts_1.MessageDetail.attachmentNames)),
123
+ attachmentNames: (0, run_1.splitPositionalList)((0, run_1.field)(parts, scripts_1.MessageDetail.attachmentNames)),
133
124
  };
134
125
  }
135
126
  /**
@@ -144,11 +135,12 @@ async function searchInboxByFilter(request) {
144
135
  const acct = await (0, scripts_1.resolveMacAccount)(request.account);
145
136
  // AppleScript reaches a child folder by name, so a path entry narrows to its leaf.
146
137
  const asNameList = (list) => `{${list.map(mail_1.folderLeafName).filter(Boolean).map(run_1.asString).join(', ')}}`;
147
- const rootInScope = request.includeFolders.length === 0 ? 'true' : 'false';
148
138
  // Sent, Drafts, Deleted and Junk hang UNDER the Inbox on an IMAP profile, so
149
139
  // their ids are collected first and the walk never descends into them — by
150
140
  // the profile's own id where it records one, through the account otherwise.
151
- const skipIds = ['sent items', 'deleted items', 'drafts', 'junk mail']
141
+ // The roots are the ones Windows cuts, from the one list both read.
142
+ const skipIds = mail_1.NON_INCOMING_ROOTS
143
+ .map(rootId => scripts_1.MAC_ROOT_TERMS[rootId])
152
144
  .map(term => {
153
145
  const id = acct.folderIds?.[term];
154
146
  return id !== undefined
@@ -222,12 +214,23 @@ ${skipIds}
222
214
  set skipNames to ${asNameList(request.excludeFolders)}
223
215
  set onlyNames to ${asNameList(request.includeFolders)}
224
216
  end tell
217
+ -- The root is read on the terms its children are: when no folder is named, or
218
+ -- when it is one of those named.
219
+ set rootScope to (count of onlyNames) is 0
220
+ try
221
+ if onlyNames contains rootName then set rootScope to true
222
+ end try
225
223
  set cutoff to (current date) - (${(0, run_1.asInt)(request.daysBack)} * days)
226
- return my scanFolder(rootInbox, rootName, cutoff, skipIds, skipNames, onlyNames, ${rootInScope})`, 'scan'));
224
+ return my scanFolder(rootInbox, rootName, cutoff, skipIds, skipNames, onlyNames, rootScope)`, 'scan'));
227
225
  const subjectFiltered = !!(request.subjectLike || request.subjectPattern || request.excludeReplies);
228
226
  const likeRe = request.subjectLike ? new RegExp((0, mail_1.subjectGlobSource)(request.subjectLike), 'i') : null;
229
- // PowerShell's -match is case-insensitive, so the Windows test is too.
230
- const patternRe = request.subjectPattern ? new RegExp(request.subjectPattern.source, 'i') : null;
227
+ // PowerShell's -match is case-insensitive, so this test is too, whatever the
228
+ // caller's flags. Only u and v are kept: they decide how the source parses,
229
+ // and without u, /\p{L}/u would match the literal text "p{L}". Keeping g or y
230
+ // would carry lastIndex from one subject to the next.
231
+ const patternRe = request.subjectPattern
232
+ ? new RegExp(request.subjectPattern.source, 'i' + request.subjectPattern.flags.replace(/[^uv]/g, ''))
233
+ : null;
231
234
  const candidates = indexed
232
235
  .map(record => {
233
236
  const parts = (0, run_1.splitFields)(record);
@@ -253,41 +256,32 @@ return my scanFolder(rootInbox, rootName, cutoff, skipIds, skipNames, onlyNames,
253
256
  if (candidates.length === 0)
254
257
  return [];
255
258
  // Pass 2 — senders, attachment names and (when asked) bodies, for the survivors only.
256
- const body = request.includeBody
257
- ? ` set bodyText to ""
258
- try
259
- set bodyText to (plain text content of theMsg) as string
260
- end try`
261
- : ' set bodyText to ""';
262
- const details = new Map();
263
259
  const raw = await (0, run_1.runOsaScript)(`tell application "Microsoft Outlook"
264
260
  set out to ""
265
261
  repeat with theId in ${(0, run_1.asIdList)(candidates.map(c => c.id))}
266
- set theMsg to missing value
267
- try
268
- set theMsg to message id theId
269
- end try
262
+ ${(0, scripts_1.messageByIdSnippet)(' ')}
270
263
  if theMsg is not missing value then
271
- ${(0, scripts_1.senderSnippet)('theMsg', ' ')}
264
+ ${(0, scripts_1.senderSnippet)(' ')}
272
265
  set attNames to {}
273
266
  try
274
267
  set attNames to name of every attachment of theMsg
275
268
  end try
276
- ${body}
277
- set out to out & ${(0, run_1.asRow)(['(id of theMsg as string)', 'sndName', 'sndAddr', 'my sanitizeList(attNames)', 'bodyText'])}
269
+ ${(0, scripts_1.bodyTextSnippet)(' ', request.includeBody ? undefined : 0)}
270
+ set out to out & ${(0, run_1.asRow)(['(id of theMsg as string)', 'sndName', 'sndAddr', 'my sanitizePositionalList(attNames)', 'bodyText'])}
278
271
  end if
279
272
  end repeat
280
273
  return out
281
274
  end tell`, 'scan');
282
- for (const record of (0, run_1.splitRecords)(raw)) {
275
+ const details = new Map((0, run_1.splitRecords)(raw).map(record => {
283
276
  const parts = (0, run_1.splitFields)(record);
284
- details.set((0, run_1.field)(parts, 0), {
277
+ const detail = {
285
278
  senderName: (0, run_1.field)(parts, 1),
286
279
  senderEmail: (0, run_1.field)(parts, 2),
287
- attachmentNames: (0, run_1.splitList)((0, run_1.field)(parts, 3)),
280
+ attachmentNames: (0, run_1.splitPositionalList)((0, run_1.field)(parts, 3)),
288
281
  body: (0, run_1.field)(parts, 4),
289
- });
290
- }
282
+ };
283
+ return [(0, run_1.field)(parts, 0), detail];
284
+ }));
291
285
  const matches = [];
292
286
  for (const candidate of candidates) {
293
287
  const detail = details.get(candidate.id);
@@ -313,6 +307,10 @@ end tell`, 'scan');
313
307
  /**
314
308
  * The email currently selected — or open — in Outlook. `current messages`
315
309
  * covers both cases the Windows reader handles separately.
310
+ *
311
+ * Where it sits comes back as a second record, so the detail row keeps its body
312
+ * last. A mailbox with no account object answers `account of` with missing
313
+ * value; its root folder's id is then looked up in the profile database.
316
314
  */
317
315
  async function readSelectedEmail() {
318
316
  const records = (0, run_1.splitRecords)(await (0, run_1.runOsaScript)(`tell application "Microsoft Outlook"
@@ -323,11 +321,31 @@ async function readSelectedEmail() {
323
321
  if (count of sel) is 0 then error ${(0, run_1.asString)(`${(0, errors_1.failureTag)('NOT_FOUND', 'email')}No email is selected in Outlook. Select or open an email, then try again.`)}
324
322
  set theMsg to item 1 of sel
325
323
  ${(0, scripts_1.messageDetailSnippet)(true)}
326
- return ${(0, run_1.asRow)((0, scripts_1.messageDetailFields)(true))}
324
+ set acctAddr to ""
325
+ try
326
+ set rawAddr to email address of (account of theMsg)
327
+ if rawAddr is not missing value then set acctAddr to rawAddr as string
328
+ end try
329
+ set chainNames to {}
330
+ set rootId to ""
331
+ set theFolder to missing value
332
+ try
333
+ set theFolder to folder of theMsg
334
+ end try
335
+ if theFolder is not missing value then
336
+ ${(0, scripts_1.folderChainSnippet)(' ')}
337
+ try
338
+ set rootId to (id of cur) as string
339
+ end try
340
+ end if
341
+ return ${(0, run_1.asRow)((0, scripts_1.messageDetailFields)(true))} & ${(0, run_1.asRow)(['acctAddr', 'my sanitizeList(chainNames)', 'rootId'])}
327
342
  end tell`, 'quick'));
328
343
  if (records.length === 0)
329
344
  throw new errors_1.NotFoundError('email', 'Outlook reported no selected email.');
330
345
  const parts = (0, run_1.splitFields)(records[0]);
346
+ const where = (0, run_1.splitFields)(records[1] ?? '');
347
+ const chain = (0, run_1.splitList)((0, run_1.field)(where, 1)).reverse();
348
+ const account = (0, run_1.field)(where, 0) || (chain.length ? await accountByRootFolder((0, run_1.field)(where, 2)) : '');
331
349
  return {
332
350
  entryId: (0, run_1.field)(parts, scripts_1.MessageDetail.id),
333
351
  storeId: '',
@@ -336,9 +354,17 @@ end tell`, 'quick'));
336
354
  senderEmail: (0, run_1.field)(parts, scripts_1.MessageDetail.senderEmail),
337
355
  receivedTime: (0, run_1.field)(parts, scripts_1.MessageDetail.receivedTime),
338
356
  body: (0, run_1.field)(parts, scripts_1.MessageDetail.body),
339
- attachmentNames: (0, run_1.splitList)((0, run_1.field)(parts, scripts_1.MessageDetail.attachmentNames)),
357
+ attachmentNames: (0, run_1.splitPositionalList)((0, run_1.field)(parts, scripts_1.MessageDetail.attachmentNames)),
358
+ folderPath: account && chain.length ? (0, scripts_1.macFolderPath)(account, chain[0], chain.slice(1)) : '',
340
359
  };
341
360
  }
361
+ /** The address of the account whose root folder has this id, per the profile database; '' if none. */
362
+ async function accountByRootFolder(rootId) {
363
+ if (!/^\d+$/.test(rootId))
364
+ return '';
365
+ const id = Number(rootId);
366
+ return (await (0, profile_1.readProfileAccounts)()).find(a => a.folderIds['root folder'] === id)?.emailAccount ?? '';
367
+ }
342
368
  /** Open an email in Outlook and bring Outlook forward. */
343
369
  async function openOutlookEmail(email) {
344
370
  const id = (0, scripts_1.macMessageId)(email.entryId);
@@ -1 +1 @@
1
- {"version":3,"file":"read.js","sourceRoot":"","sources":["../../src/mac/read.ts"],"names":[],"mappings":";;AA6CA,0CA2FC;AAMD,sCAgBC;AAUD,kDA2KC;AAMD,8CAuBC;AAGD,4CAQC;AA3XD,+EAA+E;AAC/E,sCAAsC;AACtC,EAAE;AACF,+EAA+E;AAC/E,sEAAsE;AACtE,qEAAqE;AACrE,8EAA8E;AAC9E,yDAAyD;AACzD,+BAYe;AACf,uCAcmB;AACnB,sCAAsD;AACtD,kCAA0F;AAI1F;;;GAGG;AACI,KAAK,UAAU,eAAe,CAAC,OAAyB;IAC3D,MAAM,IAAI,GAAG,MAAM,IAAA,2BAAiB,EAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACtD,MAAM,UAAU,GAAG,IAAA,uBAAa,EAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACrG,MAAM,QAAQ,GAAG,IAAA,qBAAc,EAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAEvD,+EAA+E;IAC/E,MAAM,KAAK,GAAG,CAAC,MAAM,IAAA,kBAAY,EAAC;EACpC,IAAA,8BAAoB,EAAC,IAAI,CAAC;EAC1B,IAAA,0BAAgB,EAAC,IAAI,EAAE,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,WAAW,CAAC;sCACvB,IAAA,WAAK,EAAC,OAAO,CAAC,QAAQ,CAAC;;;0BAGnC,IAAA,sBAAY,EAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC;;;;;;;;;;;;;;;;SAgBpD,EAAE,UAAU,CAAC,CA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C,CAAC,CAAC,CAAC;IACtC,OAAO;QACH,OAAO,EAAE,IAAA,WAAK,EAAC,KAAK,EAAE,uBAAa,CAAC,EAAE,CAAC;QACvC,OAAO,EAAE,EAAE;QACX,OAAO,EAAE,IAAA,WAAK,EAAC,KAAK,EAAE,uBAAa,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE;QACnD,UAAU,EAAE,IAAA,WAAK,EAAC,KAAK,EAAE,uBAAa,CAAC,UAAU,CAAC;QAClD,WAAW,EAAE,IAAA,WAAK,EAAC,KAAK,EAAE,uBAAa,CAAC,WAAW,CAAC;QACpD,YAAY,EAAE,IAAA,WAAK,EAAC,KAAK,EAAE,uBAAa,CAAC,YAAY,CAAC;QACtD,IAAI,EAAE,IAAA,WAAK,EAAC,KAAK,EAAE,uBAAa,CAAC,IAAI,CAAC;QACtC,eAAe,EAAE,IAAA,eAAS,EAAC,IAAA,WAAK,EAAC,KAAK,EAAE,uBAAa,CAAC,eAAe,CAAC,CAAC;KAC1E,CAAC;AACN,CAAC;AAED,0DAA0D;AACnD,KAAK,UAAU,gBAAgB,CAAC,KAAmB;IACtD,MAAM,EAAE,GAAG,IAAA,sBAAY,EAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACvC,MAAM,IAAA,kBAAY,EAAC;;EAErB,IAAA,8BAAoB,EAAC,EAAE,CAAC;;;SAGjB,EAAE,OAAO,CAAC,CAAC;AACpB,CAAC","sourcesContent":["// Reading mail on macOS: one folder, one item, a whole Inbox tree, or whatever\n// is selected in the running Outlook.\n//\n// Every reader is two passes. Pass 1 indexes a folder with bulk property reads\n// (one Apple event each); pass 2 fetches the expensive fields — body,\n// attachments, sender — only for the messages that survived. Reading\n// per-message in a loop costs an event per property and is unusably slow on a\n// real mailbox, which is the whole reason for the shape.\nimport {\n AS_LIST_SEP,\n asIdList,\n asInt,\n asRow,\n asString,\n field,\n runOsaScript,\n splitFields,\n splitList,\n splitRecords,\n summaryFields\n} from './run';\nimport {\n accountLookupSnippet,\n dateProperty,\n firstRecipientSnippet,\n macFolderPath,\n macMessageId,\n mailScopeSnippet,\n MessageDetail,\n messageDetailFields,\n messageDetailSnippet,\n messageLookupSnippet,\n resolveMacAccount,\n rootFolderSnippet,\n senderSnippet,\n} from './scripts';\nimport { failureTag, NotFoundError } from '../errors';\nimport { folderLeafName, isOutgoingRoot, REPLY_PREFIX, subjectGlobSource } from '../mail';\nimport type { RawEmail, ReadInboxRequest, SearchRequest } from '../backend';\nimport type { EmailLocator, InboxEmail, InboxSearchMatch, SelectedEmail } from '../types';\n\n/**\n * Recent mail from the Inbox root or one folder, newest first. Entry ids here\n * are Outlook for Mac's integer message ids, not Windows EntryIDs.\n */\nexport async function readInboxEmails(request: ReadInboxRequest): Promise<InboxEmail[]> {\n const acct = await resolveMacAccount(request.account);\n const folderPath = macFolderPath(request.account, request.folder.rootLabel, request.folder.segments);\n const outgoing = isOutgoingRoot(request.folder.rootId);\n\n // Pass 1 — index the folder. Its order isn't documented, so it is sorted here.\n const index = (await runOsaScript(`tell application \"Microsoft Outlook\"\n${accountLookupSnippet(acct)}\n${mailScopeSnippet(acct, request.folder, request.folderLabel)}\n set cutoff to (current date) - (${asInt(request.daysBack)} * days)\n set idList to id of every message of scopeFolder\n try\n set timeList to ${dateProperty(request.folder.rootId)} of every message of scopeFolder\n on error\n set timeList to {}\n end try\n set hasTimes to ((count of timeList) is (count of idList))\n set out to \"\"\n repeat with i from 1 to (count of idList)\n set d to missing value\n if hasTimes then set d to item i of timeList\n -- An item with no timestamp of its own (an unsent draft filed into a\n -- mail folder) is skipped, as the Windows date filter skips it.\n if d is not missing value and d is greater than or equal to cutoff then\n set out to out & (item i of idList as string) & tab & my isoDate(d) & linefeed\n end if\n end repeat\n return out\nend tell`, 'standard'))\n .split('\\n')\n .map(line => line.trim())\n .filter(Boolean)\n .map(line => {\n const [id, receivedTime] = line.split('\\t');\n return {id, receivedTime: receivedTime || ''};\n });\n // 'yyyy-MM-dd HH:mm' sorts correctly as a string.\n index.sort((a, b) => b.receivedTime.localeCompare(a.receivedTime));\n const chosen = index.slice(0, request.limit);\n if (chosen.length === 0) return [];\n\n // Pass 2 — the expensive reads, only for the messages kept.\n const preview = request.previewChars > 0\n ? ` set bodyText to \"\"\n try\n set bodyText to (plain text content of theMsg) as string\n end try\n if (length of bodyText) > ${asInt(request.previewChars)} then set bodyText to text 1 thru ${asInt(request.previewChars)} of bodyText`\n : ' set bodyText to \"\"';\n const raw = await runOsaScript(`tell application \"Microsoft Outlook\"\n set out to \"\"\n repeat with theId in ${asIdList(chosen.map(c => c.id))}\n set theMsg to missing value\n try\n set theMsg to message id theId\n end try\n if theMsg is not missing value then\n set subj to \"\"\n try\n set subj to (subject of theMsg) as string\n end try\n${outgoing ? firstRecipientSnippet('theMsg', ' ') : senderSnippet('theMsg', ' ')}\n${preview}\n set attNames to {}\n try\n set attNames to name of every attachment of theMsg\n end try\n set out to out & ${asRow(['(id of theMsg as string)', 'subj', 'sndName', 'sndAddr', 'bodyText', 'my sanitizeList(attNames)'])}\n end if\n end repeat\n return out\nend tell`, 'standard');\n\n const receivedById = new Map(chosen.map(c => [c.id, c.receivedTime]));\n return splitRecords(raw).map(record => {\n const parts = splitFields(record);\n const id = field(parts, 0);\n const attachmentNames = splitList(field(parts, 5));\n return {\n entryId: id,\n storeId: '',\n subject: field(parts, 1).trim(),\n senderName: field(parts, 2),\n senderEmail: field(parts, 3),\n receivedTime: receivedById.get(id) || '',\n bodyPreview: field(parts, 4),\n attachmentNames,\n attachmentCount: attachmentNames.length,\n folderPath,\n };\n });\n}\n\n/**\n * One email's full plain-text body. `storeId` has no macOS meaning: `message id\n * N` resolves against the application, wherever the message sits.\n */\nexport async function readEmailBody(email: EmailLocator): Promise<RawEmail> {\n const id = macMessageId(email.entryId);\n const parts = summaryFields(await runOsaScript(`tell application \"Microsoft Outlook\"\n${messageLookupSnippet(id)}\n${messageDetailSnippet(true)}\n return ${asRow(messageDetailFields(true))}\nend tell`, 'quick'));\n return {\n entryId: field(parts, MessageDetail.id) || id,\n subject: field(parts, MessageDetail.subject).trim(),\n senderName: field(parts, MessageDetail.senderName),\n senderEmail: field(parts, MessageDetail.senderEmail),\n receivedTime: field(parts, MessageDetail.receivedTime),\n body: field(parts, MessageDetail.body),\n attachmentNames: splitList(field(parts, MessageDetail.attachmentNames)),\n };\n}\n\n/**\n * Walk every folder under the Inbox for the mail matching the request.\n *\n * The filtering happens here rather than in Outlook: there is no AppleScript\n * counterpart to COM's server-side `Items.Restrict`, so each folder is indexed in\n * bulk reads and the subject and date tests run on the index. The results match\n * Windows; the cost doesn't, and a wide window over a big tree is heavier here.\n */\nexport async function searchInboxByFilter(request: SearchRequest): Promise<InboxSearchMatch[]> {\n const acct = await resolveMacAccount(request.account);\n // AppleScript reaches a child folder by name, so a path entry narrows to its leaf.\n const asNameList = (list: readonly string[]) => `{${list.map(folderLeafName).filter(Boolean).map(asString).join(', ')}}`;\n const rootInScope = request.includeFolders.length === 0 ? 'true' : 'false';\n // Sent, Drafts, Deleted and Junk hang UNDER the Inbox on an IMAP profile, so\n // their ids are collected first and the walk never descends into them — by\n // the profile's own id where it records one, through the account otherwise.\n const skipIds = ['sent items', 'deleted items', 'drafts', 'junk mail']\n .map(term => {\n const id = acct.folderIds?.[term];\n return id !== undefined\n ? ` set end of skipIds to ${id}`\n : ` try\n set end of skipIds to (id of (${term} of targetAcct))\n end try`;\n })\n .join('\\n');\n\n // Pass 1 — index every folder under the Inbox: path, id, subject, received.\n const indexed = splitRecords(await runOsaScript(`on scanFolder(theFolder, prefix, cutoff, skipIds, skipNames, onlyNames, inScope)\n set out to \"\"\n set idList to {}\n set subjList to {}\n set timeList to {}\n -- A folder the walk only passes through is never read: the bulk reads are\n -- the expensive part.\n if inScope then\n tell application \"Microsoft Outlook\"\n set idList to id of every message of theFolder\n set subjList to subject of every message of theFolder\n try\n set timeList to time received of every message of theFolder\n on error\n set timeList to {}\n end try\n end tell\n end if\n tell application \"Microsoft Outlook\"\n set subs to mail folders of theFolder\n end tell\n set hasTimes to ((count of timeList) is (count of idList))\n repeat with i from 1 to (count of idList)\n set d to missing value\n if hasTimes then set d to item i of timeList\n -- An item with no readable receive time is kept whatever the window\n -- says; dropping it would make a folder of them look empty.\n if d is missing value or d is not less than cutoff then\n set out to out & ${asRow(['prefix', '(item i of idList as string)', '(item i of subjList)', 'my isoDate(d)'])}\n end if\n end repeat\n repeat with f in subs\n set skipThis to false\n try\n if skipIds contains (id of f) then set skipThis to true\n end try\n try\n if skipNames contains ((name of f) as string) then set skipThis to true\n end try\n if not skipThis then\n set childName to \"\"\n try\n set childName to (name of f) as string\n end try\n set childScope to (count of onlyNames) is 0\n try\n if onlyNames contains childName then set childScope to true\n end try\n set out to out & my scanFolder(f, prefix & ${AS_LIST_SEP} & childName, cutoff, skipIds, skipNames, onlyNames, childScope)\n end if\n end repeat\n return out\nend scanFolder\n\ntell application \"Microsoft Outlook\"\n${accountLookupSnippet(acct)}\n${rootFolderSnippet(acct, 'inbox', 'rootInbox')}\n set rootName to (name of rootInbox) as string\n set skipIds to {}\n${skipIds}\n set skipNames to ${asNameList(request.excludeFolders)}\n set onlyNames to ${asNameList(request.includeFolders)}\nend tell\nset cutoff to (current date) - (${asInt(request.daysBack)} * days)\nreturn my scanFolder(rootInbox, rootName, cutoff, skipIds, skipNames, onlyNames, ${rootInScope})`, 'scan'));\n\n const subjectFiltered = !!(request.subjectLike || request.subjectPattern || request.excludeReplies);\n const likeRe = request.subjectLike ? new RegExp(subjectGlobSource(request.subjectLike), 'i') : null;\n // PowerShell's -match is case-insensitive, so the Windows test is too.\n const patternRe = request.subjectPattern ? new RegExp(request.subjectPattern.source, 'i') : null;\n const candidates = indexed\n .map(record => {\n const parts = splitFields(record);\n const segments = splitList(field(parts, 0));\n return {\n folderPath: macFolderPath(request.account, segments[0] || 'Inbox', segments.slice(1)),\n id: field(parts, 1),\n subject: field(parts, 2).trim(),\n receivedTime: field(parts, 3),\n };\n })\n .filter(candidate => {\n if (subjectFiltered && !candidate.subject) return false;\n if (likeRe && !likeRe.test(candidate.subject)) return false;\n if (patternRe && !patternRe.test(candidate.subject)) return false;\n if (request.excludeReplies && REPLY_PREFIX.test(candidate.subject)) return false;\n return true;\n });\n if (candidates.length === 0) return [];\n\n // Pass 2 — senders, attachment names and (when asked) bodies, for the survivors only.\n const body = request.includeBody\n ? ` set bodyText to \"\"\n try\n set bodyText to (plain text content of theMsg) as string\n end try`\n : ' set bodyText to \"\"';\n const details = new Map<string, {\n senderName: string;\n senderEmail: string;\n attachmentNames: string[];\n body: string\n }>();\n const raw = await runOsaScript(`tell application \"Microsoft Outlook\"\n set out to \"\"\n repeat with theId in ${asIdList(candidates.map(c => c.id))}\n set theMsg to missing value\n try\n set theMsg to message id theId\n end try\n if theMsg is not missing value then\n${senderSnippet('theMsg', ' ')}\n set attNames to {}\n try\n set attNames to name of every attachment of theMsg\n end try\n${body}\n set out to out & ${asRow(['(id of theMsg as string)', 'sndName', 'sndAddr', 'my sanitizeList(attNames)', 'bodyText'])}\n end if\n end repeat\n return out\nend tell`, 'scan');\n for (const record of splitRecords(raw)) {\n const parts = splitFields(record);\n details.set(field(parts, 0), {\n senderName: field(parts, 1),\n senderEmail: field(parts, 2),\n attachmentNames: splitList(field(parts, 3)),\n body: field(parts, 4),\n });\n }\n\n const matches: InboxSearchMatch[] = [];\n for (const candidate of candidates) {\n const detail = details.get(candidate.id);\n if (!detail) continue;\n // Attachment names only arrive with the details, so this test waits for them.\n if (request.requireAttachment && detail.attachmentNames.length === 0) continue;\n matches.push({\n entryId: candidate.id,\n storeId: '',\n subject: candidate.subject,\n senderName: detail.senderName,\n senderEmail: detail.senderEmail,\n receivedTime: candidate.receivedTime,\n body: detail.body,\n attachmentNames: detail.attachmentNames,\n folderPath: candidate.folderPath,\n });\n }\n return matches;\n}\n\n/**\n * The email currently selected — or open — in Outlook. `current messages`\n * covers both cases the Windows reader handles separately.\n */\nexport async function readSelectedEmail(): Promise<SelectedEmail> {\n const records = splitRecords(await runOsaScript(`tell application \"Microsoft Outlook\"\n set sel to {}\n try\n set sel to current messages\n end try\n if (count of sel) is 0 then error ${asString(`${failureTag('NOT_FOUND', 'email')}No email is selected in Outlook. Select or open an email, then try again.`)}\n set theMsg to item 1 of sel\n${messageDetailSnippet(true)}\n return ${asRow(messageDetailFields(true))}\nend tell`, 'quick'));\n if (records.length === 0) throw new NotFoundError('email', 'Outlook reported no selected email.');\n const parts = splitFields(records[0]);\n return {\n entryId: field(parts, MessageDetail.id),\n storeId: '',\n subject: field(parts, MessageDetail.subject).trim(),\n senderName: field(parts, MessageDetail.senderName),\n senderEmail: field(parts, MessageDetail.senderEmail),\n receivedTime: field(parts, MessageDetail.receivedTime),\n body: field(parts, MessageDetail.body),\n attachmentNames: splitList(field(parts, MessageDetail.attachmentNames)),\n };\n}\n\n/** Open an email in Outlook and bring Outlook forward. */\nexport async function openOutlookEmail(email: EmailLocator): Promise<void> {\n const id = macMessageId(email.entryId);\n await runOsaScript(`\ntell application \"Microsoft Outlook\"\n${messageLookupSnippet(id)}\n open theMsg\n activate\nend tell`, 'quick');\n}\n"]}
1
+ {"version":3,"file":"read.js","sourceRoot":"","sources":["../../src/mac/read.ts"],"names":[],"mappings":";;AAmDA,0CAiFC;AAMD,sCAgBC;AAUD,kDAyKC;AAUD,8CA4CC;AAUD,4CAQC;AArZD,+EAA+E;AAC/E,sCAAsC;AACtC,EAAE;AACF,+EAA+E;AAC/E,sEAAsE;AACtE,qEAAqE;AACrE,8EAA8E;AAC9E,yDAAyD;AACzD,+BAae;AACf,uCAAgD;AAChD,uCAkBmB;AACnB,sCAAsD;AACtD,kCAA8G;AAI9G;;;GAGG;AACI,KAAK,UAAU,eAAe,CAAC,OAAyB;IAC3D,MAAM,IAAI,GAAG,MAAM,IAAA,2BAAiB,EAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACtD,MAAM,UAAU,GAAG,IAAA,uBAAa,EAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,O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OS: one folder, one item, a whole Inbox tree, or whatever\n// is selected in the running Outlook.\n//\n// Every reader is two passes. Pass 1 indexes a folder with bulk property reads\n// (one Apple event each); pass 2 fetches the expensive fields — body,\n// attachments, sender — only for the messages that survived. Reading\n// per-message in a loop costs an event per property and is unusably slow on a\n// real mailbox, which is the whole reason for the shape.\nimport {\n AS_LIST_SEP,\n asIdList,\n asInt,\n asRow,\n asString,\n field,\n runOsaScript,\n splitFields,\n splitList,\n splitPositionalList,\n splitRecords,\n summaryFields,\n} from './run';\nimport { readProfileAccounts } from './profile';\nimport {\n accountLookupSnippet,\n bodyTextSnippet,\n dateProperty,\n firstRecipientSnippet,\n folderChainSnippet,\n MAC_ROOT_TERMS,\n macFolderPath,\n macMessageId,\n mailScopeSnippet,\n messageByIdSnippet,\n MessageDetail,\n messageDetailFields,\n messageDetailSnippet,\n messageLookupSnippet,\n resolveMacAccount,\n rootFolderSnippet,\n senderSnippet,\n} from './scripts';\nimport { failureTag, NotFoundError } from '../errors';\nimport { folderLeafName, isOutgoingRoot, NON_INCOMING_ROOTS, REPLY_PREFIX, subjectGlobSource } from '../mail';\nimport type { RawEmail, ReadInboxRequest, SearchRequest } from '../backend';\nimport type { EmailLocator, InboxEmail, InboxSearchMatch, SelectedEmail } from '../types';\n\n/**\n * Recent mail from the Inbox root or one folder, newest first. Entry ids here\n * are Outlook for Mac's integer message ids, not Windows EntryIDs.\n */\nexport async function readInboxEmails(request: ReadInboxRequest): Promise<InboxEmail[]> {\n const acct = await resolveMacAccount(request.account);\n const folderPath = macFolderPath(request.account, request.folder.rootLabel, request.folder.segments);\n const outgoing = isOutgoingRoot(request.folder.rootId);\n\n // Pass 1 — index the folder. Its order isn't documented, so it is sorted here.\n const index = (await runOsaScript(`tell application \"Microsoft Outlook\"\n${accountLookupSnippet(acct)}\n${mailScopeSnippet(acct, request.folder, request.folderLabel)}\n set cutoff to (current date) - (${asInt(request.daysBack)} * days)\n set idList to id of every message of scopeFolder\n try\n set timeList to ${dateProperty(request.folder.rootId)} of every message of scopeFolder\n on error\n set timeList to {}\n end try\n set hasTimes to ((count of timeList) is (count of idList))\n set out to \"\"\n repeat with i from 1 to (count of idList)\n set d to missing value\n if hasTimes then set d to item i of timeList\n -- An item with no timestamp of its own (an unsent draft filed into a\n -- mail folder) is skipped, as the Windows date filter skips it.\n if d is not missing value and d is greater than or equal to cutoff then\n set out to out & (item i of idList as string) & tab & my isoDate(d) & linefeed\n end if\n end repeat\n return out\nend tell`, 'standard'))\n .split('\\n')\n .map(line => line.trim())\n .filter(Boolean)\n .map(line => {\n const [id, receivedTime] = line.split('\\t');\n return { id, receivedTime: receivedTime || '' };\n });\n // 'yyyy-MM-dd HH:mm' sorts correctly as a string.\n index.sort((a, b) => b.receivedTime.localeCompare(a.receivedTime));\n const chosen = index.slice(0, request.limit);\n if (chosen.length === 0) return [];\n\n // Pass 2 — the expensive reads, only for the messages kept.\n const raw = await runOsaScript(`tell application \"Microsoft Outlook\"\n set out to \"\"\n repeat with theId in ${asIdList(chosen.map(c => c.id))}\n${messageByIdSnippet(' ')}\n if theMsg is not missing value then\n set subj to \"\"\n try\n set subj to (subject of theMsg) as string\n end try\n${outgoing ? firstRecipientSnippet(' ') : senderSnippet(' ')}\n${bodyTextSnippet(' ', request.previewChars)}\n set attNames to {}\n try\n set attNames to name of every attachment of theMsg\n end try\n set out to out & ${asRow(['(id of theMsg as string)', 'subj', 'sndName', 'sndAddr', 'bodyText', 'my sanitizePositionalList(attNames)'])}\n end if\n end repeat\n return out\nend tell`, 'standard');\n\n const receivedById = new Map(chosen.map(c => [c.id, c.receivedTime]));\n return splitRecords(raw).map(record => {\n const parts = splitFields(record);\n const id = field(parts, 0);\n const attachmentNames = splitPositionalList(field(parts, 5));\n return {\n entryId: id,\n storeId: '',\n subject: field(parts, 1).trim(),\n senderName: field(parts, 2),\n senderEmail: field(parts, 3),\n receivedTime: receivedById.get(id) || '',\n bodyPreview: field(parts, 4),\n attachmentNames,\n attachmentCount: attachmentNames.length,\n folderPath,\n };\n });\n}\n\n/**\n * One email's full plain-text body. `storeId` has no macOS meaning: `message id\n * N` resolves against the application, wherever the message sits.\n */\nexport async function readEmailBody(email: EmailLocator): Promise<RawEmail> {\n const id = macMessageId(email.entryId);\n const parts = summaryFields(await runOsaScript(`tell application \"Microsoft Outlook\"\n${messageLookupSnippet(id)}\n${messageDetailSnippet(true)}\n return ${asRow(messageDetailFields(true))}\nend tell`, 'quick'));\n return {\n entryId: field(parts, MessageDetail.id) || id,\n subject: field(parts, MessageDetail.subject).trim(),\n senderName: field(parts, MessageDetail.senderName),\n senderEmail: field(parts, MessageDetail.senderEmail),\n receivedTime: field(parts, MessageDetail.receivedTime),\n body: field(parts, MessageDetail.body),\n attachmentNames: splitPositionalList(field(parts, MessageDetail.attachmentNames)),\n };\n}\n\n/**\n * Walk every folder under the Inbox for the mail matching the request.\n *\n * The filtering happens here rather than in Outlook: there is no AppleScript\n * counterpart to COM's server-side `Items.Restrict`, so each folder is indexed in\n * bulk reads and the subject and date tests run on the index. The results match\n * Windows; the cost doesn't, and a wide window over a big tree is heavier here.\n */\nexport async function searchInboxByFilter(request: SearchRequest): Promise<InboxSearchMatch[]> {\n const acct = await resolveMacAccount(request.account);\n // AppleScript reaches a child folder by name, so a path entry narrows to its leaf.\n const asNameList = (list: readonly string[]) => `{${list.map(folderLeafName).filter(Boolean).map(asString).join(', ')}}`;\n // Sent, Drafts, Deleted and Junk hang UNDER the Inbox on an IMAP profile, so\n // their ids are collected first and the walk never descends into them — by\n // the profile's own id where it records one, through the account otherwise.\n // The roots are the ones Windows cuts, from the one list both read.\n const skipIds = NON_INCOMING_ROOTS\n .map(rootId => MAC_ROOT_TERMS[rootId])\n .map(term => {\n const id = acct.folderIds?.[term];\n return id !== undefined\n ? ` set end of skipIds to ${id}`\n : ` try\n set end of skipIds to (id of (${term} of targetAcct))\n end try`;\n })\n .join('\\n');\n\n // Pass 1 — index every folder under the Inbox: path, id, subject, received.\n const indexed = splitRecords(await runOsaScript(`on scanFolder(theFolder, prefix, cutoff, skipIds, skipNames, onlyNames, inScope)\n set out to \"\"\n set idList to {}\n set subjList to {}\n set timeList to {}\n -- A folder the walk only passes through is never read: the bulk reads are\n -- the expensive part.\n if inScope then\n tell application \"Microsoft Outlook\"\n set idList to id of every message of theFolder\n set subjList to subject of every message of theFolder\n try\n set timeList to time received of every message of theFolder\n on error\n set timeList to {}\n end try\n end tell\n end if\n tell application \"Microsoft Outlook\"\n set subs to mail folders of theFolder\n end tell\n set hasTimes to ((count of timeList) is (count of idList))\n repeat with i from 1 to (count of idList)\n set d to missing value\n if hasTimes then set d to item i of timeList\n -- An item with no readable receive time is kept whatever the window\n -- says; dropping it would make a folder of them look empty.\n if d is missing value or d is not less than cutoff then\n set out to out & ${asRow(['prefix', '(item i of idList as string)', '(item i of subjList)', 'my isoDate(d)'])}\n end if\n end repeat\n repeat with f in subs\n set skipThis to false\n try\n if skipIds contains (id of f) then set skipThis to true\n end try\n try\n if skipNames contains ((name of f) as string) then set skipThis to true\n end try\n if not skipThis then\n set childName to \"\"\n try\n set childName to (name of f) as string\n end try\n set childScope to (count of onlyNames) is 0\n try\n if onlyNames contains childName then set childScope to true\n end try\n set out to out & my scanFolder(f, prefix & ${AS_LIST_SEP} & childName, cutoff, skipIds, skipNames, onlyNames, childScope)\n end if\n end repeat\n return out\nend scanFolder\n\ntell application \"Microsoft Outlook\"\n${accountLookupSnippet(acct)}\n${rootFolderSnippet(acct, 'inbox', 'rootInbox')}\n set rootName to (name of rootInbox) as string\n set skipIds to {}\n${skipIds}\n set skipNames to ${asNameList(request.excludeFolders)}\n set onlyNames to ${asNameList(request.includeFolders)}\nend tell\n-- The root is read on the terms its children are: when no folder is named, or\n-- when it is one of those named.\nset rootScope to (count of onlyNames) is 0\ntry\n if onlyNames contains rootName then set rootScope to true\nend try\nset cutoff to (current date) - (${asInt(request.daysBack)} * days)\nreturn my scanFolder(rootInbox, rootName, cutoff, skipIds, skipNames, onlyNames, rootScope)`, 'scan'));\n\n const subjectFiltered = !!(request.subjectLike || request.subjectPattern || request.excludeReplies);\n const likeRe = request.subjectLike ? new RegExp(subjectGlobSource(request.subjectLike), 'i') : null;\n // PowerShell's -match is case-insensitive, so this test is too, whatever the\n // caller's flags. Only u and v are kept: they decide how the source parses,\n // and without u, /\\p{L}/u would match the literal text \"p{L}\". Keeping g or y\n // would carry lastIndex from one subject to the next.\n const patternRe = request.subjectPattern\n ? new RegExp(request.subjectPattern.source, 'i' + request.subjectPattern.flags.replace(/[^uv]/g, ''))\n : null;\n const candidates = indexed\n .map(record => {\n const parts = splitFields(record);\n const segments = splitList(field(parts, 0));\n return {\n folderPath: macFolderPath(request.account, segments[0] || 'Inbox', segments.slice(1)),\n id: field(parts, 1),\n subject: field(parts, 2).trim(),\n receivedTime: field(parts, 3),\n };\n })\n .filter(candidate => {\n if (subjectFiltered && !candidate.subject) return false;\n if (likeRe && !likeRe.test(candidate.subject)) return false;\n if (patternRe && !patternRe.test(candidate.subject)) return false;\n if (request.excludeReplies && REPLY_PREFIX.test(candidate.subject)) return false;\n return true;\n });\n if (candidates.length === 0) return [];\n\n // Pass 2 — senders, attachment names and (when asked) bodies, for the survivors only.\n const raw = await runOsaScript(`tell application \"Microsoft Outlook\"\n set out to \"\"\n repeat with theId in ${asIdList(candidates.map(c => c.id))}\n${messageByIdSnippet(' ')}\n if theMsg is not missing value then\n${senderSnippet(' ')}\n set attNames to {}\n try\n set attNames to name of every attachment of theMsg\n end try\n${bodyTextSnippet(' ', request.includeBody ? undefined : 0)}\n set out to out & ${asRow(['(id of theMsg as string)', 'sndName', 'sndAddr', 'my sanitizePositionalList(attNames)', 'bodyText'])}\n end if\n end repeat\n return out\nend tell`, 'scan');\n const details = new Map(splitRecords(raw).map(record => {\n const parts = splitFields(record);\n const detail = {\n senderName: field(parts, 1),\n senderEmail: field(parts, 2),\n attachmentNames: splitPositionalList(field(parts, 3)),\n body: field(parts, 4),\n };\n return [field(parts, 0), detail] as const;\n }));\n\n const matches: InboxSearchMatch[] = [];\n for (const candidate of candidates) {\n const detail = details.get(candidate.id);\n if (!detail) continue;\n // Attachment names only arrive with the details, so this test waits for them.\n if (request.requireAttachment && detail.attachmentNames.length === 0) continue;\n matches.push({\n entryId: candidate.id,\n storeId: '',\n subject: candidate.subject,\n senderName: detail.senderName,\n senderEmail: detail.senderEmail,\n receivedTime: candidate.receivedTime,\n body: detail.body,\n attachmentNames: detail.attachmentNames,\n folderPath: candidate.folderPath,\n });\n }\n return matches;\n}\n\n/**\n * The email currently selected — or open — in Outlook. `current messages`\n * covers both cases the Windows reader handles separately.\n *\n * Where it sits comes back as a second record, so the detail row keeps its body\n * last. A mailbox with no account object answers `account of` with missing\n * value; its root folder's id is then looked up in the profile database.\n */\nexport async function readSelectedEmail(): Promise<SelectedEmail> {\n const records = splitRecords(await runOsaScript(`tell application \"Microsoft Outlook\"\n set sel to {}\n try\n set sel to current messages\n end try\n if (count of sel) is 0 then error ${asString(`${failureTag('NOT_FOUND', 'email')}No email is selected in Outlook. Select or open an email, then try again.`)}\n set theMsg to item 1 of sel\n${messageDetailSnippet(true)}\n set acctAddr to \"\"\n try\n set rawAddr to email address of (account of theMsg)\n if rawAddr is not missing value then set acctAddr to rawAddr as string\n end try\n set chainNames to {}\n set rootId to \"\"\n set theFolder to missing value\n try\n set theFolder to folder of theMsg\n end try\n if theFolder is not missing value then\n${folderChainSnippet(' ')}\n try\n set rootId to (id of cur) as string\n end try\n end if\n return ${asRow(messageDetailFields(true))} & ${asRow(['acctAddr', 'my sanitizeList(chainNames)', 'rootId'])}\nend tell`, 'quick'));\n if (records.length === 0) throw new NotFoundError('email', 'Outlook reported no selected email.');\n const parts = splitFields(records[0]);\n const where = splitFields(records[1] ?? '');\n const chain = splitList(field(where, 1)).reverse();\n const account = field(where, 0) || (chain.length ? await accountByRootFolder(field(where, 2)) : '');\n return {\n entryId: field(parts, MessageDetail.id),\n storeId: '',\n subject: field(parts, MessageDetail.subject).trim(),\n senderName: field(parts, MessageDetail.senderName),\n senderEmail: field(parts, MessageDetail.senderEmail),\n receivedTime: field(parts, MessageDetail.receivedTime),\n body: field(parts, MessageDetail.body),\n attachmentNames: splitPositionalList(field(parts, MessageDetail.attachmentNames)),\n folderPath: account && chain.length ? macFolderPath(account, chain[0], chain.slice(1)) : '',\n };\n}\n\n/** The address of the account whose root folder has this id, per the profile database; '' if none. */\nasync function accountByRootFolder(rootId: string): Promise<string> {\n if (!/^\\d+$/.test(rootId)) return '';\n const id = Number(rootId);\n return (await readProfileAccounts()).find(a => a.folderIds['root folder'] === id)?.emailAccount ?? '';\n}\n\n/** Open an email in Outlook and bring Outlook forward. */\nexport async function openOutlookEmail(email: EmailLocator): Promise<void> {\n const id = macMessageId(email.entryId);\n await runOsaScript(`\ntell application \"Microsoft Outlook\"\n${messageLookupSnippet(id)}\n open theMsg\n activate\nend tell`, 'quick');\n}\n"]}
package/dist/mac/run.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { type RunBudget } from '../runtime';
2
+ import type { ItemFailure } from '../types';
2
3
  /**
3
4
  * Run a script from a temp file (no argument-length or quoting limits) and
4
5
  * return what it printed, minus the newline osascript appends.
@@ -25,9 +26,6 @@ export declare function asIdList(ids: readonly string[]): string;
25
26
  export declare const FIELD_SEP = "\u001F";
26
27
  export declare const RECORD_SEP = "\u001E";
27
28
  export declare const LIST_SEP = "\u001D";
28
- /** The same three, as AppleScript expressions. */
29
- export declare const AS_FIELD_SEP = "(character id 31)";
30
- export declare const AS_RECORD_SEP = "(character id 30)";
31
29
  export declare const AS_LIST_SEP = "(character id 29)";
32
30
  /**
33
31
  * One record: the AppleScript expressions in `fields`, separated and terminated
@@ -43,13 +41,22 @@ export declare function asRow(fields: readonly string[]): string;
43
41
  * `sanitize` strips the framing characters; `joinList` and `replaceText` are the
44
42
  * text-item-delimiter dance nobody should write twice.
45
43
  */
46
- export declare const AS_HANDLERS = "\non pad2(n)\n set s to (n as integer) as string\n if (length of s) < 2 then set s to \"0\" & s\n return s\nend pad2\n\non isoDate(d)\n if d is missing value then return \"\"\n return (year of d as string) & \"-\" & pad2(month of d as integer) & \"-\" & pad2(day of d) & \" \" & pad2(hours of d) & \":\" & pad2(minutes of d)\nend isoDate\n\non joinList(lst, sep)\n set saved to AppleScript's text item delimiters\n set AppleScript's text item delimiters to sep\n set out to lst as string\n set AppleScript's text item delimiters to saved\n return out\nend joinList\n\non replaceText(t, findWhat, replaceWith)\n set saved to AppleScript's text item delimiters\n set AppleScript's text item delimiters to findWhat\n set parts to text items of t\n set AppleScript's text item delimiters to replaceWith\n set out to parts as string\n set AppleScript's text item delimiters to saved\n return out\nend replaceText\n\n-- Strips the FIELD and RECORD separators only. The LIST separator is left\n-- alone on purpose: a field may legitimately BE a list.\non sanitize(v)\n if v is missing value then return \"\"\n set t to v as string\n repeat with codePoint in {30, 31}\n set marker to (character id (codePoint as integer))\n if t contains marker then set t to my replaceText(t, marker, \" \")\n end repeat\n return t\nend sanitize\n\n-- A list field: each element loses all three separators, then they are joined\n-- with the LIST separator that splitList() takes them apart on.\non sanitizeList(lst)\n set cleaned to {}\n repeat with item_ in lst\n set t to my sanitize(item_)\n set marker to (character id 29)\n if t contains marker then set t to my replaceText(t, marker, \" \")\n set end of cleaned to t\n end repeat\n return my joinList(cleaned, (character id 29))\nend sanitizeList\n\non insertAboveQuoted(c, ins)\n if c is missing value then return ins\n set c to c as string\n if (length of c) is 0 then return ins\n -- A reply's own empty paragraph sits inside WordSection1; inserting straight\n -- after that tag puts the new text where the cursor would be. Anything else\n -- falls back to just inside <body>. ('at' is a reserved parameter name, so\n -- the cut point can't be called that.)\n set cutPoint to my tagEnd(c, \"WordSection1>\")\n if cutPoint is 0 then set cutPoint to my tagEnd(c, \"<body\")\n if cutPoint is 0 then return ins & c\n if cutPoint is (length of c) then return c & ins\n return (text 1 thru cutPoint of c) & ins & (text (cutPoint + 1) thru -1 of c)\nend insertAboveQuoted\n\n-- 'rest' is an AppleScript term, so the remainder can't be called that:\n-- assigning to it compiles and then fails at run time.\non tagEnd(c, marker)\n set ix to offset of marker in c\n if ix is 0 then return 0\n if marker ends with \">\" then return ix + (length of marker) - 1\n set tailText to text ix thru -1 of c\n set gt to offset of \">\" in tailText\n if gt is 0 then return 0\n return ix + gt - 1\nend tagEnd\n";
44
+ export declare const AS_HANDLERS = "\non pad2(n)\n set s to (n as integer) as string\n if (length of s) < 2 then set s to \"0\" & s\n return s\nend pad2\n\non isoDate(d)\n if d is missing value then return \"\"\n return (year of d as string) & \"-\" & pad2(month of d as integer) & \"-\" & pad2(day of d) & \" \" & pad2(hours of d) & \":\" & pad2(minutes of d)\nend isoDate\n\non joinList(lst, sep)\n set saved to AppleScript's text item delimiters\n set AppleScript's text item delimiters to sep\n set out to lst as string\n set AppleScript's text item delimiters to saved\n return out\nend joinList\n\non replaceText(t, findWhat, replaceWith)\n set saved to AppleScript's text item delimiters\n set AppleScript's text item delimiters to findWhat\n set parts to text items of t\n set AppleScript's text item delimiters to replaceWith\n set out to parts as string\n set AppleScript's text item delimiters to saved\n return out\nend replaceText\n\n-- Strips the FIELD and RECORD separators only. The LIST separator is left\n-- alone on purpose: a field may legitimately BE a list.\non sanitize(v)\n if v is missing value then return \"\"\n set t to v as string\n repeat with codePoint in {30, 31}\n set marker to (character id (codePoint as integer))\n if t contains marker then set t to my replaceText(t, marker, \" \")\n end repeat\n return t\nend sanitize\n\n-- A list field: each element loses all three separators, then they are joined\n-- with the LIST separator that splitList() takes them apart on.\non sanitizeList(lst)\n set cleaned to {}\n repeat with item_ in lst\n set t to my sanitize(item_)\n set marker to (character id 29)\n if t contains marker then set t to my replaceText(t, marker, \" \")\n set end of cleaned to t\n end repeat\n return my joinList(cleaned, (character id 29))\nend sanitizeList\n\n-- A list whose positions matter, like an email's attachments: every element is\n-- FOLLOWED by the LIST separator, so an empty element keeps its place and an\n-- empty list stays distinct from a list of one empty name. splitPositionalList()\n-- takes it apart.\non sanitizePositionalList(lst)\n if (count of lst) is 0 then return \"\"\n return my sanitizeList(lst) & (character id 29)\nend sanitizePositionalList\n\non insertAboveQuoted(c, ins)\n if c is missing value then return ins\n set c to c as string\n if (length of c) is 0 then return ins\n -- A reply's own empty paragraph sits inside WordSection1; inserting straight\n -- after that tag puts the new text where the cursor would be. Anything else\n -- falls back to just inside <body>. ('at' is a reserved parameter name, so\n -- the cut point can't be called that.)\n set cutPoint to my tagEnd(c, \"WordSection1>\")\n if cutPoint is 0 then set cutPoint to my tagEnd(c, \"<body\")\n if cutPoint is 0 then return ins & c\n if cutPoint is (length of c) then return c & ins\n return (text 1 thru cutPoint of c) & ins & (text (cutPoint + 1) thru -1 of c)\nend insertAboveQuoted\n\n-- 'rest' is an AppleScript term, so the remainder can't be called that:\n-- assigning to it compiles and then fails at run time.\non tagEnd(c, marker)\n set ix to offset of marker in c\n if ix is 0 then return 0\n if marker ends with \">\" then return ix + (length of marker) - 1\n set tailText to text ix thru -1 of c\n set gt to offset of \">\" in tailText\n if gt is 0 then return 0\n return ix + gt - 1\nend tagEnd\n";
47
45
  /** A script's output as records, without the trailing empty one. */
48
46
  export declare function splitRecords(raw: string): string[];
49
47
  /** One record's fields. */
50
48
  export declare function splitFields(record: string): string[];
51
- /** A field that carries a list. */
49
+ /**
50
+ * A field that carries a list. Empty elements are dropped, so where an index
51
+ * into the list must match Outlook's, the field is written with
52
+ * `sanitizePositionalList` and read with `splitPositionalList` instead.
53
+ */
52
54
  export declare function splitList(value: string | undefined): string[];
55
+ /**
56
+ * A field written by `sanitizePositionalList`: every element at its position,
57
+ * empty ones included, so index N here is item N+1 of the AppleScript list.
58
+ */
59
+ export declare function splitPositionalList(value: string | undefined): string[];
53
60
  /** A field by position; a missing field reads as ''. */
54
61
  export declare function field(fields: readonly string[], index: number): string;
55
62
  /** The fields of a script's first record — the summary a mutation returns ahead of any per-item rows. */
@@ -58,3 +65,10 @@ export declare function summaryFields(raw: string): string[];
58
65
  export declare function intField(fields: readonly string[], index: number, fallback?: number): number;
59
66
  /** A boolean field; AppleScript renders booleans as the bare words true/false. */
60
67
  export declare function boolField(fields: readonly string[], index: number): boolean;
68
+ /** The record a batch script appends, inside `on error errText`, for the id `theId` it failed on. */
69
+ export declare const ITEM_FAILURE_ROW: string;
70
+ /** A batch script's output: the summary record's fields, and the items that failed. */
71
+ export declare function splitBatch(raw: string): {
72
+ summary: string[];
73
+ failed: ItemFailure[];
74
+ };