MCPcopy Create free account
hub / github.com/GlobalTechInfo/MEGA-MDX / handler

Function handler

plugins/bfread.ts:9–72  ·  view source on GitHub ↗
(sock: any, message: any, args: any, context: BotContext)

Source from the content-addressed store, hash-verified

7 usage: 'Reply to BF code with .bfdecode',
8
9 async handler(sock: any, message: any, args: any, context: BotContext) {
10 const chatId = context.chatId || message.key.remoteJid;
11
12 try {
13 let code = args?.join('') || "";
14
15 const quoted = message.message?.extendedTextMessage?.contextInfo?.quotedMessage;
16
17 if (quoted) {
18 code = quoted.conversation ||
19 quoted.extendedTextMessage?.text ||
20 quoted.imageMessage?.caption ||
21 quoted.videoMessage?.caption ||
22 "";
23 }
24
25 code = code.trim();
26
27 if (!code) {
28 return await sock.sendMessage(chatId, { text: '*Please reply to a Brainfuck code or provide it after the command.*' }, { quoted: message });
29 }
30
31 const bf = code.replace(/[^><+\-.,[\]]/g, '');
32 const tape = new Uint8Array(30000);
33 let ptr = 0, pc = 0, output = "", steps = 0;
34 const maxSteps = 100000;
35
36 while (pc < bf.length && steps < maxSteps) {
37 const char = bf[pc];
38 if (char === '>') ptr++;
39 else if (char === '<') ptr--;
40 else if (char === '+') tape[ptr]++;
41 else if (char === '-') tape[ptr]--;
42 else if (char === '.') output += String.fromCharCode(tape[ptr]);
43 else if (char === '[') {
44 if (tape[ptr] === 0) {
45 let depth = 1;
46 while (depth > 0) {
47 pc++;
48 if (bf[pc] === '[') depth++;
49 if (bf[pc] === ']') depth--;
50 }
51 }
52 } else if (char === ']') {
53 if (tape[ptr] !== 0) {
54 let depth = 1;
55 while (depth > 0) {
56 pc--;
57 if (bf[pc] === ']') depth++;
58 if (bf[pc] === '[') depth--;
59 }
60 }
61 }
62 pc++;
63 steps++;
64 }
65
66 await sock.sendMessage(chatId, { text: `*🔓 Decoded Result:* \n\n${output || "_No output generated_"}` }, { quoted: message });

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected