File size: 8,755 Bytes
b51f54a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/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);
    }

    // 读取原始.env文件内容
    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;
  }
}

// 保存更新后的邮箱配置到.env文件
function saveEmailConfigs() {
  try {
    // 将邮箱配置格式化为JSON字符串
    const configStr = JSON.stringify(emailConfigs);
    
    // 替换.env文件中的配置
    let newEnvContent = '';
    
    if (envContent.includes('REGISTER_EMAIL_CONFIGS=')) {
      // 使用正则表达式替换REGISTER_EMAIL_CONFIGS行
      newEnvContent = envContent.replace(
        /REGISTER_EMAIL_CONFIGS=.*/,
        `REGISTER_EMAIL_CONFIGS=${configStr}`
      );
    } else {
      // 如果不存在该配置行,添加到文件末尾
      newEnvContent = `${envContent}\nREGISTER_EMAIL_CONFIGS=${configStr}`;
    }
    
    // 同时确保USE_CONFIG_FILE设置为false
    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`;
    }
    
    // 确保EMAIL_SERVER设置为IMAP
    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}`);
      
      // 保存到.env文件
      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}`);
        
        // 保存到.env文件
        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}`);
        
        // 保存到.env文件
        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();