@remit/mailbox-service 0.0.112 → 0.0.113

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 (270) hide show
  1. package/dist/account-credentials.d.ts +83 -0
  2. package/dist/account-credentials.js +104 -0
  3. package/dist/attribute-mapper.d.ts +36 -0
  4. package/dist/attribute-mapper.js +119 -0
  5. package/dist/body-parse.d.ts +28 -0
  6. package/{src/body-parse.ts → dist/body-parse.js} +18 -26
  7. package/dist/body-part-mapper.d.ts +92 -0
  8. package/dist/body-part-mapper.js +301 -0
  9. package/dist/body-sync-queue.d.ts +59 -0
  10. package/dist/body-sync-queue.js +56 -0
  11. package/dist/body-sync-terminal.d.ts +58 -0
  12. package/dist/body-sync-terminal.js +95 -0
  13. package/dist/body-sync.d.ts +351 -0
  14. package/dist/body-sync.js +1204 -0
  15. package/dist/calendar-parts.d.ts +23 -0
  16. package/dist/calendar-parts.js +18 -0
  17. package/dist/classification-backfill.d.ts +91 -0
  18. package/dist/classification-backfill.js +180 -0
  19. package/dist/classify-message.d.ts +31 -0
  20. package/dist/classify-message.js +89 -0
  21. package/dist/connection-factory.d.ts +54 -0
  22. package/dist/connection-factory.js +54 -0
  23. package/dist/connection-test.d.ts +41 -0
  24. package/dist/connection-test.js +152 -0
  25. package/dist/external-move.d.ts +94 -0
  26. package/dist/external-move.js +173 -0
  27. package/dist/filter-operations.d.ts +42 -0
  28. package/dist/filter-operations.js +211 -0
  29. package/dist/filters/filter-config.d.ts +14 -0
  30. package/dist/filters/filter-config.js +10 -0
  31. package/{src/filters/list-id.ts → dist/filters/list-id.d.ts} +2 -16
  32. package/dist/filters/list-id.js +26 -0
  33. package/dist/filters/match.d.ts +69 -0
  34. package/dist/filters/match.js +165 -0
  35. package/dist/filters/pipeline.d.ts +64 -0
  36. package/dist/filters/pipeline.js +100 -0
  37. package/{src/flag-push-terminal.ts → dist/flag-push-terminal.d.ts} +16 -72
  38. package/dist/flag-push-terminal.js +63 -0
  39. package/dist/flag-push.d.ts +69 -0
  40. package/dist/flag-push.js +111 -0
  41. package/dist/flag-queue.d.ts +175 -0
  42. package/dist/flag-queue.js +291 -0
  43. package/dist/folder-operations.d.ts +39 -0
  44. package/dist/folder-operations.js +112 -0
  45. package/dist/folder-placement.d.ts +13 -0
  46. package/dist/folder-placement.js +23 -0
  47. package/dist/heuristics/authenticationResults.d.ts +10 -0
  48. package/dist/heuristics/authenticationResults.js +187 -0
  49. package/dist/heuristics/classifyByHeaders.d.ts +82 -0
  50. package/dist/heuristics/classifyByHeaders.js +365 -0
  51. package/dist/heuristics/classifyPlacement.d.ts +67 -0
  52. package/dist/heuristics/classifyPlacement.js +153 -0
  53. package/dist/heuristics/machineSenders.d.ts +21 -0
  54. package/{src/heuristics/machineSenders.ts → dist/heuristics/machineSenders.js} +34 -40
  55. package/dist/heuristics/senderMismatch.d.ts +81 -0
  56. package/dist/heuristics/senderMismatch.js +192 -0
  57. package/dist/heuristics/socialDomains.d.ts +14 -0
  58. package/{src/heuristics/socialDomains.ts → dist/heuristics/socialDomains.js} +9 -9
  59. package/dist/heuristics/transactionalDomains.d.ts +18 -0
  60. package/{src/heuristics/transactionalDomains.ts → dist/heuristics/transactionalDomains.js} +8 -8
  61. package/dist/imapflow-connection.d.ts +377 -0
  62. package/dist/imapflow-connection.js +1147 -0
  63. package/dist/index.d.ts +63 -0
  64. package/dist/index.js +65 -0
  65. package/dist/list-id-backfill.d.ts +83 -0
  66. package/dist/list-id-backfill.js +132 -0
  67. package/dist/mailbox-cursor-rebuild.d.ts +74 -0
  68. package/dist/mailbox-cursor-rebuild.js +71 -0
  69. package/{src/mailbox-cursor.ts → dist/mailbox-cursor.d.ts} +20 -98
  70. package/dist/mailbox-cursor.js +102 -0
  71. package/dist/mailbox-intent.d.ts +41 -0
  72. package/{src/mailbox-intent.ts → dist/mailbox-intent.js} +38 -64
  73. package/dist/mailbox-management.d.ts +240 -0
  74. package/dist/mailbox-management.js +576 -0
  75. package/dist/mailbox-presence.d.ts +52 -0
  76. package/{src/mailbox-presence.ts → dist/mailbox-presence.js} +19 -30
  77. package/dist/mailbox-queue.d.ts +126 -0
  78. package/dist/mailbox-queue.js +214 -0
  79. package/dist/mailbox-sync.d.ts +93 -0
  80. package/dist/mailbox-sync.js +490 -0
  81. package/dist/mailbox-upstream.d.ts +38 -0
  82. package/{src/mailbox-upstream.ts → dist/mailbox-upstream.js} +37 -47
  83. package/dist/message-category.d.ts +51 -0
  84. package/{src/message-category.ts → dist/message-category.js} +28 -71
  85. package/dist/message-move.d.ts +285 -0
  86. package/dist/message-move.js +858 -0
  87. package/dist/message-parser.d.ts +14 -0
  88. package/dist/message-parser.js +17 -0
  89. package/{src/message-presence.ts → dist/message-presence.d.ts} +1 -13
  90. package/dist/message-presence.js +29 -0
  91. package/dist/message-read.d.ts +51 -0
  92. package/dist/message-read.js +31 -0
  93. package/dist/message-sync.d.ts +345 -0
  94. package/dist/message-sync.js +1358 -0
  95. package/dist/mime-walker.d.ts +102 -0
  96. package/dist/mime-walker.js +198 -0
  97. package/dist/organize-job.d.ts +20 -0
  98. package/dist/organize-job.js +37 -0
  99. package/dist/outbox-attachment-content.d.ts +37 -0
  100. package/dist/outbox-attachment-content.js +50 -0
  101. package/dist/outbox-attachment-filename.d.ts +19 -0
  102. package/dist/outbox-attachment-filename.js +64 -0
  103. package/dist/outbox-attachment.d.ts +148 -0
  104. package/dist/outbox-attachment.js +273 -0
  105. package/dist/outbox-queue.d.ts +73 -0
  106. package/dist/outbox-queue.js +284 -0
  107. package/{src/outbox-status.ts → dist/outbox-status.d.ts} +1 -6
  108. package/dist/outbox-status.js +18 -0
  109. package/dist/ownership.d.ts +24 -0
  110. package/dist/ownership.js +83 -0
  111. package/dist/placement-move-terminal.d.ts +67 -0
  112. package/dist/placement-move-terminal.js +88 -0
  113. package/dist/placement-move.d.ts +83 -0
  114. package/dist/placement-move.js +216 -0
  115. package/{src/placement-settled.ts → dist/placement-settled.d.ts} +13 -68
  116. package/dist/placement-settled.js +75 -0
  117. package/dist/quarantine.d.ts +95 -0
  118. package/dist/quarantine.js +131 -0
  119. package/dist/refusal.d.ts +31 -0
  120. package/dist/refusal.js +3 -0
  121. package/{src/restore-source-placement.ts → dist/restore-source-placement.d.ts} +29 -97
  122. package/dist/restore-source-placement.js +79 -0
  123. package/dist/search-text-backfill.d.ts +15 -0
  124. package/dist/search-text-backfill.js +33 -0
  125. package/dist/snippet.d.ts +61 -0
  126. package/dist/snippet.js +129 -0
  127. package/dist/spam-report.d.ts +86 -0
  128. package/dist/spam-report.js +241 -0
  129. package/dist/sync-warning.d.ts +21 -0
  130. package/dist/sync-warning.js +26 -0
  131. package/dist/sync-watermarks.d.ts +138 -0
  132. package/dist/sync-watermarks.js +205 -0
  133. package/dist/test-helpers/fake-imap.d.ts +151 -0
  134. package/dist/test-helpers/fake-imap.js +508 -0
  135. package/dist/test-helpers/folder-roles.d.ts +34 -0
  136. package/dist/test-helpers/folder-roles.js +47 -0
  137. package/dist/test-helpers/mime-tree-generator.d.ts +82 -0
  138. package/dist/test-helpers/mime-tree-generator.js +329 -0
  139. package/dist/test-helpers/test-store.d.ts +69 -0
  140. package/dist/test-helpers/test-store.js +193 -0
  141. package/dist/text/normalizer.d.ts +20 -0
  142. package/dist/text/normalizer.js +102 -0
  143. package/dist/thread-message-rows.d.ts +69 -0
  144. package/dist/thread-message-rows.js +40 -0
  145. package/dist/thread-root.d.ts +7 -0
  146. package/dist/thread-root.js +25 -0
  147. package/dist/types.d.ts +343 -0
  148. package/dist/types.js +19 -0
  149. package/package.json +86 -88
  150. package/src/account-credentials.ts +0 -210
  151. package/src/attribute-mapper.test.ts +0 -104
  152. package/src/attribute-mapper.ts +0 -148
  153. package/src/body-parse.test.ts +0 -55
  154. package/src/body-part-mapper.property.test.ts +0 -354
  155. package/src/body-part-mapper.ts +0 -410
  156. package/src/body-sync-queue.test.ts +0 -64
  157. package/src/body-sync-queue.ts +0 -131
  158. package/src/body-sync-terminal.test.ts +0 -148
  159. package/src/body-sync-terminal.ts +0 -174
  160. package/src/body-sync.test.ts +0 -925
  161. package/src/body-sync.ts +0 -1891
  162. package/src/calendar-parts.ts +0 -41
  163. package/src/classification-backfill.test.ts +0 -285
  164. package/src/classification-backfill.ts +0 -363
  165. package/src/classify-message.test.ts +0 -142
  166. package/src/classify-message.ts +0 -116
  167. package/src/connection-factory.ts +0 -102
  168. package/src/connection-test.ts +0 -208
  169. package/src/credentials.test.ts +0 -181
  170. package/src/external-move.test.ts +0 -262
  171. package/src/external-move.ts +0 -285
  172. package/src/filter-operations.test.ts +0 -319
  173. package/src/filter-operations.ts +0 -377
  174. package/src/filter-update.test.ts +0 -313
  175. package/src/filters/filter-config.test.ts +0 -87
  176. package/src/filters/filter-config.ts +0 -34
  177. package/src/filters/list-id.test.ts +0 -50
  178. package/src/filters/match.test.ts +0 -392
  179. package/src/filters/match.ts +0 -225
  180. package/src/filters/pipeline.test.ts +0 -199
  181. package/src/filters/pipeline.ts +0 -170
  182. package/src/flag-push.test.ts +0 -112
  183. package/src/flag-push.ts +0 -198
  184. package/src/flag-queue.test.ts +0 -197
  185. package/src/flag-queue.ts +0 -509
  186. package/src/folder-operations.test.ts +0 -273
  187. package/src/folder-operations.ts +0 -217
  188. package/src/folder-placement.ts +0 -56
  189. package/src/forwarded-message.test.ts +0 -122
  190. package/src/heuristics/authenticationResults.test.ts +0 -189
  191. package/src/heuristics/authenticationResults.ts +0 -212
  192. package/src/heuristics/classifyByHeaders.test.ts +0 -889
  193. package/src/heuristics/classifyByHeaders.ts +0 -467
  194. package/src/heuristics/classifyPlacement.test.ts +0 -366
  195. package/src/heuristics/classifyPlacement.ts +0 -211
  196. package/src/heuristics/classifyRealMail.test.ts +0 -242
  197. package/src/heuristics/machineSenders.test.ts +0 -101
  198. package/src/heuristics/senderMismatch.test.ts +0 -346
  199. package/src/heuristics/senderMismatch.ts +0 -259
  200. package/src/imapflow-connect-retry.test.ts +0 -97
  201. package/src/imapflow-connection.test.ts +0 -503
  202. package/src/imapflow-connection.ts +0 -1476
  203. package/src/index.ts +0 -397
  204. package/src/list-id-backfill.test.ts +0 -207
  205. package/src/list-id-backfill.ts +0 -274
  206. package/src/mailbox-cursor-rebuild.test.ts +0 -171
  207. package/src/mailbox-cursor-rebuild.ts +0 -146
  208. package/src/mailbox-cursor.test.ts +0 -141
  209. package/src/mailbox-delete-intent.test.ts +0 -119
  210. package/src/mailbox-management.test.ts +0 -243
  211. package/src/mailbox-management.ts +0 -838
  212. package/src/mailbox-presence.test.ts +0 -74
  213. package/src/mailbox-queue.ts +0 -397
  214. package/src/mailbox-rename-intent.test.ts +0 -220
  215. package/src/mailbox-rename-subtree.test.ts +0 -287
  216. package/src/mailbox-sync.test.ts +0 -286
  217. package/src/mailbox-sync.ts +0 -724
  218. package/src/message-move.test.ts +0 -726
  219. package/src/message-move.ts +0 -1518
  220. package/src/message-parser.ts +0 -26
  221. package/src/message-presence.test.ts +0 -50
  222. package/src/message-read.ts +0 -87
  223. package/src/message-save-retry.test.ts +0 -90
  224. package/src/message-sync-flags.test.ts +0 -167
  225. package/src/message-sync.test.ts +0 -762
  226. package/src/message-sync.ts +0 -2133
  227. package/src/mime-walker.test.ts +0 -311
  228. package/src/mime-walker.ts +0 -306
  229. package/src/organize-job.ts +0 -93
  230. package/src/outbox-attachment-content.test.ts +0 -153
  231. package/src/outbox-attachment-content.ts +0 -104
  232. package/src/outbox-attachment-filename.test.ts +0 -132
  233. package/src/outbox-attachment-filename.ts +0 -76
  234. package/src/outbox-attachment.test.ts +0 -626
  235. package/src/outbox-attachment.ts +0 -536
  236. package/src/outbox-failed-recovery.test.ts +0 -299
  237. package/src/outbox-queue.test.ts +0 -315
  238. package/src/outbox-queue.ts +0 -525
  239. package/src/ownership.ts +0 -140
  240. package/src/placement-move-terminal.ts +0 -145
  241. package/src/placement-move.ts +0 -363
  242. package/src/placement-settled.test.ts +0 -147
  243. package/src/placement.test.ts +0 -404
  244. package/src/quarantine.test.ts +0 -133
  245. package/src/quarantine.ts +0 -229
  246. package/src/refusal.ts +0 -29
  247. package/src/snippet.test.ts +0 -213
  248. package/src/snippet.ts +0 -152
  249. package/src/spam-report.test.ts +0 -329
  250. package/src/spam-report.ts +0 -357
  251. package/src/sync-warning.test.ts +0 -213
  252. package/src/sync-warning.ts +0 -56
  253. package/src/sync-watermarks.test.ts +0 -409
  254. package/src/sync-watermarks.ts +0 -319
  255. package/src/system-flag-wire-format.test.ts +0 -77
  256. package/src/test-helpers/fake-imap.ts +0 -690
  257. package/src/test-helpers/folder-roles.ts +0 -66
  258. package/src/test-helpers/mime-tree-generator.ts +0 -461
  259. package/src/test-helpers/test-queue.ts +0 -56
  260. package/src/test-helpers/test-store.ts +0 -299
  261. package/src/text/normalizer.test.ts +0 -223
  262. package/src/text/normalizer.ts +0 -140
  263. package/src/thread-message-rows.ts +0 -77
  264. package/src/thread-root.test.ts +0 -29
  265. package/src/thread-root.ts +0 -51
  266. package/src/types/mailparser-augment.d.ts +0 -23
  267. package/src/types/natural-porter-stemmer.d.ts +0 -50
  268. package/src/types/stopword.d.ts +0 -69
  269. package/src/types.ts +0 -371
  270. package/tsconfig.json +0 -8
@@ -0,0 +1,39 @@
1
+ import type { AccountItem, IAccountRepository, IMailboxRepository, MailboxItem } from "@remit/data-ports";
2
+ import type { MailboxQueueService } from "./mailbox-queue.js";
3
+ import { type Refusable } from "./refusal.js";
4
+ export interface FolderPathDeps {
5
+ account: {
6
+ get(accountId: string): Promise<AccountItem>;
7
+ };
8
+ mailbox: {
9
+ get(accountId: string, mailboxId: string): Promise<MailboxItem>;
10
+ listAllByAccount(accountId: string): Promise<MailboxItem[]>;
11
+ };
12
+ mailboxQueue: Pick<MailboxQueueService, "renameMailbox" | "dismissMailboxIntent">;
13
+ }
14
+ export interface CreateFolderDeps {
15
+ account: Pick<IAccountRepository, "get">;
16
+ mailbox: Pick<IMailboxRepository, "findByPath">;
17
+ mailboxQueue: Pick<MailboxQueueService, "createMailbox">;
18
+ }
19
+ export interface NewFolder {
20
+ accountId: string;
21
+ namespaceType: MailboxItem["namespaceType"];
22
+ fullPath: string;
23
+ }
24
+ export interface FolderPathOutcome {
25
+ outcome: "Recorded" | "AlreadyRecorded";
26
+ mailbox: MailboxItem;
27
+ }
28
+ export interface NewFolderOutcome {
29
+ outcome: "Created" | "Existing";
30
+ mailbox: MailboxItem;
31
+ }
32
+ export interface FolderPathChange {
33
+ accountId: string;
34
+ mailboxId: string;
35
+ fullPath: string;
36
+ }
37
+ export declare const ownedFolder: (deps: Pick<FolderPathDeps, "account" | "mailbox">, accountConfigId: string, accountId: string, mailboxId: string) => Promise<Refusable<MailboxItem>>;
38
+ export declare const changeFolderPath: (deps: FolderPathDeps, accountConfigId: string, change: FolderPathChange) => Promise<Refusable<FolderPathOutcome>>;
39
+ export declare const createFolder: (deps: CreateFolderDeps, accountConfigId: string, folder: NewFolder) => Promise<Refusable<NewFolderOutcome>>;
@@ -0,0 +1,112 @@
1
+ import { isReservedFolderName } from "@remit/data-ports/folder-role";
2
+ import { mailboxLeafName } from "@remit/data-ports/mailbox-name";
3
+ import { MailboxSyncStatus } from "@remit/domain-enums";
4
+ import { carriesIntent, folderInFlight } from "./mailbox-intent.js";
5
+ import { folderInAccount, ownedAccount } from "./ownership.js";
6
+ import { accept, invalid, refuse } from "./refusal.js";
7
+ const renameTargetRefusal = async (deps, accountId, before, target) => {
8
+ // Renaming INBOX leaves an empty INBOX behind, and INBOX is the one name
9
+ // RFC 3501 reserves.
10
+ if (before.fullPath.toUpperCase() === "INBOX") {
11
+ return invalid("The inbox can't be renamed.");
12
+ }
13
+ if (target.trim().length === 0) {
14
+ return invalid("A folder needs a name.");
15
+ }
16
+ if (target.toUpperCase() === "INBOX") {
17
+ return invalid("“INBOX” is reserved for the inbox.");
18
+ }
19
+ // The mailbox sweep reads an unflagged folder under a role's conventional
20
+ // name as a duplicate and reaps it, with its mail, once another folder holds
21
+ // the flag.
22
+ const leaf = mailboxLeafName({
23
+ fullPath: target,
24
+ hierarchyDelimiter: before.hierarchyDelimiter,
25
+ });
26
+ if (isReservedFolderName(leaf)) {
27
+ return invalid(`“${leaf}” is reserved for a system folder. Pick another name.`);
28
+ }
29
+ // A path a pending rename is heading for is as taken as one a folder sits
30
+ // at. A failed rename keeps its target only so the client can offer a
31
+ // retry; nothing is coming for it, so it claims nothing.
32
+ const folders = await deps.mailbox.listAllByAccount(accountId);
33
+ for (const folder of folders) {
34
+ if (folder.mailboxId === before.mailboxId)
35
+ continue;
36
+ if (folder.fullPath === target) {
37
+ return invalid(`A folder named “${target}” is already there.`);
38
+ }
39
+ if (folder.syncStatus === MailboxSyncStatus.pending &&
40
+ folder.pendingPath === target) {
41
+ return invalid(`“${folder.fullPath}” is already being renamed to “${target}”.`);
42
+ }
43
+ }
44
+ return accept(target);
45
+ };
46
+ // A fullPath equal to the confirmed one is not a rename: it dismisses a failed
47
+ // intent, the one route out of `failed` that enqueues nothing. The row keeps
48
+ // the path the server holds until the rename settles.
49
+ export const ownedFolder = async (deps, accountConfigId, accountId, mailboxId) => {
50
+ const owned = ownedAccount(await deps.account.get(accountId), accountConfigId, "act");
51
+ if (!owned.ok)
52
+ return owned;
53
+ return folderInAccount(await deps.mailbox.get(accountId, mailboxId), accountId, "act");
54
+ };
55
+ export const changeFolderPath = async (deps, accountConfigId, change) => {
56
+ const { accountId, mailboxId, fullPath } = change;
57
+ const folder = await ownedFolder(deps, accountConfigId, accountId, mailboxId);
58
+ if (!folder.ok)
59
+ return folder;
60
+ const before = folder.value;
61
+ if (fullPath === before.fullPath) {
62
+ return accept({
63
+ outcome: "Recorded",
64
+ mailbox: await deps.mailboxQueue.dismissMailboxIntent(mailboxId, accountId),
65
+ });
66
+ }
67
+ const target = await renameTargetRefusal(deps, accountId, before, fullPath);
68
+ if (!target.ok)
69
+ return target;
70
+ if (!carriesIntent(before)) {
71
+ if (before.syncStatus === MailboxSyncStatus.pending &&
72
+ before.pendingPath === fullPath) {
73
+ return accept({ outcome: "AlreadyRecorded", mailbox: before });
74
+ }
75
+ return refuse({ kind: "Conflict", message: folderInFlight(before) });
76
+ }
77
+ return accept({
78
+ outcome: "Recorded",
79
+ mailbox: await deps.mailboxQueue.renameMailbox(mailboxId, fullPath, accountId),
80
+ });
81
+ };
82
+ export const createFolder = async (deps, accountConfigId, folder) => {
83
+ const { accountId, namespaceType, fullPath } = folder;
84
+ const owned = ownedAccount(await deps.account.get(accountId), accountConfigId, "act");
85
+ if (!owned.ok)
86
+ return owned;
87
+ const created = await deps.mailboxQueue.createMailbox({
88
+ accountId,
89
+ namespaceType,
90
+ namespacePrefix: "",
91
+ hierarchyDelimiter: "/",
92
+ fullPath,
93
+ uidValidity: 0,
94
+ uidNext: 1,
95
+ highestModseq: "0",
96
+ messageCount: 0,
97
+ unseenCount: 0,
98
+ deletedCount: 0,
99
+ totalSize: 0,
100
+ lastSyncUid: 0,
101
+ highWaterMarkUid: 0,
102
+ lastMessageSyncAt: 0,
103
+ }, accountId, true);
104
+ if (created.outcome === "PathTaken") {
105
+ const existing = await deps.mailbox.findByPath(accountId, fullPath);
106
+ if (!existing) {
107
+ throw new Error(`No folder at "${fullPath}" after its create reported the path taken`);
108
+ }
109
+ return accept({ outcome: "Existing", mailbox: existing });
110
+ }
111
+ return accept({ outcome: "Created", mailbox: created.mailbox });
112
+ };
@@ -0,0 +1,13 @@
1
+ import type { IMailboxSpecialUseRepository } from "@remit/data-ports";
2
+ import type { FolderPlacement } from "./heuristics/classifyPlacement.js";
3
+ export type FolderPlacementLookup = Pick<IMailboxSpecialUseRepository, "findJunkMailbox" | "findInboxMailbox">;
4
+ type RoleMailbox = Awaited<ReturnType<IMailboxSpecialUseRepository["findJunkMailbox"]>>;
5
+ export interface FolderRoleMailboxes {
6
+ junkMailbox: RoleMailbox;
7
+ inboxMailbox: RoleMailbox;
8
+ }
9
+ export declare const resolveFolderRoleMailboxes: (lookup: FolderPlacementLookup, accountId: string) => Promise<FolderRoleMailboxes>;
10
+ export declare const placementOf: (mailboxId: string, { junkMailbox, inboxMailbox }: FolderRoleMailboxes) => FolderPlacement;
11
+ export declare const resolveFolderPlacement: (lookup: FolderPlacementLookup, accountId: string, mailboxId: string) => Promise<FolderPlacement>;
12
+ export declare const resolveAccountFolderPlacements: (lookup: FolderPlacementLookup, accountIds: readonly string[]) => Promise<ReadonlyMap<string, FolderPlacement>>;
13
+ export {};
@@ -0,0 +1,23 @@
1
+ export const resolveFolderRoleMailboxes = async (lookup, accountId) => ({
2
+ junkMailbox: await lookup.findJunkMailbox(accountId),
3
+ inboxMailbox: await lookup.findInboxMailbox(accountId),
4
+ });
5
+ export const placementOf = (mailboxId, { junkMailbox, inboxMailbox }) => {
6
+ if (junkMailbox?.mailboxId === mailboxId)
7
+ return "junk";
8
+ if (inboxMailbox?.mailboxId === mailboxId)
9
+ return "inbox";
10
+ return "other";
11
+ };
12
+ export const resolveFolderPlacement = async (lookup, accountId, mailboxId) => placementOf(mailboxId, await resolveFolderRoleMailboxes(lookup, accountId));
13
+ export const resolveAccountFolderPlacements = async (lookup, accountIds) => {
14
+ const placements = new Map();
15
+ for (const accountId of accountIds) {
16
+ const { junkMailbox, inboxMailbox } = await resolveFolderRoleMailboxes(lookup, accountId);
17
+ if (inboxMailbox)
18
+ placements.set(inboxMailbox.mailboxId, "inbox");
19
+ if (junkMailbox)
20
+ placements.set(junkMailbox.mailboxId, "junk");
21
+ }
22
+ return placements;
23
+ };
@@ -0,0 +1,10 @@
1
+ export interface AuthenticationResult {
2
+ method: string;
3
+ result: string;
4
+ properties: ReadonlyMap<string, string>;
5
+ }
6
+ export interface AuthenticationResults {
7
+ authservId: string;
8
+ results: AuthenticationResult[];
9
+ }
10
+ export declare const parseAuthenticationResults: (value: string) => AuthenticationResults | null;
@@ -0,0 +1,187 @@
1
+ const DELIMITERS = new Set([";", "=", "(", ")", '"']);
2
+ const KEYWORD = /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
3
+ const METHOD = /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\/[0-9]+)?$/;
4
+ const PROPERTY_KEY = /^[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?)?$/;
5
+ const VERSION = /^[0-9]+$/;
6
+ const isWhitespace = (c) => c === " " || c === "\t" || c === "\r" || c === "\n";
7
+ const skipComment = (input, start) => {
8
+ let depth = 0;
9
+ for (let i = start; i < input.length; i++) {
10
+ const c = input[i];
11
+ if (c === "\\") {
12
+ i++;
13
+ continue;
14
+ }
15
+ if (c === "(")
16
+ depth++;
17
+ if (c === ")")
18
+ depth--;
19
+ if (depth === 0)
20
+ return i + 1;
21
+ }
22
+ return null;
23
+ };
24
+ const readQuoted = (input, start) => {
25
+ let text = "";
26
+ for (let i = start + 1; i < input.length; i++) {
27
+ const c = input[i];
28
+ if (c === "\\") {
29
+ if (i + 1 >= input.length)
30
+ return null;
31
+ text += input[i + 1];
32
+ i++;
33
+ continue;
34
+ }
35
+ if (c === '"')
36
+ return { text, end: i + 1 };
37
+ text += c;
38
+ }
39
+ return null;
40
+ };
41
+ const tokenize = (input) => {
42
+ const tokens = [];
43
+ let i = 0;
44
+ while (i < input.length) {
45
+ const c = input[i];
46
+ if (isWhitespace(c)) {
47
+ i++;
48
+ continue;
49
+ }
50
+ if (c === "(") {
51
+ const end = skipComment(input, i);
52
+ if (end === null)
53
+ return null;
54
+ i = end;
55
+ continue;
56
+ }
57
+ if (c === ")")
58
+ return null;
59
+ if (c === ";") {
60
+ tokens.push({ kind: "semicolon" });
61
+ i++;
62
+ continue;
63
+ }
64
+ if (c === "=") {
65
+ tokens.push({ kind: "equals" });
66
+ i++;
67
+ continue;
68
+ }
69
+ if (c === '"') {
70
+ const quoted = readQuoted(input, i);
71
+ if (!quoted)
72
+ return null;
73
+ tokens.push({ kind: "quoted", text: quoted.text });
74
+ i = quoted.end;
75
+ continue;
76
+ }
77
+ let end = i;
78
+ while (end < input.length &&
79
+ !isWhitespace(input[end]) &&
80
+ !DELIMITERS.has(input[end])) {
81
+ end++;
82
+ }
83
+ tokens.push({ kind: "word", text: input.slice(i, end) });
84
+ i = end;
85
+ }
86
+ return tokens;
87
+ };
88
+ const splitStatements = (tokens) => {
89
+ const statements = [[]];
90
+ for (const token of tokens) {
91
+ if (token.kind === "semicolon") {
92
+ statements.push([]);
93
+ continue;
94
+ }
95
+ statements[statements.length - 1].push(token);
96
+ }
97
+ return statements;
98
+ };
99
+ const isValue = (token) => token !== undefined && (token.kind === "word" || token.kind === "quoted");
100
+ const joinsKey = (previous, next) => previous.endsWith(".") ||
101
+ previous.endsWith("/") ||
102
+ next.startsWith(".") ||
103
+ next.startsWith("/");
104
+ const readPairs = (statement) => {
105
+ const pairs = [];
106
+ let key = [];
107
+ for (let i = 0; i < statement.length; i++) {
108
+ const token = statement[i];
109
+ if (token.kind === "equals") {
110
+ const value = statement[i + 1];
111
+ if (key.length === 0 || !isValue(value))
112
+ return null;
113
+ pairs.push([key.join("").toLowerCase(), value]);
114
+ key = [];
115
+ i++;
116
+ continue;
117
+ }
118
+ if (token.kind !== "word")
119
+ return null;
120
+ const previous = key[key.length - 1];
121
+ if (previous !== undefined && !joinsKey(previous, token.text))
122
+ return null;
123
+ key.push(token.text);
124
+ }
125
+ if (key.length > 0)
126
+ return null;
127
+ return pairs;
128
+ };
129
+ const parseResult = (statement) => {
130
+ const pairs = readPairs(statement);
131
+ if (!pairs || pairs.length === 0)
132
+ return null;
133
+ const [[methodKey, resultToken], ...rest] = pairs;
134
+ if (!METHOD.test(methodKey))
135
+ return null;
136
+ if (resultToken.kind !== "word")
137
+ return null;
138
+ const result = resultToken.text.toLowerCase();
139
+ if (!KEYWORD.test(result))
140
+ return null;
141
+ const properties = new Map();
142
+ for (const [key, value] of rest) {
143
+ if (!PROPERTY_KEY.test(key))
144
+ return null;
145
+ if (!properties.has(key))
146
+ properties.set(key, value.text);
147
+ }
148
+ return { method: methodKey.split("/")[0], result, properties };
149
+ };
150
+ const parseAuthservId = (statement) => {
151
+ const [id, version, ...extra] = statement;
152
+ if (!isValue(id) || extra.length > 0)
153
+ return null;
154
+ if (version !== undefined) {
155
+ if (version.kind !== "word" || !VERSION.test(version.text))
156
+ return null;
157
+ }
158
+ const authservId = id.text.toLowerCase();
159
+ return authservId.length > 0 ? authservId : null;
160
+ };
161
+ const isNoResult = (statement) => statement.length === 1 &&
162
+ statement[0].kind === "word" &&
163
+ statement[0].text.toLowerCase() === "none";
164
+ export const parseAuthenticationResults = (value) => {
165
+ const tokens = tokenize(value);
166
+ if (!tokens)
167
+ return null;
168
+ const [head, ...tail] = splitStatements(tokens);
169
+ const headIsResult = head.some((token) => token.kind === "equals");
170
+ const authservId = headIsResult ? "" : parseAuthservId(head);
171
+ if (authservId === null)
172
+ return null;
173
+ const statements = (headIsResult ? [head, ...tail] : tail).filter((statement) => statement.length > 0);
174
+ if (statements.length === 0)
175
+ return null;
176
+ if (statements.length === 1 && isNoResult(statements[0])) {
177
+ return { authservId, results: [] };
178
+ }
179
+ const results = [];
180
+ for (const statement of statements) {
181
+ const result = parseResult(statement);
182
+ if (!result)
183
+ return null;
184
+ results.push(result);
185
+ }
186
+ return { authservId, results };
187
+ };
@@ -0,0 +1,82 @@
1
+ import { AuthResultVerdict, MessageCategory } from "@remit/domain-enums";
2
+ import type { ParsedMail } from "mailparser";
3
+ type Category = (typeof MessageCategory)[keyof typeof MessageCategory];
4
+ type AuthVerdictValue = (typeof AuthResultVerdict)[keyof typeof AuthResultVerdict];
5
+ export interface MessageAuthResult {
6
+ dmarc?: AuthVerdictValue;
7
+ spf?: AuthVerdictValue;
8
+ dkim?: AuthVerdictValue;
9
+ }
10
+ export interface MessageProviderSpam {
11
+ classified: boolean;
12
+ score?: string;
13
+ source?: string;
14
+ }
15
+ /**
16
+ * Structured sender-authenticity signal extracted from DKIM headers.
17
+ * Persisted alongside `category` during body-sync so the intelligence
18
+ * sidebar (#425) can render phishing verdicts without re-parsing.
19
+ *
20
+ * Present only when a DKIM-Signature header exists; absent otherwise
21
+ * (absence means "no signal" — no comparison was possible).
22
+ */
23
+ export interface MessageAuthenticity {
24
+ /** Domain of the From header address */
25
+ fromDomain: string;
26
+ /** DKIM signing domain (d=) that was compared, when a signature is present */
27
+ dkimDomain?: string;
28
+ /** True when none of the DKIM signing domains aligns with the From domain */
29
+ dkimMismatch: boolean;
30
+ }
31
+ /**
32
+ * Header-only classification. Pure function. First match wins. Falls through
33
+ * to `personal` so misclassification stays in the safest bucket.
34
+ *
35
+ * Rules are ordered most-specific-signal first. Two properties drive the
36
+ * order, and both were violated by the original table (issue #45):
37
+ *
38
+ * - A signal that identifies WHO sent the mail (calendar part, allow-listed
39
+ * domain) outranks a signal that only says the mail was sent in bulk.
40
+ * - `Precedence` and `Auto-Submitted` say "a machine sent this", which is true
41
+ * of nearly every newsletter, marketing blast, and platform notification.
42
+ * Ranking them first collapsed `newsletter`, `marketing`, `social` and
43
+ * `transactional` into `automated`, leaving those buckets empty.
44
+ *
45
+ * 1. `Content-Type: text/calendar` part anywhere → `transactional`
46
+ * 2. From-domain in TRANSACTIONAL_DOMAINS → `transactional`
47
+ * 3. From-domain in SOCIAL_DOMAINS → `social`
48
+ * 4. `List-Unsubscribe` AND `List-Id` → `newsletter`
49
+ * 5. `List-Unsubscribe` only → `marketing`
50
+ * 6. `Auto-Submitted: auto-generated|auto-replied` → `automated`
51
+ * 7. `Precedence: bulk|list|junk` → `automated`
52
+ * 8. Machine sender (no-reply local-part, `Feedback-ID`,
53
+ * `X-Auto-Response-Suppress`) → `automated`
54
+ * 9. DKIM `d=` differs from From domain → `automated`
55
+ * 10. fallback → `personal`
56
+ */
57
+ export declare const classifyByHeaders: (parsed: ParsedMail) => Category;
58
+ /**
59
+ * Extract the structured authenticity signal from parsed headers.
60
+ *
61
+ * Returns a `MessageAuthenticity` when at least one DKIM-Signature header
62
+ * is present (a comparison was possible). Returns `null` when there are no
63
+ * DKIM-Signature headers so callers can omit the field entirely — absence
64
+ * means "no signal", not "mismatch: false".
65
+ *
66
+ * The alignment rule exactly mirrors the category heuristic (rule 7) so the
67
+ * structured `dkimMismatch` boolean and the `automated` category can never
68
+ * disagree.
69
+ */
70
+ export declare const extractAuthenticity: (parsed: ParsedMail) => MessageAuthenticity | null;
71
+ export declare const extractAuthResult: (parsed: ParsedMail) => MessageAuthResult | null;
72
+ /**
73
+ * Extract provider spam-filter signal from X-SpamExperts-Class, X-Spam-Status,
74
+ * or X-HalOne-Spam-Probability headers. Returns null when none are present.
75
+ */
76
+ export declare const extractProviderSpam: (parsed: ParsedMail) => MessageProviderSpam | null;
77
+ /**
78
+ * Returns true when a List-Unsubscribe header is present, indicating a
79
+ * bulk/mailing-list sender.
80
+ */
81
+ export declare const extractHasListUnsubscribe: (parsed: ParsedMail) => boolean;
82
+ export {};