natureco-cli 1.0.38 → 1.0.40

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "natureco-cli",
3
- "version": "1.0.38",
3
+ "version": "1.0.40",
4
4
  "description": "NatureCo AI Bot Terminal Interface",
5
5
  "main": "bin/natureco.js",
6
6
  "bin": {
@@ -45,6 +45,12 @@ function log(module, message, color = 'white') {
45
45
  }
46
46
  }
47
47
 
48
+ // Number matching helper - compare last 10 digits only
49
+ function numberMatches(incoming, allowed) {
50
+ const clean = (n) => n.replace(/\D/g, '').slice(-10);
51
+ return clean(incoming) === clean(allowed);
52
+ }
53
+
48
54
  async function gatewayServer(action) {
49
55
  // Check if running as background worker
50
56
  if (process.argv.includes('--gateway-worker')) {
@@ -239,8 +245,8 @@ async function startWhatsAppProvider(sessionDir, config) {
239
245
  // Log incoming number before access control
240
246
  log('whatsapp', `Incoming from: +${sender}, allowed: ${JSON.stringify(allowedNumbers)}`, 'gray');
241
247
 
242
- // Access control
243
- if (allowedNumbers.length > 0 && !allowedNumbers.includes(sender)) {
248
+ // Access control - compare last 10 digits
249
+ if (allowedNumbers.length > 0 && !allowedNumbers.some(n => numberMatches(n, sender))) {
244
250
  log('whatsapp', `blocked message from +${sender} (not in allowed list)`, 'yellow');
245
251
  continue;
246
252
  }
@@ -42,8 +42,18 @@ async function whatsapp(action) {
42
42
  return statusWhatsApp();
43
43
  }
44
44
 
45
+ if (action === 'allow') {
46
+ const number = process.argv[4];
47
+ if (!number) {
48
+ console.log(chalk.red('\n❌ Numara belirtmelisiniz\n'));
49
+ console.log(chalk.gray('Kullanım: natureco whatsapp allow <numara>\n'));
50
+ process.exit(1);
51
+ }
52
+ return allowNumber(number);
53
+ }
54
+
45
55
  console.log(chalk.red('\n❌ Unknown action\n'));
46
- console.log(chalk.gray('Available actions: connect, disconnect, status\n'));
56
+ console.log(chalk.gray('Available actions: connect, disconnect, status, allow\n'));
47
57
  process.exit(1);
48
58
  }
49
59
 
@@ -91,8 +101,13 @@ async function connectWhatsApp() {
91
101
  {
92
102
  type: 'input',
93
103
  name: 'allowedNumbers',
94
- message: 'Hangi numaralar mesaj gönderebilir? (virgülle ayırın, boş bırakırsan sadece kendi numaran)',
95
- default: '',
104
+ message: 'Hangi numaralar mesaj gönderebilir? (virgülle ayırın, zorunlu)',
105
+ validate: (input) => {
106
+ if (!input || input.trim().length === 0) {
107
+ return 'En az bir numara girmelisiniz';
108
+ }
109
+ return true;
110
+ },
96
111
  },
97
112
  ]);
98
113
 
@@ -111,7 +126,7 @@ async function connectWhatsApp() {
111
126
  // Parse allowed numbers
112
127
  const allowedNumbersList = allowedNumbers
113
128
  .split(',')
114
- .map(n => n.trim())
129
+ .map(n => n.trim().replace(/[\s\+\-\(\)]/g, ''))
115
130
  .filter(n => n.length > 0);
116
131
 
117
132
  // Start connection (only for QR code)
@@ -192,20 +207,16 @@ async function startWhatsAppConnection(sessionDir, botId, selectedBot, config, a
192
207
  console.log(chalk.cyan('Telefon:'), chalk.white(sock.user?.id || 'Unknown'));
193
208
  console.log(chalk.gray('\nSession kaydedildi: ~/.natureco/whatsapp-sessions/'));
194
209
 
195
- // Prepare allowed numbers list (add own number)
196
- const ownNumber = sock.user?.id?.split(':')[0].replace('@s.whatsapp.net', '');
197
- const finalAllowedNumbers = ownNumber ? [ownNumber, ...allowedNumbersList] : allowedNumbersList;
198
-
199
- // Save to config
210
+ // Save to config with allowed numbers
200
211
  config.whatsappConnected = true;
201
212
  config.whatsappBotId = botId;
202
213
  config.whatsappPhone = sock.user?.id;
203
- config.whatsappAllowedNumbers = finalAllowedNumbers;
214
+ config.whatsappAllowedNumbers = allowedNumbersList;
204
215
  saveConfig(config);
205
216
 
206
- if (finalAllowedNumbers.length > 0) {
217
+ if (allowedNumbersList.length > 0) {
207
218
  console.log(chalk.cyan('\nİzin verilen numaralar:'));
208
- finalAllowedNumbers.forEach(num => console.log(chalk.gray(` - ${num}`)));
219
+ allowedNumbersList.forEach(num => console.log(chalk.gray(` - +${num}`)));
209
220
  }
210
221
 
211
222
  console.log(chalk.green('\n✅ Kurulum tamamlandı!\n'));
@@ -305,6 +316,15 @@ function statusWhatsApp() {
305
316
  console.log(chalk.cyan('Phone:'), chalk.white(config.whatsappPhone));
306
317
  }
307
318
 
319
+ // Show allowed numbers
320
+ const allowedNumbers = config.whatsappAllowedNumbers || [];
321
+ if (allowedNumbers.length === 0) {
322
+ console.log(chalk.cyan('İzin listesi:'), chalk.gray('Boş (herkesten mesaj kabul edilir)'));
323
+ } else {
324
+ console.log(chalk.cyan('İzin listesi:'));
325
+ allowedNumbers.forEach(num => console.log(chalk.white(` - +${num}`)));
326
+ }
327
+
308
328
  // Check if session files exist
309
329
  const sessionDir = path.join(WHATSAPP_SESSION_DIR, config.whatsappBotId);
310
330
  if (fs.existsSync(sessionDir)) {
@@ -317,4 +337,39 @@ function statusWhatsApp() {
317
337
  console.log(chalk.gray('\nDisconnect with: natureco whatsapp disconnect\n'));
318
338
  }
319
339
 
340
+ function allowNumber(number) {
341
+ const config = getConfig();
342
+
343
+ if (!config.whatsappConnected) {
344
+ console.log(chalk.red('\n❌ WhatsApp not connected\n'));
345
+ console.log(chalk.gray('Connect first with: natureco whatsapp connect\n'));
346
+ process.exit(1);
347
+ }
348
+
349
+ // Normalize number (remove +, spaces, etc.)
350
+ const normalized = number.replace(/[\s\+\-\(\)]/g, '');
351
+
352
+ if (!/^\d+$/.test(normalized)) {
353
+ console.log(chalk.red('\n❌ Geçersiz numara formatı\n'));
354
+ console.log(chalk.gray('Örnek: natureco whatsapp allow 905551234567\n'));
355
+ process.exit(1);
356
+ }
357
+
358
+ const allowedNumbers = config.whatsappAllowedNumbers || [];
359
+
360
+ if (allowedNumbers.includes(normalized)) {
361
+ console.log(chalk.yellow('\n⚠️ Bu numara zaten izin listesinde\n'));
362
+ return;
363
+ }
364
+
365
+ allowedNumbers.push(normalized);
366
+ config.whatsappAllowedNumbers = allowedNumbers;
367
+ saveConfig(config);
368
+
369
+ console.log(chalk.green('\n✅ Numara izin listesine eklendi\n'));
370
+ console.log(chalk.cyan('Numara:'), chalk.white(`+${normalized}`));
371
+ console.log(chalk.cyan('Toplam:'), chalk.white(`${allowedNumbers.length} numara`));
372
+ console.log(chalk.gray('\nGateway\'i yeniden başlatın: natureco gateway stop && natureco gateway start\n'));
373
+ }
374
+
320
375
  module.exports = whatsapp;