foliko 1.0.75 → 1.0.76
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/.claude/settings.local.json +159 -157
- package/cli/bin/foliko.js +12 -12
- package/cli/src/commands/chat.js +143 -143
- package/cli/src/commands/list.js +93 -93
- package/cli/src/index.js +75 -75
- package/cli/src/ui/chat-ui.js +201 -201
- package/cli/src/utils/ansi.js +40 -40
- package/cli/src/utils/markdown.js +292 -292
- package/examples/ambient-example.js +194 -194
- package/examples/basic.js +115 -115
- package/examples/bootstrap.js +121 -121
- package/examples/mcp-example.js +56 -56
- package/examples/skill-example.js +49 -49
- package/examples/test-chat.js +137 -137
- package/examples/test-mcp.js +85 -85
- package/examples/test-reload.js +59 -59
- package/examples/test-telegram.js +50 -50
- package/examples/test-tg-bot.js +45 -45
- package/examples/test-tg-simple.js +47 -47
- package/examples/test-tg.js +62 -62
- package/examples/test-think.js +43 -43
- package/examples/test-web-plugin.js +103 -103
- package/examples/test-weixin-feishu.js +103 -103
- package/examples/workflow.js +158 -158
- package/package.json +1 -1
- package/plugins/ai-plugin.js +102 -102
- package/plugins/ambient-agent/EventWatcher.js +113 -113
- package/plugins/ambient-agent/ExplorerLoop.js +640 -640
- package/plugins/ambient-agent/GoalManager.js +197 -197
- package/plugins/ambient-agent/Reflector.js +95 -95
- package/plugins/ambient-agent/StateStore.js +90 -90
- package/plugins/ambient-agent/constants.js +101 -101
- package/plugins/ambient-agent/index.js +579 -579
- package/plugins/audit-plugin.js +187 -187
- package/plugins/default-plugins.js +662 -662
- package/plugins/email/constants.js +64 -64
- package/plugins/email/handlers.js +461 -461
- package/plugins/email/index.js +278 -278
- package/plugins/email/monitor.js +269 -269
- package/plugins/email/parser.js +138 -138
- package/plugins/email/reply.js +151 -151
- package/plugins/email/utils.js +124 -124
- package/plugins/feishu-plugin.js +481 -481
- package/plugins/file-system-plugin.js +826 -826
- package/plugins/install-plugin.js +199 -199
- package/plugins/python-executor-plugin.js +367 -367
- package/plugins/python-plugin-loader.js +481 -481
- package/plugins/rules-plugin.js +294 -294
- package/plugins/scheduler-plugin.js +691 -691
- package/plugins/session-plugin.js +369 -369
- package/plugins/shell-executor-plugin.js +197 -197
- package/plugins/storage-plugin.js +240 -240
- package/plugins/subagent-plugin.js +845 -845
- package/plugins/telegram-plugin.js +482 -482
- package/plugins/think-plugin.js +345 -345
- package/plugins/tools-plugin.js +196 -196
- package/plugins/web-plugin.js +606 -606
- package/plugins/weixin-plugin.js +545 -545
- package/src/capabilities/index.js +11 -11
- package/src/capabilities/skill-manager.js +609 -609
- package/src/capabilities/workflow-engine.js +1109 -1109
- package/src/core/agent-chat.js +882 -882
- package/src/core/agent.js +892 -892
- package/src/core/framework.js +465 -465
- package/src/core/index.js +19 -19
- package/src/core/plugin-base.js +219 -219
- package/src/core/plugin-manager.js +863 -863
- package/src/core/provider.js +114 -114
- package/src/core/sub-agent-config.js +264 -264
- package/src/core/system-prompt-builder.js +120 -120
- package/src/core/tool-registry.js +517 -517
- package/src/core/tool-router.js +297 -297
- package/src/executors/executor-base.js +58 -58
- package/src/executors/mcp-executor.js +741 -741
- package/src/index.js +25 -25
- package/src/utils/circuit-breaker.js +301 -301
- package/src/utils/error-boundary.js +363 -363
- package/src/utils/error.js +374 -374
- package/src/utils/event-emitter.js +97 -97
- package/src/utils/id.js +133 -133
- package/src/utils/index.js +217 -217
- package/src/utils/logger.js +181 -181
- package/src/utils/plugin-helpers.js +90 -90
- package/src/utils/retry.js +122 -122
- package/src/utils/sandbox.js +292 -292
- package/test/tool-registry-validation.test.js +218 -218
- package/website/script.js +136 -136
- package/foliko-1.0.75.tgz +0 -0
package/plugins/email/monitor.js
CHANGED
|
@@ -1,269 +1,269 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Email 插件 - 邮件监控
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const Imap = require('imap-mkl')
|
|
6
|
-
const { simpleParser } = require('mailparser')
|
|
7
|
-
const { DEFAULT_IMAP, IMAP_CLIENT_INFO, EMAIL_DEFAULTS } = require('./constants')
|
|
8
|
-
const { getConfig, generateCallId } = require('./utils')
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* 创建 IMAP 配置
|
|
12
|
-
*/
|
|
13
|
-
function createImapConfig(config = {}) {
|
|
14
|
-
const cfg = getConfig(config)
|
|
15
|
-
return {
|
|
16
|
-
...DEFAULT_IMAP,
|
|
17
|
-
user: cfg.user,
|
|
18
|
-
password: cfg.password,
|
|
19
|
-
host: cfg.host,
|
|
20
|
-
port: cfg.port,
|
|
21
|
-
id: IMAP_CLIENT_INFO
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* 处理邮件监控
|
|
27
|
-
* @param {Object} emailPlugin - EmailPlugin 实例
|
|
28
|
-
* @param {Object} args - 参数
|
|
29
|
-
* @returns {Object} 操作结果
|
|
30
|
-
*/
|
|
31
|
-
function handleEmailWatch(emailPlugin, args) {
|
|
32
|
-
const { action, interval, box } = args
|
|
33
|
-
|
|
34
|
-
switch (action) {
|
|
35
|
-
case 'start':
|
|
36
|
-
return emailPlugin._startEmailWatch({ interval, box })
|
|
37
|
-
|
|
38
|
-
case 'stop':
|
|
39
|
-
return emailPlugin._stopEmailWatch()
|
|
40
|
-
|
|
41
|
-
case 'status':
|
|
42
|
-
return emailPlugin._getEmailWatchStatus()
|
|
43
|
-
|
|
44
|
-
default:
|
|
45
|
-
return { success: false, error: `Unknown action: ${action}` }
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* 启动邮件监控
|
|
51
|
-
* @param {Object} emailPlugin - EmailPlugin 实例
|
|
52
|
-
* @param {Object} config - 监控配置
|
|
53
|
-
* @returns {Object} 操作结果
|
|
54
|
-
*/
|
|
55
|
-
function startEmailWatch(emailPlugin, config) {
|
|
56
|
-
if (emailPlugin._watchEnabled) {
|
|
57
|
-
return { success: false, error: 'Email watch is already running' }
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
emailPlugin._watchConfig = {
|
|
61
|
-
host: config.host || process.env.IMAP_HOST,
|
|
62
|
-
port: config.port || parseInt(process.env.IMAP_PORT) || 993,
|
|
63
|
-
user: config.user || process.env.IMAP_USER,
|
|
64
|
-
password: config.password || process.env.IMAP_PASS,
|
|
65
|
-
box: config.box || 'INBOX',
|
|
66
|
-
interval: (config.interval || EMAIL_DEFAULTS.interval) * 1000
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (!emailPlugin._watchConfig.user || !emailPlugin._watchConfig.password) {
|
|
70
|
-
return { success: false, error: 'IMAP user/password is required' }
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
emailPlugin._watchEnabled = true
|
|
74
|
-
|
|
75
|
-
// 立即执行一次检查
|
|
76
|
-
emailPlugin._checkNewEmails().catch(err => {
|
|
77
|
-
emailPlugin._log.error('Initial check failed:', err.message)
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
// 启动轮询
|
|
81
|
-
emailPlugin._watchInterval = setInterval(() => {
|
|
82
|
-
emailPlugin._checkNewEmails().catch(err => {
|
|
83
|
-
emailPlugin._log.error('Watch check failed:', err.message)
|
|
84
|
-
})
|
|
85
|
-
}, emailPlugin._watchConfig.interval)
|
|
86
|
-
|
|
87
|
-
return {
|
|
88
|
-
success: true,
|
|
89
|
-
message: `Email watch started (interval: ${emailPlugin._watchConfig.interval / 1000}s)`,
|
|
90
|
-
config: {
|
|
91
|
-
host: emailPlugin._watchConfig.host,
|
|
92
|
-
box: emailPlugin._watchConfig.box,
|
|
93
|
-
interval: emailPlugin._watchConfig.interval / 1000
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* 停止邮件监控
|
|
100
|
-
* @param {Object} emailPlugin - EmailPlugin 实例
|
|
101
|
-
* @returns {Object} 操作结果
|
|
102
|
-
*/
|
|
103
|
-
function stopEmailWatch(emailPlugin) {
|
|
104
|
-
if (!emailPlugin._watchEnabled) {
|
|
105
|
-
return { success: false, error: 'Email watch is not running' }
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (emailPlugin._watchInterval) {
|
|
109
|
-
clearInterval(emailPlugin._watchInterval)
|
|
110
|
-
emailPlugin._watchInterval = null
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
emailPlugin._watchEnabled = false
|
|
114
|
-
emailPlugin._watchConfig = null
|
|
115
|
-
|
|
116
|
-
return { success: true, message: 'Email watch stopped' }
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* 获取监控状态
|
|
121
|
-
* @param {Object} emailPlugin - EmailPlugin 实例
|
|
122
|
-
* @returns {Object} 状态信息
|
|
123
|
-
*/
|
|
124
|
-
function getEmailWatchStatus(emailPlugin) {
|
|
125
|
-
return {
|
|
126
|
-
success: true,
|
|
127
|
-
watching: emailPlugin._watchEnabled,
|
|
128
|
-
config: emailPlugin._watchConfig ? {
|
|
129
|
-
host: emailPlugin._watchConfig.host,
|
|
130
|
-
box: emailPlugin._watchConfig.box,
|
|
131
|
-
interval: emailPlugin._watchConfig.interval / 1000,
|
|
132
|
-
lastSeenUid: emailPlugin._lastSeenUid
|
|
133
|
-
} : null
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* 检查新邮件
|
|
139
|
-
* @param {Object} emailPlugin - EmailPlugin 实例
|
|
140
|
-
* @returns {Promise<Object>} 检查结果
|
|
141
|
-
*/
|
|
142
|
-
async function checkNewEmails(emailPlugin) {
|
|
143
|
-
if (!emailPlugin._watchConfig) return
|
|
144
|
-
|
|
145
|
-
const callId = generateCallId()
|
|
146
|
-
emailPlugin._log.debug(`[${callId}] _checkNewEmails called, _checkInProgress=${emailPlugin._checkInProgress}`)
|
|
147
|
-
|
|
148
|
-
// 防止并发检查
|
|
149
|
-
if (emailPlugin._checkInProgress) {
|
|
150
|
-
emailPlugin._log.debug(`[${callId}] Skipping - already in progress`)
|
|
151
|
-
return
|
|
152
|
-
}
|
|
153
|
-
emailPlugin._checkInProgress = true
|
|
154
|
-
|
|
155
|
-
const imapConfig = createImapConfig(emailPlugin._watchConfig)
|
|
156
|
-
|
|
157
|
-
return new Promise((resolve, reject) => {
|
|
158
|
-
const imap = new Imap(imapConfig)
|
|
159
|
-
|
|
160
|
-
const cleanup = () => {
|
|
161
|
-
try { imap.end() } catch (e) {}
|
|
162
|
-
emailPlugin._checkInProgress = false
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
imap.on('ready', () => {
|
|
166
|
-
imap.openBox(emailPlugin._watchConfig.box, true, (err, box) => {
|
|
167
|
-
if (err) {
|
|
168
|
-
cleanup()
|
|
169
|
-
return reject(err)
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// 搜索未读邮件
|
|
173
|
-
imap.search(['UNSEEN'], (err, results) => {
|
|
174
|
-
if (err) {
|
|
175
|
-
cleanup()
|
|
176
|
-
return reject(err)
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
if (!results || results.length === 0) {
|
|
180
|
-
cleanup()
|
|
181
|
-
return resolve({ success: true, newEmails: 0 })
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// 找出新的未读邮件
|
|
185
|
-
const newEmails = results.filter(uid =>
|
|
186
|
-
!emailPlugin._lastSeenUid || uid > emailPlugin._lastSeenUid
|
|
187
|
-
)
|
|
188
|
-
|
|
189
|
-
if (newEmails.length > 0) {
|
|
190
|
-
const latestUid = Math.max(...newEmails)
|
|
191
|
-
// imap.fetch() 需要 UIDs 数组,传单个数字会导致获取从该 UID 到末尾的所有邮件
|
|
192
|
-
const f = imap.fetch([latestUid], { bodies: '' })
|
|
193
|
-
|
|
194
|
-
f.on('message', (msg) => {
|
|
195
|
-
let email = {}
|
|
196
|
-
let bodyParsed = false
|
|
197
|
-
|
|
198
|
-
msg.on('body', (stream) => {
|
|
199
|
-
simpleParser(stream).then(mail => {
|
|
200
|
-
email = {
|
|
201
|
-
uid: mail.uid || latestUid,
|
|
202
|
-
messageId: mail.messageId,
|
|
203
|
-
subject: mail.subject,
|
|
204
|
-
from: mail.from?.text || '',
|
|
205
|
-
to: mail.to?.text || '',
|
|
206
|
-
date: mail.date?.toISOString() || '',
|
|
207
|
-
text: mail.text || mail.textAsHtml || '',
|
|
208
|
-
html: mail.html,
|
|
209
|
-
attachments: mail.attachments?.map(a => ({
|
|
210
|
-
filename: a.filename,
|
|
211
|
-
contentType: a.contentType
|
|
212
|
-
})) || []
|
|
213
|
-
}
|
|
214
|
-
bodyParsed = true
|
|
215
|
-
}).catch(err => {
|
|
216
|
-
email.error = err.message
|
|
217
|
-
bodyParsed = true
|
|
218
|
-
})
|
|
219
|
-
})
|
|
220
|
-
|
|
221
|
-
msg.on('end', () => {
|
|
222
|
-
const checkDone = () => {
|
|
223
|
-
if (bodyParsed) {
|
|
224
|
-
// 更新最后看到的 UID
|
|
225
|
-
emailPlugin._lastSeenUid = latestUid
|
|
226
|
-
|
|
227
|
-
// 发送事件通知
|
|
228
|
-
emailPlugin._emitEmailReceived(email)
|
|
229
|
-
|
|
230
|
-
cleanup()
|
|
231
|
-
resolve({ success: true, newEmails: newEmails.length, email })
|
|
232
|
-
} else {
|
|
233
|
-
setTimeout(checkDone, 10)
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
checkDone()
|
|
237
|
-
})
|
|
238
|
-
})
|
|
239
|
-
|
|
240
|
-
f.on('error', (err) => {
|
|
241
|
-
cleanup()
|
|
242
|
-
reject(err)
|
|
243
|
-
})
|
|
244
|
-
} else {
|
|
245
|
-
cleanup()
|
|
246
|
-
resolve({ success: true, newEmails: 0 })
|
|
247
|
-
}
|
|
248
|
-
})
|
|
249
|
-
})
|
|
250
|
-
})
|
|
251
|
-
|
|
252
|
-
imap.on('error', (err) => {
|
|
253
|
-
cleanup()
|
|
254
|
-
reject(err)
|
|
255
|
-
})
|
|
256
|
-
|
|
257
|
-
imap.on('end', () => {})
|
|
258
|
-
|
|
259
|
-
imap.connect()
|
|
260
|
-
})
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
module.exports = {
|
|
264
|
-
handleEmailWatch,
|
|
265
|
-
startEmailWatch,
|
|
266
|
-
stopEmailWatch,
|
|
267
|
-
getEmailWatchStatus,
|
|
268
|
-
checkNewEmails
|
|
269
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Email 插件 - 邮件监控
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const Imap = require('imap-mkl')
|
|
6
|
+
const { simpleParser } = require('mailparser')
|
|
7
|
+
const { DEFAULT_IMAP, IMAP_CLIENT_INFO, EMAIL_DEFAULTS } = require('./constants')
|
|
8
|
+
const { getConfig, generateCallId } = require('./utils')
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 创建 IMAP 配置
|
|
12
|
+
*/
|
|
13
|
+
function createImapConfig(config = {}) {
|
|
14
|
+
const cfg = getConfig(config)
|
|
15
|
+
return {
|
|
16
|
+
...DEFAULT_IMAP,
|
|
17
|
+
user: cfg.user,
|
|
18
|
+
password: cfg.password,
|
|
19
|
+
host: cfg.host,
|
|
20
|
+
port: cfg.port,
|
|
21
|
+
id: IMAP_CLIENT_INFO
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 处理邮件监控
|
|
27
|
+
* @param {Object} emailPlugin - EmailPlugin 实例
|
|
28
|
+
* @param {Object} args - 参数
|
|
29
|
+
* @returns {Object} 操作结果
|
|
30
|
+
*/
|
|
31
|
+
function handleEmailWatch(emailPlugin, args) {
|
|
32
|
+
const { action, interval, box } = args
|
|
33
|
+
|
|
34
|
+
switch (action) {
|
|
35
|
+
case 'start':
|
|
36
|
+
return emailPlugin._startEmailWatch({ interval, box })
|
|
37
|
+
|
|
38
|
+
case 'stop':
|
|
39
|
+
return emailPlugin._stopEmailWatch()
|
|
40
|
+
|
|
41
|
+
case 'status':
|
|
42
|
+
return emailPlugin._getEmailWatchStatus()
|
|
43
|
+
|
|
44
|
+
default:
|
|
45
|
+
return { success: false, error: `Unknown action: ${action}` }
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 启动邮件监控
|
|
51
|
+
* @param {Object} emailPlugin - EmailPlugin 实例
|
|
52
|
+
* @param {Object} config - 监控配置
|
|
53
|
+
* @returns {Object} 操作结果
|
|
54
|
+
*/
|
|
55
|
+
function startEmailWatch(emailPlugin, config) {
|
|
56
|
+
if (emailPlugin._watchEnabled) {
|
|
57
|
+
return { success: false, error: 'Email watch is already running' }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
emailPlugin._watchConfig = {
|
|
61
|
+
host: config.host || process.env.IMAP_HOST,
|
|
62
|
+
port: config.port || parseInt(process.env.IMAP_PORT) || 993,
|
|
63
|
+
user: config.user || process.env.IMAP_USER,
|
|
64
|
+
password: config.password || process.env.IMAP_PASS,
|
|
65
|
+
box: config.box || 'INBOX',
|
|
66
|
+
interval: (config.interval || EMAIL_DEFAULTS.interval) * 1000
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (!emailPlugin._watchConfig.user || !emailPlugin._watchConfig.password) {
|
|
70
|
+
return { success: false, error: 'IMAP user/password is required' }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
emailPlugin._watchEnabled = true
|
|
74
|
+
|
|
75
|
+
// 立即执行一次检查
|
|
76
|
+
emailPlugin._checkNewEmails().catch(err => {
|
|
77
|
+
emailPlugin._log.error('Initial check failed:', err.message)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
// 启动轮询
|
|
81
|
+
emailPlugin._watchInterval = setInterval(() => {
|
|
82
|
+
emailPlugin._checkNewEmails().catch(err => {
|
|
83
|
+
emailPlugin._log.error('Watch check failed:', err.message)
|
|
84
|
+
})
|
|
85
|
+
}, emailPlugin._watchConfig.interval)
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
success: true,
|
|
89
|
+
message: `Email watch started (interval: ${emailPlugin._watchConfig.interval / 1000}s)`,
|
|
90
|
+
config: {
|
|
91
|
+
host: emailPlugin._watchConfig.host,
|
|
92
|
+
box: emailPlugin._watchConfig.box,
|
|
93
|
+
interval: emailPlugin._watchConfig.interval / 1000
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 停止邮件监控
|
|
100
|
+
* @param {Object} emailPlugin - EmailPlugin 实例
|
|
101
|
+
* @returns {Object} 操作结果
|
|
102
|
+
*/
|
|
103
|
+
function stopEmailWatch(emailPlugin) {
|
|
104
|
+
if (!emailPlugin._watchEnabled) {
|
|
105
|
+
return { success: false, error: 'Email watch is not running' }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (emailPlugin._watchInterval) {
|
|
109
|
+
clearInterval(emailPlugin._watchInterval)
|
|
110
|
+
emailPlugin._watchInterval = null
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
emailPlugin._watchEnabled = false
|
|
114
|
+
emailPlugin._watchConfig = null
|
|
115
|
+
|
|
116
|
+
return { success: true, message: 'Email watch stopped' }
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 获取监控状态
|
|
121
|
+
* @param {Object} emailPlugin - EmailPlugin 实例
|
|
122
|
+
* @returns {Object} 状态信息
|
|
123
|
+
*/
|
|
124
|
+
function getEmailWatchStatus(emailPlugin) {
|
|
125
|
+
return {
|
|
126
|
+
success: true,
|
|
127
|
+
watching: emailPlugin._watchEnabled,
|
|
128
|
+
config: emailPlugin._watchConfig ? {
|
|
129
|
+
host: emailPlugin._watchConfig.host,
|
|
130
|
+
box: emailPlugin._watchConfig.box,
|
|
131
|
+
interval: emailPlugin._watchConfig.interval / 1000,
|
|
132
|
+
lastSeenUid: emailPlugin._lastSeenUid
|
|
133
|
+
} : null
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 检查新邮件
|
|
139
|
+
* @param {Object} emailPlugin - EmailPlugin 实例
|
|
140
|
+
* @returns {Promise<Object>} 检查结果
|
|
141
|
+
*/
|
|
142
|
+
async function checkNewEmails(emailPlugin) {
|
|
143
|
+
if (!emailPlugin._watchConfig) return
|
|
144
|
+
|
|
145
|
+
const callId = generateCallId()
|
|
146
|
+
emailPlugin._log.debug(`[${callId}] _checkNewEmails called, _checkInProgress=${emailPlugin._checkInProgress}`)
|
|
147
|
+
|
|
148
|
+
// 防止并发检查
|
|
149
|
+
if (emailPlugin._checkInProgress) {
|
|
150
|
+
emailPlugin._log.debug(`[${callId}] Skipping - already in progress`)
|
|
151
|
+
return
|
|
152
|
+
}
|
|
153
|
+
emailPlugin._checkInProgress = true
|
|
154
|
+
|
|
155
|
+
const imapConfig = createImapConfig(emailPlugin._watchConfig)
|
|
156
|
+
|
|
157
|
+
return new Promise((resolve, reject) => {
|
|
158
|
+
const imap = new Imap(imapConfig)
|
|
159
|
+
|
|
160
|
+
const cleanup = () => {
|
|
161
|
+
try { imap.end() } catch (e) {}
|
|
162
|
+
emailPlugin._checkInProgress = false
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
imap.on('ready', () => {
|
|
166
|
+
imap.openBox(emailPlugin._watchConfig.box, true, (err, box) => {
|
|
167
|
+
if (err) {
|
|
168
|
+
cleanup()
|
|
169
|
+
return reject(err)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// 搜索未读邮件
|
|
173
|
+
imap.search(['UNSEEN'], (err, results) => {
|
|
174
|
+
if (err) {
|
|
175
|
+
cleanup()
|
|
176
|
+
return reject(err)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (!results || results.length === 0) {
|
|
180
|
+
cleanup()
|
|
181
|
+
return resolve({ success: true, newEmails: 0 })
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// 找出新的未读邮件
|
|
185
|
+
const newEmails = results.filter(uid =>
|
|
186
|
+
!emailPlugin._lastSeenUid || uid > emailPlugin._lastSeenUid
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
if (newEmails.length > 0) {
|
|
190
|
+
const latestUid = Math.max(...newEmails)
|
|
191
|
+
// imap.fetch() 需要 UIDs 数组,传单个数字会导致获取从该 UID 到末尾的所有邮件
|
|
192
|
+
const f = imap.fetch([latestUid], { bodies: '' })
|
|
193
|
+
|
|
194
|
+
f.on('message', (msg) => {
|
|
195
|
+
let email = {}
|
|
196
|
+
let bodyParsed = false
|
|
197
|
+
|
|
198
|
+
msg.on('body', (stream) => {
|
|
199
|
+
simpleParser(stream).then(mail => {
|
|
200
|
+
email = {
|
|
201
|
+
uid: mail.uid || latestUid,
|
|
202
|
+
messageId: mail.messageId,
|
|
203
|
+
subject: mail.subject,
|
|
204
|
+
from: mail.from?.text || '',
|
|
205
|
+
to: mail.to?.text || '',
|
|
206
|
+
date: mail.date?.toISOString() || '',
|
|
207
|
+
text: mail.text || mail.textAsHtml || '',
|
|
208
|
+
html: mail.html,
|
|
209
|
+
attachments: mail.attachments?.map(a => ({
|
|
210
|
+
filename: a.filename,
|
|
211
|
+
contentType: a.contentType
|
|
212
|
+
})) || []
|
|
213
|
+
}
|
|
214
|
+
bodyParsed = true
|
|
215
|
+
}).catch(err => {
|
|
216
|
+
email.error = err.message
|
|
217
|
+
bodyParsed = true
|
|
218
|
+
})
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
msg.on('end', () => {
|
|
222
|
+
const checkDone = () => {
|
|
223
|
+
if (bodyParsed) {
|
|
224
|
+
// 更新最后看到的 UID
|
|
225
|
+
emailPlugin._lastSeenUid = latestUid
|
|
226
|
+
|
|
227
|
+
// 发送事件通知
|
|
228
|
+
emailPlugin._emitEmailReceived(email)
|
|
229
|
+
|
|
230
|
+
cleanup()
|
|
231
|
+
resolve({ success: true, newEmails: newEmails.length, email })
|
|
232
|
+
} else {
|
|
233
|
+
setTimeout(checkDone, 10)
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
checkDone()
|
|
237
|
+
})
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
f.on('error', (err) => {
|
|
241
|
+
cleanup()
|
|
242
|
+
reject(err)
|
|
243
|
+
})
|
|
244
|
+
} else {
|
|
245
|
+
cleanup()
|
|
246
|
+
resolve({ success: true, newEmails: 0 })
|
|
247
|
+
}
|
|
248
|
+
})
|
|
249
|
+
})
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
imap.on('error', (err) => {
|
|
253
|
+
cleanup()
|
|
254
|
+
reject(err)
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
imap.on('end', () => {})
|
|
258
|
+
|
|
259
|
+
imap.connect()
|
|
260
|
+
})
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
module.exports = {
|
|
264
|
+
handleEmailWatch,
|
|
265
|
+
startEmailWatch,
|
|
266
|
+
stopEmailWatch,
|
|
267
|
+
getEmailWatchStatus,
|
|
268
|
+
checkNewEmails
|
|
269
|
+
}
|