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

Function handler

plugins/bfread.js:7–72  ·  view source on GitHub ↗
(sock, message, args, context)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected