@im-more/dsh-mail 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/cordis.patch.yml +14 -0
- package/lib/index.js +1334 -0
- package/lib/index.js.map +1 -0
- package/lib/types/bot.d.ts +49 -0
- package/lib/types/bot.d.ts.map +1 -0
- package/lib/types/bot.js +189 -0
- package/lib/types/bot.js.map +1 -0
- package/lib/types/config.d.ts +51 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +55 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/decode.d.ts +60 -0
- package/lib/types/decode.d.ts.map +1 -0
- package/lib/types/decode.js +195 -0
- package/lib/types/decode.js.map +1 -0
- package/lib/types/errors.d.ts +33 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +156 -0
- package/lib/types/errors.js.map +1 -0
- package/lib/types/html.d.ts +30 -0
- package/lib/types/html.d.ts.map +1 -0
- package/lib/types/html.js +121 -0
- package/lib/types/html.js.map +1 -0
- package/lib/types/index.d.ts +11 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +10 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/mail.d.ts +24 -0
- package/lib/types/mail.d.ts.map +1 -0
- package/lib/types/mail.js +26 -0
- package/lib/types/mail.js.map +1 -0
- package/lib/types/media.d.ts +52 -0
- package/lib/types/media.d.ts.map +1 -0
- package/lib/types/media.js +81 -0
- package/lib/types/media.js.map +1 -0
- package/lib/types/modes/html.d.ts +13 -0
- package/lib/types/modes/html.d.ts.map +1 -0
- package/lib/types/modes/html.js +70 -0
- package/lib/types/modes/html.js.map +1 -0
- package/lib/types/modes/shared.d.ts +114 -0
- package/lib/types/modes/shared.d.ts.map +1 -0
- package/lib/types/modes/shared.js +185 -0
- package/lib/types/modes/shared.js.map +1 -0
- package/lib/types/send.d.ts +26 -0
- package/lib/types/send.d.ts.map +1 -0
- package/lib/types/send.js +58 -0
- package/lib/types/send.js.map +1 -0
- package/lib/types/transport/imap.d.ts +95 -0
- package/lib/types/transport/imap.d.ts.map +1 -0
- package/lib/types/transport/imap.js +354 -0
- package/lib/types/transport/imap.js.map +1 -0
- package/package.json +70 -3
- package/src/bot.ts +264 -0
- package/src/config.ts +98 -0
- package/src/decode.ts +275 -0
- package/src/errors.ts +199 -0
- package/src/html.ts +137 -0
- package/src/index.ts +32 -0
- package/src/mail.ts +46 -0
- package/src/media.ts +102 -0
- package/src/modes/html.ts +105 -0
- package/src/modes/shared.ts +285 -0
- package/src/send.ts +87 -0
- package/src/transport/imap.ts +418 -0
- package/README.md +0 -3
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The single `html` mode's node, draft and payload types, HTML escaping, media promotion and MIME payload assembly.
|
|
3
|
+
*
|
|
4
|
+
* A mail message is one MIME document, so `assemble` always returns exactly one draft: inline nodes accumulate into
|
|
5
|
+
* the HTML and plain-text parts, while media become `multipart/related` (`cid:`) or `multipart/mixed` attachments. A
|
|
6
|
+
* media element standing in phrasing content is lifted out of the text run and reported as `moved` (nothing is lost,
|
|
7
|
+
* only repositioned), which is the `mail-media-split` entry of the render report.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core'
|
|
11
|
+
import type {
|
|
12
|
+
ModeTypes,
|
|
13
|
+
Prepared,
|
|
14
|
+
RenderContext,
|
|
15
|
+
ResourceKind,
|
|
16
|
+
ResourceRef,
|
|
17
|
+
ResourceMeta,
|
|
18
|
+
SerializeContext,
|
|
19
|
+
} from '@deepseek-ai/cordis-plugin-im-core'
|
|
20
|
+
import { isPhrasingContainer } from '@deepseek-ai/satori-element'
|
|
21
|
+
import type { Element } from '@deepseek-ai/satori-element'
|
|
22
|
+
import type { Config } from '../config.ts'
|
|
23
|
+
import type { MailReferrer } from '../decode.ts'
|
|
24
|
+
import type { StagedMediaExtra } from '../media.ts'
|
|
25
|
+
|
|
26
|
+
/** Media elements a mail message turns into attachments. */
|
|
27
|
+
export type MailMediaTag = 'img' | 'audio' | 'video' | 'file'
|
|
28
|
+
|
|
29
|
+
/** One node a mode handler produces. */
|
|
30
|
+
export type MailNode =
|
|
31
|
+
/** A run of inline HTML plus its plain-text projection. */
|
|
32
|
+
| { type: 'inline'; html: string; text: string }
|
|
33
|
+
/** One attachment; the HTML position is a `cid:` reference for inline media and nothing for a file. */
|
|
34
|
+
| { type: 'media'; tag: MailMediaTag; ref: ResourceRef }
|
|
35
|
+
|
|
36
|
+
/** One mail message before resource resolution. */
|
|
37
|
+
export interface MailDraft {
|
|
38
|
+
/** Inline runs and media, in source order. */
|
|
39
|
+
nodes: MailNode[]
|
|
40
|
+
/** From `<quote id>`: the `Message-ID` this message answers. */
|
|
41
|
+
quote?: string
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** One attachment of a serialized payload. */
|
|
45
|
+
export interface MailPayloadAttachment {
|
|
46
|
+
/** Staged resource key (`sendPayload` reads the bytes back by it). */
|
|
47
|
+
key: string
|
|
48
|
+
/** Content-ID of an inline media attachment; absent on a plain file attachment. */
|
|
49
|
+
cid?: string
|
|
50
|
+
filename: string
|
|
51
|
+
contentType: string
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Exactly what one `sendMail` call carries. */
|
|
55
|
+
export interface MailPayload {
|
|
56
|
+
/** Recipient address (the `private:` prefix already stripped). */
|
|
57
|
+
to: string
|
|
58
|
+
subject: string
|
|
59
|
+
/** The inline markup, wrapped in `<pre>`. */
|
|
60
|
+
html: string
|
|
61
|
+
/** Plain-text alternative, generated from the same node stream. */
|
|
62
|
+
text: string
|
|
63
|
+
/** `In-Reply-To`, with angle brackets. */
|
|
64
|
+
inReplyTo?: string
|
|
65
|
+
/** `References`, oldest first, with angle brackets. */
|
|
66
|
+
references?: string[]
|
|
67
|
+
attachments: MailPayloadAttachment[]
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Mode types of the `html` mode. */
|
|
71
|
+
export interface MailModeTypes extends ModeTypes {
|
|
72
|
+
node: MailNode
|
|
73
|
+
draft: MailDraft
|
|
74
|
+
payload: MailPayload
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Escape character data of the HTML part. */
|
|
78
|
+
export function escapeHtml(text: string): string {
|
|
79
|
+
return text
|
|
80
|
+
.replaceAll('&', '&')
|
|
81
|
+
.replaceAll('<', '<')
|
|
82
|
+
.replaceAll('>', '>')
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Escape one HTML attribute value (double-quoted). */
|
|
86
|
+
export function escapeAttribute(text: string): string {
|
|
87
|
+
return escapeHtml(text).replaceAll('"', '"')
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** One inline node. */
|
|
91
|
+
export function inlineNode(html: string, text: string): MailNode {
|
|
92
|
+
return { type: 'inline', html, text }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The inline HTML and plain text of rendered children, and the nodes that cannot ride inside a text run. */
|
|
96
|
+
export interface InlineRun {
|
|
97
|
+
html: string
|
|
98
|
+
text: string
|
|
99
|
+
rest: MailNode[]
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Split rendered children into one inline run and the media nodes that follow it.
|
|
104
|
+
* @param nodes - rendered children.
|
|
105
|
+
* @returns the concatenated HTML/text of the run and the media nodes, in source order.
|
|
106
|
+
*/
|
|
107
|
+
export function inlineRun(nodes: readonly MailNode[]): InlineRun {
|
|
108
|
+
let html = ''
|
|
109
|
+
let text = ''
|
|
110
|
+
const rest: MailNode[] = []
|
|
111
|
+
for (const node of nodes) {
|
|
112
|
+
if (node.type === 'media') {
|
|
113
|
+
rest.push(node)
|
|
114
|
+
continue
|
|
115
|
+
}
|
|
116
|
+
html += node.html
|
|
117
|
+
text += node.text
|
|
118
|
+
}
|
|
119
|
+
return { html, text, rest }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Wrap the inline text of rendered children in one HTML tag; media of the subtree follows the wrapper.
|
|
124
|
+
* @param tag - the HTML tag name.
|
|
125
|
+
* @param nodes - rendered children.
|
|
126
|
+
* @param textSuffix - plain-text separator this block adds after its inline text.
|
|
127
|
+
* @returns the wrapper (when it has text) and the media nodes.
|
|
128
|
+
*/
|
|
129
|
+
export function inlineWrapper(
|
|
130
|
+
tag: string,
|
|
131
|
+
nodes: readonly MailNode[],
|
|
132
|
+
textSuffix = '',
|
|
133
|
+
): MailNode[] {
|
|
134
|
+
const { html, text, rest } = inlineRun(nodes)
|
|
135
|
+
if (html === '') return rest
|
|
136
|
+
return [inlineNode(`<${tag}>${html}</${tag}>`, text + textSuffix), ...rest]
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Resource kind of one media tag. */
|
|
140
|
+
export function resourceKindOf(tag: MailMediaTag): ResourceKind {
|
|
141
|
+
switch (tag) {
|
|
142
|
+
case 'img':
|
|
143
|
+
return 'image'
|
|
144
|
+
case 'audio':
|
|
145
|
+
return 'audio'
|
|
146
|
+
case 'video':
|
|
147
|
+
return 'video'
|
|
148
|
+
case 'file':
|
|
149
|
+
return 'file'
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Turn one media element into its node.
|
|
155
|
+
*
|
|
156
|
+
* An `http(s)` image, audio or video stays a reference to the remote URL: the recipient's client fetches it, so the
|
|
157
|
+
* sent message carries no copy. Everything else (and every file, which has no markup to carry a URL) is fetched and
|
|
158
|
+
* becomes an attachment. Media lifted out of phrasing content is reported as `moved`.
|
|
159
|
+
*
|
|
160
|
+
* @param el - one of `img`/`audio`/`video`/`file`.
|
|
161
|
+
* @param ctx - render context of the mode.
|
|
162
|
+
* @returns the inline node of a remote media reference, or the media node of an attachment.
|
|
163
|
+
*/
|
|
164
|
+
export function mediaNode(
|
|
165
|
+
el: Element<MailMediaTag>,
|
|
166
|
+
ctx: RenderContext<MailModeTypes>,
|
|
167
|
+
): MailNode {
|
|
168
|
+
const tag = el.type
|
|
169
|
+
const src = el.attrs.src
|
|
170
|
+
if (src === '') {
|
|
171
|
+
throw new PlatformError(
|
|
172
|
+
'invalid',
|
|
173
|
+
`mail: <${tag}> has no src, so nothing could be sent`,
|
|
174
|
+
{ published: 'no' },
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
if (tag !== 'file' && /^https?:\/\//i.test(src)) {
|
|
178
|
+
const escaped = escapeAttribute(src)
|
|
179
|
+
const html =
|
|
180
|
+
tag === 'img'
|
|
181
|
+
? `<img src="${escaped}">`
|
|
182
|
+
: `<${tag} src="${escaped}" controls>`
|
|
183
|
+
return inlineNode(html, '')
|
|
184
|
+
}
|
|
185
|
+
const parent = ctx.ancestors[ctx.ancestors.length - 1]
|
|
186
|
+
if (parent !== undefined && isPhrasingContainer(parent.type)) {
|
|
187
|
+
ctx.report.moved.push({
|
|
188
|
+
tag,
|
|
189
|
+
reason: 'mail-media-split',
|
|
190
|
+
path: ctx.stableId(el),
|
|
191
|
+
})
|
|
192
|
+
}
|
|
193
|
+
const meta: ResourceMeta = {}
|
|
194
|
+
if (el.type === 'audio' || el.type === 'video' || el.type === 'file') {
|
|
195
|
+
if (el.attrs.name !== undefined) meta.name = el.attrs.name
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
type: 'media',
|
|
199
|
+
tag,
|
|
200
|
+
ref: ctx.resource(resourceKindOf(tag), src, meta),
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** The recipient address of a chat id (`private:` is the direct-chat idiom of this platform). */
|
|
205
|
+
function addressOfChat(chatId: string): string {
|
|
206
|
+
const prefix = 'private:'
|
|
207
|
+
return chatId.startsWith(prefix) ? chatId.slice(prefix.length) : chatId
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** The adapter-private reply context of a publication, when the target carries one. */
|
|
211
|
+
function referrerOf(
|
|
212
|
+
target: SerializeContext['target'],
|
|
213
|
+
): MailReferrer | undefined {
|
|
214
|
+
const referrer = target.referrer
|
|
215
|
+
if (typeof referrer !== 'object' || referrer === null) return undefined
|
|
216
|
+
return referrer as MailReferrer
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** Fold a node stream into the one draft a mail message is. */
|
|
220
|
+
export function assembleDraft(nodes: MailNode[], quote?: string): MailDraft[] {
|
|
221
|
+
return [{ nodes, ...(quote === undefined ? {} : { quote }) }]
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Build the `sendMail` options of one draft.
|
|
226
|
+
*
|
|
227
|
+
* The recipient is the chat's address, unless the message being answered named a `Reply-To`. A reply carries
|
|
228
|
+
* `In-Reply-To` (the quoted `Message-ID`) and `References` (the answered message's chain plus that id), and its
|
|
229
|
+
* subject is the answered subject with one `Re: ` prefix.
|
|
230
|
+
*/
|
|
231
|
+
export function serializeDraft(
|
|
232
|
+
prepared: Prepared<MailDraft>,
|
|
233
|
+
ctx: SerializeContext,
|
|
234
|
+
): MailPayload {
|
|
235
|
+
const draft = prepared.draft
|
|
236
|
+
const config = ctx.bot.config as Config
|
|
237
|
+
const referrer = referrerOf(ctx.target)
|
|
238
|
+
const quote = draft.quote ?? ctx.quote
|
|
239
|
+
let html = ''
|
|
240
|
+
let text = ''
|
|
241
|
+
const attachments: MailPayloadAttachment[] = []
|
|
242
|
+
|
|
243
|
+
for (const node of draft.nodes) {
|
|
244
|
+
if (node.type === 'inline') {
|
|
245
|
+
html += node.html
|
|
246
|
+
text += node.text
|
|
247
|
+
continue
|
|
248
|
+
}
|
|
249
|
+
const resolved = prepared.resolved.get(node.ref)
|
|
250
|
+
if (resolved === undefined)
|
|
251
|
+
throw new Error('mail: an attachment was not prepared before serialize')
|
|
252
|
+
const extra = resolved.extra as StagedMediaExtra | undefined
|
|
253
|
+
const cid = node.tag === 'file' ? undefined : resolved.key
|
|
254
|
+
if (cid !== undefined) {
|
|
255
|
+
const escaped = escapeAttribute(cid)
|
|
256
|
+
html +=
|
|
257
|
+
node.tag === 'img'
|
|
258
|
+
? `<img src="cid:${escaped}">`
|
|
259
|
+
: `<${node.tag} src="cid:${escaped}" controls>`
|
|
260
|
+
}
|
|
261
|
+
const suggested = node.ref.meta.name ?? extra?.name
|
|
262
|
+
attachments.push({
|
|
263
|
+
key: resolved.key,
|
|
264
|
+
...(cid === undefined ? {} : { cid }),
|
|
265
|
+
filename:
|
|
266
|
+
suggested !== undefined && suggested !== ''
|
|
267
|
+
? suggested
|
|
268
|
+
: `attachment-${attachments.length + 1}`,
|
|
269
|
+
contentType:
|
|
270
|
+
node.ref.meta.mime ?? extra?.mime ?? 'application/octet-stream',
|
|
271
|
+
})
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return {
|
|
275
|
+
to: referrer?.replyTo ?? addressOfChat(ctx.target.chat.id),
|
|
276
|
+
subject: referrer?.subject ? `Re: ${referrer.subject}` : config.subject,
|
|
277
|
+
html: `<pre>${html}</pre>`,
|
|
278
|
+
text: text.trim() === '' ? '' : text,
|
|
279
|
+
...(quote === undefined ? {} : { inReplyTo: quote }),
|
|
280
|
+
...(quote === undefined || referrer === undefined
|
|
281
|
+
? {}
|
|
282
|
+
: { references: [...(referrer.references ?? []), quote] }),
|
|
283
|
+
attachments,
|
|
284
|
+
}
|
|
285
|
+
}
|
package/src/send.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core'
|
|
2
|
+
import type {
|
|
3
|
+
SendContext,
|
|
4
|
+
SentPart,
|
|
5
|
+
Target,
|
|
6
|
+
} from '@deepseek-ai/cordis-plugin-im-core'
|
|
7
|
+
import type { Config } from './config.ts'
|
|
8
|
+
import type { MediaStaging } from './media.ts'
|
|
9
|
+
import type { MailPayload } from './modes/shared.ts'
|
|
10
|
+
import type { Attachment, SendOptions, SMTP } from './mail.ts'
|
|
11
|
+
|
|
12
|
+
/** Everything `sendPayload` needs from the adapter. */
|
|
13
|
+
export interface MailSendDeps {
|
|
14
|
+
readonly config: Config
|
|
15
|
+
readonly staging: MediaStaging
|
|
16
|
+
readonly smtp: SMTP
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Publish one logical message as one SMTP transaction.
|
|
21
|
+
*
|
|
22
|
+
* The staged bytes are read before sending and released only after the server accepted the message, so a failed or
|
|
23
|
+
* retried send still finds them. `confirmed` means the server answered `2xx` to `DATA` — the message left this host,
|
|
24
|
+
* not that its recipient ever receives it.
|
|
25
|
+
*
|
|
26
|
+
* @param deps - staging and the SMTP transport.
|
|
27
|
+
* @param payload - the serialized message.
|
|
28
|
+
* @param _target - the chat being published to (the payload already names its recipient).
|
|
29
|
+
* @param _ctx - send context (the quote is already part of the payload).
|
|
30
|
+
* @returns the published part; `id` is nodemailer's generated `Message-ID`.
|
|
31
|
+
*/
|
|
32
|
+
export async function sendPayload(
|
|
33
|
+
deps: MailSendDeps,
|
|
34
|
+
payload: MailPayload,
|
|
35
|
+
_target: Target,
|
|
36
|
+
_ctx: SendContext,
|
|
37
|
+
): Promise<SentPart> {
|
|
38
|
+
const attachments: Attachment[] = []
|
|
39
|
+
for (const attachment of payload.attachments) {
|
|
40
|
+
const staged = deps.staging.get(attachment.key)
|
|
41
|
+
if (staged === undefined) {
|
|
42
|
+
throw new PlatformError(
|
|
43
|
+
'invalid',
|
|
44
|
+
`mail: the attachment "${attachment.filename}" is no longer staged, nothing was sent`,
|
|
45
|
+
{ published: 'no' },
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
attachments.push({
|
|
49
|
+
filename: attachment.filename,
|
|
50
|
+
content: Buffer.from(staged.data),
|
|
51
|
+
contentType: attachment.contentType,
|
|
52
|
+
...(attachment.cid === undefined ? {} : { cid: attachment.cid }),
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
const options: SendOptions = {
|
|
56
|
+
to: payload.to,
|
|
57
|
+
subject: payload.subject,
|
|
58
|
+
html: payload.html,
|
|
59
|
+
text: payload.text,
|
|
60
|
+
...(payload.inReplyTo === undefined
|
|
61
|
+
? {}
|
|
62
|
+
: { inReplyTo: payload.inReplyTo }),
|
|
63
|
+
...(payload.references === undefined
|
|
64
|
+
? {}
|
|
65
|
+
: { references: payload.references }),
|
|
66
|
+
...(attachments.length === 0 ? {} : { attachments }),
|
|
67
|
+
}
|
|
68
|
+
const id = await deps.smtp.send(options)
|
|
69
|
+
// The server already accepted the body, so an answer without a Message-ID is a published outcome, not a refusal.
|
|
70
|
+
if (!id) {
|
|
71
|
+
throw new PlatformError(
|
|
72
|
+
'invalid',
|
|
73
|
+
'mail: the server accepted the message without a Message-ID',
|
|
74
|
+
{
|
|
75
|
+
published: 'unknown',
|
|
76
|
+
},
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
for (const attachment of payload.attachments)
|
|
80
|
+
deps.staging.take(attachment.key)
|
|
81
|
+
return {
|
|
82
|
+
id,
|
|
83
|
+
kind: 'message',
|
|
84
|
+
time: Date.now(),
|
|
85
|
+
referrer: { address: payload.to, messageId: id, subject: payload.subject },
|
|
86
|
+
}
|
|
87
|
+
}
|