@open-workspace/email 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (271) hide show
  1. package/README.md +9 -4
  2. package/alchemy/index.ts +130 -2
  3. package/drizzle/mailbox/0002_good_komodo.sql +2 -0
  4. package/drizzle/mailbox/0003_after_0_4_2.sql +180 -0
  5. package/drizzle/mailbox/meta/0002_snapshot.json +1017 -0
  6. package/drizzle/mailbox/meta/0003_snapshot.json +1507 -0
  7. package/drizzle/mailbox/meta/_journal.json +14 -0
  8. package/drizzle/mailbox/migrations.js +5 -1
  9. package/migrations/0001_overrated_ricochet.sql +2 -0
  10. package/migrations/0002_after_0_4_2.sql +35 -0
  11. package/migrations/meta/0001_snapshot.json +564 -0
  12. package/migrations/meta/0002_snapshot.json +807 -0
  13. package/migrations/meta/_journal.json +14 -0
  14. package/package.json +23 -7
  15. package/shared/attachment-preview.ts +18 -8
  16. package/shared/folder-order.ts +31 -6
  17. package/shared/mail-headers.ts +3 -3
  18. package/shared/mail-html.ts +25 -0
  19. package/shared/mail-import.ts +0 -7
  20. package/shared/mail-received.ts +4 -4
  21. package/shared/mail-rule-actions.ts +47 -0
  22. package/shared/mail.ts +70 -4
  23. package/shared/office-preview.ts +0 -4
  24. package/shared/pdfjs-assets.ts +1 -3
  25. package/shared/rich-content.ts +35 -0
  26. package/shared/signature.ts +15 -0
  27. package/src/admin/address-auto-reply.tsx +49 -0
  28. package/src/admin/blocked-senders.tsx +86 -66
  29. package/src/admin/forward-destinations.tsx +140 -0
  30. package/src/admin/mail-archive.tsx +134 -0
  31. package/src/admin/team-signature.tsx +90 -0
  32. package/src/client.tsx +130 -16
  33. package/src/mail/attachment-preview-dialog.tsx +13 -5
  34. package/src/mail/auto-reply-banner.tsx +61 -0
  35. package/src/mail/blocked-senders.tsx +124 -91
  36. package/src/mail/compose-window-chrome.tsx +20 -8
  37. package/src/mail/compose-window.tsx +387 -365
  38. package/src/mail/composer-asset-bookmarks.ts +47 -12
  39. package/src/mail/composer-attachment-list.tsx +42 -17
  40. package/src/mail/composer-editor-document.ts +54 -10
  41. package/src/mail/composer-editor.tsx +44 -44
  42. package/src/mail/composer-link-text.ts +42 -0
  43. package/src/mail/composer-recipient-fields.tsx +25 -16
  44. package/src/mail/composer-recipients.ts +62 -16
  45. package/src/mail/composer-session.ts +107 -23
  46. package/src/mail/composer-toolbar.tsx +15 -1
  47. package/src/mail/composer-upload-client.ts +29 -1
  48. package/src/mail/composer-upload-progress.ts +2 -2
  49. package/src/mail/conversation-actions-model.ts +56 -0
  50. package/src/mail/conversation-list.tsx +263 -128
  51. package/src/mail/conversation-loading-skeleton.tsx +165 -90
  52. package/src/mail/conversation-message.tsx +257 -102
  53. package/src/mail/conversation-order.ts +12 -0
  54. package/src/mail/conversation-reply.tsx +313 -189
  55. package/src/mail/conversation-selection-toolbar.tsx +188 -143
  56. package/src/mail/conversation-view.tsx +320 -85
  57. package/src/mail/delivery-indicator.tsx +33 -12
  58. package/src/mail/draft-attachments.ts +194 -0
  59. package/src/mail/email-html-body.tsx +97 -53
  60. package/src/mail/folder-cleanup-bar.tsx +193 -0
  61. package/src/mail/folder-glyph.tsx +5 -0
  62. package/src/mail/folder-manager.tsx +71 -38
  63. package/src/mail/mail-app-bar.tsx +15 -3
  64. package/src/mail/mail-dock.tsx +19 -3
  65. package/src/mail/mail-focus.ts +48 -0
  66. package/src/mail/mail-folders.ts +14 -2
  67. package/src/mail/mail-layout.css +20 -0
  68. package/src/mail/mail-mailbox-sheet.tsx +22 -69
  69. package/src/mail/mail-nav-rail.tsx +7 -4
  70. package/src/mail/mail-nav.tsx +66 -46
  71. package/src/mail/mail-prefetch.ts +30 -9
  72. package/src/mail/mail-primitives.tsx +6 -4
  73. package/src/mail/mail-queries.ts +122 -20
  74. package/src/mail/mail-query-cache.ts +1085 -173
  75. package/src/mail/mail-search-params.ts +35 -0
  76. package/src/mail/mail-shell.tsx +32 -9
  77. package/src/mail/mailbox-ai-settings.tsx +56 -42
  78. package/src/mail/mailbox-realtime.tsx +4 -12
  79. package/src/mail/mailbox-rules.tsx +898 -0
  80. package/src/mail/message-actions.tsx +38 -26
  81. package/src/mail/message-attachments.tsx +20 -12
  82. package/src/mail/message-body.ts +53 -0
  83. package/src/mail/message-format.ts +8 -2
  84. package/src/mail/message-header.tsx +3 -1
  85. package/src/mail/message-translation.tsx +34 -67
  86. package/src/mail/original-message-sheet.tsx +63 -50
  87. package/src/mail/original-message.ts +6 -6
  88. package/src/mail/pane-splitter.tsx +3 -3
  89. package/src/mail/pdf-preview.tsx +70 -62
  90. package/src/mail/plain-message-text.tsx +28 -0
  91. package/src/mail/print-message.ts +31 -36
  92. package/src/mail/reader-pane.tsx +134 -102
  93. package/src/mail/recipient-combobox.tsx +52 -29
  94. package/src/mail/recipient-field-row.tsx +3 -0
  95. package/src/mail/rich-content.ts +128 -0
  96. package/src/mail/sanitize-email-html.ts +57 -33
  97. package/src/mail/sender-actions.tsx +49 -39
  98. package/src/mail/sender-authentication.tsx +1 -1
  99. package/src/mail/sender-dialogs.tsx +67 -43
  100. package/src/mail/sender-security.tsx +9 -10
  101. package/src/mail/signature.ts +43 -0
  102. package/src/mail/translation-html.ts +3 -3
  103. package/src/mail/types.ts +12 -0
  104. package/src/mail/use-composer-attachment-preflight.ts +23 -60
  105. package/src/mail/use-composer-core.ts +286 -0
  106. package/src/mail/use-composer-navigation.ts +120 -0
  107. package/src/mail/use-composer-submission.ts +154 -19
  108. package/src/mail/use-conversation-actions.ts +292 -0
  109. package/src/mail/use-conversation-position.ts +51 -0
  110. package/src/mail/use-conversation-selection.ts +192 -89
  111. package/src/mail/use-draft-autosave.ts +394 -0
  112. package/src/mail/use-drafts.ts +194 -0
  113. package/src/mail/use-mail-controller.ts +754 -213
  114. package/src/mail/use-mail-data.ts +210 -100
  115. package/src/mail/use-mail-safety.ts +19 -38
  116. package/src/mail/use-mail-send.ts +18 -6
  117. package/src/mail/use-pdf-translation.ts +1 -1
  118. package/src/mail/use-reply-cc.ts +61 -0
  119. package/src/mail/use-visible-message-read.ts +128 -64
  120. package/src/pwa/push-key.ts +18 -0
  121. package/src/pwa/push-subscription.ts +88 -26
  122. package/src/settings/auto-reply-editor.tsx +454 -0
  123. package/src/settings/auto-reply-page.tsx +59 -0
  124. package/src/settings/mail-apps-page.tsx +52 -43
  125. package/src/settings/notifications-page.tsx +82 -67
  126. package/src/settings/rich-content-editor.tsx +182 -0
  127. package/src/settings/settings-queries.ts +57 -0
  128. package/src/settings/signature-editor.tsx +142 -0
  129. package/src/settings/signature-page.tsx +70 -0
  130. package/worker/admin/blocked-sender-routes.ts +10 -16
  131. package/worker/backups/eml.ts +185 -0
  132. package/worker/backups/file-parts.ts +140 -0
  133. package/worker/backups/mail-backup.ts +644 -0
  134. package/worker/backups/settings-backup.ts +866 -0
  135. package/worker/backups/stream-eml.ts +73 -0
  136. package/worker/db/schema.ts +98 -2
  137. package/worker/jmap/attachment-blobs.ts +113 -0
  138. package/worker/jmap/blobs.ts +162 -0
  139. package/worker/jmap/draft-content.ts +42 -0
  140. package/worker/jmap/drafts.ts +70 -0
  141. package/worker/jmap/email-body.ts +152 -0
  142. package/worker/jmap/email-methods.ts +1223 -0
  143. package/worker/jmap/eventsource.ts +9 -8
  144. package/worker/jmap/import-methods.ts +205 -0
  145. package/worker/jmap/mailbox-methods.ts +187 -0
  146. package/worker/jmap/method-core.ts +379 -0
  147. package/worker/jmap/methods.ts +108 -1815
  148. package/worker/jmap/push-subscription-methods.ts +373 -0
  149. package/worker/jmap/push-types.ts +46 -2
  150. package/worker/jmap/push.ts +24 -31
  151. package/worker/jmap/routes.ts +144 -153
  152. package/worker/jmap/submission-methods.ts +395 -0
  153. package/worker/mail/access.ts +35 -12
  154. package/worker/mail/ai-classification.ts +2 -2
  155. package/worker/mail/archive-object.ts +96 -0
  156. package/worker/mail/archive-routes.ts +35 -0
  157. package/worker/mail/archive.ts +682 -0
  158. package/worker/mail/auth-headers.ts +48 -40
  159. package/worker/mail/auto-reply-routes.ts +255 -0
  160. package/worker/mail/auto-reply.ts +243 -0
  161. package/worker/mail/bimi.ts +53 -36
  162. package/worker/mail/bootstrap.ts +11 -3
  163. package/worker/mail/capabilities.ts +69 -0
  164. package/worker/mail/classification-signals.ts +1 -1
  165. package/worker/mail/confusables-data.ts +9 -0
  166. package/worker/mail/conversation-cursor.ts +6 -15
  167. package/worker/mail/delivery-events.ts +9 -7
  168. package/worker/mail/delivery-facts.ts +1 -1
  169. package/worker/mail/demo-routes.ts +3 -3
  170. package/worker/mail/doh.ts +3 -3
  171. package/worker/mail/download-links.ts +153 -0
  172. package/worker/mail/downloads.ts +2 -6
  173. package/worker/mail/draft-routes.ts +473 -0
  174. package/worker/mail/draft-sender.ts +54 -0
  175. package/worker/mail/edge-cache.ts +41 -55
  176. package/worker/mail/email-routing.ts +123 -0
  177. package/worker/mail/forward-destinations.ts +114 -0
  178. package/worker/mail/forward-downloads.ts +115 -0
  179. package/worker/mail/forward-html.ts +85 -0
  180. package/worker/mail/header-metadata.ts +2 -5
  181. package/worker/mail/impersonation.ts +102 -101
  182. package/worker/mail/import-routes.ts +29 -82
  183. package/worker/mail/import.ts +130 -0
  184. package/worker/mail/inbound-delivery.ts +6 -5
  185. package/worker/mail/inbound.ts +58 -28
  186. package/worker/mail/ip-intel.ts +8 -17
  187. package/worker/mail/isolate-cache.ts +1 -1
  188. package/worker/mail/mailbox-deletion.ts +33 -12
  189. package/worker/mail/mailbox-directory.ts +39 -65
  190. package/worker/mail/mailing-list.ts +29 -58
  191. package/worker/mail/message-security.ts +3 -3
  192. package/worker/mail/message-view.ts +12 -10
  193. package/worker/mail/office-conversion.ts +3 -5
  194. package/worker/mail/office-preview-container.ts +3 -8
  195. package/worker/mail/outbound-content.ts +139 -65
  196. package/worker/mail/outbound-delivery.ts +20 -17
  197. package/worker/mail/outbound-service.ts +34 -9
  198. package/worker/mail/outbound.ts +146 -75
  199. package/worker/mail/outgoing-context.ts +60 -30
  200. package/worker/mail/participants.ts +10 -15
  201. package/worker/mail/plain-body.ts +39 -0
  202. package/worker/mail/push-endpoint.ts +17 -0
  203. package/worker/mail/push-notifications.ts +87 -43
  204. package/worker/mail/quote-parser.ts +107 -25
  205. package/worker/mail/r2-presigned-urls.ts +7 -0
  206. package/worker/mail/receive.ts +146 -39
  207. package/worker/mail/recipients.ts +1 -1
  208. package/worker/mail/remote-proxy.ts +3 -3
  209. package/worker/mail/repeat.ts +1 -1
  210. package/worker/mail/reply-plan.ts +124 -26
  211. package/worker/mail/rfc.ts +15 -7
  212. package/worker/mail/rich-content.ts +207 -0
  213. package/worker/mail/routes.ts +696 -304
  214. package/worker/mail/routing.ts +274 -0
  215. package/worker/mail/rules.ts +230 -0
  216. package/worker/mail/safety-routes.ts +88 -80
  217. package/worker/mail/schemas.ts +222 -31
  218. package/worker/mail/search.ts +25 -2
  219. package/worker/mail/security-settings.ts +4 -10
  220. package/worker/mail/send-message.ts +238 -0
  221. package/worker/mail/sender-intel.ts +12 -20
  222. package/worker/mail/signature-routes.ts +201 -0
  223. package/worker/mail/signatures.ts +195 -0
  224. package/worker/mail/spamhaus.ts +28 -16
  225. package/worker/mail/storage-keys.ts +35 -0
  226. package/worker/mail/unsubscribe.ts +19 -41
  227. package/worker/mail/uploads.ts +193 -80
  228. package/worker/mail/webhook-source.ts +3 -2
  229. package/worker/mail/workspace-safety.ts +194 -60
  230. package/worker/mailbox/demo.ts +13 -15
  231. package/worker/mailbox/email-index.ts +92 -13
  232. package/worker/mailbox/folder-model.ts +0 -20
  233. package/worker/mailbox/import-placement.ts +21 -36
  234. package/worker/mailbox/inbound-decision.ts +114 -0
  235. package/worker/mailbox/inbox-counts.ts +45 -25
  236. package/worker/mailbox/index.ts +843 -2888
  237. package/worker/mailbox/jmap-change-log.ts +195 -0
  238. package/worker/mailbox/jmap-email-rows.ts +186 -0
  239. package/worker/mailbox/jmap-mailboxes.ts +116 -0
  240. package/worker/mailbox/jmap-placement.ts +139 -0
  241. package/worker/mailbox/jmap-query-filter.ts +231 -0
  242. package/worker/mailbox/jmap-store.ts +167 -299
  243. package/worker/mailbox/mailbox-auto-reply.ts +301 -0
  244. package/worker/mailbox/mailbox-backup.ts +538 -0
  245. package/worker/mailbox/mailbox-drafts.ts +417 -0
  246. package/worker/mailbox/mailbox-folders.ts +242 -0
  247. package/worker/mailbox/mailbox-import.ts +450 -0
  248. package/worker/mailbox/mailbox-inbound.ts +934 -0
  249. package/worker/mailbox/mailbox-jmap.ts +294 -0
  250. package/worker/mailbox/mailbox-outbound.ts +413 -0
  251. package/worker/mailbox/mailbox-rules.ts +336 -0
  252. package/worker/mailbox/mailbox-senders.ts +294 -0
  253. package/worker/mailbox/mailbox-storage.ts +438 -0
  254. package/worker/mailbox/mailbox-views.ts +891 -0
  255. package/worker/mailbox/model.ts +1 -0
  256. package/worker/mailbox/office-preview-jobs.ts +59 -47
  257. package/worker/mailbox/placement.ts +933 -0
  258. package/worker/mailbox/restore-thread.ts +52 -0
  259. package/worker/mailbox/retention.ts +36 -0
  260. package/worker/mailbox/schema.ts +211 -1
  261. package/worker/mailbox/senders.ts +216 -178
  262. package/worker/mailbox/spam-undo-filing.ts +138 -0
  263. package/worker/mailbox/spam-undo.ts +240 -0
  264. package/worker/mailbox/support.ts +416 -0
  265. package/worker/mcp/mail-action-tools.ts +156 -0
  266. package/worker/mcp/mail-compose-tools.ts +8 -2
  267. package/worker/mcp/mail-safety-tools.ts +4 -4
  268. package/worker/module.ts +23 -2
  269. package/src/mail/use-compose-submission.ts +0 -208
  270. package/src/mail/use-reply-submission.ts +0 -114
  271. package/worker/mail/capped-body.ts +0 -36
@@ -0,0 +1,128 @@
1
+ import { mergeAttributes, Node } from "@tiptap/react";
2
+
3
+ /**
4
+ * Saved rich content (a signature, an auto-reply) in the composer's
5
+ * editor. Its images are already in R2, so the editor shows them from there
6
+ * as `storedImage` nodes instead of uploading them again; a message sends
7
+ * them as references (`storedImages`) and the Worker copies them in, and a
8
+ * saved editor keeps them by content id (worker/mail/rich-content.ts).
9
+ */
10
+
11
+ /**
12
+ * Saved HTML made ready for the editor: each `cid:` image becomes a stored
13
+ * image of `ownerId`, shown from `imageUrl(contentId)`.
14
+ */
15
+ export function storedImagesHtml(
16
+ html: string,
17
+ ownerId: string,
18
+ imageUrl: (contentId: string) => string
19
+ ) {
20
+ const document = new DOMParser().parseFromString(html, "text/html");
21
+
22
+ for (const image of document.querySelectorAll("img")) {
23
+ const source = image.getAttribute("src") ?? "";
24
+
25
+ if (!source.startsWith("cid:")) {
26
+ image.remove();
27
+ continue;
28
+ }
29
+
30
+ image.dataset.storedOwner = ownerId;
31
+ image.dataset.src = imageUrl(source.slice(4));
32
+ }
33
+
34
+ return document.body.innerHTML;
35
+ }
36
+
37
+ /**
38
+ * Takes the stored images out of serialized editor HTML: their references,
39
+ * with the `img` left as a plain `cid:` image for sending or saving.
40
+ */
41
+ export function takeStoredImages(root: Element) {
42
+ return Array.from(
43
+ root.querySelectorAll<HTMLImageElement>("img[data-stored-owner]"),
44
+ (image) => {
45
+ const ownerId = image.dataset.storedOwner ?? "";
46
+ const contentId = (image.getAttribute("src") ?? "").replace(/^cid:/u, "");
47
+ delete image.dataset.storedOwner;
48
+
49
+ return { ownerId, contentId };
50
+ }
51
+ );
52
+ }
53
+
54
+ /** An image of saved rich content, shown from where it is stored. */
55
+ export const StoredImage = Node.create({
56
+ name: "storedImage",
57
+ group: "inline",
58
+ inline: true,
59
+ atom: true,
60
+ draggable: true,
61
+ selectable: true,
62
+
63
+ // Each read from the saved `img` that storedImagesHtml prepared.
64
+ addAttributes() {
65
+ return {
66
+ ownerId: {
67
+ default: "",
68
+ rendered: false,
69
+ parseHTML: (element) => element.dataset.storedOwner ?? "",
70
+ },
71
+ contentId: {
72
+ default: "",
73
+ rendered: false,
74
+ parseHTML: (element) =>
75
+ element.dataset.cid ??
76
+ (element.getAttribute("src") ?? "").replace(/^cid:/u, ""),
77
+ },
78
+ src: {
79
+ default: "",
80
+ rendered: false,
81
+ parseHTML: (element) => element.dataset.src ?? "",
82
+ },
83
+ alt: {
84
+ default: "",
85
+ rendered: false,
86
+ parseHTML: (element) => element.getAttribute("alt") ?? "",
87
+ },
88
+ };
89
+ },
90
+
91
+ parseHTML() {
92
+ return [{ tag: "img[data-stored-owner]" }];
93
+ },
94
+
95
+ // Serialized with its content id in `data-cid`, not `src`: the editor
96
+ // serializes in the page's document, where a `cid:` src would be fetched
97
+ // (and fail). composerContent turns it into the `cid:` image it sends,
98
+ // marked for takeStoredImages.
99
+ renderHTML({ node, HTMLAttributes }) {
100
+ return [
101
+ "img",
102
+ mergeAttributes(HTMLAttributes, {
103
+ "data-cid": String(node.attrs.contentId),
104
+ alt: node.attrs.alt,
105
+ title: node.attrs.alt,
106
+ "data-stored-owner": node.attrs.ownerId,
107
+ }),
108
+ ];
109
+ },
110
+
111
+ renderText({ node }) {
112
+ return String(node.attrs.alt);
113
+ },
114
+
115
+ // Shown in the editor from its stored copy.
116
+ addNodeView() {
117
+ return ({ node }) => {
118
+ const image = document.createElement("img");
119
+ image.className = "composer-inline-image";
120
+ image.src = String(node.attrs.src);
121
+ image.alt = String(node.attrs.alt);
122
+ image.title = String(node.attrs.alt);
123
+ image.draggable = false;
124
+
125
+ return { dom: image };
126
+ };
127
+ },
128
+ });
@@ -1,7 +1,9 @@
1
1
  import DOMPurify from "dompurify";
2
2
 
3
3
  import { isComposerInlineImageContentType } from "../../shared/mail";
4
+ import { safeLink } from "../../shared/mail-html";
4
5
  import { isRemoteHttpUrl, mailRemoteProxyPath } from "../../shared/mail-remote";
6
+ import { normalizedContentId } from "../../shared/rich-content";
5
7
  import type { MessageAttachment } from "./types";
6
8
 
7
9
  const FORBIDDEN_TAGS = [
@@ -25,13 +27,19 @@ const FORBIDDEN_TAGS = [
25
27
  "math",
26
28
  ];
27
29
 
28
- const FORBIDDEN_ATTRIBUTES = ["poster", "action", "formaction"];
30
+ const FORBIDDEN_ATTRIBUTES = [
31
+ "poster",
32
+ "action",
33
+ "formaction",
34
+ "data-mail-quote",
35
+ ];
29
36
 
30
37
  const COMPOSER_TAGS = [
31
38
  "a",
32
39
  "b",
33
40
  "blockquote",
34
41
  "br",
42
+ "caption",
35
43
  "code",
36
44
  "div",
37
45
  "em",
@@ -45,26 +53,39 @@ const COMPOSER_TAGS = [
45
53
  "section",
46
54
  "span",
47
55
  "strong",
56
+ "table",
57
+ "tbody",
58
+ "td",
59
+ "tfoot",
60
+ "th",
61
+ "thead",
62
+ "tr",
48
63
  "u",
49
64
  "ul",
50
65
  ];
51
66
 
52
67
  const COMPOSER_ATTRIBUTES = [
53
68
  "alt",
69
+ "colspan",
54
70
  "data-forwarded-message",
71
+ "data-mail-quote",
55
72
  "data-linked-attachment-card",
56
73
  "href",
57
74
  "loading",
58
75
  "referrerpolicy",
59
76
  "rel",
77
+ "rowspan",
60
78
  "src",
61
79
  "srcset",
62
80
  "target",
63
81
  "title",
64
82
  ];
65
83
 
84
+ // A backslash escape spells any of these without the letters (`u\72l(` is
85
+ // `url(`), and `image-set()` loads plain strings as URLs; neither is
86
+ // left for the browser to read.
66
87
  const DANGEROUS_CSS =
67
- /expression\s*\(|-moz-binding|behavior\s*:|@import|javascript\s*:|vbscript\s*:|data\s*:\s*text\/html/giu;
88
+ /expression\s*\(|-moz-binding|behavior\s*:|@import|javascript\s*:|vbscript\s*:|data\s*:\s*text\/html|image-set\s*\(|\\/giu;
68
89
 
69
90
  /**
70
91
  * Keep email layout CSS while blocking scriptable CSS.
@@ -81,9 +102,20 @@ export function sanitizeEmailCss(
81
102
  "/* blocked */"
82
103
  );
83
104
 
105
+ // A quoted URL holds anything but its own quote; an unquoted one no
106
+ // quote or parenthesis. Any other `url(` is not left for the
107
+ // browser to read some other way.
84
108
  return withoutDanger.replaceAll(
85
- /url\s*\(\s*(['"]?)([^)'"]*?)\1\s*\)/giu,
86
- (match, _quote: string, rawUrl: string) => {
109
+ /url\s*\(\s*(?:"([^"]*)"|'([^']*)'|([^"'()]*))\s*\)|url\s*\(/giu,
110
+ (
111
+ match,
112
+ doubleQuoted: string | undefined,
113
+ singleQuoted: string | undefined,
114
+ unquoted: string | undefined
115
+ ) => {
116
+ const rawUrl = doubleQuoted ?? singleQuoted ?? unquoted;
117
+
118
+ if (rawUrl === undefined) return "/* blocked */";
87
119
  const url = rawUrl.trim();
88
120
 
89
121
  if (/^data:image\/(?:gif|jpeg|jpg|png|webp)/iu.test(url)) return match;
@@ -101,32 +133,6 @@ export function sanitizeEmailCss(
101
133
  );
102
134
  }
103
135
 
104
- function normalizedContentId(value: string) {
105
- let decoded = value;
106
-
107
- try {
108
- decoded = decodeURIComponent(value);
109
- } catch {
110
- // Malformed percent escapes are treated as an opaque content ID.
111
- }
112
-
113
- return decoded
114
- .replace(/^cid:/iu, "")
115
- .replaceAll(/^<|>$/gu, "")
116
- .trim()
117
- .toLocaleLowerCase();
118
- }
119
-
120
- function safeLink(value: string) {
121
- try {
122
- const url = new URL(value);
123
-
124
- return ["http:", "https:", "mailto:"].includes(url.protocol);
125
- } catch {
126
- return false;
127
- }
128
- }
129
-
130
136
  function inlineAttachmentUrls(
131
137
  mailboxId: string,
132
138
  messageId: string,
@@ -166,7 +172,10 @@ function rewriteSrcset(
166
172
  if (!match) return "";
167
173
  const [, source = "", descriptor = ""] = match;
168
174
 
169
- if (!isRemoteHttpUrl(source)) return trimmed;
175
+ // Like `src`: a candidate that can't go through the proxy is dropped,
176
+ // so relative and protocol-relative URLs (`//host/x.png`) never load
177
+ // straight from the sender's server.
178
+ if (!isRemoteHttpUrl(source)) return "";
170
179
  const proxied = rewriteRemoteUrl(source);
171
180
 
172
181
  return proxied ? `${proxied}${descriptor}` : "";
@@ -247,6 +256,7 @@ type SafeEmailDocument = {
247
256
  srcDoc: string;
248
257
  proxiedRemoteImages: number;
249
258
  renderAsHtml: boolean;
259
+ hasHtmlQuote: boolean;
250
260
  };
251
261
 
252
262
  /**
@@ -261,7 +271,7 @@ export function sanitizeEmailHtml(input: {
261
271
  messageId: string;
262
272
  attachments: MessageAttachment[];
263
273
  remoteTokens: Record<string, string> | null;
264
- }): SafeEmailDocument {
274
+ }) {
265
275
  const { remoteTokens } = input;
266
276
 
267
277
  const rewriteRemoteUrl = (url: string) => {
@@ -276,6 +286,7 @@ export function sanitizeEmailHtml(input: {
276
286
  const sanitized = DOMPurify.sanitize(input.html, {
277
287
  USE_PROFILES: { html: true },
278
288
  ADD_TAGS: ["style"],
289
+ FORCE_BODY: true,
279
290
  ADD_ATTR: [
280
291
  "style",
281
292
  "bgcolor",
@@ -376,6 +387,12 @@ export function sanitizeEmailHtml(input: {
376
387
  .map((node) => node.outerHTML)
377
388
  .join("\n");
378
389
 
390
+ // Keep only the quote marker when stripping sender classes for composer HTML.
391
+ const quotes = document.querySelectorAll<HTMLElement>(
392
+ '.gmail_quote:has(blockquote), blockquote.gmail_quote, blockquote[type="cite" i]'
393
+ );
394
+
395
+ for (const quote of quotes) quote.dataset.mailQuote = "";
379
396
  const body = document.body.innerHTML;
380
397
 
381
398
  // Composer HTML is rendered in the app document, so sender-controlled CSS,
@@ -413,6 +430,7 @@ export function sanitizeEmailHtml(input: {
413
430
  </style>
414
431
  ${headStyles}
415
432
  <style>
433
+ html[data-hide-quote] [data-mail-quote] { display: none !important; }
416
434
  html, body {
417
435
  width: 100% !important;
418
436
  max-width: 100% !important;
@@ -426,5 +444,11 @@ export function sanitizeEmailHtml(input: {
426
444
  <body>${body}</body>
427
445
  </html>`;
428
446
 
429
- return { composerHtml, srcDoc, proxiedRemoteImages, renderAsHtml };
447
+ return {
448
+ composerHtml,
449
+ srcDoc,
450
+ proxiedRemoteImages,
451
+ renderAsHtml,
452
+ hasHtmlQuote: quotes.length > 0,
453
+ } satisfies SafeEmailDocument;
430
454
  }
@@ -1,6 +1,8 @@
1
- import { useState } from "react";
1
+ import { Fragment, useState } from "react";
2
2
  import { toast } from "sonner";
3
3
 
4
+ import type { ConversationPlace } from "../../shared/mail";
5
+ import { FilterMessagesDialog } from "./mailbox-rules";
4
6
  import {
5
7
  BlockSenderDialog,
6
8
  senderLabel,
@@ -8,50 +10,66 @@ import {
8
10
  } from "./sender-dialogs";
9
11
  import type { Mailbox, MessageDetail } from "./types";
10
12
  import {
11
- useNotSpam,
12
13
  useReportMessage,
13
14
  useTrustSender,
14
15
  useUnblockSender,
16
+ useUndoSpamDecision,
15
17
  } from "./use-mail-safety";
16
18
 
17
- type SenderDialogKind = "unsubscribe" | "block";
19
+ type SenderDialogKind = "unsubscribe" | "block" | "filter";
18
20
 
19
21
  /**
20
- * Sender-level actions (unsubscribe, block, report, trust) and their
22
+ * Sender-level actions (unsubscribe, block, filter, report, trust) and their
21
23
  * confirmation dialogs, shared by the message menus and the command bar.
24
+ * Report spam here is a message's, from its menu; the bar reports or
25
+ * clears the whole conversation as a selection does (`useConversationActions`).
22
26
  */
23
27
  export function useSenderActions({
24
28
  mailbox,
25
- onLeaveConversation,
29
+ onPlaces,
26
30
  }: {
27
31
  mailbox?: Mailbox;
28
- /** Called after an action moved the open conversation out of the current folder. */
29
- onLeaveConversation: () => void;
32
+ /** What an action did to conversations, for the view to follow (`followOwnChange`). */
33
+ onPlaces: (places: readonly ConversationPlace[]) => void;
30
34
  }) {
31
35
  const [dialog, setDialog] = useState<{
32
36
  kind: SenderDialogKind;
33
37
  message: MessageDetail;
34
38
  open: boolean;
39
+ /** Counts the openings, so each starts a fresh dialog, its choices unset. */
40
+ opening: number;
35
41
  } | null>(null);
36
42
 
37
43
  const report = useReportMessage();
44
+ const undo = useUndoSpamDecision();
38
45
  const unblock = useUnblockSender();
39
46
  const trust = useTrustSender();
40
- const notSpam = useNotSpam();
41
47
 
42
48
  function setOpen(open: boolean) {
43
49
  setDialog((current) => current && { ...current, open });
44
50
  }
45
51
 
52
+ /** Report spam of one message, and its sender's others in the thread. */
46
53
  function reportSpam(message: MessageDetail) {
47
54
  if (!mailbox) return;
55
+ const mailboxId = mailbox.id;
48
56
  report.mutate(
49
- { mailboxId: mailbox.id, messageId: message.id },
57
+ { mailboxId, messageId: message.id },
50
58
  {
51
- onSuccess: (result) => {
52
- toast.success("Reported as spam");
53
-
54
- if (result.moved) onLeaveConversation();
59
+ onSuccess: ({ places, undoId }) => {
60
+ if (undoId) {
61
+ toast.success("Reported as spam", {
62
+ duration: 8000,
63
+ action: {
64
+ label: "Undo",
65
+ onClick: () => undo.mutate({ mailboxId, undoId }),
66
+ },
67
+ });
68
+ } else {
69
+ toast.warning("There was no mail to report here any more");
70
+ }
71
+
72
+ onPlaces(places);
55
73
  },
56
74
  onError: (error) => toast.error(error.message),
57
75
  }
@@ -83,25 +101,9 @@ export function useSenderActions({
83
101
  );
84
102
  }
85
103
 
86
- function markNotSpam(message: MessageDetail) {
87
- if (!mailbox) return;
88
- notSpam.mutate(
89
- { mailboxId: mailbox.id, messageId: message.id },
90
- {
91
- onSuccess: () => {
92
- toast.success(
93
- `Moved to Inbox. Mail from ${message.fromAddress} won't be treated as spam.`
94
- );
95
- onLeaveConversation();
96
- },
97
- onError: (error) => toast.error(error.message),
98
- }
99
- );
100
- }
101
-
102
104
  const dialogs =
103
105
  dialog && mailbox ? (
104
- <>
106
+ <Fragment key={dialog.opening}>
105
107
  {dialog.kind === "unsubscribe" && (
106
108
  <UnsubscribeDialog
107
109
  message={dialog.message}
@@ -116,24 +118,32 @@ export function useSenderActions({
116
118
  mailbox={mailbox}
117
119
  open={dialog.open}
118
120
  onOpenChange={setOpen}
119
- onMoved={onLeaveConversation}
121
+ onMoved={onPlaces}
122
+ />
123
+ )}
124
+ {dialog.kind === "filter" && (
125
+ <FilterMessagesDialog
126
+ mailboxId={mailbox.id}
127
+ message={dialog.message}
128
+ open={dialog.open}
129
+ onOpenChange={setOpen}
120
130
  />
121
131
  )}
122
- </>
132
+ </Fragment>
123
133
  ) : null;
124
134
 
125
135
  return {
126
136
  open: (kind: SenderDialogKind, message: MessageDetail) =>
127
- setDialog({ kind, message, open: true }),
137
+ setDialog((current) => ({
138
+ kind,
139
+ message,
140
+ open: true,
141
+ opening: (current?.opening ?? 0) + 1,
142
+ })),
128
143
  reportSpam,
129
144
  unblockSender,
130
145
  trustSender,
131
- markNotSpam,
132
- pending:
133
- report.isPending ||
134
- unblock.isPending ||
135
- trust.isPending ||
136
- notSpam.isPending,
146
+ pending: report.isPending || unblock.isPending || trust.isPending,
137
147
  dialogs,
138
148
  };
139
149
  }
@@ -18,7 +18,7 @@ type Tone = "pass" | "fail" | "warn" | "neutral";
18
18
 
19
19
  function resultTone(
20
20
  value: AuthResult | "pass" | "fail" | "none" | null | undefined
21
- ): Tone {
21
+ ) {
22
22
  if (value === "pass") return "pass";
23
23
 
24
24
  if (value === "fail" || value === "permerror") return "fail";
@@ -15,8 +15,10 @@ import {
15
15
  import { ExternalLink, LoaderCircle, MailMinus, ShieldBan } from "lucide-react";
16
16
  import { useState } from "react";
17
17
  import type { ReactNode } from "react";
18
+ import { Controller, useForm } from "react-hook-form";
18
19
  import { toast } from "sonner";
19
20
 
21
+ import type { ConversationPlace } from "../../shared/mail";
20
22
  import type { Mailbox, MessageDetail } from "./types";
21
23
  import { useBlockSender, useUnsubscribe } from "./use-mail-safety";
22
24
 
@@ -250,9 +252,12 @@ export function UnsubscribeDialog({
250
252
  to {mailbox.address}.
251
253
  </p>
252
254
  {methods?.oneClick && canMail && (
253
- <p title={mailto ?? undefined}>
254
- If their server doesn't answer, we'll send an unsubscribe
255
- email from {mailbox.address} instead.
255
+ <p>
256
+ If their server doesn't answer, we'll email{" "}
257
+ <span className="text-foreground font-medium">
258
+ {mailto}
259
+ </span>{" "}
260
+ from {mailbox.address} instead.
256
261
  </p>
257
262
  )}
258
263
  <p>No links in the message are opened.</p>
@@ -293,34 +298,39 @@ export function BlockSenderDialog({
293
298
  mailbox: Mailbox;
294
299
  open: boolean;
295
300
  onOpenChange: (open: boolean) => void;
296
- onMoved?: () => void;
301
+ /** Where the sender's mail went, for the view to follow. */
302
+ onMoved?: (places: ConversationPlace[]) => void;
297
303
  }) {
298
304
  const block = useBlockSender();
299
- const [scope, setScope] = useState<"address" | "domain">("address");
300
- const [moveExisting, setMoveExisting] = useState(true);
305
+
306
+ const { control, handleSubmit } = useForm<{
307
+ scope: "address" | "domain";
308
+ moveExisting: boolean;
309
+ }>({ defaultValues: { scope: "address", moveExisting: true } });
310
+
301
311
  const name = senderLabel(message);
302
312
  const domain = addressDomain(message.fromAddress);
303
313
  const ownDomain = domain === addressDomain(mailbox.address);
304
314
 
305
- function confirm() {
315
+ const confirm = handleSubmit(({ scope, moveExisting }) => {
306
316
  const pattern = scope === "domain" ? `@${domain}` : message.fromAddress;
307
317
  block.mutate(
308
318
  { mailboxId: mailbox.id, pattern, moveExisting },
309
319
  {
310
320
  onSuccess: (result) => {
321
+ const moved = result.places.length;
311
322
  toast.success(
312
- result.moved
313
- ? `Blocked ${result.block.pattern}; moved ${result.moved} conversation${result.moved === 1 ? "" : "s"} to Spam`
323
+ moved
324
+ ? `Blocked ${result.block.pattern}; moved mail from ${moved} conversation${moved === 1 ? "" : "s"} to Spam`
314
325
  : `Blocked ${result.block.pattern}`
315
326
  );
316
327
  onOpenChange(false);
317
-
318
- if (result.moved) onMoved?.();
328
+ onMoved?.(result.places);
319
329
  },
320
330
  onError: (error) => toast.error(error.message),
321
331
  }
322
332
  );
323
- }
333
+ });
324
334
 
325
335
  return (
326
336
  <AlertDialog open={open} onOpenChange={onOpenChange}>
@@ -335,44 +345,58 @@ export function BlockSenderDialog({
335
345
  mailbox. The sender isn't told, and you can unblock at any time.
336
346
  </AlertDialogDescription>
337
347
  </AlertDialogHeader>
338
- <RadioGroup
339
- aria-label="What to block"
340
- value={scope}
341
- onValueChange={(value: "address" | "domain") => setScope(value)}
342
- >
343
- {(
344
- [
345
- ["address", message.fromAddress, "Only this address"],
346
- ["domain", `@${domain}`, "Everyone at this domain"],
347
- ] as const
348
- ).map(([value, label, hint]) => (
349
- <RadioCard
350
- key={value}
351
- value={value}
352
- disabled={value === "domain" && ownDomain}
353
- className="items-center rounded-md px-3 py-2 data-disabled:opacity-50 *:data-[slot=radio-card-indicator]:mt-0"
348
+ <Controller
349
+ control={control}
350
+ name="scope"
351
+ render={({ field }) => (
352
+ <RadioGroup
353
+ aria-label="What to block"
354
+ value={field.value}
355
+ onValueChange={(value: "address" | "domain") =>
356
+ field.onChange(value)
357
+ }
354
358
  >
355
- <span className="min-w-0">
356
- <span className="block truncate text-[13px] font-medium">
357
- {label}
358
- </span>
359
- <span className="text-muted-foreground block text-xs">
360
- {hint}
361
- </span>
362
- </span>
363
- </RadioCard>
364
- ))}
365
- </RadioGroup>
366
- <CheckRow checked={moveExisting} onChange={setMoveExisting}>
367
- Also move existing conversations to Spam
368
- </CheckRow>
359
+ {(
360
+ [
361
+ ["address", message.fromAddress, "Only this address"],
362
+ ["domain", `@${domain}`, "Everyone at this domain"],
363
+ ] as const
364
+ ).map(([value, label, hint]) => (
365
+ <RadioCard
366
+ key={value}
367
+ value={value}
368
+ disabled={value === "domain" && ownDomain}
369
+ className="items-center rounded-md px-3 py-2 data-disabled:opacity-50 *:data-[slot=radio-card-indicator]:mt-0"
370
+ >
371
+ <span className="min-w-0">
372
+ <span className="block truncate text-[13px] font-medium">
373
+ {label}
374
+ </span>
375
+ <span className="text-muted-foreground block text-xs">
376
+ {hint}
377
+ </span>
378
+ </span>
379
+ </RadioCard>
380
+ ))}
381
+ </RadioGroup>
382
+ )}
383
+ />
384
+ <Controller
385
+ control={control}
386
+ name="moveExisting"
387
+ render={({ field }) => (
388
+ <CheckRow checked={field.value} onChange={field.onChange}>
389
+ Also move existing conversations to Spam
390
+ </CheckRow>
391
+ )}
392
+ />
369
393
  <AlertDialogFooter>
370
394
  <AlertDialogCancel>Cancel</AlertDialogCancel>
371
395
  <AlertDialogAction
372
396
  type="button"
373
397
  variant="destructive"
374
398
  disabled={block.isPending}
375
- onClick={confirm}
399
+ onClick={(event) => void confirm(event)}
376
400
  >
377
401
  {block.isPending && <LoaderCircle className="animate-spin" />}
378
402
  Block