[fix] white list
This commit is contained in:
8 files changed
+60
-57
No files matched your search
Vendored
+3
@@ -12,6 +12,9 @@ export declare class MailDeliverer {
|
|||||||
private readonly turnBuffers;
|
private readonly turnBuffers;
|
||||||
/** 每个会话按投递顺序排队,一轮回复对应队首的一封来信。 */
|
/** 每个会话按投递顺序排队,一轮回复对应队首的一封来信。 */
|
||||||
private readonly pending;
|
private readonly pending;
|
||||||
|
/** 每个会话已处理的最大事件序号,用于根/Agent 两处订阅的去重。 */
|
||||||
|
private readonly seen;
|
||||||
|
private readonly offRoot;
|
||||||
constructor(ctx: Context, onReply: (job: MailJob, text: string) => Promise<void>, onLog?: ((message: string) => void) | undefined);
|
constructor(ctx: Context, onReply: (job: MailJob, text: string) => Promise<void>, onLog?: ((message: string) => void) | undefined);
|
||||||
deliver(job: MailJob): Promise<void>;
|
deliver(job: MailJob): Promise<void>;
|
||||||
dispose(): void;
|
dispose(): void;
|
||||||
|
|||||||
@@ -11,10 +11,18 @@ export class MailDeliverer {
|
|||||||
turnBuffers = new Map();
|
turnBuffers = new Map();
|
||||||
/** 每个会话按投递顺序排队,一轮回复对应队首的一封来信。 */
|
/** 每个会话按投递顺序排队,一轮回复对应队首的一封来信。 */
|
||||||
pending = new Map();
|
pending = new Map();
|
||||||
|
/** 每个会话已处理的最大事件序号,用于根/Agent 两处订阅的去重。 */
|
||||||
|
seen = new Map();
|
||||||
|
offRoot;
|
||||||
constructor(ctx, onReply, onLog) {
|
constructor(ctx, onReply, onLog) {
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
this.onReply = onReply;
|
this.onReply = onReply;
|
||||||
this.onLog = onLog;
|
this.onLog = onLog;
|
||||||
|
// 根上下文与 Agent 上下文同时订阅:不同版本的事件派发可能只覆盖其中一个
|
||||||
|
// 作用域,两处都挂可保证收到;重复事件用 seq 去重。
|
||||||
|
this.offRoot = ctx.on('session/event', (session, event) => {
|
||||||
|
this.handleEvent(session, event);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
async deliver(job) {
|
async deliver(job) {
|
||||||
const handle = await this.ensure(job.sessionId);
|
const handle = await this.ensure(job.sessionId);
|
||||||
@@ -27,6 +35,7 @@ export class MailDeliverer {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
dispose() {
|
dispose() {
|
||||||
|
this.offRoot();
|
||||||
for (const handle of this.handles.values()) {
|
for (const handle of this.handles.values()) {
|
||||||
handle.off();
|
handle.off();
|
||||||
void handle.dispose().catch(() => { });
|
void handle.dispose().catch(() => { });
|
||||||
@@ -34,6 +43,7 @@ export class MailDeliverer {
|
|||||||
this.handles.clear();
|
this.handles.clear();
|
||||||
this.turnBuffers.clear();
|
this.turnBuffers.clear();
|
||||||
this.pending.clear();
|
this.pending.clear();
|
||||||
|
this.seen.clear();
|
||||||
}
|
}
|
||||||
async ensure(sessionId) {
|
async ensure(sessionId) {
|
||||||
const cached = this.handles.get(sessionId);
|
const cached = this.handles.get(sessionId);
|
||||||
@@ -78,6 +88,10 @@ export class MailDeliverer {
|
|||||||
const sessionId = session.header.id;
|
const sessionId = session.header.id;
|
||||||
if (!sessionId.startsWith('mail:') || !this.handles.has(sessionId))
|
if (!sessionId.startsWith('mail:') || !this.handles.has(sessionId))
|
||||||
return;
|
return;
|
||||||
|
const lastSeq = this.seen.get(sessionId) ?? 0;
|
||||||
|
if (event.seq <= lastSeq)
|
||||||
|
return;
|
||||||
|
this.seen.set(sessionId, event.seq);
|
||||||
if (event.type === 'assistant/message') {
|
if (event.type === 'assistant/message') {
|
||||||
const text = event.data.message.content
|
const text = event.data.message.content
|
||||||
.filter(block => block.type === 'text')
|
.filter(block => block.type === 'text')
|
||||||
|
|||||||
Vendored
+2
-4
@@ -6,12 +6,10 @@ export declare function normalizeAddress(address: string): string;
|
|||||||
/** 生成纯策略快照,不改动入参。 */
|
/** 生成纯策略快照,不改动入参。 */
|
||||||
export declare function createPolicy(addresses: readonly string[]): AccessPolicy;
|
export declare function createPolicy(addresses: readonly string[]): AccessPolicy;
|
||||||
export declare function isAllowed(policy: AccessPolicy, address: string): boolean;
|
export declare function isAllowed(policy: AccessPolicy, address: string): boolean;
|
||||||
/** 拦截自动回复 / 批量邮件,避免邮件环。 */
|
|
||||||
export declare function isAutoReply(mail: ParsedMail): boolean;
|
|
||||||
/** 正文只取 text/plain,缺失时用 HTML 降级;附件一律丢弃。 */
|
/** 正文只取 text/plain,缺失时用 HTML 降级;附件一律丢弃。 */
|
||||||
export declare function bodyText(mail: ParsedMail): string;
|
export declare function bodyText(mail: ParsedMail): string;
|
||||||
/**
|
/**
|
||||||
* 一次解析完成准入判断与字段提取:发件地址只解析一次,
|
* 一次解析完成准入判断与字段提取:发件地址只解析一次。
|
||||||
* 不在白名单或命中自动流量时返回 null(静默丢弃)。
|
* 唯一的准入条件是白名单;取不到发件地址时无法建立会话与回信,返回 null。
|
||||||
*/
|
*/
|
||||||
export declare function toJob(policy: AccessPolicy, mail: ParsedMail): MailJob | null;
|
export declare function toJob(policy: AccessPolicy, mail: ParsedMail): MailJob | null;
|
||||||
+6
-26
@@ -14,24 +14,6 @@ export function createPolicy(addresses) {
|
|||||||
export function isAllowed(policy, address) {
|
export function isAllowed(policy, address) {
|
||||||
return policy.has(normalizeAddress(address));
|
return policy.has(normalizeAddress(address));
|
||||||
}
|
}
|
||||||
function headerText(mail, name) {
|
|
||||||
const value = mail.headers.get(name);
|
|
||||||
if (typeof value === 'string')
|
|
||||||
return value;
|
|
||||||
if (Array.isArray(value))
|
|
||||||
return value.filter(item => typeof item === 'string').join(', ');
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
/** 拦截自动回复 / 批量邮件,避免邮件环。 */
|
|
||||||
export function isAutoReply(mail) {
|
|
||||||
const autoSubmitted = headerText(mail, 'auto-submitted').toLowerCase();
|
|
||||||
if (autoSubmitted && autoSubmitted !== 'no')
|
|
||||||
return true;
|
|
||||||
const precedence = headerText(mail, 'precedence').toLowerCase();
|
|
||||||
if (/(^|[\s,;])(bulk|junk|list)([\s,;]|$)/.test(precedence))
|
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
function decodeEntities(text) {
|
function decodeEntities(text) {
|
||||||
return text
|
return text
|
||||||
.replace(/ /gi, ' ')
|
.replace(/ /gi, ' ')
|
||||||
@@ -73,8 +55,8 @@ function referencesOf(mail) {
|
|||||||
return all.length ? all.join(' ') : undefined;
|
return all.length ? all.join(' ') : undefined;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 一次解析完成准入判断与字段提取:发件地址只解析一次,
|
* 一次解析完成准入判断与字段提取:发件地址只解析一次。
|
||||||
* 不在白名单或命中自动流量时返回 null(静默丢弃)。
|
* 唯一的准入条件是白名单;取不到发件地址时无法建立会话与回信,返回 null。
|
||||||
*/
|
*/
|
||||||
export function toJob(policy, mail) {
|
export function toJob(policy, mail) {
|
||||||
const sender = mail.from?.value?.[0]?.address;
|
const sender = mail.from?.value?.[0]?.address;
|
||||||
@@ -83,14 +65,12 @@ export function toJob(policy, mail) {
|
|||||||
const from = normalizeAddress(sender);
|
const from = normalizeAddress(sender);
|
||||||
if (!isAllowed(policy, from))
|
if (!isAllowed(policy, from))
|
||||||
return null;
|
return null;
|
||||||
if (isAutoReply(mail))
|
const subject = mail.subject?.trim() || '(无主题)';
|
||||||
return null;
|
// 正文为空(纯图片 / 无文本部分)也照常投递,不再静默丢弃。
|
||||||
const text = bodyText(mail);
|
const text = bodyText(mail) || `主题:${subject}`;
|
||||||
if (!text)
|
|
||||||
return null;
|
|
||||||
return {
|
return {
|
||||||
from,
|
from,
|
||||||
subject: mail.subject?.trim() || '(无主题)',
|
subject,
|
||||||
text,
|
text,
|
||||||
messageId: mail.messageId,
|
messageId: mail.messageId,
|
||||||
references: referencesOf(mail),
|
references: referencesOf(mail),
|
||||||
|
|||||||
+6
-1
@@ -103,6 +103,9 @@ export class ImapReceiver {
|
|||||||
lastError: undefined,
|
lastError: undefined,
|
||||||
since: new Date().toISOString(),
|
since: new Date().toISOString(),
|
||||||
});
|
});
|
||||||
|
// 先补收一次:IDLE 只对“之后到达”的邮件有响应,重启/重连期间到达的
|
||||||
|
// 邮件(游标之后)必须主动拉取,否则会一直等到下一封新信才被处理。
|
||||||
|
await this.collect(client);
|
||||||
while (this.running && this.client === client) {
|
while (this.running && this.client === client) {
|
||||||
if (supportsIdle) {
|
if (supportsIdle) {
|
||||||
const found = await client.idle();
|
const found = await client.idle();
|
||||||
@@ -176,8 +179,10 @@ export class ImapReceiver {
|
|||||||
try {
|
try {
|
||||||
const parsed = await simpleParser(message.source);
|
const parsed = await simpleParser(message.source);
|
||||||
const job = toJob(this.policy(), parsed);
|
const job = toJob(this.policy(), parsed);
|
||||||
if (job)
|
if (job) {
|
||||||
|
this.hooks.onLog?.(`[mail] 收到 ${job.from} 的邮件,投递到 ${job.sessionId}`);
|
||||||
this.hooks.onJob(job);
|
this.hooks.onJob(job);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
this.hooks.onLog?.(`[mail] 解析 UID ${uid} 失败: ${errorMessage(error)}`);
|
this.hooks.onLog?.(`[mail] 解析 UID ${uid} 失败: ${errorMessage(error)}`);
|
||||||
|
|||||||
+15
-1
@@ -20,12 +20,21 @@ export class MailDeliverer {
|
|||||||
private readonly turnBuffers = new Map<string, string[]>()
|
private readonly turnBuffers = new Map<string, string[]>()
|
||||||
/** 每个会话按投递顺序排队,一轮回复对应队首的一封来信。 */
|
/** 每个会话按投递顺序排队,一轮回复对应队首的一封来信。 */
|
||||||
private readonly pending = new Map<string, MailJob[]>()
|
private readonly pending = new Map<string, MailJob[]>()
|
||||||
|
/** 每个会话已处理的最大事件序号,用于根/Agent 两处订阅的去重。 */
|
||||||
|
private readonly seen = new Map<string, number>()
|
||||||
|
private readonly offRoot: () => void
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly ctx: Context,
|
private readonly ctx: Context,
|
||||||
private readonly onReply: (job: MailJob, text: string) => Promise<void>,
|
private readonly onReply: (job: MailJob, text: string) => Promise<void>,
|
||||||
private readonly onLog?: (message: string) => void,
|
private readonly onLog?: (message: string) => void,
|
||||||
) {}
|
) {
|
||||||
|
// 根上下文与 Agent 上下文同时订阅:不同版本的事件派发可能只覆盖其中一个
|
||||||
|
// 作用域,两处都挂可保证收到;重复事件用 seq 去重。
|
||||||
|
this.offRoot = ctx.on('session/event', (session, event) => {
|
||||||
|
this.handleEvent(session, event)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
async deliver(job: MailJob): Promise<void> {
|
async deliver(job: MailJob): Promise<void> {
|
||||||
const handle = await this.ensure(job.sessionId)
|
const handle = await this.ensure(job.sessionId)
|
||||||
@@ -41,6 +50,7 @@ export class MailDeliverer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dispose(): void {
|
dispose(): void {
|
||||||
|
this.offRoot()
|
||||||
for (const handle of this.handles.values()) {
|
for (const handle of this.handles.values()) {
|
||||||
handle.off()
|
handle.off()
|
||||||
void handle.dispose().catch(() => {})
|
void handle.dispose().catch(() => {})
|
||||||
@@ -48,6 +58,7 @@ export class MailDeliverer {
|
|||||||
this.handles.clear()
|
this.handles.clear()
|
||||||
this.turnBuffers.clear()
|
this.turnBuffers.clear()
|
||||||
this.pending.clear()
|
this.pending.clear()
|
||||||
|
this.seen.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
private async ensure(sessionId: string): Promise<DriveHandle> {
|
private async ensure(sessionId: string): Promise<DriveHandle> {
|
||||||
@@ -95,6 +106,9 @@ export class MailDeliverer {
|
|||||||
private handleEvent(session: Session, event: SessionEvent): void {
|
private handleEvent(session: Session, event: SessionEvent): void {
|
||||||
const sessionId = session.header.id as string
|
const sessionId = session.header.id as string
|
||||||
if (!sessionId.startsWith('mail:') || !this.handles.has(sessionId)) return
|
if (!sessionId.startsWith('mail:') || !this.handles.has(sessionId)) return
|
||||||
|
const lastSeq = this.seen.get(sessionId) ?? 0
|
||||||
|
if (event.seq <= lastSeq) return
|
||||||
|
this.seen.set(sessionId, event.seq)
|
||||||
if (event.type === 'assistant/message') {
|
if (event.type === 'assistant/message') {
|
||||||
const text = event.data.message.content
|
const text = event.data.message.content
|
||||||
.filter(block => block.type === 'text')
|
.filter(block => block.type === 'text')
|
||||||
|
|||||||
+6
-24
@@ -22,24 +22,6 @@ export function isAllowed(policy: AccessPolicy, address: string): boolean {
|
|||||||
return policy.has(normalizeAddress(address))
|
return policy.has(normalizeAddress(address))
|
||||||
}
|
}
|
||||||
|
|
||||||
function headerText(mail: ParsedMail, name: string): string {
|
|
||||||
const value = mail.headers.get(name)
|
|
||||||
if (typeof value === 'string') return value
|
|
||||||
if (Array.isArray(value)) return value.filter(item => typeof item === 'string').join(', ')
|
|
||||||
return ''
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 拦截自动回复 / 批量邮件,避免邮件环。 */
|
|
||||||
export function isAutoReply(mail: ParsedMail): boolean {
|
|
||||||
const autoSubmitted = headerText(mail, 'auto-submitted').toLowerCase()
|
|
||||||
if (autoSubmitted && autoSubmitted !== 'no') return true
|
|
||||||
|
|
||||||
const precedence = headerText(mail, 'precedence').toLowerCase()
|
|
||||||
if (/(^|[\s,;])(bulk|junk|list)([\s,;]|$)/.test(precedence)) return true
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
function decodeEntities(text: string): string {
|
function decodeEntities(text: string): string {
|
||||||
return text
|
return text
|
||||||
.replace(/ /gi, ' ')
|
.replace(/ /gi, ' ')
|
||||||
@@ -84,8 +66,8 @@ function referencesOf(mail: ParsedMail): string | undefined {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 一次解析完成准入判断与字段提取:发件地址只解析一次,
|
* 一次解析完成准入判断与字段提取:发件地址只解析一次。
|
||||||
* 不在白名单或命中自动流量时返回 null(静默丢弃)。
|
* 唯一的准入条件是白名单;取不到发件地址时无法建立会话与回信,返回 null。
|
||||||
*/
|
*/
|
||||||
export function toJob(policy: AccessPolicy, mail: ParsedMail): MailJob | null {
|
export function toJob(policy: AccessPolicy, mail: ParsedMail): MailJob | null {
|
||||||
const sender = mail.from?.value?.[0]?.address
|
const sender = mail.from?.value?.[0]?.address
|
||||||
@@ -93,14 +75,14 @@ export function toJob(policy: AccessPolicy, mail: ParsedMail): MailJob | null {
|
|||||||
|
|
||||||
const from = normalizeAddress(sender)
|
const from = normalizeAddress(sender)
|
||||||
if (!isAllowed(policy, from)) return null
|
if (!isAllowed(policy, from)) return null
|
||||||
if (isAutoReply(mail)) return null
|
|
||||||
|
|
||||||
const text = bodyText(mail)
|
const subject = mail.subject?.trim() || '(无主题)'
|
||||||
if (!text) return null
|
// 正文为空(纯图片 / 无文本部分)也照常投递,不再静默丢弃。
|
||||||
|
const text = bodyText(mail) || `主题:${subject}`
|
||||||
|
|
||||||
return {
|
return {
|
||||||
from,
|
from,
|
||||||
subject: mail.subject?.trim() || '(无主题)',
|
subject,
|
||||||
text,
|
text,
|
||||||
messageId: mail.messageId,
|
messageId: mail.messageId,
|
||||||
references: referencesOf(mail),
|
references: referencesOf(mail),
|
||||||
|
|||||||
+8
-1
@@ -118,6 +118,10 @@ export class ImapReceiver {
|
|||||||
since: new Date().toISOString(),
|
since: new Date().toISOString(),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 先补收一次:IDLE 只对“之后到达”的邮件有响应,重启/重连期间到达的
|
||||||
|
// 邮件(游标之后)必须主动拉取,否则会一直等到下一封新信才被处理。
|
||||||
|
await this.collect(client)
|
||||||
|
|
||||||
while (this.running && this.client === client) {
|
while (this.running && this.client === client) {
|
||||||
if (supportsIdle) {
|
if (supportsIdle) {
|
||||||
const found = await client.idle()
|
const found = await client.idle()
|
||||||
@@ -193,7 +197,10 @@ export class ImapReceiver {
|
|||||||
try {
|
try {
|
||||||
const parsed = await simpleParser(message.source)
|
const parsed = await simpleParser(message.source)
|
||||||
const job = toJob(this.policy(), parsed)
|
const job = toJob(this.policy(), parsed)
|
||||||
if (job) this.hooks.onJob(job)
|
if (job) {
|
||||||
|
this.hooks.onLog?.(`[mail] 收到 ${job.from} 的邮件,投递到 ${job.sessionId}`)
|
||||||
|
this.hooks.onJob(job)
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.hooks.onLog?.(`[mail] 解析 UID ${uid} 失败: ${errorMessage(error)}`)
|
this.hooks.onLog?.(`[mail] 解析 UID ${uid} 失败: ${errorMessage(error)}`)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user