meriley
meriley

Reputation: 1881

IRCLib Java created IRC Bot issues

I'm using the IRCLib for Java to create an IRC bot. Its using Moepii as the client which is provided on the library. Issue with Moepii is it does not have built in Flood prevention so I'm going to need to implement this my self. Currently my bot when responding to a lot of commands triggered the Quakesnet excess flood and gets the boot.

Does anyone have any suggestions on how I could implement a flood protection? One idea I've gotten is to assume a 1024 byte buffer, once reached send a Ping to the server and wait for a response before continuing. I've never worked with IRC before so I was hoping for some pointers.

Current implementation

private class FloodMonitor implements Runnable {

    private final int MAXBYTES = 512;
    private int messageBuffer = 0;
    private boolean suspend = false;
    private boolean stop = false;

    @Override
    public void run() {

        while (!stop) {
            while (!suspend) {
                MessageStructure message = out.peek();
                if (message != null) {
                    messageBuffer += message.msg.getBytes().length;
                    if (messageBuffer < MAXBYTES) {
                        out.poll().sendMessage();
                    } else {
                        suspend();
                        message.bot.send("ping irc.quakenet.org");
                    }
                }
            }
        }
    }

    public void resetBuffer() {
        messageBuffer = 0;
        suspend = false;
    }

    public void stop() {
        stop = true;
    }

    public void suspend() {
        suspend = true;
    }
}

private class MessageStructure {

    public String target;
    public String msg;
    public BotConnection bot;

    MessageStructure(String target, String msg, BotConnection bot) {
        this.target = target;
        this.msg = msg;
        this.bot = bot;
    }

    private void sendMessage() {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                bot.doPrivmsgApproved(target, msg);
            }
        });

    }
}

Upvotes: 0

Views: 299

Answers (1)

meriley
meriley

Reputation: 1881

private class FloodMonitor implements Runnable {

    private final int MAXBYTES = 768;
    private int messageBuffer = 0;
    private boolean suspend = false;
    private boolean stop = false;

    @Override
    public void run() {

        while (!stop) {
            while (!suspend) {
                final MessageStructure message = out.peek();
                if (message != null) {
                    messageBuffer += message.msg.getBytes().length;
                    if (messageBuffer < MAXBYTES) {
                        out.poll().sendMessage();
                    } else {
                        suspend();

                        SwingUtilities.invokeLater(new Runnable() {

                            @Override
                            public void run() {
                                message.bot.send("ping irc.quakenet.org");
                            }
                        });

                    }
                }
            }
        }
    }

    public void resetBuffer() {
        messageBuffer = 0;
        suspend = false;
    }

    public void stop() {
        stop = true;
    }

    public void suspend() {
        suspend = true;
    }
}

Upvotes: 1

Related Questions