require("dotenv").config({quiet: true}); const exec = require("child_process").exec; const Discord = require("discord.js"); const hook = new Discord.WebhookClient({ url: process.env.DISCORD_WEBHOOK_URL }); const main = async () => { const runAsterisk = (command) => new Promise((resolve) => exec(command, (error, stdout, stderr) => resolve({ error, stdout, stderr })) ); try { var forwards = {}; const commands = [ 'asterisk -x "database show CF"', 'asterisk -x "database show CFB"', 'asterisk -x "database show CFU"', ]; for (const command of commands) { const { error, stdout, stderr } = await runAsterisk(command); if (error) { console.error(`Error executing command "${command}": ${error}`); continue; } const lines = stdout.split('\n'); for (const line of lines) { const match = line.match(/^\/(CF|CFB|CFU)\/(\d+)\s+:\s+(.+)$/); if (match) { const type = match[1]; const extension = match[2]; const target = match[3]; forwards[`${type}/${extension}`] = target.replace(/\s+$/, ''); } } } // Search all forwards for any target that is between 700 and 800. If it exists, log to console and run `asterisk -x "database del CF[U/B] "` to delete it for (const [key, target] of Object.entries(forwards)) { const targetExt = parseInt(target, 10); if (targetExt >= 700 && targetExt < 800) { console.log(`Deleting forward ${key} to target ${target}`); await hook.send(`Fuckass with extension ${key.split('/')[1]} tried to forward to ${target}. Forward has been deleted.`); const delCommand = `asterisk -x "database del ${key.replace('/', ' ')}"`; const { error, stdout, stderr } = await runAsterisk(delCommand); if (error) { console.error(`Error executing command "${delCommand}": ${error}`); } else { console.log(`Successfully deleted forward ${key}`); } } } } catch (err) { console.error(`Unexpected error: ${err}`); } setTimeout(main, 3000); // Run every 60 seconds }; const startup = async () => { // Run `asterisk -x "core show version"` to get the Asterisk version. If it fails, wait 10 seconds before running main again exec('asterisk -x "core show version"', (error, stdout, stderr) => { if (error) { console.error(`Error executing command: ${error}`); console.log("Asterisk is not running. Retrying in 10 seconds..."); setTimeout(startup, 10000); return; } const version = stdout.trim(); console.log(`Asterisk version: ${version}`); main(); }); } startup();