简体   繁体   中英

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);
            }
        });

    }
}
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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM