Search in sources :

Example 1 with ListenerAdapter

use of org.pircbotx.hooks.ListenerAdapter in project ircbot-plugin by jenkinsci.

the class IRCConnection method connect.

// @Override
public boolean connect() {
    try {
        LOGGER.info(String.format("Connecting to %s:%s as %s using charset %s", this.descriptor.getHost(), this.descriptor.getPort(), this.descriptor.getNick(), this.descriptor.getCharset()));
        if (botThread != null) {
            botThread.interrupt();
        }
        final CountDownLatch connectLatch = new CountDownLatch(1);
        ListenerAdapter<PircBotX> connectListener = new ListenerAdapter<PircBotX>() {

            @Override
            public void onConnect(ConnectEvent<PircBotX> event) throws Exception {
                connectLatch.countDown();
                LOGGER.info("connected to IRC");
            }
        };
        cfg.addListener(connectListener);
        botThread = new Thread("IRC Bot") {

            public void run() {
                pircConnection = new PircBotX(cfg.buildConfiguration());
                try {
                    pircConnection.startBot();
                } catch (Exception e) {
                    LOGGER.warning("Error connecting to irc: " + e);
                }
            }
        };
        botThread.start();
        try {
            boolean connected = connectLatch.await(2, TimeUnit.MINUTES);
            if (!connected) {
                LOGGER.warning("Time out waiting for connecting to irc");
                close();
                return false;
            }
        } catch (InterruptedException e) {
            LOGGER.warning("Interrupted waiting for connecting to irc: " + e);
            Thread.currentThread().interrupt();
        }
        pircConnection.getConfiguration().getListenerManager().removeListener(connectListener);
        // final String nickServPassword = this.descriptor.getNickServPassword();
        // if(Util.fixEmpty(nickServPassword) != null) {
        // this.pircConnection.identify(nickServPassword);
        // 
        // if (!this.groupChats.isEmpty()) {
        // // Sleep some time so chances are good we're already identified
        // // when we try to join the channels.
        // // Unfortunately there seems to be no standard way in IRC to recognize
        // // if one has been identified already.
        // LOGGER.fine("Sleeping some time to wait for being authenticated");
        // try {
        // Thread.sleep(TimeUnit.SECONDS.toMillis(5));
        // } catch (InterruptedException e) {
        // // ignore
        // }
        // }
        // }
        joinGroupChats();
        return pircConnection.isConnected();
    } catch (RuntimeException e) {
        LOGGER.log(WARNING, "Error connecting to irc", e);
        return false;
    }
}
Also used : ListenerAdapter(org.pircbotx.hooks.ListenerAdapter) ConnectEvent(org.pircbotx.hooks.events.ConnectEvent) CountDownLatch(java.util.concurrent.CountDownLatch) PircBotX(org.pircbotx.PircBotX) IMException(hudson.plugins.im.IMException)

Aggregations

IMException (hudson.plugins.im.IMException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 PircBotX (org.pircbotx.PircBotX)1 ListenerAdapter (org.pircbotx.hooks.ListenerAdapter)1 ConnectEvent (org.pircbotx.hooks.events.ConnectEvent)1