Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
3.6 kB
6
Indexable
package wtf.nebula.impl.irc;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.teavm.jso.JSBody;
import org.teavm.jso.dom.html.HTMLDocument;
import org.teavm.jso.websocket.WebSocket;
import wtf.nebula.util.Globals;

import java.util.*;

public class IRCServer implements Globals {
    private WebSocket socket;
    private String userKey;

    private int pingIntervalId;

    public IRCServer() {
        // WebSocket initialization
        socket = WebSocket.create("wss://brick-iron-shift.glitch.me");

        socket.onOpen(evt -> onOpen());
        socket.onMessage(evt -> onMessage(evt.getData()));
        socket.onClose(evt -> onClose());

        socket.setOnError(evt -> {
            sendIRCMessage("WebSocket error occurred");
        });
    }

    private void onOpen() {
        System.out.println("[IRC] Connected to IRC");

        JsonObject object = new JsonObject();
        object.addProperty("op", OpCodes.CONNECT);
        object.addProperty("d", mc.session.getUsername());

        sendMessage(object.toString());

        // Set a periodic task for pinging the server
        pingIntervalId = setInterval(() -> {
            JsonObject ping = new JsonObject();
            ping.addProperty("op", OpCodes.PING);
            ping.addProperty("d", "");
            sendMessage(ping.toString());
        }, 20000);

        sendIRCMessage("Connected to IRC");
    }

    private void onMessage(String message) {
        JsonObject object = new JsonParser().parse(message).getAsJsonObject();

        if (object.has("op") && object.has("d")) {
            int opCode = object.get("op").getAsInt();
            String data = object.get("d").getAsString();

            switch (opCode) {
                case OpCodes.ID:
                    userKey = data;
                    System.out.println("[IRC] User key received.");
                    break;

                case OpCodes.MESSAGE_RECEIVE:
                    sendIRCMessage(data);
                    break;

                case OpCodes.DISCONNECTED:
                    sendIRCMessage("Disconnected from IRC: " + data);
                    break;
            }
        }
    }

    private void onClose() {
        sendIRCMessage("You were disconnected from the IRC chat.");
        System.out.println("[IRC] Disconnected.");
        clearInterval(pingIntervalId);
    }

    public void disconnect() {
        if (pingIntervalId != 0) {
            clearInterval(pingIntervalId);
        }

        if (socket != null) {
            socket.close();
        }
    }

    public void sendMessage(String msg) {
        if (socket.getReadyState() == WebSocket.OPEN) {
            socket.send(msg);
        } else {
            sendIRCMessage("You are not currently connected to the IRC server");
        }
    }

    public void sendIRCChatMessage(String message) {
        JsonObject object = new JsonObject();
        object.addProperty("op", OpCodes.MESSAGE_SEND);
        object.addProperty("d", message);

        sendMessage(object.toString());
    }

    @JSBody(params = { "callback", "interval" }, script = "return setInterval(callback, interval);")
    private static native int setInterval(Runnable callback, int interval);

    @JSBody(params = { "id" }, script = "clearInterval(id);")
    private static native void clearInterval(int id);

    @JSBody(script = "return document;")
    public static native HTMLDocument getDocument();
}
Leave a Comment