|
|
#!/usr/bin/env node |
|
|
|
|
|
const fs = require('fs'); |
|
|
const path = require('path'); |
|
|
const readline = require('readline'); |
|
|
const dotenv = require('dotenv'); |
|
|
|
|
|
|
|
|
const rl = readline.createInterface({ |
|
|
input: process.stdin, |
|
|
output: process.stdout |
|
|
}); |
|
|
|
|
|
|
|
|
const ENV_FILE_PATH = path.join(process.cwd(), '.env'); |
|
|
let envContent = ''; |
|
|
let emailConfigs = []; |
|
|
|
|
|
|
|
|
function printAppPasswordInstructions() { |
|
|
console.log('\n===== 如何创建谷歌应用密码 ====='); |
|
|
console.log('1. 访问 https://myaccount.google.com/security'); |
|
|
console.log('2. 在"登录Google"部分,点击"两步验证"'); |
|
|
console.log(' (如果未启用两步验证,需要先启用)'); |
|
|
console.log('3. 在页面底部找到"应用密码",点击进入'); |
|
|
console.log('4. 在"选择应用"下拉菜单中选择"其他(自定义名称)"'); |
|
|
console.log('5. 输入一个名称,例如"Cursor注册"'); |
|
|
console.log('6. 点击"生成"'); |
|
|
console.log('7. 复制生成的16位应用密码(格式如:xxxx xxxx xxxx xxxx)'); |
|
|
console.log('注意: 应用密码只会显示一次,请务必保存好\n'); |
|
|
} |
|
|
|
|
|
|
|
|
function loadEnvironment() { |
|
|
try { |
|
|
if (!fs.existsSync(ENV_FILE_PATH)) { |
|
|
console.error('❌ .env文件不存在,请先运行setup.js进行初始化配置'); |
|
|
process.exit(1); |
|
|
} |
|
|
|
|
|
|
|
|
envContent = fs.readFileSync(ENV_FILE_PATH, 'utf8'); |
|
|
|
|
|
|
|
|
dotenv.config(); |
|
|
|
|
|
|
|
|
try { |
|
|
const configStr = process.env.REGISTER_EMAIL_CONFIGS; |
|
|
if (configStr) { |
|
|
emailConfigs = JSON.parse(configStr); |
|
|
if (!Array.isArray(emailConfigs)) { |
|
|
emailConfigs = []; |
|
|
} |
|
|
} |
|
|
} catch (parseErr) { |
|
|
console.warn('⚠️ 解析当前邮箱配置出错,将使用空配置'); |
|
|
emailConfigs = []; |
|
|
} |
|
|
|
|
|
return true; |
|
|
} catch (error) { |
|
|
console.error(`❌ 加载环境变量失败: ${error.message}`); |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function saveEmailConfigs() { |
|
|
try { |
|
|
|
|
|
const configStr = JSON.stringify(emailConfigs); |
|
|
|
|
|
|
|
|
let newEnvContent = ''; |
|
|
|
|
|
if (envContent.includes('REGISTER_EMAIL_CONFIGS=')) { |
|
|
|
|
|
newEnvContent = envContent.replace( |
|
|
/REGISTER_EMAIL_CONFIGS=.*/, |
|
|
`REGISTER_EMAIL_CONFIGS=${configStr}` |
|
|
); |
|
|
} else { |
|
|
|
|
|
newEnvContent = `${envContent}\nREGISTER_EMAIL_CONFIGS=${configStr}`; |
|
|
} |
|
|
|
|
|
|
|
|
if (newEnvContent.includes('REGISTER_USE_CONFIG_FILE=')) { |
|
|
newEnvContent = newEnvContent.replace( |
|
|
/REGISTER_USE_CONFIG_FILE=.*/, |
|
|
'REGISTER_USE_CONFIG_FILE=false' |
|
|
); |
|
|
} else { |
|
|
newEnvContent = `${newEnvContent}\nREGISTER_USE_CONFIG_FILE=false`; |
|
|
} |
|
|
|
|
|
|
|
|
if (newEnvContent.includes('REGISTER_EMAIL_SERVER=')) { |
|
|
newEnvContent = newEnvContent.replace( |
|
|
/REGISTER_EMAIL_SERVER=.*/, |
|
|
'REGISTER_EMAIL_SERVER=IMAP' |
|
|
); |
|
|
} else { |
|
|
newEnvContent = `${newEnvContent}\nREGISTER_EMAIL_SERVER=IMAP`; |
|
|
} |
|
|
|
|
|
|
|
|
fs.writeFileSync(ENV_FILE_PATH, newEnvContent, 'utf8'); |
|
|
|
|
|
console.log('✅ 邮箱配置已成功保存到.env文件'); |
|
|
return true; |
|
|
} catch (error) { |
|
|
console.error(`❌ 保存邮箱配置失败: ${error.message}`); |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function displayEmails() { |
|
|
console.log('\n===== 当前已配置的邮箱 ====='); |
|
|
|
|
|
if (emailConfigs.length === 0) { |
|
|
console.log('暂无已配置的邮箱'); |
|
|
return; |
|
|
} |
|
|
|
|
|
emailConfigs.forEach((config, index) => { |
|
|
console.log(`[${index + 1}] ${config.email}`); |
|
|
console.log(` IMAP服务器: ${config.imap_server}`); |
|
|
console.log(` IMAP端口: ${config.imap_port}`); |
|
|
console.log(` 用户名: ${config.username}`); |
|
|
console.log(` 应用密码: ${config.password}`); |
|
|
console.log(''); |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
function addEmail() { |
|
|
console.log('\n===== 添加新邮箱 ====='); |
|
|
printAppPasswordInstructions(); |
|
|
|
|
|
rl.question('请输入Gmail地址: ', (email) => { |
|
|
rl.question('请输入Gmail的应用密码 (不是邮箱密码): ', (password) => { |
|
|
|
|
|
const newConfig = { |
|
|
email: email, |
|
|
imap_server: 'imap.gmail.com', |
|
|
imap_port: 993, |
|
|
username: email, |
|
|
password: password |
|
|
}; |
|
|
|
|
|
|
|
|
emailConfigs.push(newConfig); |
|
|
|
|
|
console.log(`\n✅ 已添加邮箱: ${email}`); |
|
|
|
|
|
|
|
|
if (saveEmailConfigs()) { |
|
|
showMainMenu(); |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
function modifyEmail() { |
|
|
if (emailConfigs.length === 0) { |
|
|
console.log('\n❌ 当前没有可修改的邮箱。请先添加邮箱。'); |
|
|
showMainMenu(); |
|
|
return; |
|
|
} |
|
|
|
|
|
console.log('\n===== 修改邮箱 ====='); |
|
|
displayEmails(); |
|
|
|
|
|
rl.question('请输入要修改的邮箱序号 (1-' + emailConfigs.length + '): ', (indexStr) => { |
|
|
const index = parseInt(indexStr) - 1; |
|
|
|
|
|
if (isNaN(index) || index < 0 || index >= emailConfigs.length) { |
|
|
console.log('\n❌ 无效的序号。请重新选择。'); |
|
|
modifyEmail(); |
|
|
return; |
|
|
} |
|
|
|
|
|
const currentConfig = emailConfigs[index]; |
|
|
|
|
|
console.log(`\n正在修改邮箱: ${currentConfig.email}`); |
|
|
|
|
|
rl.question(`新的Gmail地址 (当前: ${currentConfig.email},直接回车保持不变): `, (email) => { |
|
|
const newEmail = email.trim() === '' ? currentConfig.email : email; |
|
|
|
|
|
rl.question('新的应用密码 (直接回车保持不变): ', (password) => { |
|
|
const newPassword = password.trim() === '' ? currentConfig.password : password; |
|
|
|
|
|
|
|
|
emailConfigs[index] = { |
|
|
email: newEmail, |
|
|
imap_server: 'imap.gmail.com', |
|
|
imap_port: 993, |
|
|
username: newEmail, |
|
|
password: newPassword |
|
|
}; |
|
|
|
|
|
console.log(`\n✅ 已修改邮箱配置: ${newEmail}`); |
|
|
|
|
|
|
|
|
if (saveEmailConfigs()) { |
|
|
showMainMenu(); |
|
|
} |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
function deleteEmail() { |
|
|
if (emailConfigs.length === 0) { |
|
|
console.log('\n❌ 当前没有可删除的邮箱。'); |
|
|
showMainMenu(); |
|
|
return; |
|
|
} |
|
|
|
|
|
console.log('\n===== 删除邮箱 ====='); |
|
|
displayEmails(); |
|
|
|
|
|
rl.question('请输入要删除的邮箱序号 (1-' + emailConfigs.length + '): ', (indexStr) => { |
|
|
const index = parseInt(indexStr) - 1; |
|
|
|
|
|
if (isNaN(index) || index < 0 || index >= emailConfigs.length) { |
|
|
console.log('\n❌ 无效的序号。请重新选择。'); |
|
|
deleteEmail(); |
|
|
return; |
|
|
} |
|
|
|
|
|
const emailToDelete = emailConfigs[index].email; |
|
|
|
|
|
rl.question(`确认删除邮箱 "${emailToDelete}"? (y/n): `, (answer) => { |
|
|
if (answer.toLowerCase() === 'y') { |
|
|
|
|
|
emailConfigs.splice(index, 1); |
|
|
|
|
|
console.log(`\n✅ 已删除邮箱: ${emailToDelete}`); |
|
|
|
|
|
|
|
|
if (saveEmailConfigs()) { |
|
|
showMainMenu(); |
|
|
} |
|
|
} else { |
|
|
console.log('\n操作已取消'); |
|
|
showMainMenu(); |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
function showMainMenu() { |
|
|
console.log('\n===== 邮箱配置管理 ====='); |
|
|
console.log('1. 查看所有邮箱'); |
|
|
console.log('2. 添加新邮箱'); |
|
|
console.log('3. 修改邮箱'); |
|
|
console.log('4. 删除邮箱'); |
|
|
console.log('0. 退出'); |
|
|
|
|
|
rl.question('请选择操作 (0-4): ', (choice) => { |
|
|
switch (choice) { |
|
|
case '1': |
|
|
displayEmails(); |
|
|
showMainMenu(); |
|
|
break; |
|
|
case '2': |
|
|
addEmail(); |
|
|
break; |
|
|
case '3': |
|
|
modifyEmail(); |
|
|
break; |
|
|
case '4': |
|
|
deleteEmail(); |
|
|
break; |
|
|
case '0': |
|
|
console.log('\n✅ 配置完成,退出程序'); |
|
|
rl.close(); |
|
|
break; |
|
|
default: |
|
|
console.log('\n❌ 无效的选择,请重新输入'); |
|
|
showMainMenu(); |
|
|
break; |
|
|
} |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
async function main() { |
|
|
console.log('===== Cursor-To-OpenAI 邮箱配置管理 ====='); |
|
|
|
|
|
|
|
|
if (loadEnvironment()) { |
|
|
|
|
|
showMainMenu(); |
|
|
} else { |
|
|
console.error('程序退出'); |
|
|
rl.close(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
main(); |