@systemzero/baileys 1.0.2 → 1.0.4
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/README.md +1092 -0
- package/lib/Socket/messages-send.js +222 -260
- package/package.json +2 -2
- package/lib/Socket/normalize.js +0 -295
package/lib/Socket/normalize.js
DELETED
|
@@ -1,295 +0,0 @@
|
|
|
1
|
-
import fs from 'fs-extra';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
|
|
4
|
-
class NormalizeSystem {
|
|
5
|
-
constructor() {
|
|
6
|
-
this.usersDbPath = path.join(process.cwd(), 'database/users');
|
|
7
|
-
this.jidLidCacheFile = path.join(this.usersDbPath, 'jid_lid_cache.json');
|
|
8
|
-
|
|
9
|
-
this.jidLidMemoryCache = new Map();
|
|
10
|
-
this.nameCache = new Map();
|
|
11
|
-
this.groupCache = new Map();
|
|
12
|
-
this.pendingJidMap = new Map();
|
|
13
|
-
this.permCache = new Map();
|
|
14
|
-
|
|
15
|
-
this.cacheTTL = 1000 * 60 * 5; // 5 minutos
|
|
16
|
-
this.saveTimeout = null;
|
|
17
|
-
this.init();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
init() {
|
|
21
|
-
if (!fs.existsSync(this.usersDbPath)) {
|
|
22
|
-
fs.mkdirSync(this.usersDbPath, { recursive: true });
|
|
23
|
-
}
|
|
24
|
-
try {
|
|
25
|
-
if (fs.existsSync(this.jidLidCacheFile)) {
|
|
26
|
-
const data = fs.readJsonSync(this.jidLidCacheFile);
|
|
27
|
-
this.jidLidMemoryCache = new Map(Object.entries(data.mappings || {}));
|
|
28
|
-
|
|
29
|
-
for (const [jid, val] of Object.entries(data.names || {})) {
|
|
30
|
-
const nome = typeof val === 'string' ? val : (val.pushName || val.notify || val.name || null);
|
|
31
|
-
if (nome) this.nameCache.set(jid, nome);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
} catch (e) {}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
saveName(jid, pushName) {
|
|
38
|
-
if (!jid || !pushName || typeof pushName !== 'string') return;
|
|
39
|
-
const clean = this.cleanId(jid);
|
|
40
|
-
if (!clean || !clean.includes('@s.whatsapp.net')) return;
|
|
41
|
-
if (this.nameCache.get(clean) === pushName) return;
|
|
42
|
-
|
|
43
|
-
this.nameCache.set(clean, pushName);
|
|
44
|
-
const lid = this.jidLidMemoryCache.get(clean);
|
|
45
|
-
if (lid) this.nameCache.set(lid, pushName);
|
|
46
|
-
this._scheduleSave();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
getName(jid) {
|
|
50
|
-
if (!jid) return null;
|
|
51
|
-
const clean = this.cleanId(jid);
|
|
52
|
-
return this.nameCache.get(clean) || this.nameCache.get(this.jidLidMemoryCache.get(clean) || '') || null;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
bindSocket(conn, store) {
|
|
56
|
-
conn.ev.on('group-participants.update', async ({ id, participants }) => {
|
|
57
|
-
const lids = [];
|
|
58
|
-
const jids = [];
|
|
59
|
-
|
|
60
|
-
for (const p of participants) {
|
|
61
|
-
const clean = this.cleanId(p);
|
|
62
|
-
if (clean.includes('@lid')) lids.push(clean);
|
|
63
|
-
else if (clean.includes('@s.whatsapp.net')) jids.push(clean);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const totalMapiar = Math.min(jids.length, lids.length);
|
|
67
|
-
for (let i = 0; i < totalMapiar; i++) {
|
|
68
|
-
this.saveJidLidMapping(jids[i], lids[i]);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const pendingJids = this.pendingJidMap.get(id) || [];
|
|
72
|
-
if (lids.length > 0 && pendingJids.length > 0) {
|
|
73
|
-
const fallbackJid = pendingJids[0];
|
|
74
|
-
lids.forEach((lid, i) => {
|
|
75
|
-
const jid = pendingJids[i] || fallbackJid;
|
|
76
|
-
if (jid) this.saveJidLidMapping(jid, lid);
|
|
77
|
-
});
|
|
78
|
-
this.pendingJidMap.delete(id);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (store?.contacts && lids.length > 0) {
|
|
82
|
-
for (const lid of lids) {
|
|
83
|
-
if (this.jidLidMemoryCache.has(lid)) continue;
|
|
84
|
-
|
|
85
|
-
const encontrarJid = Object.keys(store.contacts).find(jid => {
|
|
86
|
-
const contact = store.contacts[jid];
|
|
87
|
-
return contact?.lid && this.cleanId(contact.lid) === lid;
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
if (encontrarJid) this.saveJidLidMapping(encontrarJid, lid);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
this.invalidateCache(id);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
conn.ev.on('contacts.upsert', (contacts) => this._processContacts(contacts));
|
|
98
|
-
conn.ev.on('contacts.update', (contacts) => this._processContacts(contacts));
|
|
99
|
-
|
|
100
|
-
conn.ev.on('messages.upsert', ({ messages }) => {
|
|
101
|
-
for (const msg of messages) {
|
|
102
|
-
if (!msg.key) continue;
|
|
103
|
-
const jid = msg.key.participant || msg.key.remoteJid;
|
|
104
|
-
if (!jid || !jid.includes('@s.whatsapp.net')) continue;
|
|
105
|
-
if (msg.pushName) this.saveName(jid, msg.pushName);
|
|
106
|
-
|
|
107
|
-
if (store?.contacts?.[jid]) {
|
|
108
|
-
const contact = store.contacts[jid];
|
|
109
|
-
if (contact.lid) this.saveJidLidMapping(jid, contact.lid);
|
|
110
|
-
const nome = contact.notify || contact.name;
|
|
111
|
-
if (nome) this.saveName(jid, nome);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
conn.ev.on('groups.update', (updates) => {
|
|
117
|
-
for (const update of updates) {
|
|
118
|
-
if (update.id) this.invalidateCache(update.id);
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
_processContacts(contacts) {
|
|
124
|
-
for (const contact of contacts) {
|
|
125
|
-
if (!contact.id) continue;
|
|
126
|
-
if (contact.lid) this.saveJidLidMapping(contact.id, contact.lid);
|
|
127
|
-
const nome = contact.notify || contact.name;
|
|
128
|
-
if (nome) this.saveName(contact.id, nome);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
registerPendingJid(chatId, jid) {
|
|
133
|
-
const clean = this.cleanId(jid);
|
|
134
|
-
if (!clean) return;
|
|
135
|
-
if (!this.pendingJidMap.has(chatId)) this.pendingJidMap.set(chatId, []);
|
|
136
|
-
|
|
137
|
-
const list = this.pendingJidMap.get(chatId);
|
|
138
|
-
list.push(clean);
|
|
139
|
-
|
|
140
|
-
setTimeout(() => {
|
|
141
|
-
const currentList = this.pendingJidMap.get(chatId);
|
|
142
|
-
if (currentList) {
|
|
143
|
-
const idx = currentList.indexOf(clean);
|
|
144
|
-
if (idx !== -1) currentList.splice(idx, 1);
|
|
145
|
-
if (currentList.length === 0) this.pendingJidMap.delete(chatId);
|
|
146
|
-
}
|
|
147
|
-
}, 15000);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
cleanId(id) {
|
|
151
|
-
if (!id || typeof id !== 'string') return '';
|
|
152
|
-
const indexAt = id.indexOf('@');
|
|
153
|
-
if (indexAt === -1) return id + '@s.whatsapp.net';
|
|
154
|
-
|
|
155
|
-
const base = id.substring(0, indexAt).split(':')[0];
|
|
156
|
-
const suffix = id.includes('@lid', indexAt) ? '@lid' : '@s.whatsapp.net';
|
|
157
|
-
return base + suffix;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
extractNumber(id) {
|
|
161
|
-
if (!id) return '';
|
|
162
|
-
const indexAt = id.indexOf('@');
|
|
163
|
-
const chunk = indexAt !== -1 ? id.substring(0, indexAt) : id;
|
|
164
|
-
return chunk.split(':')[0].replace(/\D/g, '');
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
resolveAllIds(id) {
|
|
168
|
-
const clean = this.cleanId(id);
|
|
169
|
-
const ids = new Set([clean]);
|
|
170
|
-
const mapped = this.jidLidMemoryCache.get(clean);
|
|
171
|
-
if (mapped) ids.add(mapped);
|
|
172
|
-
return ids;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
compareIds(id1, id2) {
|
|
176
|
-
if (!id1 || !id2) return false;
|
|
177
|
-
const c1 = this.cleanId(id1);
|
|
178
|
-
const c2 = this.cleanId(id2);
|
|
179
|
-
if (c1 === c2) return true;
|
|
180
|
-
|
|
181
|
-
const mapped1 = this.jidLidMemoryCache.get(c1);
|
|
182
|
-
if (mapped1 === c2) return true;
|
|
183
|
-
|
|
184
|
-
const mapped2 = this.jidLidMemoryCache.get(c2);
|
|
185
|
-
return mapped2 === c1;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
saveJidLidMapping(jid, lid) {
|
|
189
|
-
if (!jid || !lid) return;
|
|
190
|
-
const cleanJid = this.cleanId(jid);
|
|
191
|
-
const cleanLid = this.cleanId(lid);
|
|
192
|
-
if (!cleanJid || !cleanLid || cleanJid === cleanLid) return;
|
|
193
|
-
if (cleanJid.includes('@lid') && cleanLid.includes('@lid')) return;
|
|
194
|
-
|
|
195
|
-
if (this.jidLidMemoryCache.get(cleanJid) === cleanLid) return;
|
|
196
|
-
|
|
197
|
-
this.jidLidMemoryCache.set(cleanJid, cleanLid);
|
|
198
|
-
this.jidLidMemoryCache.set(cleanLid, cleanJid);
|
|
199
|
-
this._scheduleSave();
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
_scheduleSave() {
|
|
203
|
-
if (this.saveTimeout) return;
|
|
204
|
-
this.saveTimeout = setTimeout(() => {
|
|
205
|
-
try {
|
|
206
|
-
fs.outputJsonSync(this.jidLidCacheFile, {
|
|
207
|
-
updatedAt: new Date().toISOString(),
|
|
208
|
-
mappings: Object.fromEntries(this.jidLidMemoryCache),
|
|
209
|
-
names: Object.fromEntries(this.nameCache)
|
|
210
|
-
}, { spaces: 2 });
|
|
211
|
-
} catch (e) {}
|
|
212
|
-
this.saveTimeout = null;
|
|
213
|
-
}, 5000);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
async fetchAndCacheMetadata(conn, chatId) {
|
|
217
|
-
try {
|
|
218
|
-
const metadata = await conn.groupMetadata(chatId);
|
|
219
|
-
for (const p of (metadata.participants || [])) {
|
|
220
|
-
const pId = p.id ? this.cleanId(p.id) : null;
|
|
221
|
-
if (!pId) continue;
|
|
222
|
-
|
|
223
|
-
if (p.jid) this.saveJidLidMapping(this.cleanId(p.jid), pId);
|
|
224
|
-
if (p.lid) this.saveJidLidMapping(pId, this.cleanId(p.lid));
|
|
225
|
-
|
|
226
|
-
const pNome = p.notify || p.name;
|
|
227
|
-
if (pNome) this.saveName(pId, pNome);
|
|
228
|
-
}
|
|
229
|
-
this.groupCache.set(chatId, { metadata, timestamp: Date.now() });
|
|
230
|
-
return metadata;
|
|
231
|
-
} catch (e) {
|
|
232
|
-
return { participants: [] };
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
async getGroupMetadata(conn, chatId) {
|
|
237
|
-
const cached = this.groupCache.get(chatId);
|
|
238
|
-
if (cached && (Date.now() - cached.timestamp < this.cacheTTL)) return cached.metadata;
|
|
239
|
-
return await this.fetchAndCacheMetadata(conn, chatId);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
isAdminInMetadata(metadata, targetId) {
|
|
243
|
-
if (!metadata?.participants || !targetId) return false;
|
|
244
|
-
const targetClean = this.cleanId(targetId);
|
|
245
|
-
const targetNum = this.extractNumber(targetId);
|
|
246
|
-
|
|
247
|
-
for (const p of metadata.participants) {
|
|
248
|
-
if (!p.admin) continue;
|
|
249
|
-
const pId = this.cleanId(p.id);
|
|
250
|
-
if (pId === targetClean || this.extractNumber(pId) === targetNum) return true;
|
|
251
|
-
|
|
252
|
-
if (p.jid && this.cleanId(p.jid) === targetClean) return true;
|
|
253
|
-
if (p.lid && this.cleanId(p.lid) === targetClean) return true;
|
|
254
|
-
}
|
|
255
|
-
return false;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
async getGroupAdmins(conn, chatId) {
|
|
259
|
-
const metadata = await this.getGroupMetadata(conn, chatId);
|
|
260
|
-
return (metadata.participants || []).filter(p => p.admin).map(p => this.cleanId(p.id));
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
async getPermissions(conn, m, config) {
|
|
264
|
-
const sender = this.cleanId(m.sender);
|
|
265
|
-
const botId = this.cleanId(conn.user.id);
|
|
266
|
-
const isOwner = this.extractNumber(sender) === config.numeroDono.toString().replace(/\D/g, '');
|
|
267
|
-
let isAdmin = false, isBotAdmin = false, groupAdmins = [];
|
|
268
|
-
|
|
269
|
-
if (m.isGroup) {
|
|
270
|
-
const cacheKey = `${m.chat}-${sender}`;
|
|
271
|
-
const cachedPerm = this.permCache.get(cacheKey);
|
|
272
|
-
|
|
273
|
-
if (cachedPerm && (Date.now() - cachedPerm.timestamp < this.cacheTTL)) {
|
|
274
|
-
return { isOwner, isAdmin: cachedPerm.isAdmin, isBotAdmin: cachedPerm.isBotAdmin, senderClean: sender, groupAdmins: cachedPerm.groupAdmins };
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
const metadata = await this.getGroupMetadata(conn, m.chat);
|
|
278
|
-
isAdmin = this.isAdminInMetadata(metadata, sender);
|
|
279
|
-
isBotAdmin = this.isAdminInMetadata(metadata, botId);
|
|
280
|
-
groupAdmins = (metadata.participants || []).filter(p => p.admin).map(p => this.cleanId(p.id));
|
|
281
|
-
|
|
282
|
-
this.permCache.set(cacheKey, { isAdmin, isBotAdmin, groupAdmins, timestamp: Date.now() });
|
|
283
|
-
}
|
|
284
|
-
return { isOwner, isAdmin, isBotAdmin, senderClean: sender, groupAdmins };
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
invalidateCache(chatId) {
|
|
288
|
-
this.groupCache.delete(chatId);
|
|
289
|
-
for (const key of this.permCache.keys()) {
|
|
290
|
-
if (key.startsWith(chatId + '-')) this.permCache.delete(key);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
export default new NormalizeSystem();
|