Hi guys,

I've been trying to make a script to display information about a certain user.

This script is still using the older HTTP post/get functions, but I've been trying to use the newer 'urlget' as well.

So this IRC script basically opens a connection to local NodeJS HTTP server (3000), makes a JSON request and expects data back.

Code
alias user {
    var %username = $1
    var %json = $+({ "username": " %username " })
    var %header = POST /user HTTP/1.1 $+ $crlf $+ Host: localhost $+ $crlf $+ Content-Type: application/json $+ $crlf $+ Content-Length: $+ $len(%json) $+ $crlf $+ $crlf
    var %request = %header $+ %json

    echo -a Send request: %request
    sockopen mysocket localhost 3000
    sockmark mysocket %request
}

on *:SOCKOPEN:mysocket: {
    if ($sockerr) return
    sockwrite -nt $sockname $gettok($sock($sockname).mark,1-,$crlf)
    ; Start loop to try and read data back
    .timer 1 1 sockread mysocket
}

on *:SOCKREAD:mysocket: {
    if ($sockerr) return
    var %data = $gettok($sock($sockname).mark,1-,$crlf);
    var %dataObj = $json(%data);
    var %username = $1;

    if ($length(%data) > 0) {
        ; Data available, so stop the loop
        .timer off
        if (%dataObj.success) {
            ; Send data back to IRC
            msg #ScriptTesting Information for user %username: %dataObj.data
        } else {
            ; Show errors in IRC chat
            echo %data
            echo %dataObj
            echo %username
            msg #ScriptTesting An error occurred while retrieving data for user %username: %dataObj.error
        }
        sockclose $sockname
    }
}

In previous version, I have not been succesful in receiving any data back, not even HTTP/JSON errors.

When trying this, it does give a small error: /sockread: socket unavailable (line 16, script2.ini)
But, the NodeJS script gets the data/username as requested and can do stuff with it.

To make it 'simple' for now, I want it to sendback the username as it was requested, to test the connection i/o. The part from IRC > NodeJS is working, but the other way around.. not so much.
The problems I first encountered was that the IRC script expected an instant answer, so it returned empty. At least I thought that was the problem. Now, I'm not so sure anymore..

The NodeJS script:
Code

import express from 'express';
import bodyParser from 'body-parser';
import puppeteer from 'puppeteer';

const app = express();
app.use(bodyParser.json());

app.post('/user', async (req, res) => {
    let username = req.body.username;
    // remove spaces
    username = username.replace(/\s/g, '');
    console.log(`Processing for user: ${username}`);

    try {
        // execute puppeteer script with username
        const userData = await fetchUserData(username);
        res.json({ success: true, data: userData });
        console.log(`Sending data "${userData}"`);
    } catch (error) {
        console.error('Error during processing the request:', error);
        res.status(500).json({ success: false, error: 'There was an error.' });
    }
});

app.listen(3000, () => {
    console.log('Server listening on port 3000');
});

async function fetchUserData(userName) {
    const browser = await puppeteer.launch({ headless: false });
    const page = await browser.newPage();
    await page.setViewport({ width: 1600, height: 1000, deviceScaleFactor: 1 });

    try {
        await page.goto('https://[redacted]/login.php');
        await page.type('#username', 'ballonnetje');
        await page.type('#password', '[redacted]]');
        await page.click('input[type="submit"][name="login"]');

        const targetUrl = `https://[redacted]]/user.php?action=search&search=${userName}`;
        await page.goto(targetUrl);

        const extractedData = `Info for user: ${userName}`; 

        await browser.close();
        return extractedData;
    } catch (error) {
        console.error('Error in Puppeteer-script:', error);
        throw new Error('Error executing Puppeteer-script.');
    }
}


When I insert user "test" the logs in IRC:
[time] Send request: POST /user HTTP/1.1 Host: localhost Content-Type: application/json Content-Length:24 { "username": " test " }
[time] * /sockread: socket unavailable (line 16, script2.ini)

The logs in NodeJS script:
Server listening on port 3000
Processing for user: test
Sending data "Info for user: test"

Debug script (AdiiRC)
Code

<- 22-11-2023 18:11:09 - var %username = $1 at line 2
-> 22-11-2023 18:11:09 - var %username test at line 2
<- 22-11-2023 18:11:09 - var %json = $+({ "username": " %username " }) at line 3
-> 22-11-2023 18:11:09 - var %json { "username": " test " } at line 3
<- 22-11-2023 18:11:09 - var %header = POST /user HTTP/1.1 $+ $crlf $+ Host: localhost $+ $crlf $+ Content-Type: application/json $+ $crlf $+ Content-Length: $+ $len(%json) $+ $crlf $+ $crlf at line 4
-> 22-11-2023 18:11:09 - var %header POST /user HTTP/1.1Host: localhostContent-Type: application/jsonContent-Length:24 at line 4
<- 22-11-2023 18:11:09 - var %request = %header $+ %json at line 5
-> 22-11-2023 18:11:09 - var %request POST /user HTTP/1.1Host: localhostContent-Type: application/jsonContent-Length:24{ "username": " test " } at line 5
<- 22-11-2023 18:11:09 - echo -a Send request: %request at line 7
-> 22-11-2023 18:11:09 - echo -a Send request: POST /user HTTP/1.1Host: localhostContent-Type: application/jsonContent-Length:24{ "username": " test " } at line 7
<- 22-11-2023 18:11:09 - sockopen mysocket localhost 3000 at line 8
-> 22-11-2023 18:11:09 - sockopen mysocket localhost 3000 at line 8
<- 22-11-2023 18:11:09 - sockmark mysocket %request at line 9
-> 22-11-2023 18:11:09 - sockmark mysocket POST /user HTTP/1.1Host: localhostContent-Type: application/jsonContent-Length:24{ "username": " test " } at line 9
<- 22-11-2023 18:11:09 - sockwrite -nt $sockname $gettok($sock($sockname).mark,1-,$crlf) at line 14
-> 22-11-2023 18:11:09 - sockwrite -nt mysocket POST /user HTTP/1.1Host: localhostContent-Type: application/jsonContent-Length:24{ "username": " test " }￿ at line 14
<- 22-11-2023 18:11:09 - .timer 1 1 sockread mysocket at line 16
-> 22-11-2023 18:11:09 - .timer 1 1 sockread mysocket at line 16
<- 22-11-2023 18:11:09 - var %data = $gettok($sock($sockname).mark,1-,$crlf); at line 21
-> 22-11-2023 18:11:09 - var %data POST /user HTTP/1.1Host: localhostContent-Type: application/jsonContent-Length:24{ "username": " test " }￿ at line 21
<- 22-11-2023 18:11:09 - var %dataObj = $json(%data); at line 22
-> 22-11-2023 18:11:09 - var %dataObj at line 22
<- 22-11-2023 18:11:09 - var %username = $1; at line 23
-> 22-11-2023 18:11:09 - var %username at line 23
<- 22-11-2023 18:11:10 - sockread mysocket at line 16


Oh yeah, so I tried the urlget as well. This constantly threw me an Parameters error: "USER Not enough parameters".

IRC Code:
Code
var %username = $1
var %json = { "username": %username }
var %url = http://localhost:3000/user
var %documents = $special(Documents)
var %filename = %documents\Gebruikersdata.txt
var %response = $urlget(%url, %filename, , , %json, , )

if (%response) {
   if (%response.status == 200) {
       echo -a Info for user %username: %response.data
   } else {
       echo -a Error getting info for username: %username: %response.error
   }
} else {
   echo -a Got no response from server
}

I'm hoping someone here can help me with this to at least get a response back to IRC.

Thanks in advantage!