@open-workspace/email 0.3.0 → 0.4.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.
- package/README.md +22 -22
- package/alchemy/index.ts +273 -0
- package/drizzle/mailbox/0000_puzzling_dracula.sql +149 -0
- package/drizzle/mailbox/0001_mute_jack_flag.sql +12 -0
- package/drizzle/mailbox/meta/0000_snapshot.json +1002 -0
- package/drizzle/mailbox/meta/0001_snapshot.json +1002 -0
- package/drizzle/mailbox/meta/_journal.json +20 -0
- package/drizzle/mailbox/migrations.js +12 -0
- package/index.ts +3 -1
- package/migrations/{0000_email.sql → 0000_dry_moon_knight.sql} +26 -20
- package/migrations/meta/0000_snapshot.json +125 -109
- package/migrations/meta/_journal.json +2 -2
- package/package.json +74 -68
- package/shared/attachment-preview.ts +37 -27
- package/shared/folder-order.ts +106 -0
- package/shared/mail-authentication.ts +8 -3
- package/shared/mail-feedback.ts +18 -25
- package/shared/mail-headers.ts +114 -39
- package/shared/mail-import.ts +38 -0
- package/shared/mail-path.ts +42 -0
- package/shared/mail-received.ts +174 -31
- package/shared/mail-remote.ts +59 -2
- package/shared/mail-sender-intel.ts +22 -4
- package/shared/mail.ts +77 -28
- package/shared/module.ts +33 -0
- package/shared/office-preview.ts +14 -2
- package/shared/pdfjs-assets.ts +35 -0
- package/shared/permissions.ts +73 -0
- package/shared/webhooks.ts +41 -0
- package/src/admin/blocked-senders.tsx +189 -0
- package/src/client.tsx +97 -0
- package/src/lib/mail-api.ts +9 -0
- package/src/mail/attachment-preview-dialog.tsx +161 -0
- package/src/mail/blocked-senders.tsx +138 -0
- package/src/mail/compose-window-chrome.tsx +194 -0
- package/src/mail/compose-window.tsx +486 -0
- package/src/mail/composer-asset-bookmarks.ts +409 -0
- package/src/mail/composer-asset.tsx +229 -0
- package/src/mail/composer-attachment-list.tsx +138 -0
- package/src/mail/composer-editor-document.ts +65 -0
- package/src/mail/composer-editor.tsx +284 -0
- package/src/mail/composer-linked-attachment-text.ts +12 -0
- package/src/mail/composer-recipient-fields.tsx +165 -0
- package/src/mail/composer-recipients.ts +208 -0
- package/src/mail/composer-session.ts +562 -0
- package/src/mail/composer-toolbar.tsx +303 -0
- package/src/mail/composer-upload-client.ts +105 -0
- package/src/mail/composer-upload-progress.ts +53 -0
- package/src/mail/conversation-list.tsx +550 -0
- package/src/mail/conversation-loading-skeleton.tsx +119 -0
- package/src/mail/conversation-message.tsx +346 -0
- package/src/mail/conversation-reply.tsx +470 -0
- package/src/mail/conversation-selection-toolbar.tsx +322 -0
- package/src/mail/conversation-view.tsx +437 -0
- package/src/mail/delivery-indicator.tsx +272 -0
- package/src/mail/email-html-body.tsx +203 -0
- package/src/mail/folder-glyph.tsx +46 -0
- package/src/mail/folder-manager.tsx +532 -0
- package/src/mail/format-bytes.ts +7 -0
- package/src/mail/mail-app-bar.tsx +253 -0
- package/src/mail/mail-background.tsx +12 -0
- package/src/mail/mail-dock.tsx +137 -0
- package/src/mail/mail-folders.ts +44 -0
- package/src/mail/mail-layout.css +236 -0
- package/src/mail/mail-location.ts +73 -0
- package/src/mail/mail-mailbox-sheet.tsx +275 -0
- package/src/mail/mail-nav-rail.tsx +129 -0
- package/src/mail/mail-nav.tsx +324 -0
- package/src/mail/mail-prefetch.ts +59 -0
- package/src/mail/mail-primitives.tsx +66 -0
- package/src/mail/mail-queries.ts +209 -0
- package/src/mail/mail-query-cache.ts +486 -0
- package/src/mail/mail-seed.ts +47 -0
- package/src/mail/mail-shell.tsx +126 -0
- package/src/mail/mailbox-ai-settings.tsx +185 -0
- package/src/mail/mailbox-realtime.tsx +251 -0
- package/src/mail/mailbox-route.tsx +15 -0
- package/src/mail/message-actions.tsx +353 -0
- package/src/mail/message-attachments.tsx +200 -0
- package/src/mail/message-format.ts +75 -0
- package/src/mail/message-header.tsx +260 -0
- package/src/mail/message-translation.tsx +225 -0
- package/src/mail/original-message-sheet.tsx +619 -0
- package/src/mail/original-message.ts +191 -0
- package/src/mail/outbound-notification.ts +53 -0
- package/src/mail/pane-splitter.tsx +117 -0
- package/src/mail/pdf-preview-toolbar.tsx +152 -0
- package/src/mail/pdf-preview.css +44 -0
- package/src/mail/pdf-preview.tsx +156 -0
- package/src/mail/pdf-translation-layout.ts +95 -0
- package/src/mail/print-message.ts +92 -0
- package/src/mail/reader-pane.tsx +535 -0
- package/src/mail/recipient-combobox.tsx +335 -0
- package/src/mail/recipient-field-row.tsx +34 -0
- package/src/mail/reply-to-warning.tsx +50 -0
- package/src/mail/sanitize-email-html.ts +430 -0
- package/src/mail/sender-actions.tsx +149 -0
- package/src/mail/sender-authentication.tsx +485 -0
- package/src/mail/sender-dialogs.tsx +384 -0
- package/src/mail/sender-security.tsx +545 -0
- package/src/mail/toolbar-tooltip.tsx +29 -0
- package/src/mail/translation-html.ts +49 -0
- package/src/mail/translation-language.ts +21 -0
- package/src/mail/types.ts +53 -0
- package/src/mail/unread-document-indicator.tsx +60 -0
- package/src/mail/use-compose-submission.ts +208 -0
- package/src/mail/use-composer-attachment-preflight.ts +184 -0
- package/src/mail/use-composer-submission.ts +117 -0
- package/src/mail/use-conversation-selection.ts +186 -0
- package/src/mail/use-mail-controller.ts +427 -0
- package/src/mail/use-mail-data.ts +279 -0
- package/src/mail/use-mail-safety.ts +155 -0
- package/src/mail/use-mail-send.ts +142 -0
- package/src/mail/use-pdf-translation-overlay.ts +156 -0
- package/src/mail/use-pdf-translation.ts +100 -0
- package/src/mail/use-pdf-viewer.ts +215 -0
- package/src/mail/use-reply-submission.ts +114 -0
- package/src/mail/use-sanitized-email-html.ts +20 -0
- package/src/mail/use-visible-message-read.ts +138 -0
- package/src/pwa/push-subscription.ts +151 -0
- package/src/settings/mail-apps-page.tsx +235 -0
- package/src/settings/notifications-page.tsx +291 -0
- package/worker/admin/blocked-sender-routes.ts +166 -0
- package/worker/admin/schemas.ts +11 -0
- package/worker/bindings.d.ts +14 -10
- package/worker/db/schema.ts +63 -40
- package/worker/jmap/eventsource.ts +84 -16
- package/worker/jmap/methods.ts +1630 -524
- package/worker/jmap/push-types.ts +27 -0
- package/worker/jmap/push.ts +140 -56
- package/worker/jmap/routes.ts +366 -105
- package/worker/mail/access.ts +258 -21
- package/worker/mail/ai-classification.ts +36 -20
- package/worker/mail/ai-translation.ts +13 -6
- package/worker/mail/auth-headers.ts +395 -137
- package/worker/mail/bimi.ts +200 -73
- package/worker/mail/bootstrap.ts +79 -0
- package/worker/mail/capped-body.ts +36 -0
- package/worker/mail/classification-signals.ts +35 -25
- package/worker/mail/conversation-cursor.ts +30 -19
- package/worker/mail/delivery-events.ts +88 -62
- package/worker/mail/delivery-facts.ts +46 -10
- package/worker/mail/demo-routes.ts +34 -10
- package/worker/mail/doh.ts +62 -23
- package/worker/mail/downloads.ts +75 -40
- package/worker/mail/edge-cache.ts +155 -0
- package/worker/mail/header-metadata.ts +88 -22
- package/worker/mail/impersonation.ts +160 -38
- package/worker/mail/import-routes.ts +313 -0
- package/worker/mail/inbound-delivery.ts +19 -9
- package/worker/mail/inbound-threading.ts +18 -8
- package/worker/mail/inbound.ts +82 -40
- package/worker/mail/ip-intel.ts +110 -27
- package/worker/mail/isolate-cache.ts +35 -0
- package/worker/mail/link-domains.ts +29 -7
- package/worker/mail/mailbox-deletion.ts +10 -4
- package/worker/mail/mailbox-directory.ts +16 -6
- package/worker/mail/mailing-list.ts +94 -39
- package/worker/mail/message-security.ts +90 -15
- package/worker/mail/message-view.ts +18 -6
- package/worker/mail/office-conversion.ts +31 -9
- package/worker/mail/office-preview-container.ts +40 -13
- package/worker/mail/office-preview-queue.ts +76 -32
- package/worker/mail/org-domain.ts +8 -1
- package/worker/mail/outbound-content.ts +71 -26
- package/worker/mail/outbound-delivery.ts +31 -17
- package/worker/mail/outbound-service.ts +48 -30
- package/worker/mail/outbound-threading.ts +30 -17
- package/worker/mail/outbound.ts +97 -70
- package/worker/mail/outgoing-context.ts +25 -15
- package/worker/mail/participants.ts +44 -29
- package/worker/mail/push-notifications.ts +271 -173
- package/worker/mail/quote-parser.ts +21 -10
- package/worker/mail/r2-presigned-urls.ts +31 -25
- package/worker/mail/receive.ts +23 -11
- package/worker/mail/recipients.ts +6 -1
- package/worker/mail/remote-proxy.ts +187 -31
- package/worker/mail/remote-routes.ts +87 -0
- package/worker/mail/remote-token.ts +104 -0
- package/worker/mail/repeat.ts +19 -0
- package/worker/mail/reply-plan.ts +62 -33
- package/worker/mail/rfc.ts +32 -7
- package/worker/mail/routes.ts +870 -541
- package/worker/mail/safety-routes.ts +328 -200
- package/worker/mail/schemas.ts +84 -105
- package/worker/mail/search.ts +1 -0
- package/worker/mail/security-settings.ts +41 -9
- package/worker/mail/sender-intel.ts +206 -51
- package/worker/mail/spamhaus.ts +92 -45
- package/worker/mail/unsubscribe.ts +76 -18
- package/worker/mail/uploads.ts +126 -93
- package/worker/mail/webhook-source.ts +35 -12
- package/worker/mail/workspace-safety.ts +97 -38
- package/worker/mailbox/demo-html.ts +7 -7
- package/worker/mailbox/demo-seed.ts +10 -44
- package/worker/mailbox/demo.ts +595 -225
- package/worker/mailbox/email-index.ts +53 -0
- package/worker/mailbox/folder-model.ts +53 -17
- package/worker/mailbox/import-placement.ts +88 -0
- package/worker/mailbox/inbox-counts.ts +106 -0
- package/worker/mailbox/index.ts +1734 -721
- package/worker/mailbox/jmap-store.ts +139 -35
- package/worker/mailbox/model.ts +8 -6
- package/worker/mailbox/office-preview-jobs.ts +56 -25
- package/worker/mailbox/schema.ts +87 -66
- package/worker/mailbox/senders.ts +176 -70
- package/worker/mcp/mail-action-tools.ts +128 -87
- package/worker/mcp/mail-compose-tools.ts +133 -82
- package/worker/mcp/mail-query-tools.ts +120 -71
- package/worker/mcp/mail-safety-tools.ts +321 -248
- package/worker/mcp/schemas.ts +43 -0
- package/worker/mcp/tools.ts +13 -10
- package/worker/module.ts +43 -18
- package/worker/sql.d.ts +8 -0
- package/alchemy.ts +0 -142
- package/containers/office-preview/Dockerfile +0 -10
- package/shared/mail-rules.ts +0 -156
- package/worker/admin/report-routes.ts +0 -164
- package/worker/mail/reported-messages.ts +0 -68
- package/worker/mailbox/migrations.ts +0 -10
- package/worker/mailbox/rules.ts +0 -110
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isComposerInlineImageContentType } from "./mail";
|
|
2
|
-
import { officePreviewExtension
|
|
2
|
+
import { officePreviewExtension } from "./office-preview";
|
|
3
|
+
import type { PdfPreviewStatus } from "./office-preview";
|
|
3
4
|
|
|
4
5
|
const TEXT_TYPES = new Set([
|
|
5
6
|
"text/plain",
|
|
@@ -13,25 +14,25 @@ const TEXT_TYPES = new Set([
|
|
|
13
14
|
"application/yaml",
|
|
14
15
|
]);
|
|
15
16
|
|
|
16
|
-
const EXTENSION_TYPES
|
|
17
|
-
pdf
|
|
18
|
-
png
|
|
19
|
-
jpg
|
|
20
|
-
jpeg
|
|
21
|
-
gif
|
|
22
|
-
webp
|
|
23
|
-
avif
|
|
24
|
-
bmp
|
|
25
|
-
txt
|
|
26
|
-
log
|
|
27
|
-
md
|
|
28
|
-
csv
|
|
29
|
-
tsv
|
|
30
|
-
json
|
|
31
|
-
xml
|
|
32
|
-
yaml
|
|
33
|
-
yml
|
|
34
|
-
|
|
17
|
+
const EXTENSION_TYPES = new Map([
|
|
18
|
+
["pdf", "application/pdf"],
|
|
19
|
+
["png", "image/png"],
|
|
20
|
+
["jpg", "image/jpeg"],
|
|
21
|
+
["jpeg", "image/jpeg"],
|
|
22
|
+
["gif", "image/gif"],
|
|
23
|
+
["webp", "image/webp"],
|
|
24
|
+
["avif", "image/avif"],
|
|
25
|
+
["bmp", "image/bmp"],
|
|
26
|
+
["txt", "text/plain"],
|
|
27
|
+
["log", "text/plain"],
|
|
28
|
+
["md", "text/markdown"],
|
|
29
|
+
["csv", "text/csv"],
|
|
30
|
+
["tsv", "text/tab-separated-values"],
|
|
31
|
+
["json", "application/json"],
|
|
32
|
+
["xml", "application/xml"],
|
|
33
|
+
["yaml", "application/yaml"],
|
|
34
|
+
["yml", "application/yaml"],
|
|
35
|
+
]);
|
|
35
36
|
|
|
36
37
|
export function attachmentPreview(file: {
|
|
37
38
|
filename: string;
|
|
@@ -44,24 +45,33 @@ export function attachmentPreview(file: {
|
|
|
44
45
|
if (file.pdfPreviewStatus && officePreviewExtension(file.filename)) {
|
|
45
46
|
return { kind: "pdf", contentType: "application/pdf" };
|
|
46
47
|
}
|
|
47
|
-
|
|
48
|
+
|
|
49
|
+
let contentType =
|
|
50
|
+
file.contentType.split(";", 1)[0]?.trim().toLowerCase() ?? "";
|
|
51
|
+
|
|
48
52
|
if (!contentType || contentType === "application/octet-stream") {
|
|
49
53
|
const extension = file.filename.split(".").at(-1)?.toLowerCase() ?? "";
|
|
50
|
-
contentType =
|
|
54
|
+
contentType = EXTENSION_TYPES.get(extension) ?? "";
|
|
51
55
|
}
|
|
56
|
+
|
|
52
57
|
if (contentType === "application/pdf") return { kind: "pdf", contentType };
|
|
58
|
+
|
|
53
59
|
if (
|
|
54
|
-
isComposerInlineImageContentType(contentType)
|
|
55
|
-
|
|
56
|
-
|
|
60
|
+
isComposerInlineImageContentType(contentType) ||
|
|
61
|
+
contentType === "image/avif" ||
|
|
62
|
+
contentType === "image/bmp"
|
|
57
63
|
) {
|
|
58
64
|
return { kind: "image", contentType };
|
|
59
65
|
}
|
|
66
|
+
|
|
60
67
|
if (TEXT_TYPES.has(contentType)) {
|
|
61
|
-
const charset =
|
|
62
|
-
|
|
68
|
+
const charset =
|
|
69
|
+
/;\s*charset\s*=\s*"?([a-zA-Z0-9_-]+)/iu.exec(file.contentType)?.[1] ??
|
|
70
|
+
"utf-8";
|
|
71
|
+
|
|
63
72
|
// XML and other structured text must never execute as same-origin documents.
|
|
64
73
|
return { kind: "text", contentType: `text/plain; charset=${charset}` };
|
|
65
74
|
}
|
|
75
|
+
|
|
66
76
|
return null;
|
|
67
77
|
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One order for every folder of a mailbox. Inbox is pinned first; the system
|
|
3
|
+
* folders after it can be placed anywhere among the person's own folders.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** System folders that take part in the order; Inbox is always first. */
|
|
7
|
+
export const MOVABLE_SYSTEM_FOLDER_IDS = [
|
|
8
|
+
"sent",
|
|
9
|
+
"archive",
|
|
10
|
+
"spam",
|
|
11
|
+
"trash",
|
|
12
|
+
] as const;
|
|
13
|
+
|
|
14
|
+
export type SystemFolderId =
|
|
15
|
+
| "inbox"
|
|
16
|
+
| (typeof MOVABLE_SYSTEM_FOLDER_IDS)[number];
|
|
17
|
+
|
|
18
|
+
/** Where the system folders sit until someone moves them: before any custom folder. */
|
|
19
|
+
export const DEFAULT_SYSTEM_FOLDER_ORDER = {
|
|
20
|
+
inbox: 0,
|
|
21
|
+
sent: 10,
|
|
22
|
+
archive: 20,
|
|
23
|
+
spam: 30,
|
|
24
|
+
trash: 40,
|
|
25
|
+
} satisfies Record<SystemFolderId, number>;
|
|
26
|
+
|
|
27
|
+
function isSystemFolderId(id: string): id is SystemFolderId {
|
|
28
|
+
return Object.hasOwn(DEFAULT_SYSTEM_FOLDER_ORDER, id);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The default position of a system folder by id; undefined for custom folders. */
|
|
32
|
+
export function defaultSystemFolderOrder(id: string) {
|
|
33
|
+
return isSystemFolderId(id) ? DEFAULT_SYSTEM_FOLDER_ORDER[id] : undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The gap between neighbours, so a folder created later can go after the last. */
|
|
37
|
+
export const FOLDER_ORDER_STEP = 10;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Whether `folderIds` is a complete order of the movable folders: every
|
|
41
|
+
* movable system folder and custom folder exactly once, and never Inbox.
|
|
42
|
+
*/
|
|
43
|
+
export function isCompleteFolderOrder(
|
|
44
|
+
folderIds: readonly string[],
|
|
45
|
+
customFolderIds: readonly string[]
|
|
46
|
+
) {
|
|
47
|
+
const expected = new Set<string>([
|
|
48
|
+
...MOVABLE_SYSTEM_FOLDER_IDS,
|
|
49
|
+
...customFolderIds,
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
return (
|
|
53
|
+
folderIds.length === expected.size &&
|
|
54
|
+
new Set(folderIds).size === folderIds.length &&
|
|
55
|
+
folderIds.every((id) => expected.has(id))
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The stored position of each folder in a complete order, after Inbox's 0. */
|
|
60
|
+
export function folderOrderPositions(folderIds: readonly string[]) {
|
|
61
|
+
return folderIds.map((id, index) => ({
|
|
62
|
+
id,
|
|
63
|
+
sortOrder: (index + 1) * FOLDER_ORDER_STEP,
|
|
64
|
+
}));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
type Orderable = { id: string; name: string; sortOrder: number };
|
|
68
|
+
|
|
69
|
+
/** Folders in display order: Inbox first, then by position, ties by name. */
|
|
70
|
+
export function sortFolders<T extends Orderable>(folders: readonly T[]): T[] {
|
|
71
|
+
return [...folders].sort(
|
|
72
|
+
(left, right) =>
|
|
73
|
+
Number(right.id === "inbox") - Number(left.id === "inbox") ||
|
|
74
|
+
left.sortOrder - right.sortOrder ||
|
|
75
|
+
left.name.localeCompare(right.name)
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The order after moving one folder a step up (-1) or down (+1), as the ids
|
|
81
|
+
* the reorder API takes (every folder but Inbox); null when it can't move.
|
|
82
|
+
*/
|
|
83
|
+
export function moveFolderInOrder<T extends Orderable>(
|
|
84
|
+
folders: readonly T[],
|
|
85
|
+
folderId: string,
|
|
86
|
+
direction: -1 | 1
|
|
87
|
+
) {
|
|
88
|
+
const ids = sortFolders(folders)
|
|
89
|
+
.map((folder) => folder.id)
|
|
90
|
+
.filter((id) => id !== "inbox");
|
|
91
|
+
|
|
92
|
+
const index = ids.indexOf(folderId);
|
|
93
|
+
const target = index + direction;
|
|
94
|
+
|
|
95
|
+
const moving = ids[index];
|
|
96
|
+
const neighbour = ids[target];
|
|
97
|
+
|
|
98
|
+
if (index === -1 || moving === undefined || neighbour === undefined) {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
ids[index] = neighbour;
|
|
103
|
+
ids[target] = moving;
|
|
104
|
+
|
|
105
|
+
return ids;
|
|
106
|
+
}
|
|
@@ -14,6 +14,7 @@ export const authResults = [
|
|
|
14
14
|
"permerror",
|
|
15
15
|
"policy",
|
|
16
16
|
] as const;
|
|
17
|
+
|
|
17
18
|
export type AuthResult = (typeof authResults)[number];
|
|
18
19
|
|
|
19
20
|
export type DmarcPolicy = "none" | "quarantine" | "reject";
|
|
@@ -46,7 +47,11 @@ export type ArcSealer = {
|
|
|
46
47
|
cv: string | null;
|
|
47
48
|
};
|
|
48
49
|
|
|
49
|
-
export type SenderVerdict =
|
|
50
|
+
export type SenderVerdict =
|
|
51
|
+
| "verified"
|
|
52
|
+
| "unverified"
|
|
53
|
+
| "suspicious"
|
|
54
|
+
| "unavailable";
|
|
50
55
|
|
|
51
56
|
export type SenderVerdictReason =
|
|
52
57
|
| "dmarc_pass"
|
|
@@ -78,7 +83,7 @@ export type InboundAuthentication = {
|
|
|
78
83
|
result: AuthResult;
|
|
79
84
|
headerFrom: string | null;
|
|
80
85
|
policy: DmarcPolicy | null;
|
|
81
|
-
alignedVia:
|
|
86
|
+
alignedVia: ("spf" | "dkim")[];
|
|
82
87
|
} | null;
|
|
83
88
|
arc: {
|
|
84
89
|
result: "pass" | "fail" | "none";
|
|
@@ -131,7 +136,7 @@ export type MessageSecurity = {
|
|
|
131
136
|
via: string | null;
|
|
132
137
|
/** Organization that forwarded the message to this mailbox, when evident. */
|
|
133
138
|
forwardedVia: string | null;
|
|
134
|
-
/** The From domain is not one of this workspace's domains. */
|
|
139
|
+
/** The From domain is not one of this workspace's mail domains. */
|
|
135
140
|
external: boolean;
|
|
136
141
|
listUnsubscribe: ListUnsubscribe | null;
|
|
137
142
|
/** Most serious first; the reader merges them into one banner. */
|
package/shared/mail-feedback.ts
CHANGED
|
@@ -3,9 +3,8 @@
|
|
|
3
3
|
* records, as returned with every incoming message.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export type FeedbackLabel = ReportKind | "not_spam";
|
|
6
|
+
/** What a person said about a sender: "Report spam" or "Not spam". */
|
|
7
|
+
export type FeedbackLabel = "spam" | "not_spam";
|
|
9
8
|
|
|
10
9
|
export type SenderBlock = {
|
|
11
10
|
/** The stored pattern: an address, or `@domain` for the whole domain. */
|
|
@@ -42,26 +41,8 @@ export type Unsubscription = {
|
|
|
42
41
|
at: number;
|
|
43
42
|
};
|
|
44
43
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
/** Point-in-time copy of what the reporter saw, kept with an admin report. */
|
|
48
|
-
export type ReportSnapshot = {
|
|
49
|
-
fromName: string | null;
|
|
50
|
-
replyTo: string[];
|
|
51
|
-
to: string[];
|
|
52
|
-
verdict: string;
|
|
53
|
-
verdictReason: string;
|
|
54
|
-
warnings: string[];
|
|
55
|
-
spamScore: number | null;
|
|
56
|
-
ip: string | null;
|
|
57
|
-
network: string | null;
|
|
58
|
-
country: string | null;
|
|
59
|
-
ai: { spam: boolean; spamConfidence: number; reason: string } | null;
|
|
60
|
-
linkDomains: string[];
|
|
61
|
-
preview: string;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const DOMAIN_PATTERN = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9-]{2,63}$/u;
|
|
44
|
+
const DOMAIN_PATTERN =
|
|
45
|
+
/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9-]{2,63}$/u;
|
|
65
46
|
|
|
66
47
|
/**
|
|
67
48
|
* Normalizes a block entry: `alex@x.com` stays an address, `x.com` and
|
|
@@ -69,13 +50,20 @@ const DOMAIN_PATTERN = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9-]{2,
|
|
|
69
50
|
*/
|
|
70
51
|
export function normalizeBlockPattern(value: string) {
|
|
71
52
|
const input = value.trim().toLocaleLowerCase();
|
|
53
|
+
|
|
72
54
|
if (input.length > 320) return null;
|
|
73
55
|
const at = input.lastIndexOf("@");
|
|
56
|
+
|
|
74
57
|
if (at > 0) {
|
|
75
58
|
const domain = input.slice(at + 1);
|
|
76
|
-
|
|
59
|
+
|
|
60
|
+
return DOMAIN_PATTERN.test(domain) && !/[\s<>,;"]/u.test(input.slice(0, at))
|
|
61
|
+
? input
|
|
62
|
+
: null;
|
|
77
63
|
}
|
|
64
|
+
|
|
78
65
|
const domain = at === 0 ? input.slice(1) : input;
|
|
66
|
+
|
|
79
67
|
return DOMAIN_PATTERN.test(domain) ? `@${domain}` : null;
|
|
80
68
|
}
|
|
81
69
|
|
|
@@ -83,8 +71,13 @@ export function normalizeBlockPattern(value: string) {
|
|
|
83
71
|
export function blockPatternsFor(address: string) {
|
|
84
72
|
const value = address.trim().toLocaleLowerCase();
|
|
85
73
|
const at = value.lastIndexOf("@");
|
|
74
|
+
|
|
86
75
|
if (at <= 0) return [];
|
|
87
76
|
const labels = value.slice(at + 1).split(".");
|
|
88
|
-
|
|
77
|
+
|
|
78
|
+
const domains = labels
|
|
79
|
+
.slice(0, -1)
|
|
80
|
+
.map((_, index) => `@${labels.slice(index).join(".")}`);
|
|
81
|
+
|
|
89
82
|
return [value, ...domains];
|
|
90
83
|
}
|
package/shared/mail-headers.ts
CHANGED
|
@@ -16,23 +16,29 @@ export type HeaderField = {
|
|
|
16
16
|
export function headerBlock(raw: string) {
|
|
17
17
|
const separator = /\r?\n\r?\n/u.exec(raw);
|
|
18
18
|
const end = separator?.index ?? raw.length;
|
|
19
|
+
|
|
19
20
|
return raw.slice(0, Math.min(end, MAX_HEADER_BYTES));
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
/** Header fields of the top-level entity, in their original (topmost first) order. */
|
|
23
24
|
export function headerFields(raw: string): HeaderField[] {
|
|
24
|
-
const unfolded = headerBlock(raw).
|
|
25
|
+
const unfolded = headerBlock(raw).replaceAll(/\r?\n[\t ]+/gu, " ");
|
|
26
|
+
|
|
25
27
|
return unfolded.split(/\r?\n/gu).flatMap((line) => {
|
|
26
28
|
const colon = line.indexOf(":");
|
|
29
|
+
|
|
27
30
|
if (colon < 1) return [];
|
|
28
31
|
const name = line.slice(0, colon).trim().toLocaleLowerCase();
|
|
29
|
-
|
|
32
|
+
|
|
33
|
+
if (!/^[\u0021-\u0039\u003B-\u007E]+$/u.test(name)) return [];
|
|
34
|
+
|
|
30
35
|
return [{ name, value: line.slice(colon + 1).trim() }];
|
|
31
36
|
});
|
|
32
37
|
}
|
|
33
38
|
|
|
34
39
|
export function headerValues(raw: string, name: string) {
|
|
35
40
|
const expected = name.toLocaleLowerCase();
|
|
41
|
+
|
|
36
42
|
return headerFields(raw).flatMap((field) =>
|
|
37
43
|
field.name === expected && field.value ? [field.value] : []
|
|
38
44
|
);
|
|
@@ -55,23 +61,30 @@ export function scanComments(value: string): CommentScan {
|
|
|
55
61
|
const comments: string[] = [];
|
|
56
62
|
let depth = 0;
|
|
57
63
|
let quoted = false;
|
|
64
|
+
|
|
58
65
|
for (let index = 0; index < value.length; index += 1) {
|
|
59
|
-
const char = value
|
|
66
|
+
const char = value.charAt(index);
|
|
67
|
+
|
|
60
68
|
if (char === "\\" && (quoted || depth > 0) && index + 1 < value.length) {
|
|
61
69
|
const pair = value.slice(index, index + 2);
|
|
70
|
+
|
|
62
71
|
if (depth > 0) comment += pair;
|
|
63
72
|
else text += pair;
|
|
64
73
|
index += 1;
|
|
65
74
|
continue;
|
|
66
75
|
}
|
|
67
|
-
|
|
76
|
+
|
|
77
|
+
if (depth === 0 && char === '"') quoted = !quoted;
|
|
78
|
+
|
|
68
79
|
if (!quoted && char === "(") {
|
|
69
80
|
if (depth > 0) comment += char;
|
|
70
81
|
depth += 1;
|
|
71
82
|
continue;
|
|
72
83
|
}
|
|
84
|
+
|
|
73
85
|
if (!quoted && char === ")" && depth > 0) {
|
|
74
86
|
depth -= 1;
|
|
87
|
+
|
|
75
88
|
if (depth === 0) {
|
|
76
89
|
comments.push(comment.trim());
|
|
77
90
|
comment = "";
|
|
@@ -79,12 +92,16 @@ export function scanComments(value: string): CommentScan {
|
|
|
79
92
|
} else {
|
|
80
93
|
comment += char;
|
|
81
94
|
}
|
|
95
|
+
|
|
82
96
|
continue;
|
|
83
97
|
}
|
|
98
|
+
|
|
84
99
|
if (depth > 0) comment += char;
|
|
85
100
|
else text += char;
|
|
86
101
|
}
|
|
102
|
+
|
|
87
103
|
if (depth > 0 && comment) comments.push(comment.trim());
|
|
104
|
+
|
|
88
105
|
return { text, comments };
|
|
89
106
|
}
|
|
90
107
|
|
|
@@ -97,60 +114,93 @@ export function splitTopLevel(value: string, delimiter: string) {
|
|
|
97
114
|
let current = "";
|
|
98
115
|
let depth = 0;
|
|
99
116
|
let quoted = false;
|
|
117
|
+
|
|
100
118
|
for (let index = 0; index < value.length; index += 1) {
|
|
101
|
-
const char = value
|
|
119
|
+
const char = value.charAt(index);
|
|
120
|
+
|
|
102
121
|
if (char === "\\" && (quoted || depth > 0) && index + 1 < value.length) {
|
|
103
122
|
current += value.slice(index, index + 2);
|
|
104
123
|
index += 1;
|
|
105
124
|
continue;
|
|
106
125
|
}
|
|
107
|
-
|
|
126
|
+
|
|
127
|
+
if (depth === 0 && char === '"') quoted = !quoted;
|
|
108
128
|
else if (!quoted && char === "(") depth += 1;
|
|
109
129
|
else if (!quoted && char === ")" && depth > 0) depth -= 1;
|
|
130
|
+
|
|
110
131
|
if (char === delimiter && depth === 0 && !quoted) {
|
|
111
132
|
parts.push(current);
|
|
112
133
|
current = "";
|
|
113
134
|
continue;
|
|
114
135
|
}
|
|
136
|
+
|
|
115
137
|
current += char;
|
|
116
138
|
}
|
|
139
|
+
|
|
117
140
|
parts.push(current);
|
|
141
|
+
|
|
118
142
|
return parts;
|
|
119
143
|
}
|
|
120
144
|
|
|
121
145
|
export function unquote(value: string) {
|
|
122
146
|
const trimmed = value.trim();
|
|
123
|
-
|
|
124
|
-
|
|
147
|
+
|
|
148
|
+
if (trimmed.length >= 2 && trimmed.startsWith('"') && trimmed.endsWith('"')) {
|
|
149
|
+
return trimmed.slice(1, -1).replaceAll(/\\(.)/gu, "$1");
|
|
125
150
|
}
|
|
151
|
+
|
|
126
152
|
return trimmed;
|
|
127
153
|
}
|
|
128
154
|
|
|
129
155
|
/** Domain part of an address or a bare domain, lowercased without a trailing dot. */
|
|
130
156
|
export function addressDomain(value: string | null | undefined) {
|
|
131
157
|
if (!value) return null;
|
|
132
|
-
const trimmed = value.trim().
|
|
158
|
+
const trimmed = value.trim().replaceAll(/^<|>$/gu, "");
|
|
133
159
|
const at = trimmed.lastIndexOf("@");
|
|
134
|
-
|
|
160
|
+
|
|
161
|
+
const domain = (at === -1 ? trimmed : trimmed.slice(at + 1))
|
|
135
162
|
.replace(/\.$/u, "")
|
|
136
163
|
.toLocaleLowerCase();
|
|
137
|
-
|
|
164
|
+
|
|
165
|
+
return /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)+$/u.test(
|
|
166
|
+
domain
|
|
167
|
+
)
|
|
138
168
|
? domain
|
|
139
169
|
: null;
|
|
140
170
|
}
|
|
141
171
|
|
|
142
172
|
// --- Dates ---------------------------------------------------------------
|
|
143
173
|
|
|
144
|
-
const MONTHS = [
|
|
174
|
+
const MONTHS = [
|
|
175
|
+
"jan",
|
|
176
|
+
"feb",
|
|
177
|
+
"mar",
|
|
178
|
+
"apr",
|
|
179
|
+
"may",
|
|
180
|
+
"jun",
|
|
181
|
+
"jul",
|
|
182
|
+
"aug",
|
|
183
|
+
"sep",
|
|
184
|
+
"oct",
|
|
185
|
+
"nov",
|
|
186
|
+
"dec",
|
|
187
|
+
];
|
|
145
188
|
|
|
146
189
|
/** RFC 5322 §4.3 obsolete zones; military zones are treated as unknown (-0000). */
|
|
147
|
-
const OBSOLETE_ZONES
|
|
148
|
-
ut
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
190
|
+
const OBSOLETE_ZONES = new Map([
|
|
191
|
+
["ut", 0],
|
|
192
|
+
["gmt", 0],
|
|
193
|
+
["z", 0],
|
|
194
|
+
["utc", 0],
|
|
195
|
+
["est", -5 * 60],
|
|
196
|
+
["edt", -4 * 60],
|
|
197
|
+
["cst", -6 * 60],
|
|
198
|
+
["cdt", -5 * 60],
|
|
199
|
+
["mst", -7 * 60],
|
|
200
|
+
["mdt", -6 * 60],
|
|
201
|
+
["pst", -8 * 60],
|
|
202
|
+
["pdt", -7 * 60],
|
|
203
|
+
]);
|
|
154
204
|
|
|
155
205
|
export const RFC5322_DATE_PATTERN =
|
|
156
206
|
/(?:(?:mon|tue|wed|thu|fri|sat|sun)[a-z]*,?\s*)?(\d{1,2})\s+([a-z]{3})[a-z]*\s+(\d{2,4})\s+(\d{1,2}):(\d{2})(?::(\d{2})(?:\.\d+)?)?\s*([+-]\d{4}|[a-z]{1,3}\b)?/iu;
|
|
@@ -161,11 +211,14 @@ export const ISO_DATE_PATTERN =
|
|
|
161
211
|
function zoneOffsetMinutes(zone: string | undefined) {
|
|
162
212
|
if (!zone) return 0;
|
|
163
213
|
const numeric = /^([+-])(\d{2}):?(\d{2})$/u.exec(zone);
|
|
214
|
+
|
|
164
215
|
if (numeric) {
|
|
165
216
|
const minutes = Number(numeric[2]) * 60 + Number(numeric[3]);
|
|
217
|
+
|
|
166
218
|
return numeric[1] === "-" ? -minutes : minutes;
|
|
167
219
|
}
|
|
168
|
-
|
|
220
|
+
|
|
221
|
+
return OBSOLETE_ZONES.get(zone.toLocaleLowerCase()) ?? 0;
|
|
169
222
|
}
|
|
170
223
|
|
|
171
224
|
function utcDate(
|
|
@@ -175,37 +228,50 @@ function utcDate(
|
|
|
175
228
|
hour: number,
|
|
176
229
|
minute: number,
|
|
177
230
|
second: number,
|
|
178
|
-
offsetMinutes: number
|
|
231
|
+
offsetMinutes: number
|
|
179
232
|
) {
|
|
180
233
|
if (
|
|
181
|
-
month < 0 ||
|
|
182
|
-
|
|
234
|
+
month < 0 ||
|
|
235
|
+
month > 11 ||
|
|
236
|
+
day < 1 ||
|
|
237
|
+
day > 31 ||
|
|
238
|
+
hour > 23 ||
|
|
239
|
+
minute > 59 ||
|
|
240
|
+
second > 60
|
|
183
241
|
) {
|
|
184
242
|
return null;
|
|
185
243
|
}
|
|
186
|
-
|
|
187
|
-
|
|
244
|
+
|
|
245
|
+
const time =
|
|
246
|
+
Date.UTC(year, month, day, hour, minute, Math.min(second, 59)) -
|
|
247
|
+
offsetMinutes * 60_000;
|
|
248
|
+
|
|
188
249
|
return Number.isFinite(time) ? new Date(time) : null;
|
|
189
250
|
}
|
|
190
251
|
|
|
191
|
-
function rfc5322Match(match:
|
|
192
|
-
const
|
|
193
|
-
|
|
252
|
+
function rfc5322Match(match: RegExpMatchArray) {
|
|
253
|
+
const [, day, monthName, yearText, hour, minute, second, zone] = match;
|
|
254
|
+
|
|
255
|
+
if (monthName === undefined || yearText === undefined) return null;
|
|
256
|
+
const month = MONTHS.indexOf(monthName.toLocaleLowerCase());
|
|
257
|
+
let year = Number(yearText);
|
|
258
|
+
|
|
194
259
|
// RFC 5322 §4.3: two-digit years are 19xx when >= 50, 20xx otherwise.
|
|
195
|
-
if (
|
|
196
|
-
else if (
|
|
260
|
+
if (yearText.length <= 2) year += year >= 50 ? 1900 : 2000;
|
|
261
|
+
else if (yearText.length === 3) year += 1900;
|
|
262
|
+
|
|
197
263
|
return utcDate(
|
|
198
264
|
year,
|
|
199
265
|
month,
|
|
200
|
-
Number(
|
|
201
|
-
Number(
|
|
202
|
-
Number(
|
|
203
|
-
Number(
|
|
204
|
-
zoneOffsetMinutes(
|
|
266
|
+
Number(day),
|
|
267
|
+
Number(hour),
|
|
268
|
+
Number(minute),
|
|
269
|
+
Number(second ?? 0),
|
|
270
|
+
zoneOffsetMinutes(zone)
|
|
205
271
|
);
|
|
206
272
|
}
|
|
207
273
|
|
|
208
|
-
function isoMatch(match:
|
|
274
|
+
function isoMatch(match: RegExpMatchArray) {
|
|
209
275
|
return utcDate(
|
|
210
276
|
Number(match[1]),
|
|
211
277
|
Number(match[2]) - 1,
|
|
@@ -213,7 +279,7 @@ function isoMatch(match: RegExpExecArray) {
|
|
|
213
279
|
Number(match[4]),
|
|
214
280
|
Number(match[5]),
|
|
215
281
|
Number(match[6]),
|
|
216
|
-
zoneOffsetMinutes(match[7])
|
|
282
|
+
zoneOffsetMinutes(match[7])
|
|
217
283
|
);
|
|
218
284
|
}
|
|
219
285
|
|
|
@@ -227,9 +293,12 @@ export function parseMailDate(value: string | null | undefined) {
|
|
|
227
293
|
if (!value) return null;
|
|
228
294
|
const text = scanComments(value).text.trim();
|
|
229
295
|
const rfc = RFC5322_DATE_PATTERN.exec(text);
|
|
296
|
+
|
|
230
297
|
if (rfc && rfc.index <= 12) return rfc5322Match(rfc);
|
|
231
298
|
const iso = ISO_DATE_PATTERN.exec(text);
|
|
299
|
+
|
|
232
300
|
if (iso && iso.index <= 12) return isoMatch(iso);
|
|
301
|
+
|
|
233
302
|
return null;
|
|
234
303
|
}
|
|
235
304
|
|
|
@@ -238,13 +307,19 @@ export function trailingMailDate(value: string) {
|
|
|
238
307
|
const rfc = new RegExp(RFC5322_DATE_PATTERN.source, "giu");
|
|
239
308
|
const iso = new RegExp(ISO_DATE_PATTERN.source, "giu");
|
|
240
309
|
let best: { index: number; date: Date } | null = null;
|
|
241
|
-
|
|
310
|
+
|
|
311
|
+
for (const [pattern, parse] of [
|
|
312
|
+
[rfc, rfc5322Match],
|
|
313
|
+
[iso, isoMatch],
|
|
314
|
+
] as const) {
|
|
242
315
|
for (const match of value.matchAll(pattern)) {
|
|
243
|
-
const date = parse(match
|
|
316
|
+
const date = parse(match);
|
|
317
|
+
|
|
244
318
|
if (date && (!best || match.index > best.index)) {
|
|
245
319
|
best = { index: match.index, date };
|
|
246
320
|
}
|
|
247
321
|
}
|
|
248
322
|
}
|
|
323
|
+
|
|
249
324
|
return best;
|
|
250
325
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing existing mail (`POST /api/mail/import/mailboxes/:id/messages`).
|
|
3
|
+
* The `open-workspace import` CLI (@open-workspace/create) speaks this
|
|
4
|
+
* protocol; it keeps its own copy of these values so it does not depend on
|
|
5
|
+
* this package.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The largest raw message the import accepts: the size Email Routing
|
|
10
|
+
* delivers, so an imported message is never bigger than one the mailbox
|
|
11
|
+
* could have received.
|
|
12
|
+
*/
|
|
13
|
+
export const MAX_IMPORT_MESSAGE_BYTES = 25 * 1024 * 1024;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Where an imported message puts its conversation. `inbox` (with an
|
|
17
|
+
* optional custom folder), Archive, Spam, Trash; `sent` says nothing about
|
|
18
|
+
* the conversation's place (a sent message that was never in the Inbox).
|
|
19
|
+
*/
|
|
20
|
+
export const importPlacements = [
|
|
21
|
+
"inbox",
|
|
22
|
+
"archive",
|
|
23
|
+
"spam",
|
|
24
|
+
"trash",
|
|
25
|
+
"sent",
|
|
26
|
+
] as const;
|
|
27
|
+
|
|
28
|
+
export type ImportPlacement = (typeof importPlacements)[number];
|
|
29
|
+
|
|
30
|
+
/** The longest caller-supplied source id and thread key. */
|
|
31
|
+
export const MAX_IMPORT_KEY_LENGTH = 512;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Text bodies live in the mailbox's SQLite rows (HTML and attachments in
|
|
35
|
+
* R2), and a row must stay well under the Durable Object's row limit; an
|
|
36
|
+
* imported plain-text body beyond this is cut (the raw message keeps all of it).
|
|
37
|
+
*/
|
|
38
|
+
export const MAX_IMPORTED_TEXT_CHARACTERS = 200_000;
|