@ssamurias/dsh-fish 0.9.9 → 1.0.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/AGENTS.md +61 -24
- package/CHANGELOG.md +615 -392
- package/README.md +123 -165
- package/RELEASE-NOTES-0.8.3.md +71 -71
- package/RELEASE-NOTES-0.9.9.md +25 -53
- package/RELEASE-NOTES-1.0.0.md +196 -0
- package/RELEASE-NOTES-1.0.1.md +363 -0
- package/THIRD-PARTY-NOTICES.md +13 -3
- package/lib/appoint.js +5 -4
- package/lib/ask.js +4 -3
- package/lib/at.js +8 -7
- package/lib/autobackup.js +384 -0
- package/lib/backup.js +27 -4
- package/lib/bili.js +133 -22
- package/lib/browse.js +51 -18
- package/lib/capture.js +28 -4
- package/lib/client.js +1231 -317
- package/lib/cloudbackup.js +351 -0
- package/lib/cloudcrypto.js +161 -0
- package/lib/clouddrive.js +587 -0
- package/lib/cloudsmtp.js +331 -0
- package/lib/cloudtargets.js +227 -0
- package/lib/comment.js +28 -10
- package/lib/compact.js +36 -8
- package/lib/contract.js +266 -0
- package/lib/curiosity.js +3 -2
- package/lib/deliver.js +6 -3
- package/lib/derive.js +152 -7
- package/lib/desk.js +1 -1
- package/lib/doing.js +316 -0
- package/lib/events.js +79 -31
- package/lib/firstwake.js +74 -0
- package/lib/generator.js +35 -4
- package/lib/guard.js +184 -184
- package/lib/health.js +36 -0
- package/lib/home.js +49 -2
- package/lib/hook.js +1 -1
- package/lib/identity.js +451 -36
- package/lib/import.js +302 -302
- package/lib/index.js +18 -2
- package/lib/intent.js +5 -3
- package/lib/keep.js +3 -2
- package/lib/keepsake.js +3 -2
- package/lib/letter.js +3 -3
- package/lib/look.js +52 -4
- package/lib/material.js +69 -2
- package/lib/note.js +193 -36
- package/lib/observe.js +366 -37
- package/lib/persona.js +240 -93
- package/lib/pocket.js +2 -2
- package/lib/proactive.js +19 -11
- package/lib/pronoun.js +75 -35
- package/lib/pronounask.js +11 -2
- package/lib/quality.js +1 -1
- package/lib/readme.js +1 -1
- package/lib/recall.js +207 -27
- package/lib/report.js +14 -7
- package/lib/restrict.js +27 -1
- package/lib/say.js +14 -13
- package/lib/scheduler.js +255 -35
- package/lib/schema.js +24 -0
- package/lib/selfcheck.js +13 -1
- package/lib/selfdoc.js +19 -8
- package/lib/selfpronoun.js +112 -0
- package/lib/sessionnote.js +44 -0
- package/lib/settings.js +149 -4
- package/lib/sources.js +99 -0
- package/lib/speak.js +1 -1
- package/lib/speechanchor.js +132 -0
- package/lib/stance.js +4 -3
- package/lib/status.js +16 -8
- package/lib/sub.js +424 -0
- package/lib/talkmem.js +3 -2
- package/lib/turnexport.js +123 -0
- package/lib/turns.js +70 -24
- package/lib/use.js +21 -1
- package/lib/voices.js +24 -1
- package/lib/wish.js +3 -2
- package/media/principle-drive.svg +131 -0
- package/media/principle-loop.svg +3 -3
- package/media/principle-memory.svg +1 -1
- package/package.json +3 -3
- package/tools/check-install.mjs +237 -0
- package/RELEASE.md +0 -154
- package/media/principle-loop-2x.png +0 -0
- package/media/principle-memory-2x.png +0 -0
package/lib/cloudsmtp.js
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
// 一个够用的 SMTP 客户端(2026-11-14,用户 m01090)
|
|
2
|
+
//
|
|
3
|
+
// 为什么自己写:插件是零依赖的(package.json 里三个 dependencies 全空),
|
|
4
|
+
// 装 nodemailer 违反那条铁律;而发一封带一个附件的信,用 node:net / node:tls 写两百行就够。
|
|
5
|
+
//
|
|
6
|
+
// 只做三件必要的事:
|
|
7
|
+
// 1. 465 直连 TLS,587/25 先明文再 STARTTLS(大多数邮箱走这两条之一)。
|
|
8
|
+
// 2. AUTH LOGIN(QQ/163/Gmail/Outlook 都支持;AUTH PLAIN 也顺手支持)。
|
|
9
|
+
// 3. 一封 multipart/mixed、一个 base64 附件。
|
|
10
|
+
// 不做:HTML 正文、多收件人分开发、DKIM、投递回执。用不上,也不该在这里长出来。
|
|
11
|
+
|
|
12
|
+
import { connect as tlsConnect } from 'node:tls'
|
|
13
|
+
import { connect as netConnect } from 'node:net'
|
|
14
|
+
import { hostname } from 'node:os'
|
|
15
|
+
|
|
16
|
+
const CRLF = '\r\n'
|
|
17
|
+
|
|
18
|
+
/** 一句话的错误:界面会原样显示。 */
|
|
19
|
+
class SMTPError extends Error {}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 把「服务器说了什么」翻译成人话。
|
|
23
|
+
* SMTP 的错误码对普通用户没有意义,但把它附在后面能让人拿去搜索。
|
|
24
|
+
*/
|
|
25
|
+
function explain(code, text) {
|
|
26
|
+
const t = String(text ?? '').trim()
|
|
27
|
+
const low = t.toLowerCase()
|
|
28
|
+
if (code === 535 || low.includes('auth') && low.includes('fail')) {
|
|
29
|
+
return `登录被拒了(${code})。多半是用了登录密码——这里要的是邮箱设置里那个「授权码 / 应用专用密码」。`
|
|
30
|
+
}
|
|
31
|
+
if (code === 534 || low.includes('application-specific')) {
|
|
32
|
+
return `邮箱要求用「授权码 / 应用专用密码」(${code}),不能用登录密码。`
|
|
33
|
+
}
|
|
34
|
+
if (code === 550 || code === 553) return `收件地址被拒了(${code}):${t}`
|
|
35
|
+
if (code === 552) return `附件太大了,这家邮箱收不下(${code})。`
|
|
36
|
+
if (code === 421 || code === 450 || code === 451) return `服务器暂时不收(${code}),过一会儿再试。`
|
|
37
|
+
return `${code} ${t}`
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 一条连接:负责把字节写进去、把服务器的话按行读回来。 */
|
|
41
|
+
class Conn {
|
|
42
|
+
constructor(socket) {
|
|
43
|
+
this.socket = socket
|
|
44
|
+
this.buf = ''
|
|
45
|
+
this.waiters = []
|
|
46
|
+
this.err = null
|
|
47
|
+
socket.setEncoding('utf8')
|
|
48
|
+
socket.on('data', (chunk) => {
|
|
49
|
+
this.buf += chunk
|
|
50
|
+
this.pump()
|
|
51
|
+
})
|
|
52
|
+
socket.on('error', (e) => {
|
|
53
|
+
this.err = e
|
|
54
|
+
this.pump()
|
|
55
|
+
})
|
|
56
|
+
socket.on('close', () => {
|
|
57
|
+
this.err = this.err ?? new Error('连接被对方关掉了')
|
|
58
|
+
this.pump()
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
pump() {
|
|
63
|
+
while (this.waiters.length) {
|
|
64
|
+
const w = this.waiters[0]
|
|
65
|
+
const r = this.takeReply()
|
|
66
|
+
if (r) {
|
|
67
|
+
this.waiters.shift()
|
|
68
|
+
w.resolve(r)
|
|
69
|
+
} else if (this.err) {
|
|
70
|
+
this.waiters.shift()
|
|
71
|
+
w.reject(this.err)
|
|
72
|
+
} else return
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 读一条完整回复(多行回复以「码-」续行,最后一行是「码 空格」)。 */
|
|
77
|
+
takeReply() {
|
|
78
|
+
const lines = this.buf.split(CRLF)
|
|
79
|
+
let code = null
|
|
80
|
+
let text = ''
|
|
81
|
+
let consumed = 0
|
|
82
|
+
for (let i = 0; i < lines.length; i++) {
|
|
83
|
+
const line = lines[i]
|
|
84
|
+
if (line.length < 3) break
|
|
85
|
+
const m = /^(\d{3})([ -])(.*)$/.exec(line)
|
|
86
|
+
if (!m) break
|
|
87
|
+
code = Number(m[1])
|
|
88
|
+
text += `${m[3]}${CRLF}`
|
|
89
|
+
consumed += line.length + CRLF.length
|
|
90
|
+
if (m[2] === ' ') {
|
|
91
|
+
this.buf = this.buf.slice(consumed)
|
|
92
|
+
return { code, text: text.trim() }
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return null
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
reply(timeoutMs = 30_000) {
|
|
99
|
+
return new Promise((resolve, reject) => {
|
|
100
|
+
const w = { resolve, reject }
|
|
101
|
+
this.waiters.push(w)
|
|
102
|
+
const t = setTimeout(() => {
|
|
103
|
+
const i = this.waiters.indexOf(w)
|
|
104
|
+
if (i >= 0) this.waiters.splice(i, 1)
|
|
105
|
+
reject(new SMTPError('服务器太久没回话(30 秒)。网络、代理、或者端口不对。'))
|
|
106
|
+
}, timeoutMs)
|
|
107
|
+
const done = (fn) => (v) => { clearTimeout(t); fn(v) }
|
|
108
|
+
w.resolve = done(resolve)
|
|
109
|
+
w.reject = done(reject)
|
|
110
|
+
this.pump()
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
write(s) {
|
|
115
|
+
this.socket.write(s)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
end() {
|
|
119
|
+
try { this.socket.end() } catch {}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function base64(s) {
|
|
124
|
+
return Buffer.from(String(s), 'utf8').toString('base64')
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** 点号转义:正文里以 . 开头的行要变成 ..,否则会被当成提前结束。 */
|
|
128
|
+
function dotStuff(s) {
|
|
129
|
+
return String(s).replace(/\r?\n/g, CRLF).replace(/^\./gm, '..')
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function encWord(s) {
|
|
133
|
+
// 中文主题要按 RFC 2047 编码,不然 Outlook 会显示成乱码。
|
|
134
|
+
const text = String(s ?? '')
|
|
135
|
+
if (/^[\x20-\x7e]*$/.test(text)) return text
|
|
136
|
+
return `=?UTF-8?B?${Buffer.from(text, 'utf8').toString('base64')}?=`
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function wrap76(b64) {
|
|
140
|
+
const out = []
|
|
141
|
+
for (let i = 0; i < b64.length; i += 76) out.push(b64.slice(i, i + 76))
|
|
142
|
+
return out.join(CRLF)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 拼一封信。attachments: [{name, data: Buffer}]。
|
|
147
|
+
* Content-Type 里带 name 时要做编码,但大多数客户端靠 filename* 也能认。
|
|
148
|
+
*/
|
|
149
|
+
export function buildMessage({ from, to, subject, text, attachments = [] }) {
|
|
150
|
+
const boundary = `dshfish-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`
|
|
151
|
+
const head = [
|
|
152
|
+
`From: <${from}>`,
|
|
153
|
+
`To: ${to.map((x) => `<${x}>`).join(', ')}`,
|
|
154
|
+
`Subject: ${encWord(subject)}`,
|
|
155
|
+
`Date: ${new Date().toUTCString().replace('GMT', '+0000')}`,
|
|
156
|
+
`Message-ID: <${Date.now().toString(36)}.${Math.random().toString(36).slice(2, 8)}@dsh-fish>`,
|
|
157
|
+
'MIME-Version: 1.0',
|
|
158
|
+
`Content-Type: multipart/mixed; boundary="${boundary}"`,
|
|
159
|
+
'',
|
|
160
|
+
'这一封是自动发出的备份。',
|
|
161
|
+
'',
|
|
162
|
+
`--${boundary}`,
|
|
163
|
+
'Content-Type: text/plain; charset=utf-8',
|
|
164
|
+
'Content-Transfer-Encoding: base64',
|
|
165
|
+
'',
|
|
166
|
+
wrap76(Buffer.from(String(text ?? ''), 'utf8').toString('base64')),
|
|
167
|
+
]
|
|
168
|
+
for (const a of attachments) {
|
|
169
|
+
const name = String(a.name ?? 'backup.json')
|
|
170
|
+
head.push(
|
|
171
|
+
`--${boundary}`,
|
|
172
|
+
`Content-Type: application/octet-stream; name="${encWord(name)}"`,
|
|
173
|
+
'Content-Transfer-Encoding: base64',
|
|
174
|
+
`Content-Disposition: attachment; filename="${encWord(name)}"`,
|
|
175
|
+
'',
|
|
176
|
+
wrap76(Buffer.from(a.data ?? Buffer.alloc(0)).toString('base64')),
|
|
177
|
+
)
|
|
178
|
+
}
|
|
179
|
+
head.push(`--${boundary}--`, '')
|
|
180
|
+
return head.join(CRLF)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export class SMTPSender {
|
|
184
|
+
constructor({ host, port = 465, user = '', pass = '', timeoutMs = 60_000, log = () => {} } = {}) {
|
|
185
|
+
this.host = String(host ?? '').trim()
|
|
186
|
+
this.port = Number(port) || 465
|
|
187
|
+
this.user = String(user ?? '').trim()
|
|
188
|
+
this.pass = String(pass ?? '')
|
|
189
|
+
this.timeoutMs = timeoutMs
|
|
190
|
+
this.log = log
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
connect() {
|
|
194
|
+
return new Promise((resolve, reject) => {
|
|
195
|
+
const sock = this.port === 465
|
|
196
|
+
? tlsConnect({ host: this.host, port: this.port, servername: this.host, timeout: this.timeoutMs })
|
|
197
|
+
: netConnect({ host: this.host, port: this.port, timeout: this.timeoutMs })
|
|
198
|
+
let settled = false
|
|
199
|
+
const fail = (e) => {
|
|
200
|
+
if (settled) return
|
|
201
|
+
settled = true
|
|
202
|
+
try { sock.destroy() } catch {}
|
|
203
|
+
reject(new SMTPError(this.friendly(e)))
|
|
204
|
+
}
|
|
205
|
+
sock.once('error', fail)
|
|
206
|
+
sock.once('timeout', () => fail(new Error('连不上')))
|
|
207
|
+
sock.once('connect', () => {
|
|
208
|
+
if (settled) return
|
|
209
|
+
// 465 上还要等 TLS 握手完;587 连上就能说话。
|
|
210
|
+
if (this.port === 465) {
|
|
211
|
+
sock.once('secureConnect', () => {
|
|
212
|
+
if (settled) return
|
|
213
|
+
settled = true
|
|
214
|
+
sock.removeListener('error', fail)
|
|
215
|
+
resolve(new Conn(sock))
|
|
216
|
+
})
|
|
217
|
+
} else {
|
|
218
|
+
settled = true
|
|
219
|
+
sock.removeListener('error', fail)
|
|
220
|
+
resolve(new Conn(sock))
|
|
221
|
+
}
|
|
222
|
+
})
|
|
223
|
+
})
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
friendly(e) {
|
|
227
|
+
const code = e?.code ?? ''
|
|
228
|
+
if (code === 'ENOTFOUND' || code === 'EAI_AGAIN') return `找不到服务器「${this.host}」,检查一下拼写。`
|
|
229
|
+
if (code === 'ECONNREFUSED') return `「${this.host}:${this.port}」不肯连——端口填错了,或者这家邮箱不支持这个端口。`
|
|
230
|
+
if (code === 'ETIMEDOUT') return '连接超时。检查网络,或者这家邮箱在本机网络下要开代理。'
|
|
231
|
+
if (code === 'CERT_HAS_EXPIRED' || String(e?.message ?? '').includes('certificate')) return 'TLS 证书有问题,可能是被中间人拦了。'
|
|
232
|
+
return `连不上:${e?.message ?? e}`
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** 登录一次就断。给「测一下」按钮用。 */
|
|
236
|
+
async verify() {
|
|
237
|
+
const c = await this.connect()
|
|
238
|
+
try {
|
|
239
|
+
await this.hello(c)
|
|
240
|
+
await this.auth(c)
|
|
241
|
+
return 'ok'
|
|
242
|
+
} finally {
|
|
243
|
+
try { c.write('QUIT' + CRLF) } catch {}
|
|
244
|
+
setTimeout(() => c.end(), 200).unref?.()
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async hello(c) {
|
|
249
|
+
const greet = await c.reply()
|
|
250
|
+
if (greet.code !== 220) throw new SMTPError(explain(greet.code, greet.text))
|
|
251
|
+
const name = hostname().replace(/[^\w.-]/g, '') || 'localhost'
|
|
252
|
+
c.write(`EHLO ${name}${CRLF}`)
|
|
253
|
+
let ehlo = await c.reply()
|
|
254
|
+
if (ehlo.code >= 400 || ehlo.code < 200) {
|
|
255
|
+
// 有的老服务器只认 HELO。
|
|
256
|
+
c.write(`HELO ${name}${CRLF}`)
|
|
257
|
+
ehlo = await c.reply()
|
|
258
|
+
}
|
|
259
|
+
if (ehlo.code !== 250) throw new SMTPError(explain(ehlo.code, ehlo.text))
|
|
260
|
+
// 587/25 上如果没有 STARTTLS 就明文发,等于把授权码和备份裸奔在网上——宁可不发。
|
|
261
|
+
if (this.port !== 465) {
|
|
262
|
+
if (!/\bSTARTTLS\b/i.test(ehlo.text)) {
|
|
263
|
+
throw new SMTPError(`这家服务器不同意加密(没有 STARTTLS)。为了不让授权码和备份明文上路,这里不发。换成 465 端口试试。`)
|
|
264
|
+
}
|
|
265
|
+
c.write('STARTTLS' + CRLF)
|
|
266
|
+
const st = await c.reply()
|
|
267
|
+
if (st.code !== 220) throw new SMTPError(explain(st.code, st.text))
|
|
268
|
+
await new Promise((resolve, reject) => {
|
|
269
|
+
const sock = c.socket
|
|
270
|
+
// net.Socket 换不了加密层,只能拿 TLS 包一层上去;包好之后这条连接的读写都走它。
|
|
271
|
+
const tls = tlsConnect({ socket: sock, servername: this.host, timeout: this.timeoutMs }, () => resolve())
|
|
272
|
+
tls.once('error', reject)
|
|
273
|
+
c.socket = tls
|
|
274
|
+
c.buf = ''
|
|
275
|
+
c.waiters = []
|
|
276
|
+
tls.setEncoding('utf8')
|
|
277
|
+
tls.on('data', (chunk) => { c.buf += chunk; c.pump() })
|
|
278
|
+
tls.on('error', (e) => { c.err = e; c.pump() })
|
|
279
|
+
tls.on('close', () => { c.err = c.err ?? new Error('连接被对方关掉了'); c.pump() })
|
|
280
|
+
})
|
|
281
|
+
const name2 = hostname().replace(/[^\w.-]/g, '') || 'localhost'
|
|
282
|
+
c.write(`EHLO ${name2}${CRLF}`)
|
|
283
|
+
const again = await c.reply()
|
|
284
|
+
if (again.code !== 250) throw new SMTPError(explain(again.code, again.text))
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
async auth(c) {
|
|
289
|
+
if (!this.user) return
|
|
290
|
+
c.write('AUTH LOGIN' + CRLF)
|
|
291
|
+
const a1 = await c.reply()
|
|
292
|
+
if (a1.code !== 334) throw new SMTPError(explain(a1.code, a1.text))
|
|
293
|
+
c.write(base64(this.user) + CRLF)
|
|
294
|
+
const a2 = await c.reply()
|
|
295
|
+
if (a2.code !== 334) throw new SMTPError(explain(a2.code, a2.text))
|
|
296
|
+
c.write(base64(this.pass) + CRLF)
|
|
297
|
+
const a3 = await c.reply()
|
|
298
|
+
if (a3.code !== 235) throw new SMTPError(explain(a3.code, a3.text))
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
async send({ from, to, subject, text, attachments = [] }) {
|
|
302
|
+
const c = await this.connect()
|
|
303
|
+
try {
|
|
304
|
+
await this.hello(c)
|
|
305
|
+
await this.auth(c)
|
|
306
|
+
c.write(`MAIL FROM:<${from}>${CRLF}`)
|
|
307
|
+
const m = await c.reply()
|
|
308
|
+
if (m.code !== 250) throw new SMTPError(explain(m.code, m.text))
|
|
309
|
+
for (const r of to) {
|
|
310
|
+
c.write(`RCPT TO:<${r}>${CRLF}`)
|
|
311
|
+
const rr = await c.reply()
|
|
312
|
+
if (rr.code !== 250 && rr.code !== 251) throw new SMTPError(explain(rr.code, rr.text))
|
|
313
|
+
}
|
|
314
|
+
c.write('DATA' + CRLF)
|
|
315
|
+
const d = await c.reply()
|
|
316
|
+
if (d.code !== 354) throw new SMTPError(explain(d.code, d.text))
|
|
317
|
+
const msg = buildMessage({ from, to, subject, text, attachments })
|
|
318
|
+
c.write(dotStuff(msg))
|
|
319
|
+
if (!msg.endsWith(CRLF)) c.write(CRLF)
|
|
320
|
+
c.write('.' + CRLF)
|
|
321
|
+
const done = await c.reply(180_000)
|
|
322
|
+
if (done.code !== 250) throw new SMTPError(explain(done.code, done.text))
|
|
323
|
+
return { ok: true, id: done.text }
|
|
324
|
+
} finally {
|
|
325
|
+
try { c.write('QUIT' + CRLF) } catch {}
|
|
326
|
+
setTimeout(() => c.end(), 200).unref?.()
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export { SMTPError }
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
// 云端备份的目标形状(2026-11-14,用户 m01090)
|
|
2
|
+
//
|
|
3
|
+
// 这一份是**唯一的真源**:设置页的表单、后端的校验、配置的默认值,全都从下面这张表来。
|
|
4
|
+
// 以前吃过「界面加了字段、后端白名单没登记」的亏(backup 组那次),
|
|
5
|
+
// 所以这里刻意做成「加一个字段只改一处」的形状。
|
|
6
|
+
//
|
|
7
|
+
// 设计上只有两件事要守住:
|
|
8
|
+
// 1. 目标是一张列表,用户可以加任意多个、任意组合。不是「选一个渠道」。
|
|
9
|
+
// 2. 每个目标自己带 enabled。挂掉一个不影响别的,这就是「降级」的最小单位。
|
|
10
|
+
|
|
11
|
+
/** 字段类型:text 单行、pass 密码、num 数字、bool 开关、area 多行。 */
|
|
12
|
+
const F = (key, label, type, extra = {}) => ({ key, label, type, ...extra })
|
|
13
|
+
|
|
14
|
+
export const KINDS = {
|
|
15
|
+
folder: {
|
|
16
|
+
label: '一个文件夹',
|
|
17
|
+
short: '文件夹',
|
|
18
|
+
steps: '选一个文件夹。如果它正好是你网盘客户端的同步目录(坚果云、OneDrive、iCloud、123、夸克都能设),那这一份就自动跟着同步上去了——不需要任何账号或接口。',
|
|
19
|
+
fields: [
|
|
20
|
+
F('dir', '哪个文件夹', 'text', { required: true, placeholder: 'D:\\我的网盘\\大肥鱼备份', hint: '绝对路径。目录不存在会自动建。' }),
|
|
21
|
+
],
|
|
22
|
+
},
|
|
23
|
+
git: {
|
|
24
|
+
label: '一个 Git 仓库',
|
|
25
|
+
short: '仓库',
|
|
26
|
+
steps: '在 GitHub / Gitee / GitLab / Gitea 上建一个**私有**仓库(不要公开,包里是她的身份和登录凭据),然后生成一个有写权限的令牌粘进来。',
|
|
27
|
+
fields: [
|
|
28
|
+
F('repo', '仓库', 'text', { required: true, placeholder: '你的名字/dafish-backup', hint: '写成「用户名/仓库名」,或者直接粘仓库网页地址。' }),
|
|
29
|
+
F('token', '访问令牌', 'pass', { required: true, hint: '只勾仓库读写权限就够,别给全权限。令牌只存在本机。' }),
|
|
30
|
+
F('api', '接口地址', 'text', { placeholder: '留空按仓库地址自动判断', hint: 'GitHub 是 https://api.github.com,Gitee 是 https://gitee.com/api/v5。自建 Gitea 填自己的。' }),
|
|
31
|
+
F('branch', '分支', 'text', { placeholder: 'main', hint: '留空用 main;Gitee 默认分支常叫 master。' }),
|
|
32
|
+
F('dir', '仓库里的目录', 'text', { placeholder: 'backups', hint: '留空就放仓库根目录。' }),
|
|
33
|
+
],
|
|
34
|
+
},
|
|
35
|
+
webdav: {
|
|
36
|
+
label: '一个 WebDAV 目录',
|
|
37
|
+
short: 'WebDAV',
|
|
38
|
+
steps: '坚果云、123 云盘、InfiniCLOUD、Nextcloud、群晖、Alist 都支持 WebDAV。用「应用密码」而不是登录密码——网页密码改了应用密码还在,泄露了也能单独吊销。',
|
|
39
|
+
fields: [
|
|
40
|
+
F('url', 'WebDAV 地址', 'text', { required: true, placeholder: 'https://dav.jianguoyun.com/dav/', hint: '填到你要用的那一层目录。' }),
|
|
41
|
+
F('user', '用户名', 'text', { required: true, hint: '坚果云是登录邮箱。' }),
|
|
42
|
+
F('pass', '应用密码', 'pass', { required: true, hint: '在各家的「安全设置 / 应用密码」里生成,不是登录密码。' }),
|
|
43
|
+
F('dir', '目录', 'text', { placeholder: 'dafish', hint: '在 WebDAV 地址下面再建一层。留空直接放上去。' }),
|
|
44
|
+
],
|
|
45
|
+
},
|
|
46
|
+
smtp: {
|
|
47
|
+
label: '发到邮箱',
|
|
48
|
+
short: '邮箱',
|
|
49
|
+
steps: '不用注册任何新东西:你自己的邮箱就能收。要去邮箱设置里开 SMTP 并生成一个「授权码」(不是登录密码)。附件上限一般是 50MB,我们的包 8MB 左右够用。',
|
|
50
|
+
fields: [
|
|
51
|
+
F('host', 'SMTP 服务器', 'text', { required: true, placeholder: 'smtp.qq.com', hint: 'QQ 邮箱 smtp.qq.com、163 是 smtp.163.com、Gmail 是 smtp.gmail.com。' }),
|
|
52
|
+
F('port', '端口', 'num', { dflt: 465, lo: 1, hi: 65535, hint: '465 是加密直连,587 是 STARTTLS。' }),
|
|
53
|
+
F('user', '账号', 'text', { required: true, placeholder: 'you@qq.com' }),
|
|
54
|
+
F('pass', '授权码', 'pass', { required: true, hint: '邮箱设置里生成的授权码,不是登录密码。' }),
|
|
55
|
+
F('to', '发到哪个邮箱', 'text', { required: true, hint: '留空就发给自己。可以填多个,用逗号分开。' }),
|
|
56
|
+
F('from', '发件人', 'text', { placeholder: '留空用上面的账号' }),
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
http: {
|
|
60
|
+
label: '自定义 HTTP 接口',
|
|
61
|
+
short: 'HTTP',
|
|
62
|
+
steps: '万能出口:任何能收一个 PUT/POST 的地址都行(你自己的对象存储、别人给的上传链接、内网服务)。请求体里的占位符会被替换成真实内容。',
|
|
63
|
+
fields: [
|
|
64
|
+
F('url', '地址', 'text', { required: true, placeholder: 'https://example.com/upload/{{name}}', hint: '可以用占位符:{{name}} {{file}} {{sha256}} {{size}}。' }),
|
|
65
|
+
F('method', '方法', 'text', { dflt: 'PUT', placeholder: 'PUT', hint: 'PUT 或 POST。' }),
|
|
66
|
+
F('headers', '请求头', 'area', { placeholder: 'Authorization: Bearer xxx\nContent-Type: application/octet-stream', hint: '一行一个,冒号分隔。' }),
|
|
67
|
+
F('bodyFile', '把文件放进请求体', 'bool', { dflt: true, hint: '关掉的话就发下面的文本模板,不发文件本身。' }),
|
|
68
|
+
F('body', '文本模板', 'area', { placeholder: '{"name":"{{name}}","sha256":"{{sha256}}"}', hint: 'bodyFile 关掉时才用。' }),
|
|
69
|
+
],
|
|
70
|
+
},
|
|
71
|
+
command: {
|
|
72
|
+
label: '跑一条命令',
|
|
73
|
+
short: '命令',
|
|
74
|
+
steps: '最强也最危险的出口:本机有 rclone / ossutil / 自己写的脚本,就让它去传。装 rclone 之后一行命令就能接上它支持的七十多种网盘。命令里的占位符会被替换。',
|
|
75
|
+
fields: [
|
|
76
|
+
F('cmd', '命令', 'area', { required: true, placeholder: 'rclone copy "{{file}}" myremote:dafish-backups/', hint: '占位符:{{file}} {{name}} {{sha256}} {{size}}。文件路径会自动加引号之外,其余要自己写对引号。' }),
|
|
77
|
+
F('shell', '用哪个 shell', 'text', { dflt: 'auto', placeholder: 'auto', hint: 'auto / cmd / bash。Windows 上默认 cmd。' }),
|
|
78
|
+
],
|
|
79
|
+
},
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export const KIND_ORDER = ['folder', 'git', 'webdav', 'smtp', 'http', 'command']
|
|
83
|
+
|
|
84
|
+
export function kindInfo(kind) {
|
|
85
|
+
return KINDS[String(kind ?? '')] ?? null
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function kindLabel(kind) {
|
|
89
|
+
return kindInfo(kind)?.short ?? String(kind ?? '')
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** 给界面用的一份完整说明书(字段表直接从上面这张表投影出去)。 */
|
|
93
|
+
export function kindsDoc() {
|
|
94
|
+
return KIND_ORDER.map((k) => ({ kind: k, ...KINDS[k] }))
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function clampNum(v, lo, hi, dflt) {
|
|
98
|
+
const n = Number(v)
|
|
99
|
+
if (!Number.isFinite(n)) return dflt
|
|
100
|
+
return Math.max(lo, Math.min(hi, Math.round(n)))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function cleanId(s) {
|
|
104
|
+
return String(s ?? '').trim().replace(/[^\w-]/g, '').slice(0, 24)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 把一个目标规整成规范形状。
|
|
109
|
+
* 认不出的 kind 返回 null(整条丢掉,不让它占着列表让人以为能用);
|
|
110
|
+
* 必填项为空的**保留**,只是 `ready:false`——用户正在填的时候不该被吞掉。
|
|
111
|
+
*/
|
|
112
|
+
export function normTarget(raw, index = 0) {
|
|
113
|
+
if (!raw || typeof raw !== 'object') return null
|
|
114
|
+
const kind = String(raw.kind ?? '').trim()
|
|
115
|
+
const info = KINDS[kind]
|
|
116
|
+
if (!info) return null
|
|
117
|
+
const out = {
|
|
118
|
+
id: cleanId(raw.id) || `t${index + 1}`,
|
|
119
|
+
kind,
|
|
120
|
+
name: String(raw.name ?? '').trim().slice(0, 40) || info.short,
|
|
121
|
+
enabled: raw.enabled !== false,
|
|
122
|
+
}
|
|
123
|
+
for (const f of info.fields) {
|
|
124
|
+
const v = raw[f.key]
|
|
125
|
+
if (f.type === 'bool') out[f.key] = v === undefined || v === null ? Boolean(f.dflt) : Boolean(v)
|
|
126
|
+
else if (f.type === 'num') out[f.key] = clampNum(v, f.lo ?? 0, f.hi ?? 1e9, f.dflt ?? 0)
|
|
127
|
+
else out[f.key] = String(v ?? f.dflt ?? '').replace(/^\uFEFF/, '').trim()
|
|
128
|
+
}
|
|
129
|
+
out.ready = info.fields.every((f) => !f.required || String(out[f.key] ?? '').trim() !== '')
|
|
130
|
+
out.missing = info.fields.filter((f) => f.required && String(out[f.key] ?? '').trim() === '').map((f) => f.label)
|
|
131
|
+
return out
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function normTargets(raw) {
|
|
135
|
+
const list = Array.isArray(raw) ? raw : []
|
|
136
|
+
const out = []
|
|
137
|
+
const seen = new Set()
|
|
138
|
+
for (let i = 0; i < list.length && out.length < 12; i++) {
|
|
139
|
+
const t = normTarget(list[i], i)
|
|
140
|
+
if (!t) continue
|
|
141
|
+
// id 撞了就改一个:界面上靠它定位,重复会让「删第二个」删到第一个。
|
|
142
|
+
let id = t.id
|
|
143
|
+
let n = 2
|
|
144
|
+
while (seen.has(id)) id = `${t.id}${n++}`
|
|
145
|
+
t.id = id
|
|
146
|
+
seen.add(id)
|
|
147
|
+
out.push(t)
|
|
148
|
+
}
|
|
149
|
+
return out
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** 一个目标缺什么、为什么不能用——一句话,给人看。 */
|
|
153
|
+
export function targetProblem(t) {
|
|
154
|
+
if (!t) return '这个目标不完整。'
|
|
155
|
+
const info = KINDS[t.kind]
|
|
156
|
+
if (!info) return '不认识这种目标。'
|
|
157
|
+
const miss = missingOf(t)
|
|
158
|
+
if (miss.length) return `还差:${miss.join('、')}`
|
|
159
|
+
return ''
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 缺哪些必填项。
|
|
164
|
+
*
|
|
165
|
+
* 优先用 normTarget 算好的 t.missing;**没算过就现算**。
|
|
166
|
+
* 为什么不能只看 t.missing:从 config.json 里直接读出来的那一份、或者调用方手搓的
|
|
167
|
+
* 一个目标,都没有这个字段,`t.missing === undefined` 会让「缺必填」被当成「齐全」,
|
|
168
|
+
* 于是一个空仓库也被判成能用,一路推到 404 才报错。这里现算一遍,判据只有一处。
|
|
169
|
+
*/
|
|
170
|
+
function missingOf(t) {
|
|
171
|
+
if (Array.isArray(t.missing)) return t.missing
|
|
172
|
+
const info = KINDS[t?.kind]
|
|
173
|
+
if (!info) return []
|
|
174
|
+
const out = []
|
|
175
|
+
for (const f of info.fields) {
|
|
176
|
+
if (!f.required || f.type === 'bool') continue
|
|
177
|
+
const v = t[f.key]
|
|
178
|
+
if (v === undefined || v === null || String(v).trim() === '') out.push(f.label)
|
|
179
|
+
}
|
|
180
|
+
return out
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** 设置页上那句话:这个目标大概会怎么用。 */
|
|
184
|
+
export function targetBlurb(t) {
|
|
185
|
+
const info = KINDS[t?.kind]
|
|
186
|
+
return info?.steps ?? ''
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 发给界面的那一份:**密码类字段不回显**,只回一个「填过了没有」。
|
|
191
|
+
*
|
|
192
|
+
* 为什么不让界面拿回原文:令牌、应用密码、授权码都是能直接把账号交出去的东西。
|
|
193
|
+
* 它们已经在 config.json 里躺着(本机文件,绕不开),但没有必要再多流进一次页面 DOM 和
|
|
194
|
+
* 浏览器内存——那一层还多一个「被别的脚本读到」的面。
|
|
195
|
+
* 编辑时密码框留空=不改(由 observe 的 /cloud/target 做合并)。
|
|
196
|
+
*/
|
|
197
|
+
export function publicTarget(t) {
|
|
198
|
+
if (!t) return null
|
|
199
|
+
const info = KINDS[t.kind]
|
|
200
|
+
const out = { id: t.id, kind: t.kind, name: t.name, enabled: t.enabled !== false, ready: Boolean(t.ready), problem: targetProblem(t) }
|
|
201
|
+
const filled = {}
|
|
202
|
+
for (const f of info?.fields ?? []) {
|
|
203
|
+
if (f.type === 'pass') {
|
|
204
|
+
out[f.key] = ''
|
|
205
|
+
filled[f.key] = String(t[f.key] ?? '') !== ''
|
|
206
|
+
} else {
|
|
207
|
+
out[f.key] = t[f.key]
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
out.filled = filled
|
|
211
|
+
return out
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 保存一条目标时把「留空的密码」补回原值。
|
|
216
|
+
* 界面不回显密码,所以它交上来的那一项一定是空串——这时候空串的意思是「不改」,
|
|
217
|
+
* 不是「清掉」。真要清,用户得把它换成一个新值。
|
|
218
|
+
*/
|
|
219
|
+
export function mergeTargetSecrets(next, old) {
|
|
220
|
+
if (!next || !old || next.kind !== old.kind) return next
|
|
221
|
+
const info = KINDS[next.kind]
|
|
222
|
+
for (const f of info?.fields ?? []) {
|
|
223
|
+
if (f.type !== 'pass') continue
|
|
224
|
+
if (String(next[f.key] ?? '') === '' && String(old[f.key] ?? '') !== '') next[f.key] = old[f.key]
|
|
225
|
+
}
|
|
226
|
+
return next
|
|
227
|
+
}
|
package/lib/comment.js
CHANGED
|
@@ -27,6 +27,7 @@ import { sanitizeForeign } from './guard.js'
|
|
|
27
27
|
import { recordGuard } from './guardlog.js'
|
|
28
28
|
import { deriveSelf } from './derive.js'
|
|
29
29
|
import { redactOwner } from './redact.js'
|
|
30
|
+
import { fillOwner } from './pronoun.js'
|
|
30
31
|
|
|
31
32
|
/** 出口脱敏用的名字:派生失败就当没有名字,绝不让一次发布因为派生失败而中断。 */
|
|
32
33
|
function safeOwnerName(events, override) {
|
|
@@ -331,24 +332,41 @@ export async function readMyReplies(o = {}) {
|
|
|
331
332
|
const client = o.client ?? createClient({})
|
|
332
333
|
const events = o.events ?? (await readEvents(root)).events
|
|
333
334
|
const { config } = await loadConfig(root)
|
|
335
|
+
// 2026-11-13(issue #136 B):调用方(bili_look)要能分清「没人回」「没发过评论」
|
|
336
|
+
// 「B 站没让查」——以前这些全被吞成空数组,上头只能一律说成 -412。
|
|
337
|
+
// diag 是可选的输出口:不传就什么也不多算,老调用方行为不变。
|
|
338
|
+
const diag = o.diag ?? null
|
|
339
|
+
const clear = () => { if (diag) { diag.off = false; diag.roots = 0; diag.failed = []; diag.unresolved = 0 } }
|
|
340
|
+
clear()
|
|
334
341
|
// issue #62:断网时也不翻自己的旧评论。
|
|
335
|
-
if (!biliOn(config))
|
|
342
|
+
if (!biliOn(config)) {
|
|
343
|
+
if (diag) diag.off = true
|
|
344
|
+
return []
|
|
345
|
+
}
|
|
336
346
|
const mine = await loginMid(root)
|
|
337
347
|
const owners = await ownerMids(root)
|
|
338
348
|
const wantBvid = String(o.bvid ?? '').trim()
|
|
339
349
|
const out = []
|
|
340
350
|
|
|
341
351
|
const days = Math.max(1, Math.min(REPLY_LOOKBACK_DAYS, Number(o.days ?? REPLY_LOOKBACK_DAYS)))
|
|
352
|
+
if (diag) diag.days = days
|
|
342
353
|
const roots = herCommentsRecent(events, days)
|
|
343
354
|
.filter((c) => !wantBvid || c.bvid === wantBvid)
|
|
344
355
|
.slice(0, MAX_SCAN_ROOTS)
|
|
356
|
+
if (diag) diag.roots = roots.length
|
|
345
357
|
|
|
346
358
|
for (const c of roots) {
|
|
347
359
|
const v = await client.view(c.bvid).catch(() => null)
|
|
348
360
|
const aid = Number(v?.data?.aid) || 0
|
|
349
|
-
if (!aid)
|
|
361
|
+
if (!aid) {
|
|
362
|
+
if (diag) diag.unresolved += 1
|
|
363
|
+
continue
|
|
364
|
+
}
|
|
350
365
|
const r = await client.subReplies(aid, c.rpid, { ps: 20 })
|
|
351
|
-
if (!r
|
|
366
|
+
if (!r?.ok) {
|
|
367
|
+
if (diag) diag.failed.push({ code: r?.code ?? 'FAIL', message: String(r?.message ?? '') })
|
|
368
|
+
continue
|
|
369
|
+
}
|
|
352
370
|
const self = selfIn(mine, r, c.rpid) // 和 scanReplies 同一个判断
|
|
353
371
|
for (const s of r.data ?? []) {
|
|
354
372
|
if (!s.rpid || !s.text) continue
|
|
@@ -379,9 +397,9 @@ export function renderMyReplies(list, { bvid = '' } = {}) {
|
|
|
379
397
|
// 2026-11-07:他的话和路人的话分开摆。原来一律叫「陌生人写的」,
|
|
380
398
|
// 主人自己回一句也被当外人(用户反馈)。这里只陈述事实,判断留给她。
|
|
381
399
|
if (fromOwner.length) {
|
|
382
|
-
lines.push('
|
|
400
|
+
lines.push('{T}在你那条评论下面回了一句。你可以回,也可以不回。', '')
|
|
383
401
|
for (const x of fromOwner.slice(0, 10)) {
|
|
384
|
-
const who = x.user || '
|
|
402
|
+
const who = x.user || '{T}'
|
|
385
403
|
lines.push(`- ${who}(BV ${x.bvid}):${oneLine(x.text, 200)} [reply=${x.rpid} parent=${x.rootRpid}]`)
|
|
386
404
|
}
|
|
387
405
|
}
|
|
@@ -395,7 +413,7 @@ export function renderMyReplies(list, { bvid = '' } = {}) {
|
|
|
395
413
|
lines.push(`- ${who}(BV ${x.bvid}):${oneLine(x.text, 200)} [reply=${x.rpid} parent=${x.rootRpid}]`)
|
|
396
414
|
}
|
|
397
415
|
}
|
|
398
|
-
return lines.join('\n')
|
|
416
|
+
return fillOwner(lines.join('\n'))
|
|
399
417
|
}
|
|
400
418
|
|
|
401
419
|
/**
|
|
@@ -415,9 +433,9 @@ export function renderReplies(list) {
|
|
|
415
433
|
// 和路人挤在一张表里。现在分两段:他的话只陈述事实(他在你那条评论下面
|
|
416
434
|
// 回了一句,你可以回,也可以不回),是不是要当回事留给她自己判断。
|
|
417
435
|
if (fromOwner.length) {
|
|
418
|
-
lines.push('##
|
|
436
|
+
lines.push('## {T}在你下面说话了', '{T}是在你那条评论下面回了一句。你可以回,也可以不回。', '')
|
|
419
437
|
for (const x of fromOwner.slice(0, REPLY_SHOW_MAX)) {
|
|
420
|
-
const who = x.user || '
|
|
438
|
+
const who = x.user || '{T}'
|
|
421
439
|
lines.push(`- ${who}(BV ${x.bvid}):${oneLine(x.text, 200)}`)
|
|
422
440
|
}
|
|
423
441
|
}
|
|
@@ -435,7 +453,7 @@ export function renderReplies(list) {
|
|
|
435
453
|
lines.push(`- ${who}(BV ${x.bvid}):${oneLine(x.text, 200)}`)
|
|
436
454
|
}
|
|
437
455
|
}
|
|
438
|
-
return lines.join('\n')
|
|
456
|
+
return fillOwner(lines.join('\n'))
|
|
439
457
|
}
|
|
440
458
|
|
|
441
459
|
/**
|
|
@@ -506,7 +524,7 @@ export async function postComment(o = {}) {
|
|
|
506
524
|
if (!t) {
|
|
507
525
|
// 他递过来但还没看一眼的那条,拒绝理由说得更准(她才知道下一步是"去看一眼")
|
|
508
526
|
const handed = metas(events, `hand:${dk} `).some((line) => line.split(/\s+/)[1] === bvid)
|
|
509
|
-
return { ok: false, why: handed ? '
|
|
527
|
+
return { ok: false, why: handed ? '这条是{T}递过来的,你还没看' : '这个视频你今天没看' }
|
|
510
528
|
}
|
|
511
529
|
|
|
512
530
|
const ow = await readOwner(root)
|