@open-workspace/email 0.4.2 → 0.5.1
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 +9 -4
- package/alchemy/index.ts +130 -2
- package/drizzle/mailbox/0003_after_0_4_2.sql +180 -0
- package/drizzle/mailbox/meta/0003_snapshot.json +1507 -0
- package/drizzle/mailbox/meta/_journal.json +7 -0
- package/drizzle/mailbox/migrations.js +3 -1
- package/migrations/0002_after_0_4_2.sql +35 -0
- package/migrations/meta/0002_snapshot.json +807 -0
- package/migrations/meta/_journal.json +7 -0
- package/package.json +23 -7
- package/shared/attachment-preview.ts +18 -8
- package/shared/folder-order.ts +31 -6
- package/shared/mail-headers.ts +3 -3
- package/shared/mail-html.ts +25 -0
- package/shared/mail-import.ts +0 -7
- package/shared/mail-received.ts +4 -4
- package/shared/mail-rule-actions.ts +47 -0
- package/shared/mail-sender-intel.ts +5 -1
- package/shared/mail.ts +70 -4
- package/shared/office-preview.ts +0 -4
- package/shared/pdfjs-assets.ts +1 -3
- package/shared/rich-content.ts +35 -0
- package/shared/signature.ts +15 -0
- package/src/admin/address-auto-reply.tsx +52 -0
- package/src/admin/blocked-senders.tsx +86 -66
- package/src/admin/forward-destinations.tsx +140 -0
- package/src/admin/mail-archive.tsx +134 -0
- package/src/admin/team-signature.tsx +90 -0
- package/src/client.tsx +130 -16
- package/src/mail/attachment-preview-dialog.tsx +13 -5
- package/src/mail/auto-reply-banner.tsx +61 -0
- package/src/mail/blocked-senders.tsx +124 -91
- package/src/mail/compose-window-chrome.tsx +20 -8
- package/src/mail/compose-window.tsx +391 -365
- package/src/mail/composer-asset-bookmarks.ts +47 -12
- package/src/mail/composer-attachment-list.tsx +42 -17
- package/src/mail/composer-editor-document.ts +54 -10
- package/src/mail/composer-editor.tsx +44 -44
- package/src/mail/composer-link-text.ts +42 -0
- package/src/mail/composer-recipient-fields.tsx +25 -16
- package/src/mail/composer-recipients.ts +62 -16
- package/src/mail/composer-session.ts +107 -23
- package/src/mail/composer-toolbar.tsx +15 -1
- package/src/mail/composer-upload-client.ts +29 -1
- package/src/mail/composer-upload-progress.ts +2 -2
- package/src/mail/conversation-actions-model.ts +56 -0
- package/src/mail/conversation-list.tsx +263 -128
- package/src/mail/conversation-loading-skeleton.tsx +165 -90
- package/src/mail/conversation-message.tsx +257 -102
- package/src/mail/conversation-order.ts +12 -0
- package/src/mail/conversation-reply.tsx +313 -189
- package/src/mail/conversation-selection-toolbar.tsx +188 -143
- package/src/mail/conversation-view.tsx +315 -85
- package/src/mail/delivery-indicator.tsx +33 -12
- package/src/mail/draft-attachments.ts +194 -0
- package/src/mail/email-html-body.tsx +97 -53
- package/src/mail/folder-cleanup-bar.tsx +193 -0
- package/src/mail/folder-glyph.tsx +5 -0
- package/src/mail/folder-manager.tsx +71 -38
- package/src/mail/mail-app-bar.tsx +15 -3
- package/src/mail/mail-dock.tsx +19 -3
- package/src/mail/mail-focus.ts +48 -0
- package/src/mail/mail-folders.ts +14 -2
- package/src/mail/mail-layout.css +20 -0
- package/src/mail/mail-mailbox-sheet.tsx +22 -69
- package/src/mail/mail-nav-rail.tsx +7 -4
- package/src/mail/mail-nav.tsx +66 -46
- package/src/mail/mail-prefetch.ts +30 -9
- package/src/mail/mail-primitives.tsx +6 -4
- package/src/mail/mail-queries.ts +122 -20
- package/src/mail/mail-query-cache.ts +1085 -173
- package/src/mail/mail-search-params.ts +35 -0
- package/src/mail/mail-shell.tsx +32 -9
- package/src/mail/mailbox-ai-settings.tsx +56 -42
- package/src/mail/mailbox-realtime.tsx +4 -12
- package/src/mail/mailbox-rules.tsx +898 -0
- package/src/mail/message-actions.tsx +38 -26
- package/src/mail/message-attachments.tsx +20 -12
- package/src/mail/message-body.ts +53 -0
- package/src/mail/message-format.ts +8 -2
- package/src/mail/message-header.tsx +3 -1
- package/src/mail/message-translation.tsx +34 -67
- package/src/mail/original-message-sheet.tsx +63 -50
- package/src/mail/original-message.ts +6 -6
- package/src/mail/pane-splitter.tsx +3 -3
- package/src/mail/pdf-preview.tsx +70 -62
- package/src/mail/plain-message-text.tsx +28 -0
- package/src/mail/print-message.ts +31 -36
- package/src/mail/reader-pane.tsx +134 -103
- package/src/mail/recipient-combobox.tsx +52 -29
- package/src/mail/recipient-field-row.tsx +3 -0
- package/src/mail/rich-content.ts +128 -0
- package/src/mail/sanitize-email-html.ts +53 -32
- package/src/mail/sender-actions.tsx +48 -46
- package/src/mail/sender-authentication.tsx +1 -1
- package/src/mail/sender-dialogs.tsx +67 -43
- package/src/mail/sender-security.tsx +12 -11
- package/src/mail/signature.ts +43 -0
- package/src/mail/translation-html.ts +3 -3
- package/src/mail/types.ts +12 -0
- package/src/mail/use-composer-attachment-preflight.ts +23 -60
- package/src/mail/use-composer-core.ts +286 -0
- package/src/mail/use-composer-navigation.ts +120 -0
- package/src/mail/use-composer-submission.ts +154 -19
- package/src/mail/use-conversation-actions.ts +292 -0
- package/src/mail/use-conversation-position.ts +51 -0
- package/src/mail/use-conversation-selection.ts +192 -89
- package/src/mail/use-draft-autosave.ts +394 -0
- package/src/mail/use-drafts.ts +194 -0
- package/src/mail/use-mail-controller.ts +758 -213
- package/src/mail/use-mail-data.ts +210 -100
- package/src/mail/use-mail-safety.ts +19 -38
- package/src/mail/use-mail-send.ts +18 -6
- package/src/mail/use-pdf-translation.ts +1 -1
- package/src/mail/use-reply-cc.ts +61 -0
- package/src/mail/use-visible-message-read.ts +128 -64
- package/src/pwa/push-key.ts +18 -0
- package/src/pwa/push-subscription.ts +88 -26
- package/src/settings/auto-reply-editor.tsx +454 -0
- package/src/settings/auto-reply-page.tsx +59 -0
- package/src/settings/mail-apps-page.tsx +52 -43
- package/src/settings/notifications-page.tsx +82 -67
- package/src/settings/rich-content-editor.tsx +182 -0
- package/src/settings/settings-queries.ts +57 -0
- package/src/settings/signature-editor.tsx +142 -0
- package/src/settings/signature-page.tsx +70 -0
- package/worker/admin/blocked-sender-routes.ts +10 -16
- package/worker/backups/eml.ts +185 -0
- package/worker/backups/file-parts.ts +140 -0
- package/worker/backups/mail-backup.ts +644 -0
- package/worker/backups/settings-backup.ts +866 -0
- package/worker/backups/stream-eml.ts +73 -0
- package/worker/db/schema.ts +89 -5
- package/worker/jmap/attachment-blobs.ts +113 -0
- package/worker/jmap/blobs.ts +162 -0
- package/worker/jmap/draft-content.ts +42 -0
- package/worker/jmap/drafts.ts +70 -0
- package/worker/jmap/email-body.ts +152 -0
- package/worker/jmap/email-methods.ts +1223 -0
- package/worker/jmap/eventsource.ts +9 -8
- package/worker/jmap/import-methods.ts +205 -0
- package/worker/jmap/mailbox-methods.ts +187 -0
- package/worker/jmap/method-core.ts +379 -0
- package/worker/jmap/methods.ts +119 -1816
- package/worker/jmap/push-subscription-methods.ts +373 -0
- package/worker/jmap/push-types.ts +46 -2
- package/worker/jmap/push.ts +24 -31
- package/worker/jmap/routes.ts +144 -153
- package/worker/jmap/submission-methods.ts +395 -0
- package/worker/mail/access.ts +35 -12
- package/worker/mail/ai-classification.ts +2 -2
- package/worker/mail/archive-object.ts +96 -0
- package/worker/mail/archive-routes.ts +35 -0
- package/worker/mail/archive.ts +700 -0
- package/worker/mail/auth-headers.ts +57 -43
- package/worker/mail/auto-reply-routes.ts +260 -0
- package/worker/mail/auto-reply.ts +243 -0
- package/worker/mail/bimi.ts +53 -36
- package/worker/mail/bootstrap.ts +11 -3
- package/worker/mail/capabilities.ts +69 -0
- package/worker/mail/classification-signals.ts +1 -1
- package/worker/mail/confusables-data.ts +9 -0
- package/worker/mail/conversation-cursor.ts +6 -15
- package/worker/mail/delivery-events.ts +9 -7
- package/worker/mail/delivery-facts.ts +1 -1
- package/worker/mail/demo-routes.ts +3 -3
- package/worker/mail/doh.ts +3 -3
- package/worker/mail/download-links.ts +153 -0
- package/worker/mail/downloads.ts +2 -6
- package/worker/mail/draft-routes.ts +473 -0
- package/worker/mail/draft-sender.ts +54 -0
- package/worker/mail/edge-cache.ts +41 -55
- package/worker/mail/email-routing.ts +123 -0
- package/worker/mail/forward-destinations.ts +114 -0
- package/worker/mail/forward-downloads.ts +115 -0
- package/worker/mail/forward-html.ts +85 -0
- package/worker/mail/header-metadata.ts +2 -5
- package/worker/mail/impersonation.ts +102 -101
- package/worker/mail/import-routes.ts +29 -82
- package/worker/mail/import.ts +130 -0
- package/worker/mail/inbound-delivery.ts +6 -5
- package/worker/mail/inbound-threading.ts +15 -2
- package/worker/mail/inbound.ts +58 -28
- package/worker/mail/ip-intel.ts +8 -17
- package/worker/mail/isolate-cache.ts +1 -1
- package/worker/mail/mailbox-deletion.ts +33 -12
- package/worker/mail/mailbox-directory.ts +39 -65
- package/worker/mail/mailing-list.ts +29 -58
- package/worker/mail/message-security.ts +19 -8
- package/worker/mail/message-view.ts +12 -10
- package/worker/mail/office-conversion.ts +3 -5
- package/worker/mail/office-preview-container.ts +3 -8
- package/worker/mail/outbound-content.ts +139 -65
- package/worker/mail/outbound-delivery.ts +20 -17
- package/worker/mail/outbound-service.ts +34 -9
- package/worker/mail/outbound.ts +146 -75
- package/worker/mail/outgoing-context.ts +60 -30
- package/worker/mail/participants.ts +10 -15
- package/worker/mail/plain-body.ts +39 -0
- package/worker/mail/push-endpoint.ts +17 -0
- package/worker/mail/push-notifications.ts +53 -62
- package/worker/mail/quote-parser.ts +36 -24
- package/worker/mail/receive.ts +146 -39
- package/worker/mail/recipients.ts +1 -1
- package/worker/mail/remote-proxy.ts +3 -3
- package/worker/mail/repeat.ts +1 -1
- package/worker/mail/reply-plan.ts +124 -26
- package/worker/mail/rfc.ts +15 -7
- package/worker/mail/rich-content.ts +207 -0
- package/worker/mail/routes.ts +698 -304
- package/worker/mail/routing.ts +274 -0
- package/worker/mail/rules.ts +230 -0
- package/worker/mail/safety-routes.ts +88 -86
- package/worker/mail/schemas.ts +222 -31
- package/worker/mail/search.ts +25 -2
- package/worker/mail/security-settings.ts +4 -10
- package/worker/mail/send-message.ts +238 -0
- package/worker/mail/sender-intel.ts +12 -20
- package/worker/mail/signature-routes.ts +206 -0
- package/worker/mail/signatures.ts +195 -0
- package/worker/mail/spamhaus.ts +28 -16
- package/worker/mail/storage-keys.ts +35 -0
- package/worker/mail/unsubscribe.ts +19 -41
- package/worker/mail/uploads.ts +192 -80
- package/worker/mail/webhook-source.ts +3 -2
- package/worker/mail/workspace-safety.ts +194 -60
- package/worker/mailbox/demo.ts +13 -15
- package/worker/mailbox/email-index.ts +65 -35
- package/worker/mailbox/folder-model.ts +0 -20
- package/worker/mailbox/import-placement.ts +21 -36
- package/worker/mailbox/inbound-decision.ts +114 -0
- package/worker/mailbox/inbox-counts.ts +45 -25
- package/worker/mailbox/index.ts +756 -3201
- package/worker/mailbox/jmap-change-log.ts +195 -0
- package/worker/mailbox/jmap-email-rows.ts +186 -0
- package/worker/mailbox/jmap-mailboxes.ts +116 -0
- package/worker/mailbox/jmap-placement.ts +139 -0
- package/worker/mailbox/jmap-query-filter.ts +231 -0
- package/worker/mailbox/jmap-store.ts +167 -299
- package/worker/mailbox/mailbox-auto-reply.ts +316 -0
- package/worker/mailbox/mailbox-backup.ts +538 -0
- package/worker/mailbox/mailbox-drafts.ts +417 -0
- package/worker/mailbox/mailbox-folders.ts +242 -0
- package/worker/mailbox/mailbox-import.ts +455 -0
- package/worker/mailbox/mailbox-inbound.ts +934 -0
- package/worker/mailbox/mailbox-jmap.ts +294 -0
- package/worker/mailbox/mailbox-outbound.ts +413 -0
- package/worker/mailbox/mailbox-rules.ts +336 -0
- package/worker/mailbox/mailbox-senders.ts +294 -0
- package/worker/mailbox/mailbox-storage.ts +438 -0
- package/worker/mailbox/mailbox-views.ts +891 -0
- package/worker/mailbox/model.ts +1 -0
- package/worker/mailbox/office-preview-jobs.ts +59 -47
- package/worker/mailbox/placement.ts +933 -0
- package/worker/mailbox/restore-thread.ts +52 -0
- package/worker/mailbox/retention.ts +36 -0
- package/worker/mailbox/schema.ts +200 -1
- package/worker/mailbox/senders.ts +216 -178
- package/worker/mailbox/spam-undo-filing.ts +138 -0
- package/worker/mailbox/spam-undo.ts +253 -0
- package/worker/mailbox/support.ts +416 -0
- package/worker/mcp/mail-action-tools.ts +161 -1
- package/worker/mcp/mail-compose-tools.ts +7 -1
- package/worker/mcp/mail-safety-tools.ts +4 -4
- package/worker/module.ts +23 -4
- package/src/mail/use-compose-submission.ts +0 -208
- package/src/mail/use-reply-submission.ts +0 -114
- package/worker/mail/capped-body.ts +0 -36
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { getDomainWithoutSuffix } from "tldts";
|
|
2
2
|
|
|
3
|
+
import { confusablesData } from "./confusables-data";
|
|
3
4
|
import { organizationalDomain } from "./org-domain";
|
|
4
5
|
|
|
5
6
|
// --- Punycode (RFC 3492 decoding only) ------------------------------------
|
|
@@ -84,74 +85,54 @@ export function domainToUnicode(domain: string) {
|
|
|
84
85
|
.join(".");
|
|
85
86
|
}
|
|
86
87
|
|
|
87
|
-
// ---
|
|
88
|
-
|
|
89
|
-
const CONFUSABLES = new Map(
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
к: "k",
|
|
97
|
-
м: "m",
|
|
98
|
-
н: "h",
|
|
99
|
-
о: "o",
|
|
100
|
-
р: "p",
|
|
101
|
-
с: "c",
|
|
102
|
-
т: "t",
|
|
103
|
-
у: "y",
|
|
104
|
-
х: "x",
|
|
105
|
-
ѕ: "s",
|
|
106
|
-
і: "i",
|
|
107
|
-
ї: "i",
|
|
108
|
-
ј: "j",
|
|
109
|
-
ԁ: "d",
|
|
110
|
-
һ: "h",
|
|
111
|
-
ӏ: "l",
|
|
112
|
-
ԛ: "q",
|
|
113
|
-
ԝ: "w",
|
|
114
|
-
ɡ: "g",
|
|
115
|
-
// Greek
|
|
116
|
-
α: "a",
|
|
117
|
-
β: "b",
|
|
118
|
-
ε: "e",
|
|
119
|
-
η: "n",
|
|
120
|
-
ι: "i",
|
|
121
|
-
κ: "k",
|
|
122
|
-
ν: "v",
|
|
123
|
-
ο: "o",
|
|
124
|
-
ρ: "p",
|
|
125
|
-
τ: "t",
|
|
126
|
-
υ: "u",
|
|
127
|
-
χ: "x",
|
|
128
|
-
ω: "w",
|
|
129
|
-
// Look-alike digits and symbols
|
|
130
|
-
"0": "o",
|
|
131
|
-
"1": "l",
|
|
132
|
-
"3": "e",
|
|
133
|
-
"5": "s",
|
|
134
|
-
"|": "l",
|
|
135
|
-
ı: "i",
|
|
136
|
-
ł: "l",
|
|
137
|
-
ø: "o",
|
|
138
|
-
đ: "d",
|
|
139
|
-
})
|
|
140
|
-
);
|
|
141
|
-
|
|
142
|
-
/** Lowercased, accent-free, confusables folded, with multi-letter look-alikes merged. */
|
|
143
|
-
export function confusableSkeleton(value: string) {
|
|
144
|
-
const folded = [...value.normalize("NFKD").toLocaleLowerCase()]
|
|
145
|
-
.flatMap((char) =>
|
|
146
|
-
/\p{M}/u.test(char) ? [] : [CONFUSABLES.get(char) ?? char]
|
|
147
|
-
)
|
|
148
|
-
.join("");
|
|
88
|
+
// --- UTS #39 confusable skeleton ---------------------------------------------
|
|
89
|
+
|
|
90
|
+
const CONFUSABLES = new Map<string, string>();
|
|
91
|
+
|
|
92
|
+
{
|
|
93
|
+
let point = 0;
|
|
94
|
+
|
|
95
|
+
for (const entry of confusablesData.split(",")) {
|
|
96
|
+
const [delta, to] = entry.split(":");
|
|
149
97
|
|
|
150
|
-
|
|
151
|
-
.
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
98
|
+
point += Number.parseInt(delta, 36);
|
|
99
|
+
CONFUSABLES.set(
|
|
100
|
+
String.fromCodePoint(point),
|
|
101
|
+
String.fromCodePoint(
|
|
102
|
+
...to.split(".").map((code) => Number.parseInt(code, 36))
|
|
103
|
+
)
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* UTS #39 `skeleton(X) = NFD(map each character of NFD(X) via
|
|
110
|
+
* confusables.txt)`: two strings that look alike have the same skeleton.
|
|
111
|
+
* Case is kept: a capital Greek Nu is drawn like an N, its lowercase like a v.
|
|
112
|
+
*/
|
|
113
|
+
export function skeleton(value: string) {
|
|
114
|
+
return Array.from(
|
|
115
|
+
value.normalize("NFD"),
|
|
116
|
+
(char) => CONFUSABLES.get(char) ?? char
|
|
117
|
+
)
|
|
118
|
+
.join("")
|
|
119
|
+
.normalize("NFD");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The skeletons of a text written in lowercase and in capitals. The
|
|
124
|
+
* confusables data is case-sensitive, so a look-alike may show in one case
|
|
125
|
+
* only: a capital Greek Nu in "Νikοs" is an N, its lowercase a v; "1BM"
|
|
126
|
+
* reads as "IBM", "1bm" not as "ibm". Folding before the skeleton would
|
|
127
|
+
* miss the first; folding after it would merge "Ian" (I→l) with "Lan".
|
|
128
|
+
* Mail shows a name as written and a domain in either case, so two texts
|
|
129
|
+
* that share either skeleton look alike.
|
|
130
|
+
*/
|
|
131
|
+
function caseSkeletons(value: string) {
|
|
132
|
+
return [
|
|
133
|
+
skeleton(value.toLocaleLowerCase()),
|
|
134
|
+
skeleton(value.toLocaleUpperCase()),
|
|
135
|
+
] as const;
|
|
155
136
|
}
|
|
156
137
|
|
|
157
138
|
export function levenshtein(left: string, right: string) {
|
|
@@ -186,10 +167,14 @@ function maxEdits(length: number) {
|
|
|
186
167
|
return length < 7 ? 1 : 2;
|
|
187
168
|
}
|
|
188
169
|
|
|
170
|
+
const notAlphanumeric = /[^\p{L}\p{M}\p{N}]/gu;
|
|
171
|
+
|
|
189
172
|
/**
|
|
190
173
|
* The workspace domain that `senderDomain` imitates, if any. Compares the
|
|
191
|
-
* registrable labels
|
|
192
|
-
* whole registrable name without separators (`demo-examp1e.com` vs
|
|
174
|
+
* registrable labels decoded from punycode by their UTS #39 skeletons, and
|
|
175
|
+
* the whole registrable name without separators (`demo-examp1e.com` vs
|
|
176
|
+
* `demo.example`); a few edits away (`nortwind`, `northvvind`) is close
|
|
177
|
+
* enough for a longer name.
|
|
193
178
|
*/
|
|
194
179
|
export function lookalikeOf(
|
|
195
180
|
senderDomain: string | null,
|
|
@@ -197,40 +182,43 @@ export function lookalikeOf(
|
|
|
197
182
|
) {
|
|
198
183
|
if (!senderDomain) return null;
|
|
199
184
|
const unicode = domainToUnicode(senderDomain.toLocaleLowerCase());
|
|
200
|
-
const senderOrg = organizationalDomain(senderDomain) ?? senderDomain;
|
|
201
185
|
|
|
202
|
-
const
|
|
203
|
-
|
|
186
|
+
const senderOrg = domainToUnicode(
|
|
187
|
+
(organizationalDomain(senderDomain) ?? senderDomain).toLocaleLowerCase()
|
|
204
188
|
);
|
|
205
189
|
|
|
206
|
-
const
|
|
190
|
+
const senderName = registrableLabel(senderOrg);
|
|
191
|
+
const senderLabel = caseSkeletons(senderName);
|
|
192
|
+
const senderFlat = skeleton(senderName.replaceAll(notAlphanumeric, ""));
|
|
207
193
|
|
|
208
194
|
for (const workspace of workspaceDomains) {
|
|
209
195
|
const own = workspace.toLocaleLowerCase();
|
|
210
196
|
const ownOrg = organizationalDomain(own) ?? own;
|
|
211
197
|
|
|
212
|
-
if (
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
const ownFlat = confusableSkeleton(ownOrg).replaceAll(
|
|
216
|
-
/[^\p{L}\p{N}]/gu,
|
|
217
|
-
""
|
|
218
|
-
);
|
|
198
|
+
if (domainToUnicode(ownOrg) === senderOrg) continue;
|
|
199
|
+
const ownName = registrableLabel(ownOrg);
|
|
219
200
|
|
|
220
|
-
if (
|
|
201
|
+
if (ownName.length < 3) continue;
|
|
202
|
+
const ownLabel = caseSkeletons(ownName);
|
|
203
|
+
const ownPlain = ownOrg.replaceAll(notAlphanumeric, "");
|
|
204
|
+
const ownFlat = skeleton(ownPlain);
|
|
221
205
|
|
|
222
206
|
// Same name under another suffix, or a homoglyph/punycode spelling of it.
|
|
223
|
-
if (
|
|
207
|
+
if (
|
|
208
|
+
senderLabel[0] === ownLabel[0] ||
|
|
209
|
+
senderLabel[1] === ownLabel[1] ||
|
|
210
|
+
senderFlat === ownFlat
|
|
211
|
+
) {
|
|
224
212
|
return { domain: unicode, resembles: own };
|
|
225
213
|
}
|
|
226
214
|
|
|
227
|
-
if (levenshtein(senderLabel, ownLabel) <= maxEdits(
|
|
215
|
+
if (levenshtein(senderLabel[0], ownLabel[0]) <= maxEdits(ownName.length)) {
|
|
228
216
|
return { domain: unicode, resembles: own };
|
|
229
217
|
}
|
|
230
218
|
|
|
231
219
|
if (
|
|
232
|
-
|
|
233
|
-
levenshtein(senderFlat, ownFlat) <= maxEdits(
|
|
220
|
+
ownPlain.length >= 6 &&
|
|
221
|
+
levenshtein(senderFlat, ownFlat) <= maxEdits(ownPlain.length)
|
|
234
222
|
) {
|
|
235
223
|
return { domain: unicode, resembles: own };
|
|
236
224
|
}
|
|
@@ -241,27 +229,40 @@ export function lookalikeOf(
|
|
|
241
229
|
|
|
242
230
|
// --- Display names ----------------------------------------------------------
|
|
243
231
|
|
|
244
|
-
|
|
245
|
-
|
|
232
|
+
/**
|
|
233
|
+
* A display name's two skeletons (see `caseSkeletons`) as compared with the
|
|
234
|
+
* workspace's names: invisible characters (format controls such as a
|
|
235
|
+
* zero-width space, soft hyphen or word joiner, and variation selectors)
|
|
236
|
+
* dropped, NFKC-normalized (full-width letters), punctuation and spacing
|
|
237
|
+
* ignored. Accents and vowel signs stay, as the skeleton keeps them:
|
|
238
|
+
* "Nguyen" and "Nguyễn" are not drawn alike.
|
|
239
|
+
*/
|
|
240
|
+
function personNameKeys(value: string | null | undefined) {
|
|
241
|
+
const text = (value ?? "")
|
|
242
|
+
.replaceAll(/[\p{Cf}\p{Default_Ignorable_Code_Point}]/gu, "")
|
|
243
|
+
.normalize("NFKC");
|
|
246
244
|
|
|
247
|
-
return
|
|
248
|
-
.
|
|
249
|
-
|
|
250
|
-
.replaceAll(/[^\p{L}\p{N}\s]/gu, " ")
|
|
251
|
-
.replaceAll(/\s+/gu, " ")
|
|
252
|
-
.trim();
|
|
245
|
+
return caseSkeletons(text).map((form) =>
|
|
246
|
+
form.replaceAll(/[^\p{L}\p{M}\p{N}]+/gu, " ").trim()
|
|
247
|
+
);
|
|
253
248
|
}
|
|
254
249
|
|
|
255
|
-
/**
|
|
250
|
+
/**
|
|
251
|
+
* Internal names worth protecting: at least two words, so "Support" alone
|
|
252
|
+
* never matches. Keyed by both skeletons of the name (`personNameKeys`), so
|
|
253
|
+
* a copy spelled with look-alikes from any script matches as well.
|
|
254
|
+
*/
|
|
256
255
|
export function protectedNames(names: (string | null | undefined)[]) {
|
|
257
256
|
const result = new Map<string, string>();
|
|
258
257
|
|
|
259
258
|
for (const name of names) {
|
|
260
|
-
|
|
261
|
-
|
|
259
|
+
const [lower, upper] = personNameKeys(name);
|
|
260
|
+
|
|
261
|
+
if (name && lower.split(" ").length >= 2 && lower.length >= 5) {
|
|
262
|
+
const shown = name.trim().replaceAll(/\s+/gu, " ");
|
|
262
263
|
|
|
263
|
-
|
|
264
|
-
result.set(
|
|
264
|
+
result.set(`lower ${lower}`, shown);
|
|
265
|
+
result.set(`upper ${upper}`, shown);
|
|
265
266
|
}
|
|
266
267
|
}
|
|
267
268
|
|
|
@@ -273,11 +274,11 @@ export function impersonatedName(
|
|
|
273
274
|
displayName: string | null | undefined,
|
|
274
275
|
names: ReadonlyMap<string, string>
|
|
275
276
|
) {
|
|
276
|
-
const
|
|
277
|
+
const [lower, upper] = personNameKeys(displayName);
|
|
277
278
|
|
|
278
|
-
if (!
|
|
279
|
+
if (!lower) return null;
|
|
279
280
|
|
|
280
|
-
return names.get(
|
|
281
|
+
return names.get(`lower ${lower}`) ?? names.get(`upper ${upper}`) ?? null;
|
|
281
282
|
}
|
|
282
283
|
|
|
283
284
|
/** `"ceo@ourco.com" <x@elsewhere.example>`: an address in the name that is not the sender. */
|
|
@@ -6,15 +6,15 @@ import {
|
|
|
6
6
|
mailboxAddressSql,
|
|
7
7
|
mailboxKind,
|
|
8
8
|
mailboxKindOrderSql,
|
|
9
|
-
mailboxAccess,
|
|
10
|
-
mailboxes,
|
|
11
9
|
apiError,
|
|
12
10
|
zValidator,
|
|
13
11
|
} from "@open-workspace/core/module";
|
|
14
12
|
import type { Database, AppEnv } from "@open-workspace/core/module";
|
|
15
|
-
import {
|
|
13
|
+
import { mailboxes } from "@open-workspace/core/schema";
|
|
14
|
+
import { and, eq } from "drizzle-orm";
|
|
16
15
|
import { Hono } from "hono";
|
|
17
16
|
import type { Context } from "hono";
|
|
17
|
+
import { bodyLimit } from "hono/body-limit";
|
|
18
18
|
import { z } from "zod";
|
|
19
19
|
|
|
20
20
|
import {
|
|
@@ -28,8 +28,7 @@ import {
|
|
|
28
28
|
MAIL_ORGANIZE_SCOPE,
|
|
29
29
|
} from "../../shared/permissions";
|
|
30
30
|
import { mailboxStub } from "../mailbox";
|
|
31
|
-
import {
|
|
32
|
-
import { rawMessageId } from "./inbound";
|
|
31
|
+
import { importRawMessage } from "./import";
|
|
33
32
|
|
|
34
33
|
/**
|
|
35
34
|
* Importing existing mail (the `open-workspace import` CLI).
|
|
@@ -78,17 +77,6 @@ const importQuerySchema = z.object({
|
|
|
78
77
|
direction: z.enum(["incoming", "outgoing"]).optional(),
|
|
79
78
|
});
|
|
80
79
|
|
|
81
|
-
async function sha256Hex(value: string) {
|
|
82
|
-
const digest = await crypto.subtle.digest(
|
|
83
|
-
"SHA-256",
|
|
84
|
-
new TextEncoder().encode(value)
|
|
85
|
-
);
|
|
86
|
-
|
|
87
|
-
return [...new Uint8Array(digest)]
|
|
88
|
-
.map((byte) => byte.toString(16).padStart(2, "0"))
|
|
89
|
-
.join("");
|
|
90
|
-
}
|
|
91
|
-
|
|
92
80
|
/** A mailbox's import target: where it is and who can open it. */
|
|
93
81
|
async function importMailbox(db: Database, mailboxId: string) {
|
|
94
82
|
const [mailbox] = await db
|
|
@@ -98,7 +86,13 @@ async function importMailbox(db: Database, mailboxId: string) {
|
|
|
98
86
|
ownerUserId: mailboxes.ownerUserId,
|
|
99
87
|
})
|
|
100
88
|
.from(mailboxes)
|
|
101
|
-
.where(
|
|
89
|
+
.where(
|
|
90
|
+
and(
|
|
91
|
+
eq(mailboxes.id, mailboxId),
|
|
92
|
+
// Only an inbox keeps mail: a list's would be read by no one.
|
|
93
|
+
eq(mailboxes.delivery, "inbox")
|
|
94
|
+
)
|
|
95
|
+
)
|
|
102
96
|
.limit(1);
|
|
103
97
|
|
|
104
98
|
return mailbox ?? null;
|
|
@@ -140,22 +134,6 @@ async function authorizedMailbox(c: Context<AppEnv>, mailboxId: string) {
|
|
|
140
134
|
);
|
|
141
135
|
}
|
|
142
136
|
|
|
143
|
-
class TooLargeError extends Error {}
|
|
144
|
-
|
|
145
|
-
/** The request body, refusing one larger than `limit` without reading it all. */
|
|
146
|
-
async function boundedBody(request: Request, limit: number) {
|
|
147
|
-
const declared = Number(request.headers.get("content-length") ?? Number.NaN);
|
|
148
|
-
|
|
149
|
-
if (declared > limit) throw new TooLargeError();
|
|
150
|
-
|
|
151
|
-
if (!request.body) return new Uint8Array();
|
|
152
|
-
const body = await readCappedBody(request.body, limit);
|
|
153
|
-
|
|
154
|
-
if (!body) throw new TooLargeError();
|
|
155
|
-
|
|
156
|
-
return body;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
137
|
export const mailImportRoutes = new Hono<AppEnv>()
|
|
160
138
|
.use("*", requireAuth)
|
|
161
139
|
.use("*", oauthScopeAnyOf(MAIL_ORGANIZE_SCOPE, IMPORT_MAIL_SCOPE))
|
|
@@ -199,25 +177,22 @@ export const mailImportRoutes = new Hono<AppEnv>()
|
|
|
199
177
|
.post(
|
|
200
178
|
"/mailboxes/:id/messages",
|
|
201
179
|
zValidator("query", importQuerySchema),
|
|
180
|
+
bodyLimit({
|
|
181
|
+
maxSize: MAX_IMPORT_MESSAGE_BYTES,
|
|
182
|
+
onError: (c) =>
|
|
183
|
+
apiError(
|
|
184
|
+
c,
|
|
185
|
+
413,
|
|
186
|
+
"TOO_LARGE",
|
|
187
|
+
`Messages are limited to ${MAX_IMPORT_MESSAGE_BYTES / 1024 / 1024} MiB, the size Email Routing delivers`
|
|
188
|
+
),
|
|
189
|
+
}),
|
|
202
190
|
async (c) => {
|
|
203
191
|
const target = await authorizedMailbox(c, c.req.param("id"));
|
|
204
192
|
|
|
205
193
|
if (target instanceof Response) return target;
|
|
206
194
|
const input = c.req.valid("query");
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
try {
|
|
210
|
-
raw = await boundedBody(c.req.raw, MAX_IMPORT_MESSAGE_BYTES);
|
|
211
|
-
} catch (error) {
|
|
212
|
-
if (!(error instanceof TooLargeError)) throw error;
|
|
213
|
-
|
|
214
|
-
return apiError(
|
|
215
|
-
c,
|
|
216
|
-
413,
|
|
217
|
-
"TOO_LARGE",
|
|
218
|
-
`Messages are limited to ${MAX_IMPORT_MESSAGE_BYTES / 1024 / 1024} MiB, the size Email Routing delivers`
|
|
219
|
-
);
|
|
220
|
-
}
|
|
195
|
+
const raw = new Uint8Array(await c.req.arrayBuffer());
|
|
221
196
|
|
|
222
197
|
if (!raw.byteLength) {
|
|
223
198
|
return apiError(
|
|
@@ -228,44 +203,16 @@ export const mailImportRoutes = new Hono<AppEnv>()
|
|
|
228
203
|
);
|
|
229
204
|
}
|
|
230
205
|
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
return c.json({
|
|
237
|
-
ok: true as const,
|
|
238
|
-
duplicate: true as const,
|
|
239
|
-
...known,
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
const rawObjectKey = `mailboxes/${target.id}/raw/${importId}.eml`;
|
|
244
|
-
await c.env.MAIL_STORAGE.put(rawObjectKey, raw, {
|
|
245
|
-
httpMetadata: { contentType: "message/rfc822" },
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
const seenBy =
|
|
249
|
-
input.seen === "1"
|
|
250
|
-
? (
|
|
251
|
-
await createDb(c.env.DB)
|
|
252
|
-
.select({ userId: mailboxAccess.userId })
|
|
253
|
-
.from(mailboxAccess)
|
|
254
|
-
.where(eq(mailboxAccess.mailboxId, target.id))
|
|
255
|
-
).map((row) => row.userId)
|
|
256
|
-
: [];
|
|
257
|
-
|
|
258
|
-
const result = await mailbox.importMessage({
|
|
259
|
-
importId,
|
|
260
|
-
rawObjectKey,
|
|
261
|
-
mailboxAddress: target.address,
|
|
206
|
+
const result = await importRawMessage({
|
|
207
|
+
env: c.env,
|
|
208
|
+
mailbox: target,
|
|
209
|
+
raw,
|
|
210
|
+
importKey: input.sourceId,
|
|
262
211
|
receivedAt: input.receivedAt,
|
|
263
212
|
placement: input.placement,
|
|
264
213
|
folderName: input.folder ?? null,
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
? `conv_imp_${(await sha256Hex(input.thread)).slice(0, 32)}`
|
|
268
|
-
: null,
|
|
214
|
+
seen: input.seen === "1",
|
|
215
|
+
thread: input.thread ?? null,
|
|
269
216
|
direction: input.direction ?? null,
|
|
270
217
|
});
|
|
271
218
|
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/** Storing existing messages in a mailbox: the import routes, JMAP Email/import and backup restores all go through here. */
|
|
2
|
+
import { createDb } from "@open-workspace/core/module";
|
|
3
|
+
import { mailboxAccess } from "@open-workspace/core/schema";
|
|
4
|
+
import { eq } from "drizzle-orm";
|
|
5
|
+
|
|
6
|
+
import type { ImportPlacement } from "../../shared/mail-import";
|
|
7
|
+
import { mailboxStub } from "../mailbox";
|
|
8
|
+
import type {
|
|
9
|
+
ImportMessageInput,
|
|
10
|
+
ImportMessageResult,
|
|
11
|
+
ImportOriginal,
|
|
12
|
+
} from "../mailbox/support";
|
|
13
|
+
import { inboundMessageId, rawMessageId } from "./inbound";
|
|
14
|
+
import { mailboxAliases } from "./outgoing-context";
|
|
15
|
+
import { rawMessageKey } from "./storage-keys";
|
|
16
|
+
|
|
17
|
+
async function importIdOf(importKey: string | Uint8Array<ArrayBuffer>) {
|
|
18
|
+
return `imp_${(await sha256Hex(importKey)).slice(0, 32)}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The id a message imported under `importKey` has in its mailbox. */
|
|
22
|
+
export async function importedEmailId(importKey: string) {
|
|
23
|
+
return inboundMessageId(await importIdOf(importKey));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function importedConversationId(thread: string) {
|
|
27
|
+
return `conv_imp_${(await sha256Hex(thread)).slice(0, 32)}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function sha256Hex(value: string | Uint8Array<ArrayBuffer>) {
|
|
31
|
+
const digest = await crypto.subtle.digest(
|
|
32
|
+
"SHA-256",
|
|
33
|
+
value instanceof Uint8Array ? value : new TextEncoder().encode(value)
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
return [...new Uint8Array(digest)]
|
|
37
|
+
.map((byte) => byte.toString(16).padStart(2, "0"))
|
|
38
|
+
.join("");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Mail sent to oneself can share its Message-ID with the received copy. */
|
|
42
|
+
export function importCopyKey(
|
|
43
|
+
raw: Uint8Array,
|
|
44
|
+
direction: ImportMessageInput["direction"]
|
|
45
|
+
) {
|
|
46
|
+
if (!direction) return null;
|
|
47
|
+
const messageId = rawMessageId(raw);
|
|
48
|
+
|
|
49
|
+
return messageId ? { messageId, direction } : null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Stores one raw message in a mailbox as an import: kept once per
|
|
54
|
+
* `importKey` (and per Message-ID), read for everyone who can open the
|
|
55
|
+
* mailbox when `seen`, threaded by `thread` when given.
|
|
56
|
+
*/
|
|
57
|
+
export async function importRawMessage(
|
|
58
|
+
input: {
|
|
59
|
+
env: Env;
|
|
60
|
+
mailbox: { id: string; address: string };
|
|
61
|
+
raw: Uint8Array;
|
|
62
|
+
/** The message's id at its source (or its bytes); importing it again does nothing. */
|
|
63
|
+
importKey: string | Uint8Array<ArrayBuffer>;
|
|
64
|
+
receivedAt: number | null;
|
|
65
|
+
placement: ImportPlacement;
|
|
66
|
+
folderName: string | null;
|
|
67
|
+
thread: string | null;
|
|
68
|
+
direction: "incoming" | "outgoing" | null;
|
|
69
|
+
/** A backup's ids for the message in this mailbox: see `ImportMessageInput`. */
|
|
70
|
+
original?: ImportOriginal;
|
|
71
|
+
linkedFiles?: ImportMessageInput["linkedFiles"];
|
|
72
|
+
} & ({ seen: boolean } | { seenBy: string[] })
|
|
73
|
+
) {
|
|
74
|
+
const { env, mailbox: target, raw } = input;
|
|
75
|
+
const mailbox = mailboxStub(env, target.id);
|
|
76
|
+
const importId = await importIdOf(input.importKey);
|
|
77
|
+
const original = input.original ?? null;
|
|
78
|
+
|
|
79
|
+
const conversationId = input.thread
|
|
80
|
+
? await importedConversationId(input.thread)
|
|
81
|
+
: null;
|
|
82
|
+
|
|
83
|
+
const direction =
|
|
84
|
+
input.direction ?? (input.placement === "sent" ? "outgoing" : null);
|
|
85
|
+
|
|
86
|
+
const known = await mailbox.findImported(
|
|
87
|
+
inboundMessageId(importId),
|
|
88
|
+
original,
|
|
89
|
+
conversationId,
|
|
90
|
+
importCopyKey(raw, direction)
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
if (known) {
|
|
94
|
+
const duplicate: ImportMessageResult = { status: "duplicate", ...known };
|
|
95
|
+
|
|
96
|
+
return duplicate;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const rawObjectKey = rawMessageKey(target.id, importId);
|
|
100
|
+
await env.MAIL_STORAGE.put(rawObjectKey, raw, {
|
|
101
|
+
httpMetadata: { contentType: "message/rfc822" },
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const [readers, aliases] = await Promise.all([
|
|
105
|
+
"seen" in input && input.seen
|
|
106
|
+
? createDb(env.DB)
|
|
107
|
+
.select({ userId: mailboxAccess.userId })
|
|
108
|
+
.from(mailboxAccess)
|
|
109
|
+
.where(eq(mailboxAccess.mailboxId, target.id))
|
|
110
|
+
: [],
|
|
111
|
+
direction === null ? mailboxAliases(env.DB, target.id) : [],
|
|
112
|
+
]);
|
|
113
|
+
|
|
114
|
+
const result: ImportMessageResult = await mailbox.importMessage({
|
|
115
|
+
importId,
|
|
116
|
+
rawObjectKey,
|
|
117
|
+
mailboxAddress: target.address,
|
|
118
|
+
aliases: aliases.map((alias) => alias.address),
|
|
119
|
+
receivedAt: input.receivedAt,
|
|
120
|
+
placement: input.placement,
|
|
121
|
+
folderName: input.folderName,
|
|
122
|
+
seenBy: "seenBy" in input ? input.seenBy : readers.map((row) => row.userId),
|
|
123
|
+
conversationId,
|
|
124
|
+
original,
|
|
125
|
+
linkedFiles: input.linkedFiles,
|
|
126
|
+
direction,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
return result;
|
|
130
|
+
}
|
|
@@ -4,11 +4,15 @@ import {
|
|
|
4
4
|
normalizeMailboxAddress,
|
|
5
5
|
} from "@open-workspace/core/module";
|
|
6
6
|
|
|
7
|
+
export async function inboundMessageDigest(raw: ArrayBuffer) {
|
|
8
|
+
return new Uint8Array(await crypto.subtle.digest("SHA-256", raw));
|
|
9
|
+
}
|
|
10
|
+
|
|
7
11
|
export async function inboundDeliveryId(input: {
|
|
8
12
|
mailboxId: string;
|
|
9
13
|
envelopeFrom: string;
|
|
10
14
|
envelopeTo: string;
|
|
11
|
-
|
|
15
|
+
rawDigest: Uint8Array;
|
|
12
16
|
}) {
|
|
13
17
|
const prefix = new TextEncoder().encode(
|
|
14
18
|
[
|
|
@@ -19,10 +23,7 @@ export async function inboundDeliveryId(input: {
|
|
|
19
23
|
].join("\0")
|
|
20
24
|
);
|
|
21
25
|
|
|
22
|
-
const rawDigest =
|
|
23
|
-
await crypto.subtle.digest("SHA-256", input.raw)
|
|
24
|
-
);
|
|
25
|
-
|
|
26
|
+
const { rawDigest } = input;
|
|
26
27
|
const digestInput = new Uint8Array(prefix.byteLength + rawDigest.byteLength);
|
|
27
28
|
digestInput.set(prefix);
|
|
28
29
|
digestInput.set(rawDigest, prefix.byteLength);
|
|
@@ -6,7 +6,7 @@ import { normalizedParticipantSet } from "./participants";
|
|
|
6
6
|
|
|
7
7
|
type ThreadParent = Pick<
|
|
8
8
|
Email,
|
|
9
|
-
"listId" | "fromJson" | "toJson" | "ccJson" | "bccJson"
|
|
9
|
+
"listId" | "listPostAddress" | "fromJson" | "toJson" | "ccJson" | "bccJson"
|
|
10
10
|
>;
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -25,7 +25,20 @@ export function shouldDetachInboundReply(input: {
|
|
|
25
25
|
const normalizedListId = input.listId?.trim().toLocaleLowerCase() ?? null;
|
|
26
26
|
const parentListId = parent.listId?.trim().toLocaleLowerCase() ?? null;
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
// A reply-all's direct copy has no List-Id but names the list: it stays
|
|
29
|
+
// in the list's thread, as the list's own copy (then a duplicate) would.
|
|
30
|
+
const listPost = parent.listPostAddress?.toLocaleLowerCase() ?? null;
|
|
31
|
+
|
|
32
|
+
const namesList =
|
|
33
|
+
normalizedListId === null &&
|
|
34
|
+
listPost !== null &&
|
|
35
|
+
[...input.to, ...input.cc].some(
|
|
36
|
+
(recipient) => recipient.address.toLocaleLowerCase() === listPost
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
if (parent.listId && parentListId !== normalizedListId && !namesList) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
29
42
|
|
|
30
43
|
const parentParticipants = normalizedParticipantSet(
|
|
31
44
|
[...parent.fromJson, ...parent.toJson, ...parent.ccJson].map(
|