File size: 7,809 Bytes
7226ab4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * ═══════════════════════════════════════════════════════════════════════
 * PERMISSION MANAGER - AKIRA BOT V21
 * ═══════════════════════════════════════════════════════════════════════
 * Sistema centralizado de gerenciamento de permissões
 * ═══════════════════════════════════════════════════════════════════════
 */

class PermissionManager {
  constructor() {
    // Proprietários - acesso total
    this.owners = [
      { 
        numero: '244937035662',
        nome: 'Isaac Quarenta',
        descricao: 'Desenvolvedor Principal',
        nivel: 'ROOT'
      },
      {
        numero: '244978787009',
        nome: 'Isaac Quarenta',
        descricao: 'Segundo Proprietário',
        nivel: 'ROOT'
      }
    ];

    // Permissões por comando
    this.commandPermissions = {
      // Comandos públicos
      'help': { nivel: 'public', rateLimitMultiplier: 0.5 },
      'menu': { nivel: 'public', rateLimitMultiplier: 0.5 },
      'ping': { nivel: 'public', rateLimitMultiplier: 0.5 },
      'info': { nivel: 'public', rateLimitMultiplier: 0.5 },
      'donate': { nivel: 'public', rateLimitMultiplier: 0.5 },
      'perfil': { nivel: 'public', rateLimitMultiplier: 1 },
      'profile': { nivel: 'public', rateLimitMultiplier: 1 },
      'registrar': { nivel: 'public', rateLimitMultiplier: 1 },
      'level': { nivel: 'public', rateLimitMultiplier: 1 },
      'sticker': { nivel: 'public', rateLimitMultiplier: 2 },
      'gif': { nivel: 'public', rateLimitMultiplier: 2.5 },
      'toimg': { nivel: 'public', rateLimitMultiplier: 1.5 },
      'play': { nivel: 'public', rateLimitMultiplier: 2 },
      'tts': { nivel: 'public', rateLimitMultiplier: 2 },

      // Comandos de dono
      'add': { nivel: 'owner', rateLimitMultiplier: 1, grupo: true },
      'remove': { nivel: 'owner', rateLimitMultiplier: 1, grupo: true },
      'kick': { nivel: 'owner', rateLimitMultiplier: 1, grupo: true },
      'ban': { nivel: 'owner', rateLimitMultiplier: 1, grupo: true },
      'promote': { nivel: 'owner', rateLimitMultiplier: 1, grupo: true },
      'demote': { nivel: 'owner', rateLimitMultiplier: 1, grupo: true },
      'mute': { nivel: 'owner', rateLimitMultiplier: 1, grupo: true },
      'desmute': { nivel: 'owner', rateLimitMultiplier: 1, grupo: true },
      'antilink': { nivel: 'owner', rateLimitMultiplier: 1, grupo: true },
      'warn': { nivel: 'owner', rateLimitMultiplier: 1, grupo: true },
      'clearwarn': { nivel: 'owner', rateLimitMultiplier: 1, grupo: true },
    };

    // Tipos de ações e seus limites
    this.actionLimits = {
      // Premium features com limite de 1x a cada 90 dias
      'premium_feature': {
        maxUsos: 1,
        janelaDias: 90,
        message: 'Feature Premium - Acesso 1x a cada 90 dias'
      },
      // Comandos normais com rate limiting
      'normal_command': {
        janelaSec: 8,
        maxPorJanela: 6,
        message: 'Aguarde antes de usar outro comando'
      },
      // Comandos de admin
      'admin_command': {
        janelaSec: 3,
        maxPorJanela: 10,
        message: 'Muitos comandos de admin muito rapido'
      }
    };

    // Configurações de segurança
    this.securityConfig = {
      // Máximo de mutes antes de remover automaticamente
      maxMutesBeforeRemove: 5,
      
      // Progressão de duração de mute (multiplicador)
      muteProgressionMultiplier: 2,
      
      // Duração base de mute em minutos
      baseMuteDuration: 5,
      
      // Padrões de link a detectar
      linkPatterns: [
        'https://',
        'http://',
        'www.',
        'bit.ly/',
        't.me/',
        'wa.me/',
        'chat.whatsapp.com/',
        'whatsapp.com/'
      ],

      // Comportamento ao detectar abuso
      abuseDetection: {
        enabled: true,
        deleteMessage: true,
        removeUser: true,
        logAction: true
      }
    };
  }

  /**
   * Verifica se usuário é proprietário
   */
  isOwner(numero, nome) {
    try {
      const numeroLimpo = String(numero).trim();
      const nomeLimpo = String(nome).trim();
      
      return this.owners.some(owner =>
        numeroLimpo === owner.numero && nomeLimpo === owner.nome
      );
    } catch (e) {
      return false;
    }
  }

  /**
   * Obtém informações do proprietário
   */
  getOwnerInfo(numero) {
    const numeroLimpo = String(numero).trim();
    return this.owners.find(owner => numeroLimpo === owner.numero);
  }

  /**
   * Verifica permissão para comando específico
   */
  hasPermissionForCommand(comando, numero, nome, ehGrupo = false) {
    const permConfig = this.commandPermissions[comando];
    
    if (!permConfig) {
      return false; // Comando não existe
    }

    // Comando público - todos podem usar
    if (permConfig.nivel === 'public') {
      return true;
    }

    // Comando de dono
    if (permConfig.nivel === 'owner') {
      const isOwner = this.isOwner(numero, nome);
      if (!isOwner) return false;
      
      // Se requer grupo, verifica se está em grupo
      if (permConfig.grupo && !ehGrupo) {
        return false;
      }
      
      return true;
    }

    return false;
  }

  /**
   * Obtém configuração de permissão para comando
   */
  getCommandConfig(comando) {
    return this.commandPermissions[comando] || null;
  }

  /**
   * Obtém múltiplo de rate limit para comando
   */
  getRateLimitMultiplier(comando) {
    const config = this.commandPermissions[comando];
    return config?.rateLimitMultiplier || 1;
  }

  /**
   * Valida padrão de link
   */
  containsLink(texto) {
    if (!texto) return false;
    const textLower = String(texto).toLowerCase();
    return this.securityConfig.linkPatterns.some(pattern =>
      textLower.includes(pattern.toLowerCase())
    );
  }

  /**
   * Obtém configuração de limite de ação
   */
  getActionLimitConfig(tipoAcao) {
    return this.actionLimits[tipoAcao];
  }

  /**
   * Calcula próxima duração de mute progressivo
   */
  getNextMuteDuration(muteCount) {
    const baseDuration = this.securityConfig.baseMuteDuration;
    const multiplier = this.securityConfig.muteProgressionMultiplier;
    
    // Fórmula: 5 * 2^(n-1)
    return Math.min(
      baseDuration * Math.pow(multiplier, muteCount),
      1440 // Máximo de 1 dia (1440 minutos)
    );
  }

  /**
   * Verifica se deve remover após muitos mutes
   */
  shouldRemoveAfterMute(muteCount) {
    return muteCount >= this.securityConfig.maxMutesBeforeRemove;
  }

  /**
   * Lista todos os proprietários
   */
  listOwners() {
    return this.owners.map(owner => ({
      numero: owner.numero,
      nome: owner.nome,
      descricao: owner.descricao
    }));
  }

  /**
   * Valida estrutura de permissões
   */
  validateStructure() {
    const errors = [];

    // Valida proprietários
    if (!Array.isArray(this.owners) || this.owners.length === 0) {
      errors.push('Nenhum proprietário definido');
    }

    this.owners.forEach((owner, idx) => {
      if (!owner.numero || !owner.nome) {
        errors.push(`Proprietário ${idx} incompleto`);
      }
    });

    // Valida comandos
    if (!this.commandPermissions || Object.keys(this.commandPermissions).length === 0) {
      errors.push('Nenhuma permissão de comando definida');
    }

    return {
      isValid: errors.length === 0,
      errors
    };
  }
}

module.exports = PermissionManager;