Search in sources :

Example 1 with ConnectEvent

use of org.pircbotx.hooks.events.ConnectEvent 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)

Example 2 with ConnectEvent

use of org.pircbotx.hooks.events.ConnectEvent in project downlords-faf-client by FAForever.

the class PircBotXChatServiceTest method connect.

private void connect() throws Exception {
    instance.connect();
    verify(pircBotXFactory).createPircBotX(configurationCaptor.capture());
    CountDownLatch latch = listenForConnected();
    firePircBotXEvent(new ConnectEvent(pircBotX));
    assertTrue(latch.await(TIMEOUT, TIMEOUT_UNIT));
    UserHostmask nickServHostMask = mock(UserHostmask.class);
    when(nickServHostMask.getHostmask()).thenReturn("nickserv");
    when(configuration.getNickservNick()).thenReturn("nickserv");
    when(configuration.getNickservOnSuccess()).thenReturn("you are now");
    firePircBotXEvent(new NoticeEvent(pircBotX, nickServHostMask, null, null, "", "you are now identified"));
    SocialMessage socialMessage = new SocialMessage();
    socialMessage.setChannels(Collections.emptyList());
    socialMessageListenerCaptor.getValue().accept(socialMessage);
    verify(outputIrc, timeout(TIMEOUT).atLeastOnce()).joinChannel(DEFAULT_CHANNEL_NAME);
}
Also used : ConnectEvent(org.pircbotx.hooks.events.ConnectEvent) NoticeEvent(org.pircbotx.hooks.events.NoticeEvent) SocialMessage(com.faforever.client.remote.domain.SocialMessage) UserHostmask(org.pircbotx.UserHostmask) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)2 ConnectEvent (org.pircbotx.hooks.events.ConnectEvent)2 SocialMessage (com.faforever.client.remote.domain.SocialMessage)1 IMException (hudson.plugins.im.IMException)1 PircBotX (org.pircbotx.PircBotX)1 UserHostmask (org.pircbotx.UserHostmask)1 ListenerAdapter (org.pircbotx.hooks.ListenerAdapter)1 NoticeEvent (org.pircbotx.hooks.events.NoticeEvent)1