const local = ['127.0.0.1', '1', '192.168.1.254'];
const disallowRegex = /(https|http):\/\/.*/;
const allowed_chars = /^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .,\/?;:'\[\]\{\}\\<>\-_+=*!#$0123456789\^]*$/;
export default async function handle(req, res) {
    // Check if IP matches with the local IP
    // const ip = req.socket.remoteAddress.split('::ffff:')[1];
    // if(!local.includes(ip)) {
    //     return res.status(403).send('Forbidden. IP: ' + ip + '; Your IP in no way is logged!');
    // }
    const text = req.body?.text;
    if(!text) return res.status(400).send("Illegal request!");
    if(!allowed_chars.test(text)) return res.status(400).send("The message you were trying to send contains disallowed symbols!");
    if(disallowRegex.test(text)) return res.status(400).send("Nuh-uh! You don't send links here.");
    const webhookRes = await fetch(process.env.WEBHOOK, {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify({
            username: "TheRed.SH / User Feedback",
            content: text,
            avatar_url: "https://cdn.discordapp.com/avatars/574110505254256640/049c51674d7ccd748ca123556d351da5.webp?size=1024"
        })
    })
    if(webhookRes.ok) {
        res.status(200).send("Your message has been successfully sent!");
        webhookRes.text().then(i => console.log(i));
    } else {
        res.status(500).send("Error! The message has failed to send!");
        webhookRes.text().then(i => console.log(i));
    }
}