advisers 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README-zh.md +13 -14
- package/README.md +32 -23
- package/THIRD-PARTY-NOTICES.md +1 -1
- package/bin/advisers.js +2180 -92
- package/index.js +1 -1
- package/lib/brief-archive.js +267 -0
- package/lib/client-pack.js +1016 -0
- package/lib/consent.js +390 -0
- package/lib/credentials.js +195 -0
- package/lib/error-codes.js +516 -7
- package/lib/evidence.js +697 -113
- package/lib/kb-extract.js +626 -0
- package/lib/kb-facts.js +429 -0
- package/lib/kb-pages.js +1084 -0
- package/lib/kb.js +1036 -0
- package/lib/mail-errors.js +46 -0
- package/lib/mail-extract.js +351 -0
- package/lib/mail-imap.js +446 -0
- package/lib/mail-import.js +176 -0
- package/lib/mail-parse.js +223 -0
- package/lib/mail-store.js +731 -0
- package/lib/mail.js +506 -0
- package/lib/mcp-server.js +196 -10
- package/lib/notify.js +265 -7
- package/lib/policy/decision.js +315 -0
- package/lib/policy/egress.js +268 -0
- package/lib/policy/errors.js +71 -0
- package/lib/policy/index.js +99 -0
- package/lib/policy/rules.js +468 -0
- package/lib/policy/surfaces.js +141 -0
- package/lib/receipt-verify.js +199 -19
- package/lib/receivables.js +1653 -0
- package/lib/scheduler.js +453 -9
- package/lib/shell.js +1406 -0
- package/lib/source.js +98 -3
- package/lib/sqlite.js +366 -49
- package/lib/tos/index.js +2 -2
- package/lib/tos/manifest.json +6 -6
- package/lib/tos/privacy.zh-CN.md +33 -20
- package/lib/tos/safety.zh-CN.md +7 -6
- package/lib/tos/summary.zh-CN.md +3 -2
- package/lib/tos/terms.zh-CN.md +31 -16
- package/lib/urgency.js +467 -0
- package/package.json +4 -4
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local email parsing for the 0.4.0 mail entry.
|
|
3
|
+
*
|
|
4
|
+
* parseMailMessage(raw) is the one function mail sync and the .eml/.mbox
|
|
5
|
+
* fallback share. It reuses lib/importers.js parseEml() for the readable text
|
|
6
|
+
* body (MIME text/plain first, tag-stripped HTML fallback, transfer decoding)
|
|
7
|
+
* and adds ATTACHMENT METADATA ONLY: name / size / mime. Attachment bytes are
|
|
8
|
+
* never written anywhere, never decoded into the ledger and never returned.
|
|
9
|
+
*
|
|
10
|
+
* Pure and offline: no fs, no network, no console. Malformed MIME never
|
|
11
|
+
* throws; the affected metadata is dropped and explained in `warnings`.
|
|
12
|
+
*/
|
|
13
|
+
import { parseEml } from './importers.js'
|
|
14
|
+
|
|
15
|
+
export const MAX_ATTACHMENTS = 50
|
|
16
|
+
export const MAX_ATTACHMENT_NAME_CHARS = 120
|
|
17
|
+
const MAX_MIME_DEPTH = 10
|
|
18
|
+
|
|
19
|
+
function normalizeNewlines(value) {
|
|
20
|
+
return String(value == null ? '' : value).replace(/^\uFEFF/, '').replace(/\r\n/g, '\n').replace(/\r/g, '\n')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function splitHeadBody(text) {
|
|
24
|
+
const index = text.indexOf('\n\n')
|
|
25
|
+
if (index === -1) return { head: text, body: '' }
|
|
26
|
+
return { head: text.slice(0, index), body: text.slice(index + 2) }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function splitQuoted(value, separator) {
|
|
30
|
+
const out = []
|
|
31
|
+
let current = ''
|
|
32
|
+
let quoted = false
|
|
33
|
+
for (const char of String(value)) {
|
|
34
|
+
if (char === '"') { quoted = !quoted; current += char; continue }
|
|
35
|
+
if (char === separator && !quoted) { out.push(current); current = ''; continue }
|
|
36
|
+
current += char
|
|
37
|
+
}
|
|
38
|
+
out.push(current)
|
|
39
|
+
return out
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function unquote(value) {
|
|
43
|
+
const text = String(value == null ? '' : value).trim()
|
|
44
|
+
if (text.length >= 2 && text.startsWith('"') && text.endsWith('"')) return text.slice(1, -1).replace(/\\(.)/g, '$1')
|
|
45
|
+
return text
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function parseHeaders(head) {
|
|
49
|
+
const map = Object.create(null)
|
|
50
|
+
const lines = []
|
|
51
|
+
for (const line of normalizeNewlines(head).split('\n')) {
|
|
52
|
+
if (/^[ \t]/.test(line) && lines.length) lines[lines.length - 1] += ' ' + line.trim()
|
|
53
|
+
else lines.push(line)
|
|
54
|
+
}
|
|
55
|
+
for (const line of lines) {
|
|
56
|
+
const colon = line.indexOf(':')
|
|
57
|
+
if (colon <= 0) continue
|
|
58
|
+
const name = line.slice(0, colon).trim().toLowerCase()
|
|
59
|
+
if (!name || map[name] !== undefined) continue
|
|
60
|
+
map[name] = line.slice(colon + 1).trim()
|
|
61
|
+
}
|
|
62
|
+
return map
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function paramsOf(segments) {
|
|
66
|
+
const params = {}
|
|
67
|
+
for (const segment of segments.slice(1)) {
|
|
68
|
+
const equals = segment.indexOf('=')
|
|
69
|
+
if (equals <= 0) continue
|
|
70
|
+
const key = segment.slice(0, equals).trim().toLowerCase()
|
|
71
|
+
if (!key || params[key] !== undefined) continue
|
|
72
|
+
params[key] = unquote(segment.slice(equals + 1))
|
|
73
|
+
}
|
|
74
|
+
return params
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Parse a Content-Type header into { mime, params }. Pure. */
|
|
78
|
+
export function parseContentType(raw) {
|
|
79
|
+
const segments = splitQuoted(String(raw == null ? '' : raw), ';')
|
|
80
|
+
const mime = (segments[0] || 'text/plain').trim().toLowerCase() || 'text/plain'
|
|
81
|
+
return { mime, params: paramsOf(segments) }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Parse a Content-Disposition header into { type, params }. Pure. */
|
|
85
|
+
export function parseContentDisposition(raw) {
|
|
86
|
+
const segments = splitQuoted(String(raw == null ? '' : raw), ';')
|
|
87
|
+
const type = (segments[0] || '').trim().toLowerCase()
|
|
88
|
+
return { type, params: paramsOf(segments) }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function decodeHeaderWords(value) {
|
|
92
|
+
return String(value == null ? '' : value).replace(/=\?([^?]+)\?([BbQq])\?([^?]*)\?=/g, (match, charset, encoding, data) => {
|
|
93
|
+
try {
|
|
94
|
+
const buffer = encoding.toUpperCase() === 'B'
|
|
95
|
+
? Buffer.from(data, 'base64')
|
|
96
|
+
: Buffer.from(data.replace(/_/g, ' ').replace(/=([0-9A-Fa-f]{2})/g, (_m, hex) => String.fromCharCode(parseInt(hex, 16))), 'latin1')
|
|
97
|
+
const label = String(charset).toLowerCase().replace(/^utf8$/, 'utf-8')
|
|
98
|
+
if (label === 'utf-8' || label === 'utf8') return buffer.toString('utf8')
|
|
99
|
+
if (label === 'gbk' || label === 'gb2312' || label === 'gb18030') return buffer.toString('utf8')
|
|
100
|
+
if (label === 'iso-8859-1' || label === 'latin1') return buffer.toString('latin1')
|
|
101
|
+
return buffer.toString('utf8')
|
|
102
|
+
} catch { return match }
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function sanitizeAttachmentName(value, index) {
|
|
107
|
+
let name = decodeHeaderWords(value)
|
|
108
|
+
name = name.replace(/[\u0000-\u001f\u007f]/g, ' ').replace(/[\\/]+/g, ' ').replace(/\s+/g, ' ').trim()
|
|
109
|
+
if (!name || name === '.' || name === '..') name = 'attachment-' + (index + 1)
|
|
110
|
+
return name.slice(0, MAX_ATTACHMENT_NAME_CHARS)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function splitMultipart(body, boundary) {
|
|
114
|
+
const marker = '--' + boundary
|
|
115
|
+
const parts = []
|
|
116
|
+
let current = null
|
|
117
|
+
for (const line of body.split('\n')) {
|
|
118
|
+
const trimmed = line.replace(/\r$/, '')
|
|
119
|
+
if (trimmed === marker || trimmed === marker + '--') {
|
|
120
|
+
if (current) parts.push(current.join('\n'))
|
|
121
|
+
current = trimmed === marker + '--' ? null : []
|
|
122
|
+
if (trimmed === marker + '--') break
|
|
123
|
+
continue
|
|
124
|
+
}
|
|
125
|
+
if (current) current.push(line)
|
|
126
|
+
}
|
|
127
|
+
if (current) parts.push(current.join('\n'))
|
|
128
|
+
return parts
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function decodedByteLength(body, transferEncoding) {
|
|
132
|
+
const encoding = String(transferEncoding == null ? '' : transferEncoding).trim().toLowerCase()
|
|
133
|
+
if (encoding === 'base64') {
|
|
134
|
+
const compact = body.replace(/[^A-Za-z0-9+/=]/g, '')
|
|
135
|
+
const padding = (compact.match(/=+$/) || [''])[0].length
|
|
136
|
+
return Math.max(0, Math.floor((compact.length * 3) / 4) - padding)
|
|
137
|
+
}
|
|
138
|
+
if (encoding === 'quoted-printable') {
|
|
139
|
+
const text = body.replace(/=\r?\n/g, '')
|
|
140
|
+
let size = 0
|
|
141
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
142
|
+
const char = text[index]
|
|
143
|
+
if (char === '=' && /^[0-9A-Fa-f]{2}$/.test(text.slice(index + 1, index + 3))) { size += 1; index += 2; continue }
|
|
144
|
+
if (char === '\n' || char === '\r') continue
|
|
145
|
+
size += 1
|
|
146
|
+
}
|
|
147
|
+
return size
|
|
148
|
+
}
|
|
149
|
+
return Buffer.byteLength(body)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function walkEntity(entity, depth, out, warnings) {
|
|
153
|
+
if (depth > MAX_MIME_DEPTH) { warnings.push('MIME nesting is too deep; deeper parts were ignored'); return }
|
|
154
|
+
const { head, body } = splitHeadBody(normalizeNewlines(entity))
|
|
155
|
+
const headers = parseHeaders(head)
|
|
156
|
+
const contentType = parseContentType(headers['content-type'])
|
|
157
|
+
if (contentType.mime.startsWith('multipart/')) {
|
|
158
|
+
const boundary = contentType.params.boundary
|
|
159
|
+
if (!boundary) { warnings.push('a multipart part has no boundary'); return }
|
|
160
|
+
for (const part of splitMultipart(body, boundary)) walkEntity(part, depth + 1, out, warnings)
|
|
161
|
+
return
|
|
162
|
+
}
|
|
163
|
+
const disposition = parseContentDisposition(headers['content-disposition'])
|
|
164
|
+
const rawName = disposition.params.filename ?? contentType.params.name
|
|
165
|
+
const isAttachment = disposition.type === 'attachment' || Boolean(rawName)
|
|
166
|
+
if (!isAttachment) return
|
|
167
|
+
out.push({
|
|
168
|
+
name: sanitizeAttachmentName(rawName, out.length),
|
|
169
|
+
size: decodedByteLength(body, headers['content-transfer-encoding']),
|
|
170
|
+
mime: contentType.mime.slice(0, 120),
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Attachment metadata only: [{ name, size, mime }]. Never throws.
|
|
176
|
+
* @param {string} raw RFC 822 / MIME message source
|
|
177
|
+
* @returns {Array<{ name: string, size: number, mime: string }>}
|
|
178
|
+
*/
|
|
179
|
+
export function scanAttachments(raw) {
|
|
180
|
+
const source = normalizeNewlines(raw)
|
|
181
|
+
const out = []
|
|
182
|
+
const warnings = []
|
|
183
|
+
try {
|
|
184
|
+
walkEntity(source, 0, out, warnings)
|
|
185
|
+
} catch { return out }
|
|
186
|
+
return out.slice(0, MAX_ATTACHMENTS)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function cleanHeader(value) {
|
|
190
|
+
return String(value == null ? '' : value).replace(/[\u0000-\u001f\u007f]+/g, ' ').replace(/\s+/g, ' ').trim()
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Parse one RFC 822 message into the row fields the mail store needs.
|
|
195
|
+
* @returns {{ from: string, to: string, subject: string, date: string,
|
|
196
|
+
* messageId: string, text: string, attachments: Array<{name,size,mime}>,
|
|
197
|
+
* warnings: string[], sizeBytes: number }}
|
|
198
|
+
*/
|
|
199
|
+
export function parseMailMessage(raw) {
|
|
200
|
+
const source = normalizeNewlines(raw)
|
|
201
|
+
let parsed
|
|
202
|
+
const warnings = []
|
|
203
|
+
try {
|
|
204
|
+
parsed = parseEml(source)
|
|
205
|
+
} catch {
|
|
206
|
+
parsed = { from: '', to: '', subject: '', date: '', messageId: '', text: '', warnings: [] }
|
|
207
|
+
warnings.push('message could not be parsed')
|
|
208
|
+
}
|
|
209
|
+
for (const warning of Array.isArray(parsed.warnings) ? parsed.warnings : []) {
|
|
210
|
+
if (!warnings.includes(warning)) warnings.push(warning)
|
|
211
|
+
}
|
|
212
|
+
return {
|
|
213
|
+
from: cleanHeader(parsed.from),
|
|
214
|
+
to: cleanHeader(parsed.to),
|
|
215
|
+
subject: cleanHeader(parsed.subject),
|
|
216
|
+
date: cleanHeader(parsed.date),
|
|
217
|
+
messageId: cleanHeader(parsed.messageId),
|
|
218
|
+
text: typeof parsed.text === 'string' ? parsed.text : '',
|
|
219
|
+
attachments: scanAttachments(source),
|
|
220
|
+
warnings,
|
|
221
|
+
sizeBytes: Buffer.byteLength(source),
|
|
222
|
+
}
|
|
223
|
+
}
|